From f977e76ea706deb08fda64450c5a6889cb3f44bd Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 11 Jun 2024 16:36:52 +0800 Subject: [PATCH] feature: remember window size and layout information (#165) --- src/ViewModels/LayoutInfo.cs | 80 +++++++++++++++++++++++++++++++++++ src/ViewModels/Preference.cs | 7 +++ src/Views/CommitChanges.axaml | 2 +- src/Views/Launcher.axaml | 2 + src/Views/Repository.axaml | 2 +- src/Views/RevisionFiles.axaml | 2 +- src/Views/StashesPage.axaml | 2 +- src/Views/WorkingCopy.axaml | 2 +- 8 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 src/ViewModels/LayoutInfo.cs diff --git a/src/ViewModels/LayoutInfo.cs b/src/ViewModels/LayoutInfo.cs new file mode 100644 index 00000000..f22258e9 --- /dev/null +++ b/src/ViewModels/LayoutInfo.cs @@ -0,0 +1,80 @@ +using System; +using System.Text.Json.Serialization; +using System.Text.Json; + +using Avalonia.Controls; + +using CommunityToolkit.Mvvm.ComponentModel; + +namespace SourceGit.ViewModels +{ + public class LayoutInfo : ObservableObject + { + public double LauncherWidth + { + get; + set; + } = 1280; + + public double LauncherHeight + { + get; + set; + } = 720; + + [JsonConverter(typeof(GridLengthConverter))] + public GridLength RepositorySidebarWidth + { + get => _repositorySidebarWidth; + set => SetProperty(ref _repositorySidebarWidth, value); + } + + [JsonConverter(typeof(GridLengthConverter))] + public GridLength WorkingCopyLeftWidth + { + get => _workingCopyLeftWidth; + set => SetProperty(ref _workingCopyLeftWidth, value); + } + + [JsonConverter(typeof(GridLengthConverter))] + public GridLength StashesLeftWidth + { + get => _stashesLeftWidth; + set => SetProperty(ref _stashesLeftWidth, value); + } + + [JsonConverter(typeof(GridLengthConverter))] + public GridLength CommitDetailChangesLeftWidth + { + get => _commitDetailChangesLeftWidth; + set => SetProperty(ref _commitDetailChangesLeftWidth, value); + } + + [JsonConverter(typeof(GridLengthConverter))] + public GridLength CommitDetailFilesLeftWidth + { + get => _commitDetailFilesLeftWidth; + set => SetProperty(ref _commitDetailFilesLeftWidth, value); + } + + private GridLength _repositorySidebarWidth = new GridLength(250, GridUnitType.Pixel); + private GridLength _workingCopyLeftWidth = new GridLength(300, GridUnitType.Pixel); + private GridLength _stashesLeftWidth = new GridLength(300, GridUnitType.Pixel); + private GridLength _commitDetailChangesLeftWidth = new GridLength(256, GridUnitType.Pixel); + private GridLength _commitDetailFilesLeftWidth = new GridLength(256, GridUnitType.Pixel); + } + + public class GridLengthConverter : JsonConverter + { + public override GridLength Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var size = reader.GetDouble(); + return new GridLength(size, GridUnitType.Pixel); + } + + public override void Write(Utf8JsonWriter writer, GridLength value, JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } +} diff --git a/src/ViewModels/Preference.cs b/src/ViewModels/Preference.cs index 917349e0..debe672a 100644 --- a/src/ViewModels/Preference.cs +++ b/src/ViewModels/Preference.cs @@ -109,6 +109,12 @@ namespace SourceGit.ViewModels set => SetProperty(ref _defaultFontSize, value); } + public LayoutInfo Layout + { + get => _layout; + set => SetProperty(ref _layout, value); + } + public string AvatarServer { get => Models.AvatarManager.SelectedServer; @@ -531,6 +537,7 @@ namespace SourceGit.ViewModels private FontFamily _defaultFont = null; private FontFamily _monospaceFont = null; private double _defaultFontSize = 13; + private LayoutInfo _layout = new LayoutInfo(); private int _maxHistoryCommits = 20000; private bool _restoreTabs = false; diff --git a/src/Views/CommitChanges.axaml b/src/Views/CommitChanges.axaml index 8dd585bd..fffb3bd3 100644 --- a/src/Views/CommitChanges.axaml +++ b/src/Views/CommitChanges.axaml @@ -10,7 +10,7 @@ x:DataType="vm:CommitDetail"> - + diff --git a/src/Views/Launcher.axaml b/src/Views/Launcher.axaml index 16e242bc..4399b472 100644 --- a/src/Views/Launcher.axaml +++ b/src/Views/Launcher.axaml @@ -15,6 +15,8 @@ Title="SourceGit" Background="Transparent" MinWidth="1280" MinHeight="720" + Width="{Binding Source={x:Static vm:Preference.Instance}, Path=Layout.LauncherWidth, Mode=TwoWay}" + Height="{Binding Source={x:Static vm:Preference.Instance}, Path=Layout.LauncherHeight, Mode=TwoWay}" WindowStartupLocation="CenterScreen" ExtendClientAreaToDecorationsHint="True" ExtendClientAreaChromeHints="NoChrome" diff --git a/src/Views/Repository.axaml b/src/Views/Repository.axaml index 7a631e31..40144289 100644 --- a/src/Views/Repository.axaml +++ b/src/Views/Repository.axaml @@ -103,7 +103,7 @@ - + diff --git a/src/Views/RevisionFiles.axaml b/src/Views/RevisionFiles.axaml index 9c999061..a3fb85e7 100644 --- a/src/Views/RevisionFiles.axaml +++ b/src/Views/RevisionFiles.axaml @@ -12,7 +12,7 @@ x:DataType="vm:CommitDetail"> - + diff --git a/src/Views/StashesPage.axaml b/src/Views/StashesPage.axaml index a3d91d59..3a98c852 100644 --- a/src/Views/StashesPage.axaml +++ b/src/Views/StashesPage.axaml @@ -11,7 +11,7 @@ x:DataType="vm:StashesPage"> - + diff --git a/src/Views/WorkingCopy.axaml b/src/Views/WorkingCopy.axaml index 77013178..88c0dce9 100644 --- a/src/Views/WorkingCopy.axaml +++ b/src/Views/WorkingCopy.axaml @@ -10,7 +10,7 @@ x:DataType="vm:WorkingCopy"> - +