feature: remember main window position (#1315)
Some checks are pending
Continuous Integration / Build (push) Waiting to run
Continuous Integration / Prepare version string (push) Waiting to run
Continuous Integration / Package (push) Blocked by required conditions
Localization Check / localization-check (push) Waiting to run

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-06-26 17:01:25 +08:00
parent 8d5a6bf4e9
commit f36ab4a189
No known key found for this signature in database
4 changed files with 65 additions and 17 deletions

View file

@ -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)

View file

@ -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;

View file

@ -11,8 +11,7 @@
x:Name="ThisControl"
Icon="/App.ico"
Title="{Binding Title}"
MinWidth="1024" MinHeight="600"
WindowStartupLocation="CenterScreen">
MinWidth="1024" MinHeight="600">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="{Binding #ThisControl.CaptionHeight}"/>

View file

@ -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)