From f36ab4a18923867183f5abceee352bd61915e1c4 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 26 Jun 2025 17:01:25 +0800 Subject: [PATCH] feature: remember main window position (#1315) Signed-off-by: leo --- src/ViewModels/Launcher.cs | 7 +---- src/ViewModels/LayoutInfo.cs | 13 +++++++- src/Views/Launcher.axaml | 3 +- src/Views/Launcher.axaml.cs | 59 +++++++++++++++++++++++++++++++----- 4 files changed, 65 insertions(+), 17 deletions(-) diff --git a/src/ViewModels/Launcher.cs b/src/ViewModels/Launcher.cs index fd2e2626..ede6d84c 100644 --- a/src/ViewModels/Launcher.cs +++ b/src/ViewModels/Launcher.cs @@ -118,13 +118,8 @@ namespace SourceGit.ViewModels UpdateTitle(); } - public void Quit(double width, double height) + public void Quit() { - var pref = Preferences.Instance; - pref.Layout.LauncherWidth = width; - pref.Layout.LauncherHeight = height; - pref.Save(); - _ignoreIndexChange = true; foreach (var one in Pages) diff --git a/src/ViewModels/LayoutInfo.cs b/src/ViewModels/LayoutInfo.cs index d5f7ec2b..37d6e784 100644 --- a/src/ViewModels/LayoutInfo.cs +++ b/src/ViewModels/LayoutInfo.cs @@ -1,5 +1,4 @@ using Avalonia.Controls; - using CommunityToolkit.Mvvm.ComponentModel; namespace SourceGit.ViewModels @@ -18,6 +17,18 @@ namespace SourceGit.ViewModels set; } = 720; + public int LauncherPositionX + { + get; + set; + } = int.MinValue; + + public int LauncherPositionY + { + get; + set; + } = int.MinValue; + public WindowState LauncherWindowState { get; diff --git a/src/Views/Launcher.axaml b/src/Views/Launcher.axaml index bfe03fd5..d506ca0e 100644 --- a/src/Views/Launcher.axaml +++ b/src/Views/Launcher.axaml @@ -11,8 +11,7 @@ x:Name="ThisControl" Icon="/App.ico" Title="{Binding Title}" - MinWidth="1024" MinHeight="600" - WindowStartupLocation="CenterScreen"> + MinWidth="1024" MinHeight="600"> diff --git a/src/Views/Launcher.axaml.cs b/src/Views/Launcher.axaml.cs index 08620f83..d4ecb3c9 100644 --- a/src/Views/Launcher.axaml.cs +++ b/src/Views/Launcher.axaml.cs @@ -42,13 +42,6 @@ namespace SourceGit.Views public Launcher() { - var layout = ViewModels.Preferences.Instance.Layout; - if (layout.LauncherWindowState != WindowState.Maximized) - { - Width = layout.LauncherWidth; - Height = layout.LauncherHeight; - } - if (OperatingSystem.IsMacOS()) { HasLeftCaptionButton = true; @@ -65,6 +58,31 @@ namespace SourceGit.Views } InitializeComponent(); + PositionChanged += OnPositionChanged; + + var layout = ViewModels.Preferences.Instance.Layout; + Width = layout.LauncherWidth; + Height = layout.LauncherHeight; + + var x = layout.LauncherPositionX; + var y = layout.LauncherPositionY; + if (x != int.MinValue && y != int.MinValue && Screens is { } screens) + { + var position = new PixelPoint(x, y); + var size = new PixelSize((int)layout.LauncherWidth, (int)layout.LauncherHeight); + var desiredRect = new PixelRect(position, size); + for (var i = 0; i < screens.ScreenCount; i++) + { + var screen = screens.All[i]; + if (screen.WorkingArea.Contains(desiredRect)) + { + Position = position; + return; + } + } + } + + WindowStartupLocation = WindowStartupLocation.CenterScreen; } public void BringToTop() @@ -113,6 +131,18 @@ namespace SourceGit.Views } } + protected override void OnSizeChanged(SizeChangedEventArgs e) + { + base.OnSizeChanged(e); + + if (WindowState == WindowState.Normal) + { + var layout = ViewModels.Preferences.Instance.Layout; + layout.LauncherWidth = Width; + layout.LauncherHeight = Height; + } + } + protected override void OnKeyDown(KeyEventArgs e) { var vm = DataContext as ViewModels.Launcher; @@ -311,7 +341,20 @@ namespace SourceGit.Views base.OnClosing(e); if (!Design.IsDesignMode && DataContext is ViewModels.Launcher launcher) - launcher.Quit(Width, Height); + { + ViewModels.Preferences.Instance.Save(); + launcher.Quit(); + } + } + + private void OnPositionChanged(object sender, PixelPointEventArgs e) + { + if (WindowState == WindowState.Normal) + { + var layout = ViewModels.Preferences.Instance.Layout; + layout.LauncherPositionX = Position.X; + layout.LauncherPositionY = Position.Y; + } } private void OnOpenWorkspaceMenu(object sender, RoutedEventArgs e)