From 2228150c7fd9902840b4fa1bae965a6f8e5b096d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=AB=E9=9B=A8?= <737345039@qq.com> Date: Sun, 22 Jan 2023 01:23:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AE=B0=E4=BD=8F=E4=B8=BB=E7=95=8C?= =?UTF-8?q?=E9=9D=A2=E7=9A=84=E4=BD=8D=E7=BD=AE=E5=92=8C=E5=A4=A7=E5=B0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 同时VersionUpdateView相对主界面居中 --- src/MaaWpfGui/Helper/WindowManager.cs | 68 ++++++++++++++++++++++ src/MaaWpfGui/MaaWpfGui.csproj | 1 + src/MaaWpfGui/Main/Bootstrapper.cs | 7 +++ src/MaaWpfGui/Main/RootViewModel.cs | 2 +- src/MaaWpfGui/Main/SettingsViewModel.cs | 75 +++++++++++++++++++++++++ 5 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 src/MaaWpfGui/Helper/WindowManager.cs diff --git a/src/MaaWpfGui/Helper/WindowManager.cs b/src/MaaWpfGui/Helper/WindowManager.cs new file mode 100644 index 0000000000..f17a4cd767 --- /dev/null +++ b/src/MaaWpfGui/Helper/WindowManager.cs @@ -0,0 +1,68 @@ +// +// MaaWpfGui - A part of the MaaCoreArknights project +// Copyright (C) 2021 MistEO and Contributors +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY +// + +using System; +using System.Windows; +using Stylet; + +namespace MaaWpfGui +{ + public class WindowManager : Stylet.WindowManager + { + private readonly double Left = double.Parse(ViewStatusStorage.Get("GUI.Position.Left", "NaN")); + private readonly double Top = double.Parse(ViewStatusStorage.Get("GUI.Position.Top", "NaN")); + + public WindowManager(IViewManager viewManager, Func messageBoxViewModelFactory, IWindowManagerConfig config) + : base(viewManager, messageBoxViewModelFactory, config) + { + // Change Left and Top after adjusting multiple displays + Left = Left < SystemParameters.VirtualScreenWidth ? Left : double.NaN; + Top = Top < SystemParameters.VirtualScreenHeight ? Top : double.NaN; + } + + /// + protected override Window CreateWindow(object viewModel, bool isDialog, IViewAware ownerViewModel) + { + Window window = base.CreateWindow(viewModel, isDialog, ownerViewModel); + if (bool.Parse(ViewStatusStorage.Get("GUI.PositionAndSize.Load", bool.TrueString))) + { + if (isDialog || ownerViewModel != null || double.IsNaN(Left) || double.IsNaN(Top)) + { + return window; + } + + // In Stylet, CreateWindow().WindowStartupLocation is CenterScreen or CenterOwner (if w.WSLoc == Manual && w.Left == NaN && w.Top == NaN && ...) + window.WindowStartupLocation = WindowStartupLocation.Manual; + + if (window.ToString() == "MaaWpfGui.RootView") + { + double width = double.Parse(ViewStatusStorage.Get("GUI.Size.Width", "NaN")); + double height = double.Parse(ViewStatusStorage.Get("GUI.Size.Height", "NaN")); + + window.Left = Left; + window.Top = Top; + window.Width = width; + window.Height = height; + } + else + { + // Center other windows in MaaWpfGui.RootView + window.Left = Left + ((Application.Current.MainWindow.Width - window.Width) / 2); + window.Top = Top + ((Application.Current.MainWindow.Height - window.Height) / 2); + } + } + + return window; + } + } +} diff --git a/src/MaaWpfGui/MaaWpfGui.csproj b/src/MaaWpfGui/MaaWpfGui.csproj index 558e93a3d4..ea4e98f4be 100644 --- a/src/MaaWpfGui/MaaWpfGui.csproj +++ b/src/MaaWpfGui/MaaWpfGui.csproj @@ -105,6 +105,7 @@ + diff --git a/src/MaaWpfGui/Main/Bootstrapper.cs b/src/MaaWpfGui/Main/Bootstrapper.cs index 92b5b1ab3a..0154632348 100644 --- a/src/MaaWpfGui/Main/Bootstrapper.cs +++ b/src/MaaWpfGui/Main/Bootstrapper.cs @@ -126,6 +126,13 @@ namespace MaaWpfGui builder.Bind().To().InSingletonScope(); } + /// + protected override void DisplayRootView(object rootViewModel) + { + var windowManager = (WindowManager)GetInstance(typeof(WindowManager)); + windowManager.ShowWindow(rootViewModel); + } + /// /// 退出时执行啥自己加。 protected override void OnExit(ExitEventArgs e) diff --git a/src/MaaWpfGui/Main/RootViewModel.cs b/src/MaaWpfGui/Main/RootViewModel.cs index 406b4c930d..dfa36da68f 100644 --- a/src/MaaWpfGui/Main/RootViewModel.cs +++ b/src/MaaWpfGui/Main/RootViewModel.cs @@ -86,7 +86,7 @@ namespace MaaWpfGui if (vuvm.IsFirstBootAfterUpdate) { - _windowManager.ShowWindow(vuvm); + _container.Get().ShowWindow(vuvm); } else { diff --git a/src/MaaWpfGui/Main/SettingsViewModel.cs b/src/MaaWpfGui/Main/SettingsViewModel.cs index f24b66a11b..3ea0f0e35b 100644 --- a/src/MaaWpfGui/Main/SettingsViewModel.cs +++ b/src/MaaWpfGui/Main/SettingsViewModel.cs @@ -113,6 +113,11 @@ namespace MaaWpfGui MessageBoxButton.OK, MessageBoxImage.Hand); Hangover = false; } + + if (LoadGUIParameters && SaveGUIParametersOnClosing) + { + Application.Current.MainWindow.Closing += SaveGUIParameters; + } } private List _listTitle = new List(); @@ -2107,6 +2112,76 @@ namespace MaaWpfGui } } + private bool _loadGUIParameters = Convert.ToBoolean(ViewStatusStorage.Get("GUI.PositionAndSize.Load", bool.TrueString)); + + /// + /// Gets or sets a value indicating whether to load GUI parameters. + /// + public bool LoadGUIParameters + { + get => _loadGUIParameters; + set + { + SetAndNotify(ref _loadGUIParameters, value); + ViewStatusStorage.Set("GUI.PositionAndSize.Load", value.ToString()); + if (value) + { + if (SaveGUIParametersOnClosing) + { + Application.Current.MainWindow.Closing += SaveGUIParameters; + } + else + { + SaveGUIParameters(); + } + } + } + } + + private bool _saveGUIParametersOnClosing = Convert.ToBoolean(ViewStatusStorage.Get("GUI.PositionAndSize.SaveOnClosing", bool.TrueString)); + + /// + /// Gets or sets a value indicating whether to save GUI parameters on closing main window. + /// + public bool SaveGUIParametersOnClosing + { + get => _saveGUIParametersOnClosing; + set + { + SetAndNotify(ref _saveGUIParametersOnClosing, value); + ViewStatusStorage.Set("GUI.PositionAndSize.SaveOnClosing", value.ToString()); + if (value) + { + Application.Current.MainWindow.Closing += SaveGUIParameters; + } + else + { + Application.Current.MainWindow.Closing -= SaveGUIParameters; + } + } + } + + private void SaveGUIParameters(object sender, EventArgs e) + { + SaveGUIParameters(); + } + + /// + /// Save main window left edge, top edge, width and heigth. + /// + public void SaveGUIParameters() + { + // 请在配置文件中修改该部分配置,暂不支持从GUI设置 + // Please modify this part of configuration in the configuration file. + ViewStatusStorage.Set("GUI.PositionAndSize.Load", LoadGUIParameters.ToString()); + ViewStatusStorage.Set("GUI.PositionAndSize.SaveOnClosing", SaveGUIParametersOnClosing.ToString()); + + ViewStatusStorage.Set("GUI.Position.Left", Application.Current.MainWindow.Left.ToString()); + ViewStatusStorage.Set("GUI.Position.Top", Application.Current.MainWindow.Top.ToString()); + ViewStatusStorage.Set("GUI.Size.Width", Application.Current.MainWindow.Width.ToString()); + ViewStatusStorage.Set("GUI.Size.Height", Application.Current.MainWindow.Height.ToString()); + } + private bool _useAlternateStage = Convert.ToBoolean(ViewStatusStorage.Get("GUI.UseAlternateStage", bool.FalseString)); ///