feature: supports filter displayed branches

This commit is contained in:
leo 2024-05-14 11:47:56 +08:00
parent 17e48d86fe
commit 02e71d4d75
6 changed files with 121 additions and 43 deletions

View file

@ -342,6 +342,7 @@
<x:String x:Key="Text.Repository.Configure" xml:space="preserve">Configure this repository</x:String>
<x:String x:Key="Text.Repository.Continue" xml:space="preserve">CONTINUE</x:String>
<x:String x:Key="Text.Repository.Explore" xml:space="preserve">Open In File Browser</x:String>
<x:String x:Key="Text.Repository.FilterBranchTip" xml:space="preserve">Filter Branches</x:String>
<x:String x:Key="Text.Repository.LocalBranches" xml:space="preserve">LOCAL BRANCHES</x:String>
<x:String x:Key="Text.Repository.NavigateToCurrentHead" xml:space="preserve">Navigate To HEAD</x:String>
<x:String x:Key="Text.Repository.NewBranch" xml:space="preserve">Create Branch</x:String>

View file

@ -342,6 +342,7 @@
<x:String x:Key="Text.Repository.Configure" xml:space="preserve">配置本仓库</x:String>
<x:String x:Key="Text.Repository.Continue" xml:space="preserve">下一步</x:String>
<x:String x:Key="Text.Repository.Explore" xml:space="preserve">在文件浏览器中打开</x:String>
<x:String x:Key="Text.Repository.FilterBranchTip" xml:space="preserve">过滤显示分支</x:String>
<x:String x:Key="Text.Repository.LocalBranches" xml:space="preserve">本地分支</x:String>
<x:String x:Key="Text.Repository.NavigateToCurrentHead" xml:space="preserve">定位HEAD</x:String>
<x:String x:Key="Text.Repository.NewBranch" xml:space="preserve">新建分支</x:String>

View file

@ -3,7 +3,7 @@ using System.Collections.Generic;
using Avalonia.Collections;
namespace SourceGit.Models
namespace SourceGit.ViewModels
{
public enum BranchTreeNodeType
{
@ -23,12 +23,12 @@ namespace SourceGit.Models
public bool IsUpstreamTrackStatusVisible
{
get => IsBranch && !string.IsNullOrEmpty((Backend as Branch).UpstreamTrackStatus);
get => IsBranch && !string.IsNullOrEmpty((Backend as Models.Branch).UpstreamTrackStatus);
}
public string UpstreamTrackStatus
{
get => Type == BranchTreeNodeType.Branch ? (Backend as Branch).UpstreamTrackStatus : "";
get => Type == BranchTreeNodeType.Branch ? (Backend as Models.Branch).UpstreamTrackStatus : "";
}
public bool IsRemote
@ -48,7 +48,7 @@ namespace SourceGit.Models
public bool IsCurrent
{
get => IsBranch && (Backend as Branch).IsCurrent;
get => IsBranch && (Backend as Models.Branch).IsCurrent;
}
public class Builder
@ -56,7 +56,7 @@ namespace SourceGit.Models
public List<BranchTreeNode> Locals => _locals;
public List<BranchTreeNode> Remotes => _remotes;
public void Run(List<Branch> branches, List<Remote> remotes)
public void Run(List<Models.Branch> branches, List<Models.Remote> remotes, bool bForceExpanded)
{
foreach (var remote in remotes)
{
@ -66,7 +66,7 @@ namespace SourceGit.Models
Name = remote.Name,
Type = BranchTreeNodeType.Remote,
Backend = remote,
IsExpanded = _expanded.Contains(path),
IsExpanded = bForceExpanded || _expanded.Contains(path),
};
_maps.Add(path, node);
@ -78,13 +78,13 @@ namespace SourceGit.Models
var isFiltered = _filters.Contains(branch.FullName);
if (branch.IsLocal)
{
MakeBranchNode(branch, _locals, "local", isFiltered);
MakeBranchNode(branch, _locals, "local", isFiltered, bForceExpanded);
}
else
{
var remote = _remotes.Find(x => x.Name == branch.Remote);
if (remote != null)
MakeBranchNode(branch, remote.Children, $"remote/{remote.Name}", isFiltered);
MakeBranchNode(branch, remote.Children, $"remote/{remote.Name}", isFiltered, bForceExpanded);
}
}
@ -113,7 +113,7 @@ namespace SourceGit.Models
}
}
private void MakeBranchNode(Branch branch, List<BranchTreeNode> roots, string prefix, bool isFiltered)
private void MakeBranchNode(Models.Branch branch, List<BranchTreeNode> roots, string prefix, bool isFiltered, bool bForceExpanded)
{
var subs = branch.Name.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
@ -132,8 +132,8 @@ namespace SourceGit.Models
}
BranchTreeNode lastFolder = null;
string path = prefix;
for (int i = 0; i < subs.Length - 1; i++)
var path = prefix;
for (var i = 0; i < subs.Length - 1; i++)
{
path = string.Concat(path, "/", subs[i]);
if (_maps.TryGetValue(path, out var value))
@ -146,7 +146,7 @@ namespace SourceGit.Models
{
Name = subs[i],
Type = BranchTreeNodeType.Folder,
IsExpanded = branch.IsCurrent || _expanded.Contains(path),
IsExpanded = bForceExpanded || branch.IsCurrent || _expanded.Contains(path),
};
roots.Add(lastFolder);
_maps.Add(path, lastFolder);
@ -157,7 +157,7 @@ namespace SourceGit.Models
{
Name = subs[i],
Type = BranchTreeNodeType.Folder,
IsExpanded = branch.IsCurrent || _expanded.Contains(path),
IsExpanded = bForceExpanded || branch.IsCurrent || _expanded.Contains(path),
};
_maps.Add(path, folder);
lastFolder.Children.Add(folder);
@ -186,7 +186,7 @@ namespace SourceGit.Models
}
else
{
return (int)(l.Type) - (int)(r.Type);
return (int)l.Type - (int)r.Type;
}
});

