fix window state

This commit is contained in:
Lightczx
2024-05-16 11:11:49 +08:00
parent 5868d53cca
commit f15a692f03
6 changed files with 40 additions and 12 deletions

View File

@@ -56,6 +56,21 @@ internal static class WindowExtension
ShowWindow(GetWindowHandle(window), SHOW_WINDOW_CMD.SW_NORMAL);
}
public static void SwitchTo(this Window window)
{
HWND hwnd = GetWindowHandle(window);
if (!IsWindowVisible(hwnd))
{
ShowWindow(hwnd, SHOW_WINDOW_CMD.SW_SHOW);
}
else if (IsIconic(hwnd))
{
ShowWindow(hwnd, SHOW_WINDOW_CMD.SW_RESTORE);
}
SetForegroundWindow(hwnd);
}
public static void Hide(this Window window)
{
ShowWindow(GetWindowHandle(window), SHOW_WINDOW_CMD.SW_HIDE);
@@ -92,6 +107,11 @@ internal static class WindowExtension
public static double GetRasterizationScale(this Window window)
{
if (window is { Content.XamlRoot: { } xamlRoot })
{
return xamlRoot.RasterizationScale;
}
uint dpi = GetDpiForWindow(window.GetWindowHandle());
return Math.Round(dpi / 96D, 2, MidpointRounding.AwayFromZero);
}

View File

@@ -4,6 +4,7 @@
using Microsoft.UI;
using Microsoft.UI.Composition.SystemBackdrops;
using Microsoft.UI.Content;
using Microsoft.UI.Input;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
@@ -281,13 +282,9 @@ internal sealed class XamlWindowController
return;
}
AppWindowTitleBar appTitleBar = window.AppWindow.TitleBar;
double scale = window.GetRasterizationScale();
// 48 is the navigation button leftInset
RectInt32 dragRect = StructMarshal.RectInt32(48, 0, xamlWindow.TitleBarAccess.ActualSize).Scale(scale);
appTitleBar.SetDragRectangles([dragRect]);
RectInt32 dragRect = StructMarshal.RectInt32(48, 0, xamlWindow.TitleBarAccess.ActualSize).Scale(window.GetRasterizationScale());
window.GetInputNonClientPointerSource().SetRegionRects(NonClientRegionKind.Caption, [dragRect]);
}
#endregion
}

View File

@@ -2,7 +2,7 @@
"profiles": {
"Snap.Hutao": {
"commandName": "MsixPackage",
"nativeDebugging": true,
"nativeDebugging": false,
"doNotLaunchApp": false,
"allowLocalNetworkLoopbackProperty": true
},

View File

@@ -48,7 +48,7 @@ internal sealed partial class NotifyIconViewModel : ObservableObject
case MainWindow mainWindow:
{
// MainWindow is activated, bring to foreground
mainWindow.Show();
mainWindow.SwitchTo();
mainWindow.BringToForeground();
return;
}
@@ -60,14 +60,14 @@ internal sealed partial class NotifyIconViewModel : ObservableObject
currentXamlWindowReference.Window = mainWindow;
// TODO: Can actually be no any window is initialized
mainWindow.Show();
mainWindow.SwitchTo();
mainWindow.BringToForeground();
break;
}
case Window otherWindow:
{
otherWindow.Show();
otherWindow.SwitchTo();
otherWindow.BringToForeground();
return;
}

View File

@@ -29,6 +29,7 @@ internal sealed partial class RegistryWatcher : IDisposable
private readonly Action valueChangedCallback;
private readonly object syncRoot = new();
private bool disposed;
private bool disposing;
public RegistryWatcher(string keyName, Action valueChangedCallback)
{
@@ -69,6 +70,8 @@ internal sealed partial class RegistryWatcher : IDisposable
return;
}
disposing = true;
// First cancel the outer while loop
cancellationTokenSource.Cancel();
@@ -104,11 +107,11 @@ internal sealed partial class RegistryWatcher : IDisposable
try
{
// If terminateEvent is signaled, the Dispose method
// If disposeEvent is signaled, the Dispose method
// has been called and the object is shutting down.
// The outer token has already canceled, so we can
// skip both loops and exit the method.
while (!disposeEvent.WaitOne(0, true))
while (!disposing && !disposeEvent.WaitOne(0, true))
{
HRESULT hRESULT = HRESULT_FROM_WIN32(RegNotifyChangeKeyValue(registryKey, true, RegNotifyFilters, hEvent, true));
Marshal.ThrowExceptionForHR(hRESULT);

View File

@@ -101,6 +101,14 @@ internal static class User32
}
}
[DllImport("USER32.dll", CallingConvention = CallingConvention.Winapi, ExactSpelling = true)]
[SupportedOSPlatform("windows5.0")]
public static extern BOOL IsIconic(HWND hWnd);
[DllImport("USER32.dll", CallingConvention = CallingConvention.Winapi, ExactSpelling = true)]
[SupportedOSPlatform("windows5.0")]
public static extern BOOL IsWindowVisible(HWND hWnd);
[DllImport("USER32.dll", CallingConvention = CallingConvention.Winapi, ExactSpelling = true, SetLastError = true)]
[SupportedOSPlatform("windows5.0")]
public static unsafe extern ushort RegisterClassW(WNDCLASSW* lpWndClass);