From f1af23cbb3e95e08df6499a09beae651c298eed1 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 5 Feb 2026 18:28:37 +0800 Subject: [PATCH] refactor: sub-window's startup location (#2100) Child window will use current actived window (fall back to `MainWindow`) to calculate the startup location (likes `CenterScreen` mode). The benefits of doing this are: - Solve the problem that newly opened windows always appear on the primary screen in multi-screen situations. - Newly opened windows always display on the currently used screen. This reduces the need for users to move the mouse. Signed-off-by: leo --- src/App.axaml.cs | 50 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/src/App.axaml.cs b/src/App.axaml.cs index 00a57560..723a0251 100644 --- a/src/App.axaml.cs +++ b/src/App.axaml.cs @@ -148,18 +148,52 @@ namespace SourceGit public static void ShowWindow(object data) { - if (data is Views.ChromelessWindow window) + if (data is not Views.ChromelessWindow window) { - window.Show(); - return; + window = CreateViewForViewModel(data) as Views.ChromelessWindow; + if (window == null) + return; + + window.DataContext = data; } - window = CreateViewForViewModel(data) as Views.ChromelessWindow; - if (window != null) + do { - window.DataContext = data; - window.Show(); - } + if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { Windows: { Count: > 0 } windows }) + { + // Try to find the actived window (fall back to `MainWindow`) + Window actived = windows[0]; + if (!actived.IsActive) + { + for (var i = 1; i < windows.Count; i++) + { + var test = windows[i]; + if (test.IsActive) + { + actived = test; + break; + } + } + } + + // Get the screen where current window locates. + var screen = actived.Screens.ScreenFromWindow(actived) ?? actived.Screens.Primary; + if (screen == null) + break; + + // Calculate the startup position (Center Screen Mode) of target window + var rect = new PixelRect(PixelSize.FromSize(window.ClientSize, actived.DesktopScaling)); + var centeredRect = screen.WorkingArea.CenterRect(rect); + if (actived.Screens.ScreenFromPoint(centeredRect.Position) == null) + break; + + // Use the startup position + window.WindowStartupLocation = WindowStartupLocation.Manual; + window.Position = centeredRect.Position; + } + } while (false); + + window.Show(); } public static async Task AskConfirmAsync(string message, Models.ConfirmButtonType buttonType = Models.ConfirmButtonType.OkCancel)