From 9934586ee09d6fd000a1861d5fe4c716a3546fd3 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 9 Sep 2024 19:40:44 +0800 Subject: [PATCH] enhance: remember the active page index for each workspace (#445) --- src/ViewModels/Launcher.cs | 49 +++++++++++++++++++++++++++++++------ src/ViewModels/Workspace.cs | 8 +++++- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/ViewModels/Launcher.cs b/src/ViewModels/Launcher.cs index 00080bcc..b651f5d7 100644 --- a/src/ViewModels/Launcher.cs +++ b/src/ViewModels/Launcher.cs @@ -29,12 +29,19 @@ namespace SourceGit.ViewModels set { if (SetProperty(ref _activePage, value)) + { PopupHost.Active = value; + + if (!_ignoreIndexChange && value is { Data: Repository }) + ActiveWorkspace.ActiveIdx = Pages.IndexOf(value); + } } } public Launcher(string startupRepo) { + _ignoreIndexChange = true; + Pages = new AvaloniaList(); AddNewTab(); @@ -61,7 +68,16 @@ namespace SourceGit.ViewModels OpenRepositoryInTab(node, null); } - ActivePage = Pages[0]; + var activeIdx = ActiveWorkspace.ActiveIdx; + if (activeIdx >= 0 && activeIdx < Pages.Count) + { + ActivePage = Pages[activeIdx]; + } + else + { + ActivePage = Pages[0]; + ActiveWorkspace.ActiveIdx = 0; + } } else { @@ -75,14 +91,17 @@ namespace SourceGit.ViewModels IsError = true, Message = $"Given path: '{startupRepo}' is NOT a valid repository!" }); - return; } - - var normalized = test.StdOut.Trim().Replace("\\", "/"); - var node = pref.FindOrAddNodeByRepositoryPath(normalized, null, false); - Welcome.Instance.Refresh(); - OpenRepositoryInTab(node, null); + else + { + var normalized = test.StdOut.Trim().Replace("\\", "/"); + var node = pref.FindOrAddNodeByRepositoryPath(normalized, null, false); + Welcome.Instance.Refresh(); + OpenRepositoryInTab(node, null); + } } + + _ignoreIndexChange = false; } public void AddNewTab() @@ -398,6 +417,8 @@ namespace SourceGit.ViewModels private void SwitchWorkspace(Workspace to) { + _ignoreIndexChange = true; + var pref = Preference.Instance; foreach (var w in pref.Workspaces) w.IsActive = false; @@ -409,7 +430,6 @@ namespace SourceGit.ViewModels CloseRepositoryInTab(one, false); Pages.Clear(); - ActivePage = null; AddNewTab(); var repos = to.Repositories.ToArray(); @@ -430,6 +450,18 @@ namespace SourceGit.ViewModels OpenRepositoryInTab(node, null); } + var activeIdx = to.ActiveIdx; + if (activeIdx >= 0 && activeIdx < Pages.Count) + { + ActivePage = Pages[activeIdx]; + } + else + { + ActivePage = Pages[0]; + to.ActiveIdx = 0; + } + + _ignoreIndexChange = false; GC.Collect(); } @@ -449,5 +481,6 @@ namespace SourceGit.ViewModels private Workspace _activeWorkspace = null; private LauncherPage _activePage = null; + private bool _ignoreIndexChange = false; } } diff --git a/src/ViewModels/Workspace.cs b/src/ViewModels/Workspace.cs index 8a53d577..4c7c288a 100644 --- a/src/ViewModels/Workspace.cs +++ b/src/ViewModels/Workspace.cs @@ -31,6 +31,12 @@ namespace SourceGit.ViewModels set; } = new List(); + public int ActiveIdx + { + get; + set; + } = 0; + public bool IsActive { get => _isActive; @@ -52,7 +58,7 @@ namespace SourceGit.ViewModels private string _name = string.Empty; private uint _color = 0; - private IBrush _brush = null; private bool _isActive = false; + private IBrush _brush = null; } }