diff --git a/src/MaaWpfGui/Helper/WindowManager.cs b/src/MaaWpfGui/Helper/WindowManager.cs index 9243e29491..d754daf7de 100644 --- a/src/MaaWpfGui/Helper/WindowManager.cs +++ b/src/MaaWpfGui/Helper/WindowManager.cs @@ -39,8 +39,16 @@ namespace MaaWpfGui.Helper 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); - public void MoveWindowToDisplay(string displayName, Window window) + /// + /// 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) { @@ -56,37 +64,40 @@ namespace MaaWpfGui.Helper } } + /// + /// Center other windows in MaaWpfGui.RootView + /// + private void MoveWindowToDisplay(Window window) + { + var mainWindow = Application.Current.MainWindow; + if (mainWindow.WindowState == WindowState.Normal) + { + // In Stylet, CreateWindow().WindowStartupLocation is CenterScreen or CenterOwner (if w.WSLoc == Manual && w.Left == NaN && w.Top == NaN && ...) + window.WindowStartupLocation = WindowStartupLocation.Manual; + window.Left = mainWindow!.Left + ((mainWindow.Width - window.Width) / 2); + window.Top = mainWindow.Top + ((mainWindow.Height - window.Height) / 2); + } + } + /// protected override Window CreateWindow(object viewModel, bool isDialog, IViewAware ownerViewModel) { Window window = base.CreateWindow(viewModel, isDialog, ownerViewModel); - if (bool.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.LoadPositionAndSize, bool.TrueString))) + if (window is RootView) { - if (isDialog || ownerViewModel != null || Math.Abs(Left - DefaultDouble) < 0.01f || Math.Abs(Top - DefaultDouble) < 0.01f) - { - return window; - } - - if (window is RootView) + bool needMoveRootView = bool.Parse(ConfigurationHelper.GetValue(ConfigurationKeys.LoadPositionAndSize, bool.TrueString)); + if (needMoveRootView) { MoveWindowToDisplay(ScreenName, window); } - else - { - // Center other windows in MaaWpfGui.RootView - var mainWindow = Application.Current.MainWindow; - if (mainWindow.WindowState == WindowState.Normal) - { - // In Stylet, CreateWindow().WindowStartupLocation is CenterScreen or CenterOwner (if w.WSLoc == Manual && w.Left == NaN && w.Top == NaN && ...) - window.WindowStartupLocation = WindowStartupLocation.Manual; - window.Left = mainWindow!.Left + ((mainWindow.Width - window.Width) / 2); - window.Top = mainWindow.Top + ((mainWindow.Height - window.Height) / 2); - } - } - } - var app = Application.Current as App; - app!.DarkToStart(); + var app = Application.Current as App; + app!.DarkToStart(); + } + else if (!isDialog && ownerViewModel == null) + { + MoveWindowToDisplay(window); + } return window; }