feature: remember the state of repository sidebar (#823)

This commit is contained in:
leo 2024-12-18 15:33:09 +08:00
parent 7526def448
commit 5ec8279d38
No known key found for this signature in database
4 changed files with 112 additions and 37 deletions

View file

@ -90,31 +90,31 @@ namespace SourceGit.Models
{
get;
set;
} = new AvaloniaList<Filter>();
} = [];
public AvaloniaList<CommitTemplate> CommitTemplates
{
get;
set;
} = new AvaloniaList<CommitTemplate>();
} = [];
public AvaloniaList<string> CommitMessages
{
get;
set;
} = new AvaloniaList<string>();
} = [];
public AvaloniaList<IssueTrackerRule> IssueTrackerRules
{
get;
set;
} = new AvaloniaList<IssueTrackerRule>();
} = [];
public AvaloniaList<CustomAction> CustomActions
{
get;
set;
} = new AvaloniaList<CustomAction>();
} = [];
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<string> ExpandedBranchNodesInSideBar
{
get;
set;
} = [];
public Dictionary<string, FilterMode> CollectHistoriesFilters()
{
var map = new Dictionary<string, FilterMode>();

View file

@ -59,6 +59,12 @@ namespace SourceGit.ViewModels
public List<BranchTreeNode> Locals => _locals;
public List<BranchTreeNode> Remotes => _remotes;
public void SetExpandedNodes(List<string> expanded)
{
foreach (var node in expanded)
_expanded.Add(node);
}
public void Run(List<Models.Branch> branches, List<Models.Remote> remotes, bool bForceExpanded)
{
var folders = new Dictionary<string, BranchTreeNode>();
@ -97,20 +103,6 @@ namespace SourceGit.ViewModels
SortNodes(_remotes);
}
public void CollectExpandedNodes(List<BranchTreeNode> 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<BranchTreeNode> roots, Dictionary<string, BranchTreeNode> folders, string prefix, bool bForceExpanded)
{
var fullpath = $"{prefix}/{branch.Name}";

View file

@ -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<Models.Commit> _searchedCommits = new List<Models.Commit>();
private List<string> _revisionFiles = new List<string>();
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<Models.Remote> _remotes = new List<Models.Remote>();
private List<Models.Branch> _branches = new List<Models.Branch>();

View file

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