[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

@@ -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();