From 5ec8279d383448af8b160cf23a1fffab11348713 Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 18 Dec 2024 15:33:09 +0800 Subject: [PATCH] feature: remember the state of repository sidebar (#823) --- src/Models/RepositorySettings.cs | 46 ++++++++++++++++-- src/ViewModels/BranchTreeNode.cs | 20 +++----- src/ViewModels/Repository.cs | 80 +++++++++++++++++++++++++------- src/Views/BranchTree.axaml.cs | 3 ++ 4 files changed, 112 insertions(+), 37 deletions(-) diff --git a/src/Models/RepositorySettings.cs b/src/Models/RepositorySettings.cs index 8fe33556..50530c6a 100644 --- a/src/Models/RepositorySettings.cs +++ b/src/Models/RepositorySettings.cs @@ -90,31 +90,31 @@ namespace SourceGit.Models { get; set; - } = new AvaloniaList(); + } = []; public AvaloniaList CommitTemplates { get; set; - } = new AvaloniaList(); + } = []; public AvaloniaList CommitMessages { get; set; - } = new AvaloniaList(); + } = []; public AvaloniaList IssueTrackerRules { get; set; - } = new AvaloniaList(); + } = []; public AvaloniaList CustomActions { get; set; - } = new AvaloniaList(); + } = []; public bool EnableAutoFetch { @@ -158,6 +158,42 @@ namespace SourceGit.Models set; } = "---"; + public bool IsLocalBranchesExpandedInSideBar + { + get; + set; + } = true; + + public bool IsRemotesExpandedInSideBar + { + get; + set; + } = false; + + public bool IsTagsExpandedInSideBar + { + get; + set; + } = false; + + public bool IsSubmodulesExpandedInSideBar + { + get; + set; + } = false; + + public bool IsWorktreeExpandedInSideBar + { + get; + set; + } = false; + + public List ExpandedBranchNodesInSideBar + { + get; + set; + } = []; + public Dictionary CollectHistoriesFilters() { var map = new Dictionary(); diff --git a/src/ViewModels/BranchTreeNode.cs b/src/ViewModels/BranchTreeNode.cs index 025632c1..a8232047 100644 --- a/src/ViewModels/BranchTreeNode.cs +++ b/src/ViewModels/BranchTreeNode.cs @@ -59,6 +59,12 @@ namespace SourceGit.ViewModels public List Locals => _locals; public List Remotes => _remotes; + public void SetExpandedNodes(List expanded) + { + foreach (var node in expanded) + _expanded.Add(node); + } + public void Run(List branches, List remotes, bool bForceExpanded) { var folders = new Dictionary(); @@ -97,20 +103,6 @@ namespace SourceGit.ViewModels SortNodes(_remotes); } - public void CollectExpandedNodes(List nodes) - { - foreach (var node in nodes) - { - if (node.Backend is Models.Branch) - continue; - - if (node.IsExpanded) - _expanded.Add(node.Path); - - CollectExpandedNodes(node.Children); - } - } - private void MakeBranchNode(Models.Branch branch, List roots, Dictionary folders, string prefix, bool bForceExpanded) { var fullpath = $"{prefix}/{branch.Name}"; diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index d8d6806c..df6f2e27 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -322,32 +322,67 @@ namespace SourceGit.ViewModels public bool IsLocalBranchGroupExpanded { - get => _isLocalBranchGroupExpanded; - set => SetProperty(ref _isLocalBranchGroupExpanded, value); + get => _settings.IsLocalBranchesExpandedInSideBar; + set + { + if (value != _settings.IsLocalBranchesExpandedInSideBar) + { + _settings.IsLocalBranchesExpandedInSideBar = value; + OnPropertyChanged(); + } + } } public bool IsRemoteGroupExpanded { - get => _isRemoteGroupExpanded; - set => SetProperty(ref _isRemoteGroupExpanded, value); + get => _settings.IsRemotesExpandedInSideBar; + set + { + if (value != _settings.IsRemotesExpandedInSideBar) + { + _settings.IsRemotesExpandedInSideBar = value; + OnPropertyChanged(); + } + } } public bool IsTagGroupExpanded { - get => _isTagGroupExpanded; - set => SetProperty(ref _isTagGroupExpanded, value); + get => _settings.IsTagsExpandedInSideBar; + set + { + if (value != _settings.IsTagsExpandedInSideBar) + { + _settings.IsTagsExpandedInSideBar = value; + OnPropertyChanged(); + } + } } public bool IsSubmoduleGroupExpanded { - get => _isSubmoduleGroupExpanded; - set => SetProperty(ref _isSubmoduleGroupExpanded, value); + get => _settings.IsSubmodulesExpandedInSideBar; + set + { + if (value != _settings.IsSubmodulesExpandedInSideBar) + { + _settings.IsSubmodulesExpandedInSideBar = value; + OnPropertyChanged(); + } + } } public bool IsWorktreeGroupExpanded { - get => _isWorktreeGroupExpanded; - set => SetProperty(ref _isWorktreeGroupExpanded, value); + get => _settings.IsWorktreeExpandedInSideBar; + set + { + if (value != _settings.IsWorktreeExpandedInSideBar) + { + _settings.IsWorktreeExpandedInSideBar = value; + OnPropertyChanged(); + } + } } public InProgressContext InProgressContext @@ -700,6 +735,22 @@ namespace SourceGit.ViewModels Task.Run(RefreshCommits); } + public void UpdateBranchNodeIsExpanded(BranchTreeNode node) + { + if (_settings == null || !string.IsNullOrWhiteSpace(_filter)) + return; + + if (node.IsExpanded) + { + if (!_settings.ExpandedBranchNodesInSideBar.Contains(node.Path)) + _settings.ExpandedBranchNodesInSideBar.Add(node.Path); + } + else + { + _settings.ExpandedBranchNodesInSideBar.Remove(node.Path); + } + } + public void SetTagFilterMode(Models.Tag tag, Models.FilterMode mode) { var changed = _settings.UpdateHistoriesFilter(tag.Name, Models.FilterType.Tag, mode); @@ -2014,8 +2065,7 @@ namespace SourceGit.ViewModels var builder = new BranchTreeNode.Builder(); if (string.IsNullOrEmpty(_filter)) { - builder.CollectExpandedNodes(_localBranchTrees); - builder.CollectExpandedNodes(_remoteBranchTrees); + builder.SetExpandedNodes(_settings.ExpandedBranchNodesInSideBar); builder.Run(branches, remotes, false); } else @@ -2237,12 +2287,6 @@ namespace SourceGit.ViewModels private List _searchedCommits = new List(); private List _revisionFiles = new List(); - private bool _isLocalBranchGroupExpanded = true; - private bool _isRemoteGroupExpanded = false; - private bool _isTagGroupExpanded = false; - private bool _isSubmoduleGroupExpanded = false; - private bool _isWorktreeGroupExpanded = false; - private string _filter = string.Empty; private List _remotes = new List(); private List _branches = new List(); diff --git a/src/Views/BranchTree.axaml.cs b/src/Views/BranchTree.axaml.cs index d9588a0f..bb9b886f 100644 --- a/src/Views/BranchTree.axaml.cs +++ b/src/Views/BranchTree.axaml.cs @@ -275,6 +275,9 @@ namespace SourceGit.Views rows.RemoveRange(idx + 1, removeCount); } + var repo = DataContext as ViewModels.Repository; + repo?.UpdateBranchNodeIsExpanded(node); + RaiseEvent(new RoutedEventArgs(RowsChangedEvent)); _disableSelectionChangingEvent = false; }