View file

@ -89,6 +89,21 @@ namespace SourceGit.ViewModels
set => SetProperty(ref _selectedView, value);
}
[JsonIgnore]
public string SearchBranchFilter
{
get => _searchBranchFilter;
set
{
if (SetProperty(ref _searchBranchFilter, value))
{
var builder = BuildBranchTree(_branches, _remotes);
LocalBranchTrees = builder.Locals;
RemoteBranchTrees = builder.Remotes;
}
}
}
[JsonIgnore]
public List<Models.Remote> Remotes
{
@ -104,14 +119,14 @@ namespace SourceGit.ViewModels
}
[JsonIgnore]
public List<Models.BranchTreeNode> LocalBranchTrees
public List<BranchTreeNode> LocalBranchTrees
{
get => _localBranchTrees;
private set => SetProperty(ref _localBranchTrees, value);
}
[JsonIgnore]
public List<Models.BranchTreeNode> RemoteBranchTrees
public List<BranchTreeNode> RemoteBranchTrees
{
get => _remoteBranchTrees;
private set => SetProperty(ref _remoteBranchTrees, value);
@ -422,6 +437,11 @@ namespace SourceGit.ViewModels
SearchedCommits = visible;
}
public void ClearSearchBranchFilter()
{
SearchBranchFilter = string.Empty;
}
public void SetWatcherEnabled(bool enabled)
{
if (_watcher != null)
@ -533,12 +553,7 @@ namespace SourceGit.ViewModels
{
var branches = new Commands.QueryBranches(FullPath).Result();
var remotes = new Commands.QueryRemotes(FullPath).Result();
var builder = new Models.BranchTreeNode.Builder();
builder.SetFilters(Filters);
builder.CollectExpandedNodes(_localBranchTrees, true);
builder.CollectExpandedNodes(_remoteBranchTrees, false);
builder.Run(branches, remotes);
var builder = BuildBranchTree(branches, remotes);
Dispatcher.UIThread.Invoke(() =>
{
@ -1354,6 +1369,32 @@ namespace SourceGit.ViewModels
return menu;
}
private BranchTreeNode.Builder BuildBranchTree(List<Models.Branch> branches, List<Models.Remote> remotes)
{
var builder = new BranchTreeNode.Builder();
builder.SetFilters(Filters);
if (string.IsNullOrEmpty(_searchBranchFilter))
{
builder.CollectExpandedNodes(_localBranchTrees, true);
builder.CollectExpandedNodes(_remoteBranchTrees, false);
builder.Run(branches, remotes, false);
}
else
{
var visibles = new List<Models.Branch>();
foreach (var b in branches)
{
if (b.FullName.Contains(_searchBranchFilter, StringComparison.OrdinalIgnoreCase))
visibles.Add(b);
}
builder.Run(visibles, remotes, true);
}
return builder;
}
private string _fullpath = string.Empty;
private string _gitDir = string.Empty;
private Models.GitFlow _gitflow = new Models.GitFlow();
@ -1372,10 +1413,12 @@ namespace SourceGit.ViewModels
private bool _isTagGroupExpanded = false;
private bool _isSubmoduleGroupExpanded = false;
private string _searchBranchFilter = string.Empty;
private List<Models.Remote> _remotes = new List<Models.Remote>();
private List<Models.Branch> _branches = new List<Models.Branch>();
private List<Models.BranchTreeNode> _localBranchTrees = new List<Models.BranchTreeNode>();
private List<Models.BranchTreeNode> _remoteBranchTrees = new List<Models.BranchTreeNode>();
private List<BranchTreeNode> _localBranchTrees = new List<BranchTreeNode>();
private List<BranchTreeNode> _remoteBranchTrees = new List<BranchTreeNode>();
private List<Models.Tag> _tags = new List<Models.Tag>();
private List<string> _submodules = new List<string>();
private bool _canCommitWithPush = false;

View file

@ -109,7 +109,7 @@
</Grid.ColumnDefinitions>
<!-- Left Normal Mode -->
<Grid Grid.Column="0" RowDefinitions="28,Auto,28,Auto,28,*,28,Auto,28,Auto" Margin="0,0,0,4" IsVisible="{Binding !IsSearching}">
<Grid Grid.Column="0" RowDefinitions="28,Auto,5,28,28,Auto,28,*,28,Auto,28,Auto" Margin="0,0,0,4" IsVisible="{Binding !IsSearching}">
<!-- WorkingCopy -->
<TextBlock Grid.Row="0" Classes="group_header_label" Text="{DynamicResource Text.Repository.Workspace}"/>
<ListBox Grid.Row="1" Classes="page_switcher" Background="Transparent" SelectedIndex="{Binding SelectedViewIndex, Mode=TwoWay}">
@ -159,9 +159,42 @@
</ListBoxItem>
</ListBox>
<!-- Filter Branches -->
<Rectangle Grid.Row="2" Height=".65" HorizontalAlignment="Stretch" VerticalAlignment="Center" Fill="{DynamicResource Brush.Border2}"/>
<TextBox Grid.Row="3"
Margin="4,2,4,0"
Height="24"
BorderThickness="1"
BorderBrush="{DynamicResource Brush.Border2}"
Background="{DynamicResource Brush.Contents}"
Watermark="{DynamicResource Text.Repository.FilterBranchTip}"
Text="{Binding SearchBranchFilter, Mode=TwoWay}"
VerticalContentAlignment="Center">
<TextBox.InnerLeftContent>
<Path Width="14" Height="14"
Margin="6,0,0,0"
Fill="{DynamicResource Brush.FG2}"
Data="{StaticResource Icons.Search}"/>
</TextBox.InnerLeftContent>
<TextBox.InnerRightContent>
<Button Classes="icon_button"
Width="16"
Margin="0,0,6,0"
Command="{Binding ClearSearchBranchFilter}"
IsVisible="{Binding SearchBranchFilter, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
HorizontalAlignment="Right">
<Path Width="14" Height="14"
Margin="0,1,0,0"
Fill="{DynamicResource Brush.FG1}"
Data="{StaticResource Icons.Clear}"/>
</Button>
</TextBox.InnerRightContent>
</TextBox>
<!-- Local Branches -->
<TextBlock Grid.Row="2" Classes="group_header_label" Text="{DynamicResource Text.Repository.LocalBranches}"/>
<TreeView Grid.Row="3"
<TextBlock Grid.Row="4" Classes="group_header_label" Text="{DynamicResource Text.Repository.LocalBranches}"/>
<TreeView Grid.Row="5"
x:Name="localBranchTree"
MaxHeight="400"
ItemsSource="{Binding LocalBranchTrees}"
@ -170,12 +203,12 @@
LostFocus="OnLocalBranchTreeLostFocus"
SelectionChanged="OnLocalBranchTreeSelectionChanged">
<TreeView.Styles>
<Style Selector="TreeViewItem" x:DataType="m:BranchTreeNode">
<Style Selector="TreeViewItem" x:DataType="vm:BranchTreeNode">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
</Style>
</TreeView.Styles>
<TreeView.ItemTemplate>
<TreeDataTemplate ItemsSource="{Binding Children}" x:DataType="{x:Type m:BranchTreeNode}">
<TreeDataTemplate ItemsSource="{Binding Children}" x:DataType="{x:Type vm:BranchTreeNode}">
<Grid Height="24" ColumnDefinitions="20,*,Auto,Auto" Background="Transparent" ContextRequested="OnLocalBranchContextMenuRequested" DoubleTapped="OnDoubleTappedLocalBranchNode">
<Path Grid.Column="0" Classes="folder_icon" Width="12" Height="12" HorizontalAlignment="Left" Margin="0,1,0,0" IsVisible="{Binding IsFolder}"/>
<Path Grid.Column="0" Width="12" Height="12" HorizontalAlignment="Left" Margin="0,2,0,0" Data="{StaticResource Icons.Check}" IsVisible="{Binding IsCurrent}" VerticalAlignment="Center"/>
@ -208,13 +241,13 @@
</TreeView>
<!-- Remotes -->
<Grid Grid.Row="4" ColumnDefinitions="*,Auto">
<Grid Grid.Row="6" ColumnDefinitions="*,Auto">
<TextBlock Grid.Column="0" Classes="group_header_label" Text="{DynamicResource Text.Repository.Remotes}"/>
<Button Grid.Column="1" Classes="icon_button" Width="14" Margin="8,0" Command="{Binding AddRemote}" ToolTip.Tip="{DynamicResource Text.Repository.Remotes.Add}">
<Path Width="12" Height="12" Data="{StaticResource Icons.Remote.Add}"/>
</Button>
</Grid>
<TreeView Grid.Row="5"
<TreeView Grid.Row="7"
x:Name="remoteBranchTree"
ItemsSource="{Binding RemoteBranchTrees}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
@ -222,13 +255,13 @@
LostFocus="OnRemoteBranchTreeLostFocus"
SelectionChanged="OnRemoteBranchTreeSelectionChanged">
<TreeView.Styles>
<Style Selector="TreeViewItem" x:DataType="m:BranchTreeNode">
<Style Selector="TreeViewItem" x:DataType="vm:BranchTreeNode">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
</Style>
</TreeView.Styles>
<TreeView.ItemTemplate>
<TreeDataTemplate ItemsSource="{Binding Children}" x:DataType="{x:Type m:BranchTreeNode}">
<TreeDataTemplate ItemsSource="{Binding Children}" x:DataType="{x:Type vm:BranchTreeNode}">
<Grid Height="24" ColumnDefinitions="20,*,Auto" Background="Transparent" ContextRequested="OnRemoteBranchContextMenuRequested">
<Path Grid.Column="0" Classes="folder_icon" Width="10" Height="10" HorizontalAlignment="Left" Margin="0,2,0,0" IsVisible="{Binding IsFolder}" VerticalAlignment="Center"/>
<Path Grid.Column="0" Width="12" Height="12" HorizontalAlignment="Left" Margin="0,2,0,0" Data="{StaticResource Icons.Remote}" IsVisible="{Binding IsRemote}" VerticalAlignment="Center"/>
@ -250,7 +283,7 @@
</TreeView>
<!-- Tags -->
<ToggleButton Grid.Row="6" Classes="group_expander" IsChecked="{Binding IsTagGroupExpanded, Mode=TwoWay}">
<ToggleButton Grid.Row="8" Classes="group_expander" IsChecked="{Binding IsTagGroupExpanded, Mode=TwoWay}">
<Grid ColumnDefinitions="Auto,*,Auto">
<TextBlock Grid.Column="0" Classes="group_header_label" Margin="4,0,0,0" Text="{DynamicResource Text.Repository.Tags}"/>
<TextBlock Grid.Column="1" Text="{Binding Tags, Converter={x:Static c:ListConverters.ToCount}}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
@ -259,7 +292,7 @@
</Button>
</Grid>
</ToggleButton>
<DataGrid Grid.Row="7"
<DataGrid Grid.Row="9"
MaxHeight="200"
Background="Transparent"
ItemsSource="{Binding Tags}"
@ -310,7 +343,7 @@
</DataGrid>
<!-- Submodules -->
<ToggleButton Grid.Row="8" Classes="group_expander" IsChecked="{Binding IsSubmoduleGroupExpanded, Mode=TwoWay}">
<ToggleButton Grid.Row="10" Classes="group_expander" IsChecked="{Binding IsSubmoduleGroupExpanded, Mode=TwoWay}">
<Grid ColumnDefinitions="Auto,*,Auto,Auto">
<TextBlock Grid.Column="0" Classes="group_header_label" Margin="4,0,0,0" Text="{DynamicResource Text.Repository.Submodules}"/>
<TextBlock Grid.Column="1" Text="{Binding Submodules, Converter={x:Static c:ListConverters.ToCount}}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
@ -328,7 +361,7 @@
</Button>
</Grid>
</ToggleButton>
<DataGrid Grid.Row="9"
<DataGrid Grid.Row="11"
MaxHeight="200"
Background="Transparent"
ItemsSource="{Binding Submodules}"

View file

@ -98,7 +98,7 @@ namespace SourceGit.Views
{
remoteBranchTree.UnselectAll();
var node = tree.SelectedItem as Models.BranchTreeNode;
var node = tree.SelectedItem as ViewModels.BranchTreeNode;
if (node.IsBranch && DataContext is ViewModels.Repository repo)
{
repo.NavigateToCommit((node.Backend as Models.Branch).Head);
@ -112,7 +112,7 @@ namespace SourceGit.Views
{
localBranchTree.UnselectAll();
var node = tree.SelectedItem as Models.BranchTreeNode;
var node = tree.SelectedItem as ViewModels.BranchTreeNode;
if (node.IsBranch && DataContext is ViewModels.Repository repo)
{
repo.NavigateToCommit((node.Backend as Models.Branch).Head);
@ -171,7 +171,7 @@ namespace SourceGit.Views
if (sender is ToggleButton toggle)
{
var filter = string.Empty;
if (toggle.DataContext is Models.BranchTreeNode node)
if (toggle.DataContext is ViewModels.BranchTreeNode node)
{
if (node.IsBranch)
{
@ -196,7 +196,7 @@ namespace SourceGit.Views
{
remoteBranchTree.UnselectAll();
if (sender is Grid grid && grid.DataContext is Models.BranchTreeNode node)
if (sender is Grid grid && grid.DataContext is ViewModels.BranchTreeNode node)
{
if (node.IsBranch && DataContext is ViewModels.Repository repo)
{
@ -213,7 +213,7 @@ namespace SourceGit.Views
{
localBranchTree.UnselectAll();
if (sender is Grid grid && grid.DataContext is Models.BranchTreeNode node && DataContext is ViewModels.Repository repo)
if (sender is Grid grid && grid.DataContext is ViewModels.BranchTreeNode node && DataContext is ViewModels.Repository repo)
{
if (node.IsRemote)
{
@ -291,7 +291,7 @@ namespace SourceGit.Views
if (sender is Grid grid && DataContext is ViewModels.Repository repo)
{
var node = grid.DataContext as Models.BranchTreeNode;
var node = grid.DataContext as ViewModels.BranchTreeNode;
if (node != null && node.IsBranch)
{
var branch = node.Backend as Models.Branch;