From 42073c9e6be48685630864f6b93c8efa762124d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=AB=E9=9B=A8?= <737345039@qq.com> Date: Tue, 9 May 2023 23:54:12 +0800 Subject: [PATCH 1/3] =?UTF-8?q?refactor:=20=E9=87=8D=E5=86=99=E8=AE=B0?= =?UTF-8?q?=E4=BD=8F=E4=B8=BB=E7=95=8C=E9=9D=A2=E4=BD=8D=E7=BD=AE=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ~把屎交给巨硬~ --- src/MaaWpfGui/Constants/ConfigurationKeys.cs | 12 +- src/MaaWpfGui/Helper/WindowManager.cs | 148 +++++++++++++----- src/MaaWpfGui/Models/WindowPlacement.cs | 62 ++++++++ .../ViewModels/UI/SettingsViewModel.cs | 90 ----------- .../ViewModels/UI/TaskQueueViewModel.cs | 5 - 5 files changed, 170 insertions(+), 147 deletions(-) create mode 100644 src/MaaWpfGui/Models/WindowPlacement.cs diff --git a/src/MaaWpfGui/Constants/ConfigurationKeys.cs b/src/MaaWpfGui/Constants/ConfigurationKeys.cs index 63e2253e5f..4a9846a15a 100644 --- a/src/MaaWpfGui/Constants/ConfigurationKeys.cs +++ b/src/MaaWpfGui/Constants/ConfigurationKeys.cs @@ -21,15 +21,9 @@ namespace MaaWpfGui.Constants public const string Localization = "GUI.Localization"; public const string MinimizeToTray = "GUI.MinimizeToTray"; public const string UseNotify = "GUI.UseNotify"; - public const string MonitorNumber = "GUI.Monitor.Number"; - public const string MonitorWidth = "GUI.Monitor.Width"; - public const string MonitorHeight = "GUI.Monitor.Height"; - public const string PositionLeft = "GUI.Position.Left"; - public const string PositionTop = "GUI.Position.Top"; - public const string WindowWidth = "GUI.Size.Width"; - public const string WindowHeight = "GUI.Size.Height"; - public const string LoadPositionAndSize = "GUI.PositionAndSize.Load"; - public const string SavePositionAndSize = "GUI.PositionAndSize.SaveOnClosing"; + public const string WindowPlacement = "GUI.Placement"; + public const string LoadWindowPlacement = "GUI.Placement.Load"; + public const string SaveWindowPlacement = "GUI.Placement.SaveOnClosing"; public const string UseAlternateStage = "GUI.UseAlternateStage"; public const string HideUnavailableStage = "GUI.HideUnavailableStage"; public const string CustomStageCode = "GUI.CustomStageCode"; diff --git a/src/MaaWpfGui/Helper/WindowManager.cs b/src/MaaWpfGui/Helper/WindowManager.cs index 0af287f7fb..000062ee6e 100644 --- a/src/MaaWpfGui/Helper/WindowManager.cs +++ b/src/MaaWpfGui/Helper/WindowManager.cs @@ -12,13 +12,14 @@ // using System; -using System.Globalization; -using System.Linq; +using System.Runtime.InteropServices; using System.Windows; using MaaWpfGui.Constants; +using MaaWpfGui.Models; using MaaWpfGui.Views.UI; +using Newtonsoft.Json; +using Serilog; using Stylet; -using Screen = System.Windows.Forms.Screen; namespace MaaWpfGui.Helper { @@ -29,40 +30,12 @@ namespace MaaWpfGui.Helper { } - private readonly string ScreenName = ConfigurationHelper.GetValue(ConfigurationKeys.MonitorNumber, string.Empty); - private readonly int ScreenWidth = int.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.MonitorWidth, "-1")); - private readonly int ScreenHeight = int.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.MonitorHeight, "-1")); + private static readonly ILogger _logger = Log.ForContext(); - private static readonly double DefaultDouble = -114514; - private readonly double Left = double.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.PositionLeft, DefaultDouble.ToString(CultureInfo.InvariantCulture)), CultureInfo.InvariantCulture); - private readonly double Top = double.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.PositionTop, DefaultDouble.ToString(CultureInfo.InvariantCulture)), CultureInfo.InvariantCulture); - private readonly double Width = double.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.WindowWidth, DefaultDouble.ToString(CultureInfo.InvariantCulture)), CultureInfo.InvariantCulture); - private readonly double Height = double.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.WindowHeight, DefaultDouble.ToString(CultureInfo.InvariantCulture)), CultureInfo.InvariantCulture); - - /// - /// Move MaaWpfGui.RootView - /// - private void MoveWindowToDisplay(string displayName, Window window) - { - if (Math.Abs(Left - DefaultDouble) < 0.01f || Math.Abs(Top - DefaultDouble) < 0.01f) - { - return; - } - - var screen = Screen.AllScreens.FirstOrDefault(x => x.DeviceName == displayName); - if (screen != null) - { - var screenRect = screen.Bounds; - if (screenRect.Height == ScreenHeight && screenRect.Width == ScreenWidth) - { - window.WindowStartupLocation = WindowStartupLocation.Manual; - window.Left = (int)(screenRect.Left + Left); - window.Top = (int)(screenRect.Top + Top); - window.Width = Width; - window.Height = Height; - } - } - } + private readonly bool _loadWindowPlacement = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.LoadWindowPlacement, bool.TrueString)); + private readonly bool _saveWindowPlacement = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.SaveWindowPlacement, bool.TrueString)); + private readonly bool _minimizeDirectly = bool.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.MinimizeDirectly, bool.FalseString)); + private readonly bool _minimizeToTray = bool.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.MinimizeToTray, bool.FalseString)); /// /// Center other windows in MaaWpfGui.RootView @@ -85,20 +58,30 @@ namespace MaaWpfGui.Helper Window window = base.CreateWindow(viewModel, isDialog, ownerViewModel); if (window is RootView) { - bool needMoveRootView = bool.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.LoadPositionAndSize, bool.TrueString)); - if (needMoveRootView) + if (_loadWindowPlacement && GetConfiguration(out WindowPlacement wp)) { - MoveWindowToDisplay(ScreenName, window); + window.SourceInitialized += (s, e) => + { + bool success = SetWindowPlacement(window, ref wp); + _logger.Information("Whether the window placement was set successfully: {Success}", success); + }; } - bool minimizeDirectly = bool.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.MinimizeDirectly, bool.FalseString)); - if (minimizeDirectly) + if (_loadWindowPlacement && _saveWindowPlacement) + { + window.Closing += (s, e) => + { + GetWindowPlacement(window, out WindowPlacement wp); + SetConfiguration(wp); + }; + } + + if (_minimizeDirectly) { window.WindowState = WindowState.Minimized; } - bool minimizeToTray = bool.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.MinimizeToTray, bool.FalseString)); - if (minimizeDirectly && minimizeToTray) + if (_minimizeDirectly && _minimizeToTray) { window.ShowInTaskbar = false; window.Visibility = Visibility.Hidden; @@ -111,5 +94,84 @@ namespace MaaWpfGui.Helper return window; } + + private bool SetConfiguration(WindowPlacement wp) + { + try + { + // 请在配置文件中修改该部分配置,暂不支持从GUI设置 + // Please modify this part of configuration in the configuration file. + ConfigurationHelper.SetValue(ConfigurationKeys.LoadWindowPlacement, _loadWindowPlacement.ToString()); + ConfigurationHelper.SetValue(ConfigurationKeys.SaveWindowPlacement, _saveWindowPlacement.ToString()); + + return ConfigurationHelper.SetValue(ConfigurationKeys.WindowPlacement, JsonConvert.SerializeObject(wp)); + } + catch (Exception e) + { + _logger.Error(e, "Failed to serialize json string to {Key}", ConfigurationKeys.WindowPlacement); + } + + return false; + } + + private bool GetConfiguration(out WindowPlacement wp) + { + wp = default; + var jsonStr = ConfigurationHelper.GetValue(ConfigurationKeys.WindowPlacement, string.Empty); + if (string.IsNullOrEmpty(jsonStr)) + { + return false; + } + + try + { + wp = JsonConvert.DeserializeObject(jsonStr) ?? throw new Exception("Failed to parse json string"); + return true; + } + catch (Exception e) + { + _logger.Error(e, "Failed to deserialize json string from {Key}", ConfigurationKeys.WindowPlacement); + } + + return false; + } + + private bool SetWindowPlacement(WindowHandle window, ref WindowPlacement wp) + { + try + { + // Load window placement details for previous application session from application settings + // Note - if window was closed on a monitor that is now disconnected from the computer, + // SetWindowPlacement will place the window onto a visible monitor. + wp.Length = Marshal.SizeOf(typeof(WindowPlacement)); + wp.Flags = 0; + + // wp.ShowCmd = wp.ShowCmd == SwShowminimized ? SwShownormal : wp.ShowCmd; + wp.ShowCmd = _minimizeDirectly ? SwShowminimized : SwShownormal; + return SetWindowPlacement(window.Handle, ref wp); + } + catch + { + return false; + } + } + + private bool GetWindowPlacement(WindowHandle window, out WindowPlacement wp) + { + return GetWindowPlacement(window.Handle, out wp); + } + + #region Win32 API declarations to set and get window placement + + [DllImport("user32.dll")] + private static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WindowPlacement lpwndpl); + + [DllImport("user32.dll")] + private static extern bool GetWindowPlacement(IntPtr hWnd, out WindowPlacement lpwndpl); + + private const int SwShownormal = 1; + private const int SwShowminimized = 2; + + #endregion } } diff --git a/src/MaaWpfGui/Models/WindowPlacement.cs b/src/MaaWpfGui/Models/WindowPlacement.cs new file mode 100644 index 0000000000..cddc66dea5 --- /dev/null +++ b/src/MaaWpfGui/Models/WindowPlacement.cs @@ -0,0 +1,62 @@ +// +// 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.Runtime.InteropServices; + +namespace MaaWpfGui.Models +{ + [Serializable] + [StructLayout(LayoutKind.Sequential)] + public struct WindowPlacement + { + public int Length; + public int Flags; + public int ShowCmd; + public Point MinPosition; + public Point MaxPosition; + public Rect NormalPosition; + } + + [Serializable] + [StructLayout(LayoutKind.Sequential)] + public struct Rect + { + public int Left; + public int Top; + public int Right; + public int Bottom; + + public Rect(int left, int top, int right, int bottom) + { + Left = left; + Top = top; + Right = right; + Bottom = bottom; + } + } + + [Serializable] + [StructLayout(LayoutKind.Sequential)] + public struct Point + { + public int X; + public int Y; + + public Point(int x, int y) + { + X = x; + Y = y; + } + } +} diff --git a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs index e181d6b58e..023da4f1b7 100644 --- a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs @@ -2388,96 +2388,6 @@ namespace MaaWpfGui.ViewModels.UI } } - private bool _loadGUIParameters = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.LoadPositionAndSize, bool.TrueString)); - - /// - /// Gets or sets a value indicating whether to load GUI parameters. - /// - public bool LoadGUIParameters - { - get => _loadGUIParameters; - set - { - SetAndNotify(ref _loadGUIParameters, value); - ConfigurationHelper.SetValue(ConfigurationKeys.LoadPositionAndSize, value.ToString()); - if (value) - { - if (SaveGUIParametersOnClosing) - { - Application.Current.MainWindow.Closing += SaveGUIParameters; - } - else - { - SaveGUIParameters(); - } - } - } - } - - private bool _saveGUIParametersOnClosing = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.SavePositionAndSize, 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); - ConfigurationHelper.SetValue(ConfigurationKeys.SavePositionAndSize, value.ToString()); - if (value) - { - Application.Current.MainWindow.Closing += SaveGUIParameters; - } - else - { - Application.Current.MainWindow.Closing -= SaveGUIParameters; - } - } - } - - public void SaveGUIParameters(object sender, EventArgs e) - { - SaveGUIParameters(); - } - - /// - /// Save main window left edge, top edge, width and heigth. - /// - public void SaveGUIParameters() - { - var mainWindow = Application.Current.MainWindow; - if (mainWindow.WindowState != WindowState.Normal) - { - return; - } - - var mainWindowRect = new Rect(mainWindow.Left + 25, mainWindow.Top + 25, mainWindow.Width - 50, mainWindow.Height - 50); - var virtualScreenRect = new Rect(SystemParameters.VirtualScreenLeft, SystemParameters.VirtualScreenTop, SystemParameters.VirtualScreenWidth, SystemParameters.VirtualScreenHeight); - if (!virtualScreenRect.IntersectsWith(mainWindowRect)) - { - return; - } - - // 请在配置文件中修改该部分配置,暂不支持从GUI设置 - // Please modify this part of configuration in the configuration file. - ConfigurationHelper.SetValue(ConfigurationKeys.LoadPositionAndSize, LoadGUIParameters.ToString()); - ConfigurationHelper.SetValue(ConfigurationKeys.SavePositionAndSize, SaveGUIParametersOnClosing.ToString()); - - System.Windows.Forms.Screen currentScreen = - System.Windows.Forms.Screen.FromHandle(new WindowInteropHelper(mainWindow).Handle); - var screenRect = currentScreen.Bounds; - ConfigurationHelper.SetValue(ConfigurationKeys.MonitorNumber, currentScreen.DeviceName); - ConfigurationHelper.SetValue(ConfigurationKeys.MonitorWidth, screenRect.Width.ToString()); - ConfigurationHelper.SetValue(ConfigurationKeys.MonitorHeight, screenRect.Height.ToString()); - - ConfigurationHelper.SetValue(ConfigurationKeys.PositionLeft, (mainWindow.Left - screenRect.Left).ToString(CultureInfo.InvariantCulture)); - ConfigurationHelper.SetValue(ConfigurationKeys.PositionTop, (mainWindow.Top - screenRect.Top).ToString(CultureInfo.InvariantCulture)); - ConfigurationHelper.SetValue(ConfigurationKeys.WindowWidth, mainWindow.Width.ToString(CultureInfo.InvariantCulture)); - ConfigurationHelper.SetValue(ConfigurationKeys.WindowHeight, mainWindow.Height.ToString(CultureInfo.InvariantCulture)); - } - private bool _useAlternateStage = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.UseAlternateStage, bool.FalseString)); /// diff --git a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs index 157f493922..78bd0ec1c5 100644 --- a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs @@ -135,11 +135,6 @@ namespace MaaWpfGui.ViewModels.UI LogItemViewModels = new ObservableCollection(); InitializeItems(); InitTimer(); - - if (Instances.SettingsViewModel.LoadGUIParameters && Instances.SettingsViewModel.SaveGUIParametersOnClosing) - { - Application.Current.MainWindow!.Closing += Instances.SettingsViewModel.SaveGUIParameters; - } } /* From f391e9fc70f6bd7feac854b257379e09c9436bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=AB=E9=9B=A8?= <737345039@qq.com> Date: Wed, 10 May 2023 00:23:33 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=B4=B4=E9=9D=A0?= =?UTF-8?q?=E7=9A=84=E7=AA=97=E5=8F=A3=E4=BD=8D=E7=BD=AE=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E4=B8=8D=E6=AD=A3=E7=A1=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 巨硬—— --- src/MaaWpfGui/Helper/WindowManager.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/MaaWpfGui/Helper/WindowManager.cs b/src/MaaWpfGui/Helper/WindowManager.cs index 000062ee6e..6fcc64c034 100644 --- a/src/MaaWpfGui/Helper/WindowManager.cs +++ b/src/MaaWpfGui/Helper/WindowManager.cs @@ -20,6 +20,7 @@ using MaaWpfGui.Views.UI; using Newtonsoft.Json; using Serilog; using Stylet; +using Rect = MaaWpfGui.Models.Rect; namespace MaaWpfGui.Helper { @@ -158,7 +159,18 @@ namespace MaaWpfGui.Helper private bool GetWindowPlacement(WindowHandle window, out WindowPlacement wp) { - return GetWindowPlacement(window.Handle, out wp); + if (GetWindowPlacement(window.Handle, out wp)) + { + // get the aero snap position of the window + if (wp.ShowCmd == SwShownormal && GetWindowRect(window.Handle, out Rect rect)) + { + wp.NormalPosition = rect; + } + + return true; + } + + return false; } #region Win32 API declarations to set and get window placement @@ -169,6 +181,9 @@ namespace MaaWpfGui.Helper [DllImport("user32.dll")] private static extern bool GetWindowPlacement(IntPtr hWnd, out WindowPlacement lpwndpl); + [DllImport("user32.dll")] + private static extern bool GetWindowRect(IntPtr hWnd, out Rect lpRect); + private const int SwShownormal = 1; private const int SwShowminimized = 2; From 925258aea697846fe0bb1689da999e5d93c3432b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=AB=E9=9B=A8?= <737345039@qq.com> Date: Sat, 6 May 2023 20:49:48 +0800 Subject: [PATCH 3/3] =?UTF-8?q?chore:=20=E5=85=BC=E5=AE=B9=E6=97=A7?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MaaWpfGui/Constants/ConfigurationKeys.cs | 10 ++++ src/MaaWpfGui/Helper/WindowManager.cs | 63 +++++++++++++++++++- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/MaaWpfGui/Constants/ConfigurationKeys.cs b/src/MaaWpfGui/Constants/ConfigurationKeys.cs index 4a9846a15a..55645c6600 100644 --- a/src/MaaWpfGui/Constants/ConfigurationKeys.cs +++ b/src/MaaWpfGui/Constants/ConfigurationKeys.cs @@ -31,6 +31,16 @@ namespace MaaWpfGui.Constants public const string WindowTitlePrefix = "GUI.WindowTitlePrefix"; public const string DarkMode = "GUI.DarkMode"; + public const string MonitorNumber = "GUI.Monitor.Number"; + public const string MonitorWidth = "GUI.Monitor.Width"; + public const string MonitorHeight = "GUI.Monitor.Height"; + public const string PositionLeft = "GUI.Position.Left"; + public const string PositionTop = "GUI.Position.Top"; + public const string WindowWidth = "GUI.Size.Width"; + public const string WindowHeight = "GUI.Size.Height"; + public const string LoadPositionAndSize = "GUI.PositionAndSize.Load"; + public const string SavePositionAndSize = "GUI.PositionAndSize.SaveOnClosing"; + public const string AddressHistory = "Connect.AddressHistory"; public const string AutoDetect = "Connect.AutoDetect"; public const string AlwaysAutoDetect = "Connect.AlwaysAutoDetect"; diff --git a/src/MaaWpfGui/Helper/WindowManager.cs b/src/MaaWpfGui/Helper/WindowManager.cs index 6fcc64c034..19d344c9a4 100644 --- a/src/MaaWpfGui/Helper/WindowManager.cs +++ b/src/MaaWpfGui/Helper/WindowManager.cs @@ -12,6 +12,8 @@ // using System; +using System.Globalization; +using System.Linq; using System.Runtime.InteropServices; using System.Windows; using MaaWpfGui.Constants; @@ -59,7 +61,7 @@ namespace MaaWpfGui.Helper Window window = base.CreateWindow(viewModel, isDialog, ownerViewModel); if (window is RootView) { - if (_loadWindowPlacement && GetConfiguration(out WindowPlacement wp)) + if (_loadWindowPlacement && GetConfiguration(out WindowPlacement wp, window)) { window.SourceInitialized += (s, e) => { @@ -115,12 +117,14 @@ namespace MaaWpfGui.Helper return false; } - private bool GetConfiguration(out WindowPlacement wp) + private bool GetConfiguration(out WindowPlacement wp, Window window) { wp = default; var jsonStr = ConfigurationHelper.GetValue(ConfigurationKeys.WindowPlacement, string.Empty); if (string.IsNullOrEmpty(jsonStr)) { + SetOldConfiguration(window); + DeleteOldConfiguration(); return false; } @@ -188,5 +192,60 @@ namespace MaaWpfGui.Helper private const int SwShowminimized = 2; #endregion + + /// + /// TODO: Delete this + /// 兼容旧配置,过几个版本删(请直接revert) + /// + private void SetOldConfiguration(Window window) + { + // 初始化 + string screenName = ConfigurationHelper.GetValue(ConfigurationKeys.MonitorNumber, string.Empty); + int screenWidth = int.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.MonitorWidth, "-1")); + int screenHeight = int.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.MonitorHeight, "-1")); + + double defaultDouble = -114514; + double left = double.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.PositionLeft, defaultDouble.ToString(CultureInfo.InvariantCulture)), CultureInfo.InvariantCulture); + double top = double.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.PositionTop, defaultDouble.ToString(CultureInfo.InvariantCulture)), CultureInfo.InvariantCulture); + double width = double.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.WindowWidth, defaultDouble.ToString(CultureInfo.InvariantCulture)), CultureInfo.InvariantCulture); + double height = double.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.WindowHeight, defaultDouble.ToString(CultureInfo.InvariantCulture)), CultureInfo.InvariantCulture); + + // Move MaaWpfGui.RootView + if (Math.Abs(left - defaultDouble) < 0.01f || Math.Abs(top - defaultDouble) < 0.01f) + { + return; + } + + var screen = System.Windows.Forms.Screen.AllScreens.FirstOrDefault(x => x.DeviceName == screenName); + if (screen != null) + { + var screenRect = screen.Bounds; + if (screenRect.Height == screenHeight && screenRect.Width == screenWidth) + { + window.WindowStartupLocation = WindowStartupLocation.Manual; + window.Left = (int)(screenRect.Left + left); + window.Top = (int)(screenRect.Top + top); + window.Width = width; + window.Height = height; + } + } + } + + /// + /// TODO: Delete this + /// 兼容旧配置,过几个版本删(请直接revert) + /// + private void DeleteOldConfiguration() + { + ConfigurationHelper.DeleteValue(ConfigurationKeys.MonitorNumber); + ConfigurationHelper.DeleteValue(ConfigurationKeys.MonitorWidth); + ConfigurationHelper.DeleteValue(ConfigurationKeys.MonitorHeight); + ConfigurationHelper.DeleteValue(ConfigurationKeys.PositionLeft); + ConfigurationHelper.DeleteValue(ConfigurationKeys.PositionTop); + ConfigurationHelper.DeleteValue(ConfigurationKeys.WindowWidth); + ConfigurationHelper.DeleteValue(ConfigurationKeys.WindowHeight); + ConfigurationHelper.DeleteValue(ConfigurationKeys.LoadPositionAndSize); + ConfigurationHelper.DeleteValue(ConfigurationKeys.SavePositionAndSize); + } } }