Skip to content

Commit

Permalink
Hide main settings window instead of closing (#17960)
Browse files Browse the repository at this point in the history
* Hide main settings window instead of closing

* Proper closing

* Create Settings window hidden if opening OOBE/SCOOBE
  • Loading branch information
stefansjfw committed May 19, 2022
1 parent ab43283 commit 88e79ac
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/settings-ui/Settings.UI/App.xaml.cs
Expand Up @@ -149,9 +149,11 @@ protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs ar
}
else
{
// Create the Settings window so that it's fully initialized and
// Create the Settings window hidden so that it's fully initialized and
// it will be ready to receive the notification if the user opens
// the Settings from the tray icon.
settingsWindow = new MainWindow(true);

if (ShowOobe)
{
PowerToysTelemetry.Log.WriteEvent(new OobeStartedEvent());
Expand Down
4 changes: 4 additions & 0 deletions src/settings-ui/Settings.UI/Helpers/NativeMethods.cs
Expand Up @@ -15,6 +15,7 @@ public static class NativeMethods
internal const int SPI_GETDESKWALLPAPER = 0x0073;
internal const int SW_SHOWNORMAL = 1;
internal const int SW_SHOWMAXIMIZED = 3;
internal const int SW_HIDE = 0;

[DllImport("user32.dll")]
internal static extern IntPtr GetActiveWindow();
Expand Down Expand Up @@ -44,6 +45,9 @@ public static class NativeMethods
[DllImport("user32.dll")]
public static extern int GetDpiForWindow(System.IntPtr hWnd);

[DllImport("user32.dll")]
public static extern bool IsWindowVisible(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern bool AllowSetForegroundWindow(int dwProcessId);

Expand Down
28 changes: 25 additions & 3 deletions src/settings-ui/Settings.UI/MainWindow.xaml.cs
Expand Up @@ -22,7 +22,7 @@ namespace Microsoft.PowerToys.Settings.UI
/// </summary>
public sealed partial class MainWindow : Window
{
public MainWindow()
public MainWindow(bool createHidden = false)
{
var bootTime = new System.Diagnostics.Stopwatch();
bootTime.Start();
Expand All @@ -37,6 +37,11 @@ public MainWindow()
appWindow.SetIcon("icon.ico");

var placement = Utils.DeserializePlacementOrDefault(hWnd);
if (createHidden)
{
placement.ShowCmd = NativeMethods.SW_HIDE;
}

NativeMethods.SetWindowPlacement(hWnd, ref placement);

ResourceLoader loader = ResourceLoader.GetForViewIndependentUse();
Expand Down Expand Up @@ -105,12 +110,29 @@ public void NavigateToSection(System.Type type)
ShellPage.Navigate(type);
}

private void Window_Closed(object sender, WindowEventArgs args)
public void CloseHiddenWindow()
{
App.ClearSettingsWindow();
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
if (!NativeMethods.IsWindowVisible(hWnd))
{
Close();
}
}

private void Window_Closed(object sender, WindowEventArgs args)
{
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
Utils.SerializePlacement(hWnd);

if (App.GetOobeWindow() == null)
{
App.ClearSettingsWindow();
}
else
{
args.Handled = true;
NativeMethods.ShowWindow(hWnd, NativeMethods.SW_HIDE);
}
}
}
}
6 changes: 6 additions & 0 deletions src/settings-ui/Settings.UI/OobeWindow.xaml.cs
Expand Up @@ -104,6 +104,12 @@ private void OobeWindow_SizeChanged(object sender, WindowSizeChangedEventArgs ar
private void Window_Closed(object sender, WindowEventArgs args)
{
App.ClearOobeWindow();

var mainWindow = App.GetSettingsWindow();
if (mainWindow != null)
{
mainWindow.CloseHiddenWindow();
}
}
}
}

0 comments on commit 88e79ac

Please sign in to comment.