|
| 1 | +using CommunityToolkit.Mvvm.ComponentModel; |
| 2 | +using Microsoft.UI.Windowing; |
| 3 | +using Microsoft.UI.Xaml.Controls; |
| 4 | +using Starward.Frameworks; |
| 5 | +using System; |
| 6 | +using System.ComponentModel; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +using Vanara.PInvoke; |
| 9 | +using Windows.Graphics; |
| 10 | + |
| 11 | + |
| 12 | +namespace Starward.Features.Main; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// An empty window that can be used on its own or navigated to within a Frame. |
| 16 | +/// </summary> |
| 17 | +[INotifyPropertyChanged] |
| 18 | +public sealed partial class MainWindow : WindowEx |
| 19 | +{ |
| 20 | + |
| 21 | + |
| 22 | + public static new MainWindow Current { get; private set; } |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + public MainWindow() |
| 27 | + { |
| 28 | + Current = this; |
| 29 | + this.InitializeComponent(); |
| 30 | + InitializeMainWindow(); |
| 31 | + if (string.IsNullOrWhiteSpace(AppSetting.UserDataFolder)) |
| 32 | + { |
| 33 | + MainContentHost.Content = new WelcomeView(); |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + MainContentHost.Content = new MainView(); |
| 38 | + App.Current.InitializeSystemTray(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + private void InitializeMainWindow() |
| 45 | + { |
| 46 | + Title = "Starward"; |
| 47 | + AppWindow.TitleBar.ExtendsContentIntoTitleBar = true; |
| 48 | + AppWindow.TitleBar.IconShowOptions = IconShowOptions.ShowIconAndSystemMenu; |
| 49 | + AppWindow.TitleBar.PreferredHeightOption = TitleBarHeightOption.Tall; |
| 50 | + AppWindow.Closing += AppWindow_Closing; |
| 51 | + CenterInScreen(1200, 676); |
| 52 | + AdaptTitleBarButtonColorToActuallTheme(); |
| 53 | + SetDragRectangles(new RectInt32(0, 0, 100000, (int)(48 * UIScale))); |
| 54 | + SetIcon(); |
| 55 | + WTSRegisterSessionNotification(WindowHandle, 0); |
| 56 | + if (AppWindow.Presenter is OverlappedPresenter presenter) |
| 57 | + { |
| 58 | + presenter.IsMaximizable = false; |
| 59 | + presenter.IsResizable = false; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + private async void AppWindow_Closing(AppWindow sender, AppWindowClosingEventArgs args) |
| 65 | + { |
| 66 | + try |
| 67 | + { |
| 68 | + if (string.IsNullOrWhiteSpace(AppSetting.UserDataFolder)) |
| 69 | + { |
| 70 | + App.Current.Exit(); |
| 71 | + return; |
| 72 | + } |
| 73 | + args.Cancel = true; |
| 74 | + MainWindowCloseOption option = AppSetting.CloseWindowOption; |
| 75 | + if (option is not MainWindowCloseOption.Hide and not MainWindowCloseOption.Exit) |
| 76 | + { |
| 77 | + var dialog = new MainWindowCloseDialog |
| 78 | + { |
| 79 | + Title = Lang.ExperienceSettingPage_CloseWindowOption, |
| 80 | + PrimaryButtonText = Lang.Common_Confirm, |
| 81 | + CloseButtonText = Lang.Common_Cancel, |
| 82 | + DefaultButton = ContentDialogButton.Primary, |
| 83 | + XamlRoot = Content.XamlRoot, |
| 84 | + }; |
| 85 | + var result = await dialog.ShowAsync(); |
| 86 | + if (result is not ContentDialogResult.Primary) |
| 87 | + { |
| 88 | + return; |
| 89 | + } |
| 90 | + option = dialog.MainWindowCloseOption.Value; |
| 91 | + AppSetting.CloseWindowOption = option; |
| 92 | + } |
| 93 | + if (option is MainWindowCloseOption.Hide) |
| 94 | + { |
| 95 | + Hide(); |
| 96 | + } |
| 97 | + if (option is MainWindowCloseOption.Exit) |
| 98 | + { |
| 99 | + App.Current.Exit(); |
| 100 | + } |
| 101 | + } |
| 102 | + catch { } |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + protected override nint WindowSubclassProc(HWND hWnd, uint uMsg, nint wParam, nint lParam, nuint uIdSubclass, nint dwRefData) |
| 107 | + { |
| 108 | + if (uMsg == (uint)User32.WindowMessage.WM_SYSCOMMAND) |
| 109 | + { |
| 110 | + // SC_MAXIMIZE |
| 111 | + if (wParam == 0xF030) |
| 112 | + { |
| 113 | + return IntPtr.Zero; |
| 114 | + } |
| 115 | + } |
| 116 | + if (uMsg == (uint)User32.WindowMessage.WM_WTSSESSION_CHANGE) |
| 117 | + { |
| 118 | + // WTS_SESSION_LOCK |
| 119 | + if (wParam == 0x7) |
| 120 | + { |
| 121 | + |
| 122 | + } |
| 123 | + // WTS_SESSION_UNLOCK |
| 124 | + if (wParam == 0x8) |
| 125 | + { |
| 126 | + |
| 127 | + } |
| 128 | + } |
| 129 | + return base.WindowSubclassProc(hWnd, uMsg, wParam, lParam, uIdSubclass, dwRefData); |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + [LibraryImport("wtsapi32.dll")] |
| 135 | + [return: MarshalAs(UnmanagedType.Bool)] |
| 136 | + private static partial bool WTSRegisterSessionNotification(IntPtr hWnd, int dwFlags); |
| 137 | + |
| 138 | + |
| 139 | +} |
0 commit comments