Skip to content

Commit f36ab4a

Browse files
committed
feature: remember main window position (#1315)
Signed-off-by: leo <[email protected]>
1 parent 8d5a6bf commit f36ab4a

File tree

4 files changed

+65
-17
lines changed

4 files changed

+65
-17
lines changed

src/ViewModels/Launcher.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,8 @@ public Launcher(string startupRepo)
118118
UpdateTitle();
119119
}
120120

121-
public void Quit(double width, double height)
121+
public void Quit()
122122
{
123-
var pref = Preferences.Instance;
124-
pref.Layout.LauncherWidth = width;
125-
pref.Layout.LauncherHeight = height;
126-
pref.Save();
127-
128123
_ignoreIndexChange = true;
129124

130125
foreach (var one in Pages)

src/ViewModels/LayoutInfo.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Avalonia.Controls;
2-
32
using CommunityToolkit.Mvvm.ComponentModel;
43

54
namespace SourceGit.ViewModels
@@ -18,6 +17,18 @@ public double LauncherHeight
1817
set;
1918
} = 720;
2019

20+
public int LauncherPositionX
21+
{
22+
get;
23+
set;
24+
} = int.MinValue;
25+
26+
public int LauncherPositionY
27+
{
28+
get;
29+
set;
30+
} = int.MinValue;
31+
2132
public WindowState LauncherWindowState
2233
{
2334
get;

src/Views/Launcher.axaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
x:Name="ThisControl"
1212
Icon="/App.ico"
1313
Title="{Binding Title}"
14-
MinWidth="1024" MinHeight="600"
15-
WindowStartupLocation="CenterScreen">
14+
MinWidth="1024" MinHeight="600">
1615
<Grid>
1716
<Grid.RowDefinitions>
1817
<RowDefinition Height="{Binding #ThisControl.CaptionHeight}"/>

src/Views/Launcher.axaml.cs

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@ public bool HasRightCaptionButton
4242

4343
public Launcher()
4444
{
45-
var layout = ViewModels.Preferences.Instance.Layout;
46-
if (layout.LauncherWindowState != WindowState.Maximized)
47-
{
48-
Width = layout.LauncherWidth;
49-
Height = layout.LauncherHeight;
50-
}
51-
5245
if (OperatingSystem.IsMacOS())
5346
{
5447
HasLeftCaptionButton = true;
@@ -65,6 +58,31 @@ public Launcher()
6558
}
6659

6760
InitializeComponent();
61+
PositionChanged += OnPositionChanged;
62+
63+
var layout = ViewModels.Preferences.Instance.Layout;
64+
Width = layout.LauncherWidth;
65+
Height = layout.LauncherHeight;
66+
67+
var x = layout.LauncherPositionX;
68+
var y = layout.LauncherPositionY;
69+
if (x != int.MinValue && y != int.MinValue && Screens is { } screens)
70+
{
71+
var position = new PixelPoint(x, y);
72+
var size = new PixelSize((int)layout.LauncherWidth, (int)layout.LauncherHeight);
73+
var desiredRect = new PixelRect(position, size);
74+
for (var i = 0; i < screens.ScreenCount; i++)
75+
{
76+
var screen = screens.All[i];
77+
if (screen.WorkingArea.Contains(desiredRect))
78+
{
79+
Position = position;
80+
return;
81+
}
82+
}
83+
}
84+
85+
WindowStartupLocation = WindowStartupLocation.CenterScreen;
6886
}
6987

7088
public void BringToTop()
@@ -113,6 +131,18 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
113131
}
114132
}
115133

134+
protected override void OnSizeChanged(SizeChangedEventArgs e)
135+
{
136+
base.OnSizeChanged(e);
137+
138+
if (WindowState == WindowState.Normal)
139+
{
140+
var layout = ViewModels.Preferences.Instance.Layout;
141+
layout.LauncherWidth = Width;
142+
layout.LauncherHeight = Height;
143+
}
144+
}
145+
116146
protected override void OnKeyDown(KeyEventArgs e)
117147
{
118148
var vm = DataContext as ViewModels.Launcher;
@@ -311,7 +341,20 @@ protected override void OnClosing(WindowClosingEventArgs e)
311341
base.OnClosing(e);
312342

313343
if (!Design.IsDesignMode && DataContext is ViewModels.Launcher launcher)
314-
launcher.Quit(Width, Height);
344+
{
345+
ViewModels.Preferences.Instance.Save();
346+
launcher.Quit();
347+
}
348+
}
349+
350+
private void OnPositionChanged(object sender, PixelPointEventArgs e)
351+
{
352+
if (WindowState == WindowState.Normal)
353+
{
354+
var layout = ViewModels.Preferences.Instance.Layout;
355+
layout.LauncherPositionX = Position.X;
356+
layout.LauncherPositionY = Position.Y;
357+
}
315358
}
316359

317360
private void OnOpenWorkspaceMenu(object sender, RoutedEventArgs e)

0 commit comments

Comments
 (0)