Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Peek] Fix idle CPU usage #29665

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/modules/peek/Peek.UI/PeekXAML/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,6 +12,7 @@
using Peek.Common;
using Peek.FilePreviewer;
using Peek.FilePreviewer.Models;
using Peek.UI.Native;
using Peek.UI.Telemetry.Events;
using Peek.UI.Views;

Expand All @@ -28,7 +30,7 @@ public IHost Host
get;
}

private Window? Window { get; set; }
private MainWindow? Window { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="App"/> class.
Expand Down Expand Up @@ -96,12 +98,32 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
}
}

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);
}
}
}
71 changes: 30 additions & 41 deletions src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,7 +14,6 @@
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;
Expand All @@ -30,7 +28,6 @@ public sealed partial class MainWindow : WindowEx, IDisposable
public MainWindowViewModel ViewModel { get; }

private ThemeListener? themeListener;
private bool activated;

public MainWindow()
{
Expand All @@ -49,54 +46,20 @@ public MainWindow()

ViewModel = Application.Current.GetService<MainWindowViewModel>();

NativeEventWaiter.WaitForEventLoop(Constants.ShowPeekEvent(), OnPeekHotkey);

TitleBarControl.SetTitleBarToWindow(this);

AppWindow.Closing += AppWindow_Closing;
}

private void HandleThemeChange()
{
AppWindow appWindow = this.AppWindow;

if (ThemeHelpers.GetAppTheme() == AppTheme.Light)
{
appWindow.TitleBar.ButtonForegroundColor = Colors.DarkSlateGray;
}
else
{
appWindow.TitleBar.ButtonForegroundColor = Colors.White;
}
}

private void PeekWindow_Activated(object sender, WindowActivatedEventArgs args)
{
if (args.WindowActivationState == WindowActivationState.Deactivated)
{
var userSettings = Application.Current.GetService<IUserSettings>();
if (userSettings.CloseAfterLosingFocus)
{
Uninitialize();
}
}
}

/// <summary>
/// Handle Peek hotkey, by toggling the window visibility and querying files when necessary.
/// Toggling the window visibility and querying files when necessary.
/// </summary>
private void OnPeekHotkey()
public void Toggle(bool firstActivation, Windows.Win32.Foundation.HWND foregroundWindowHandle)
{
// 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)
if (firstActivation)
{
Activate();
Initialize(foregroundWindowHandle);
activated = true;
return;
}

Expand All @@ -117,6 +80,32 @@ private void OnPeekHotkey()
}
}

private void HandleThemeChange()
{
AppWindow appWindow = this.AppWindow;

if (ThemeHelpers.GetAppTheme() == AppTheme.Light)
{
appWindow.TitleBar.ButtonForegroundColor = Colors.DarkSlateGray;
}
else
{
appWindow.TitleBar.ButtonForegroundColor = Colors.White;
}
}

private void PeekWindow_Activated(object sender, WindowActivatedEventArgs args)
{
if (args.WindowActivationState == WindowActivationState.Deactivated)
{
var userSettings = Application.Current.GetService<IUserSettings>();
if (userSettings.CloseAfterLosingFocus)
{
Uninitialize();
}
}
}

private void PreviousNavigationInvoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
ViewModel.AttemptPreviousNavigation();
Expand Down