diff --git a/src/Snap.Hutao/Snap.Hutao/Core/ExceptionService/HutaoException.cs b/src/Snap.Hutao/Snap.Hutao/Core/ExceptionService/HutaoException.cs index 05988840..ced0474d 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/ExceptionService/HutaoException.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/ExceptionService/HutaoException.cs @@ -16,7 +16,7 @@ internal sealed class HutaoException : Exception throw new HutaoException(message, innerException); } - public static void ThrowIf(bool condition, string message, Exception? innerException = default) + public static void ThrowIf([DoesNotReturnIf(true)] bool condition, string message, Exception? innerException = default) { if (condition) { @@ -24,7 +24,7 @@ internal sealed class HutaoException : Exception } } - public static void ThrowIfNot(bool condition, string message, Exception? innerException = default) + public static void ThrowIfNot([DoesNotReturnIf(false)] bool condition, string message, Exception? innerException = default) { if (!condition) { diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowController.cs b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowController.cs index 6f249c4b..066c2907 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowController.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowController.cs @@ -29,6 +29,7 @@ internal sealed class WindowController private readonly WindowOptions options; private readonly IServiceProvider serviceProvider; private readonly WindowSubclass subclass; + private readonly WindowNonRudeHWND windowNonRudeHWND; public WindowController(Window window, in WindowOptions options, IServiceProvider serviceProvider) { @@ -39,6 +40,8 @@ internal sealed class WindowController // Window reference must be set before Window Subclass created serviceProvider.GetRequiredService().Window = window; subclass = new(window, options, serviceProvider); + windowNonRudeHWND = new(options.Hwnd); + InitializeCore(); } @@ -137,27 +140,19 @@ internal sealed class WindowController { SaveOrSkipWindowSize(); subclass?.Dispose(); + windowNonRudeHWND?.Dispose(); } private void ExtendsContentIntoTitleBar() { - if (options.UseLegacyDragBarImplementation) - { - // use normal Window method to extend. - window.ExtendsContentIntoTitleBar = true; - window.SetTitleBar(options.TitleBar); - } - else - { - AppWindowTitleBar appTitleBar = window.AppWindow.TitleBar; - appTitleBar.IconShowOptions = IconShowOptions.HideIconAndSystemMenu; - appTitleBar.ExtendsContentIntoTitleBar = true; + AppWindowTitleBar appTitleBar = window.AppWindow.TitleBar; + appTitleBar.IconShowOptions = IconShowOptions.HideIconAndSystemMenu; + appTitleBar.ExtendsContentIntoTitleBar = true; - UpdateTitleButtonColor(); - UpdateDragRectangles(); - options.TitleBar.ActualThemeChanged += (_, _) => UpdateTitleButtonColor(); - options.TitleBar.SizeChanged += (_, _) => UpdateDragRectangles(); - } + UpdateTitleButtonColor(); + UpdateDragRectangles(); + options.TitleBar.ActualThemeChanged += (_, _) => UpdateTitleButtonColor(); + options.TitleBar.SizeChanged += (_, _) => UpdateDragRectangles(); } private bool UpdateSystemBackdrop(BackdropType backdropType) diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowNonRudeHWND.cs b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowNonRudeHWND.cs new file mode 100644 index 00000000..4d0d9a7f --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowNonRudeHWND.cs @@ -0,0 +1,25 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using Snap.Hutao.Win32.Foundation; +using static Snap.Hutao.Win32.User32; + +namespace Snap.Hutao.Core.Windowing; + +internal sealed class WindowNonRudeHWND : IDisposable +{ + // https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-itaskbarlist2-markfullscreenwindow#remarks + private const string NonRudeHWND = "NonRudeHWND"; + private readonly HWND hwnd; + + public WindowNonRudeHWND(HWND hwnd) + { + this.hwnd = hwnd; + SetPropW(hwnd, NonRudeHWND, BOOL.TRUE); + } + + public void Dispose() + { + RemovePropW(hwnd, NonRudeHWND); + } +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowOptions.cs b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowOptions.cs index cf67b485..3f50032d 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowOptions.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowOptions.cs @@ -41,11 +41,6 @@ internal readonly struct WindowOptions /// public readonly bool PersistSize; - /// - /// 是否使用 Win UI 3 自带的拓展标题栏实现 - /// - public readonly bool UseLegacyDragBarImplementation = !AppWindowTitleBar.IsCustomizationSupported(); - public WindowOptions(Window window, FrameworkElement titleBar, SizeInt32 initSize, bool persistSize = false) { Hwnd = WindowNative.GetWindowHandle(window); diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowSubclass.cs b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowSubclass.cs index bc4e5552..02dc0eb9 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowSubclass.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowSubclass.cs @@ -20,7 +20,6 @@ namespace Snap.Hutao.Core.Windowing; internal sealed class WindowSubclass : IDisposable { private const int WindowSubclassId = 101; - private const int DragBarSubclassId = 102; private readonly Window window; private readonly WindowOptions options; @@ -29,7 +28,6 @@ internal sealed class WindowSubclass : IDisposable // We have to explicitly hold a reference to SUBCLASSPROC private SUBCLASSPROC windowProc = default!; - private SUBCLASSPROC legacyDragBarProc = default!; public WindowSubclass(Window window, in WindowOptions options, IServiceProvider serviceProvider) { @@ -48,29 +46,9 @@ internal sealed class WindowSubclass : IDisposable { windowProc = OnSubclassProcedure; bool windowHooked = SetWindowSubclass(options.Hwnd, windowProc, WindowSubclassId, 0); - bool propHooked = SetPropW(options.Hwnd, "NonRudeHWND", BOOL.TRUE); hotKeyController.RegisterAll(); - bool titleBarHooked = true; - - // only hook up drag bar proc when use legacy Window.ExtendsContentIntoTitleBar - if (!options.UseLegacyDragBarImplementation) - { - return windowHooked && propHooked && titleBarHooked; - } - - titleBarHooked = false; - HWND hwndDragBar = FindWindowExW(options.Hwnd, default, "DRAG_BAR_WINDOW_CLASS", default); - - if (hwndDragBar.IsNull) - { - return windowHooked && propHooked && titleBarHooked; - } - - legacyDragBarProc = OnLegacyDragBarProcedure; - titleBarHooked = SetWindowSubclass(hwndDragBar, legacyDragBarProc, DragBarSubclassId, 0); - - return windowHooked && propHooked && titleBarHooked; + return windowHooked; } /// @@ -79,14 +57,7 @@ internal sealed class WindowSubclass : IDisposable hotKeyController.UnregisterAll(); RemoveWindowSubclass(options.Hwnd, windowProc, WindowSubclassId); - RemovePropW(options.Hwnd, "NonRudeHWND"); windowProc = default!; - - if (options.UseLegacyDragBarImplementation) - { - RemoveWindowSubclass(options.Hwnd, legacyDragBarProc, DragBarSubclassId); - legacyDragBarProc = default!; - } } [SuppressMessage("", "SH002")] @@ -129,19 +100,4 @@ internal sealed class WindowSubclass : IDisposable return DefSubclassProc(hwnd, uMsg, wParam, lParam); } - - [SuppressMessage("", "SH002")] - private LRESULT OnLegacyDragBarProcedure(HWND hwnd, uint uMsg, WPARAM wParam, LPARAM lParam, nuint uIdSubclass, nuint dwRefData) - { - switch (uMsg) - { - case WM_NCRBUTTONDOWN: - case WM_NCRBUTTONUP: - { - return (LRESULT)(nint)WM_NULL; - } - } - - return DefSubclassProc(hwnd, uMsg, wParam, lParam); - } } \ No newline at end of file