From d52aa0d6b294446de973d826fa0e6771dcee61f3 Mon Sep 17 00:00:00 2001 From: DismissedLight <1686188646@qq.com> Date: Tue, 14 May 2024 22:19:20 +0800 Subject: [PATCH] better resize policy --- src/Snap.Hutao/Snap.Hutao/App.xaml | 1 + .../Snap.Hutao/Core/Setting/SettingKeys.cs | 4 ++ .../NotifyIcon/NotifyIconContextMenu.xaml.cs | 4 +- .../NotifyIcon/NotifyIconController.cs | 66 ++++++++++++------- .../NotifyIcon/NotifyIconMessageWindow.cs | 18 ++--- src/Snap.Hutao/Snap.Hutao/MainWindow.xaml.cs | 8 ++- .../Snap.Hutao/View/Page/WikiMonsterPage.xaml | 25 ++----- 7 files changed, 70 insertions(+), 56 deletions(-) diff --git a/src/Snap.Hutao/Snap.Hutao/App.xaml b/src/Snap.Hutao/Snap.Hutao/App.xaml index 02dd4b50..8d40cc81 100644 --- a/src/Snap.Hutao/Snap.Hutao/App.xaml +++ b/src/Snap.Hutao/Snap.Hutao/App.xaml @@ -6,6 +6,7 @@ + diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Setting/SettingKeys.cs b/src/Snap.Hutao/Snap.Hutao/Core/Setting/SettingKeys.cs index fbb40ce2..b253332a 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/Setting/SettingKeys.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/Setting/SettingKeys.cs @@ -15,6 +15,10 @@ internal static class SettingKeys public const string GuideWindowRect = "GuideWindowRect"; public const string IsNavPaneOpen = "IsNavPaneOpen"; public const string IsInfoBarToggleChecked = "IsInfoBarToggleChecked"; + + #endregion + + #region Infrastructure public const string ExcludedAnnouncementIds = "ExcludedAnnouncementIds"; #endregion diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/NotifyIcon/NotifyIconContextMenu.xaml.cs b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/NotifyIcon/NotifyIconContextMenu.xaml.cs index c6c26e00..4b28f518 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/NotifyIcon/NotifyIconContextMenu.xaml.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/NotifyIcon/NotifyIconContextMenu.xaml.cs @@ -8,10 +8,10 @@ namespace Snap.Hutao.Core.Windowing.NotifyIcon; internal sealed partial class NotifyIconContextMenu : Flyout { - public NotifyIconContextMenu() + public NotifyIconContextMenu(IServiceProvider serviceProvider) { AllowFocusOnInteraction = false; InitializeComponent(); - Root.DataContext = Ioc.Default.GetRequiredService(); + Root.DataContext = serviceProvider.GetRequiredService(); } } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/NotifyIcon/NotifyIconController.cs b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/NotifyIcon/NotifyIconController.cs index fedc4784..287d98bd 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/NotifyIcon/NotifyIconController.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/NotifyIcon/NotifyIconController.cs @@ -14,13 +14,15 @@ namespace Snap.Hutao.Core.Windowing.NotifyIcon; [Injection(InjectAs.Singleton)] internal sealed class NotifyIconController : IDisposable { - private readonly LazySlim lazyMenu = new(() => new()); + private readonly LazySlim lazyMenu; private readonly NotifyIconXamlHostWindow xamlHostWindow; private readonly NotifyIconMessageWindow messageWindow; private readonly System.Drawing.Icon icon; - public NotifyIconController() + public NotifyIconController(IServiceProvider serviceProvider) { + lazyMenu = new(() => new(serviceProvider)); + StorageFile iconFile = StorageFile.GetFileFromApplicationUriAsync("ms-appx:///Assets/Logo.ico".ToUri()).AsTask().GetAwaiter().GetResult(); icon = new(iconFile.Path); @@ -28,31 +30,11 @@ internal sealed class NotifyIconController : IDisposable messageWindow = new() { - TaskbarCreated = window => - { - NotifyIconMethods.Delete(Id); - if (!NotifyIconMethods.Add(Id, window.HWND, "Snap Hutao", NotifyIconMessageWindow.WM_NOTIFYICON_CALLBACK, (HICON)icon.Handle)) - { - HutaoException.InvalidOperation("Failed to recreate NotifyIcon"); - } - }, - ContextMenuRequested = (window, point) => - { - RECT iconRect = NotifyIconMethods.GetRect(Id, window.HWND); - xamlHostWindow.ShowFlyoutAt(lazyMenu.Value, new Windows.Foundation.Point(point.X, point.Y), iconRect); - }, + TaskbarCreated = OnRecreateNotifyIconRequested, + ContextMenuRequested = OnContextMenuRequested, }; - NotifyIconMethods.Delete(Id); - if (!NotifyIconMethods.Add(Id, messageWindow.HWND, "Snap Hutao", NotifyIconMessageWindow.WM_NOTIFYICON_CALLBACK, (HICON)icon.Handle)) - { - HutaoException.InvalidOperation("Failed to create NotifyIcon"); - } - - if (!NotifyIconMethods.SetVersion(Id, NOTIFYICON_VERSION_4)) - { - HutaoException.InvalidOperation("Failed to set NotifyIcon version"); - } + CreateNotifyIcon(); } private static ref readonly Guid Id @@ -73,4 +55,38 @@ internal sealed class NotifyIconController : IDisposable xamlHostWindow.Dispose(); } + + private void OnRecreateNotifyIconRequested(NotifyIconMessageWindow window) + { + NotifyIconMethods.Delete(Id); + if (!NotifyIconMethods.Add(Id, window.HWND, "Snap Hutao", NotifyIconMessageWindow.WM_NOTIFYICON_CALLBACK, (HICON)icon.Handle)) + { + HutaoException.InvalidOperation("Failed to recreate NotifyIcon"); + } + + if (!NotifyIconMethods.SetVersion(Id, NOTIFYICON_VERSION_4)) + { + HutaoException.InvalidOperation("Failed to set NotifyIcon version"); + } + } + + private void CreateNotifyIcon() + { + NotifyIconMethods.Delete(Id); + if (!NotifyIconMethods.Add(Id, messageWindow.HWND, "Snap Hutao", NotifyIconMessageWindow.WM_NOTIFYICON_CALLBACK, (HICON)icon.Handle)) + { + HutaoException.InvalidOperation("Failed to create NotifyIcon"); + } + + if (!NotifyIconMethods.SetVersion(Id, NOTIFYICON_VERSION_4)) + { + HutaoException.InvalidOperation("Failed to set NotifyIcon version"); + } + } + + private void OnContextMenuRequested(NotifyIconMessageWindow window, PointUInt16 point) + { + RECT iconRect = NotifyIconMethods.GetRect(Id, window.HWND); + xamlHostWindow.ShowFlyoutAt(lazyMenu.Value, new Windows.Foundation.Point(point.X, point.Y), iconRect); + } } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/NotifyIcon/NotifyIconMessageWindow.cs b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/NotifyIcon/NotifyIconMessageWindow.cs index 889dbc8a..39b6edf0 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/NotifyIcon/NotifyIconMessageWindow.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/NotifyIcon/NotifyIconMessageWindow.cs @@ -102,9 +102,18 @@ internal sealed class NotifyIconMessageWindow : IDisposable switch (lParam2.Low) { + case WM_CONTEXTMENU: + window.ContextMenuRequested?.Invoke(window, wParam2); + break; case WM_MOUSEMOVE: // X: wParam2.X Y: wParam2.Y Low: WM_MOUSEMOVE break; + case WM_LBUTTONDOWN: + case WM_LBUTTONUP: + break; + case WM_RBUTTONDOWN: + case WM_RBUTTONUP: + break; case NIN_SELECT: // X: wParam2.X Y: wParam2.Y Low: NIN_SELECT break; @@ -114,15 +123,6 @@ internal sealed class NotifyIconMessageWindow : IDisposable case NIN_POPUPCLOSE: // X: wParam2.X Y: 0? Low: NIN_POPUPCLOSE break; - case WM_LBUTTONDOWN: - case WM_LBUTTONUP: - break; - case WM_RBUTTONDOWN: - case WM_RBUTTONUP: - break; - case WM_CONTEXTMENU: - window.ContextMenuRequested?.Invoke(window, wParam2); - break; default: break; } diff --git a/src/Snap.Hutao/Snap.Hutao/MainWindow.xaml.cs b/src/Snap.Hutao/Snap.Hutao/MainWindow.xaml.cs index ec710025..1ec58d21 100644 --- a/src/Snap.Hutao/Snap.Hutao/MainWindow.xaml.cs +++ b/src/Snap.Hutao/Snap.Hutao/MainWindow.xaml.cs @@ -1,6 +1,7 @@ // Copyright (c) DGP Studio. All rights reserved. // Licensed under the MIT license. +using Microsoft.UI.Content; using Microsoft.UI.Xaml; using Snap.Hutao.Core.Setting; using Snap.Hutao.Core.Windowing; @@ -30,8 +31,11 @@ internal sealed partial class MainWindow : Window, IXamlWindowOptionsSource, IMi windowOptions = new(this, TitleBarView.DragArea, new(1200, 741), SettingKeys.WindowRect); this.InitializeController(serviceProvider); - var policy = this.GetDesktopWindowXamlSource()!.SiteBridge.ResizePolicy; - _ = 1; + if (this.GetDesktopWindowXamlSource() is { } desktopWindowXamlSource) + { + DesktopChildSiteBridge desktopChildSiteBridge = desktopWindowXamlSource.SiteBridge; + desktopChildSiteBridge.ResizePolicy = ContentSizePolicy.ResizeContentToParentWindow; + } } /// diff --git a/src/Snap.Hutao/Snap.Hutao/View/Page/WikiMonsterPage.xaml b/src/Snap.Hutao/Snap.Hutao/View/Page/WikiMonsterPage.xaml index 8e77317f..18e2c576 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/Page/WikiMonsterPage.xaml +++ b/src/Snap.Hutao/Snap.Hutao/View/Page/WikiMonsterPage.xaml @@ -46,7 +46,13 @@ - + + + 16 + + + + @@ -94,23 +100,6 @@ Margin="6,8,0,0" LocalSettingKeySuffixForCurrent="WikiMonsterPage.Monsters"/> -