[Peek]Fix idle CPU usage before window initialization (#29665)

This commit is contained in:
Davide Giacometti
2023-11-06 12:13:31 +01:00
committed by GitHub
parent faaffe3909
commit 35b0f71317
2 changed files with 54 additions and 43 deletions

View File

@@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
using System;
using interop;
using ManagedCommon;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@@ -11,6 +12,7 @@ using Microsoft.UI.Xaml;
using Peek.Common;
using Peek.FilePreviewer;
using Peek.FilePreviewer.Models;
using Peek.UI.Native;
using Peek.UI.Telemetry.Events;
using Peek.UI.Views;
@@ -28,7 +30,7 @@ namespace Peek.UI
get;
}
private Window? Window { get; set; }
private MainWindow? Window { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="App"/> class.
@@ -96,12 +98,32 @@ namespace Peek.UI
}
}
Window = new MainWindow();
NativeEventWaiter.WaitForEventLoop(Constants.ShowPeekEvent(), OnPeekHotkey);
}
private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
{
PowerToysTelemetry.Log.WriteEvent(new ErrorEvent() { HResult = (Common.Models.HResult)e.Exception.HResult, Failure = ErrorEvent.FailureType.AppCrash });
}
/// <summary>
/// Handle Peek hotkey
/// </summary>
private void OnPeekHotkey()
{
// Need to read the foreground HWND before activating Peek to avoid focus stealing
// Foreground HWND must always be Explorer or Desktop
var foregroundWindowHandle = Windows.Win32.PInvoke.GetForegroundWindow();
bool firstActivation = false;
if (Window == null)
{
firstActivation = true;
Window = new MainWindow();
}
Window.Toggle(firstActivation, foregroundWindowHandle);
}
}
}

View File

@@ -1,9 +1,8 @@
// Copyright (c) Microsoft Corporation
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using interop;
using ManagedCommon;
using Microsoft.PowerToys.Telemetry;
using Microsoft.UI;
@@ -15,7 +14,6 @@ using Peek.Common.Extensions;
using Peek.FilePreviewer.Models;
using Peek.UI.Extensions;
using Peek.UI.Helpers;
using Peek.UI.Native;
using Peek.UI.Telemetry.Events;
using Windows.Foundation;
using WinUIEx;
@@ -30,7 +28,6 @@ namespace Peek.UI
public MainWindowViewModel ViewModel { get; }
private ThemeListener? themeListener;
private bool activated;
public MainWindow()
{
@@ -49,13 +46,40 @@ namespace Peek.UI
ViewModel = Application.Current.GetService<MainWindowViewModel>();
NativeEventWaiter.WaitForEventLoop(Constants.ShowPeekEvent(), OnPeekHotkey);
TitleBarControl.SetTitleBarToWindow(this);
AppWindow.Closing += AppWindow_Closing;
}
/// <summary>
/// Toggling the window visibility and querying files when necessary.
/// </summary>
public void Toggle(bool firstActivation, Windows.Win32.Foundation.HWND foregroundWindowHandle)
{
if (firstActivation)
{
Activate();
Initialize(foregroundWindowHandle);
return;
}
if (AppWindow.IsVisible)
{
if (IsNewSingleSelectedItem(foregroundWindowHandle))
{
Initialize(foregroundWindowHandle);
}
else
{
Uninitialize();
}
}
else
{
Initialize(foregroundWindowHandle);
}
}
private void HandleThemeChange()
{
AppWindow appWindow = this.AppWindow;
@@ -82,41 +106,6 @@ namespace Peek.UI
}
}
/// <summary>
/// Handle Peek hotkey, by toggling the window visibility and querying files when necessary.
/// </summary>
private void OnPeekHotkey()
{
// Need to read the foreground HWND before activating Peek to avoid focus stealing
// Foreground HWND must always be Explorer or Desktop
var foregroundWindowHandle = Windows.Win32.PInvoke.GetForegroundWindow();
// First Peek activation
if (!activated)
{
Activate();
Initialize(foregroundWindowHandle);
activated = true;
return;
}
if (AppWindow.IsVisible)
{
if (IsNewSingleSelectedItem(foregroundWindowHandle))
{
Initialize(foregroundWindowHandle);
}
else
{
Uninitialize();
}
}
else
{
Initialize(foregroundWindowHandle);
}
}
private void PreviousNavigationInvoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
ViewModel.AttemptPreviousNavigation();