fix<Repository>: filter not working

This commit is contained in:
leo 2024-02-06 17:52:16 +08:00
parent 5781a22b4c
commit 98e65c0f11
4 changed files with 44 additions and 10 deletions

View file

@ -1,4 +1,5 @@
using System; using Avalonia.Collections;
using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace SourceGit.Models { namespace SourceGit.Models {
@ -13,6 +14,7 @@ namespace SourceGit.Models {
public BranchTreeNodeType Type { get; set; } public BranchTreeNodeType Type { get; set; }
public object Backend { get; set; } public object Backend { get; set; }
public bool IsExpanded { get; set; } public bool IsExpanded { get; set; }
public bool IsFiltered { get; set; }
public List<BranchTreeNode> Children { get; set; } = new List<BranchTreeNode>(); public List<BranchTreeNode> Children { get; set; } = new List<BranchTreeNode>();
public bool IsUpstreamTrackStatusVisible { public bool IsUpstreamTrackStatusVisible {
@ -58,11 +60,12 @@ namespace SourceGit.Models {
} }
foreach (var branch in branches) { foreach (var branch in branches) {
var isFiltered = _filters.Contains(branch.FullName);
if (branch.IsLocal) { if (branch.IsLocal) {
MakeBranchNode(branch, _locals, "local"); MakeBranchNode(branch, _locals, "local", isFiltered);
} else { } else {
var remote = _remotes.Find(x => x.Name == branch.Remote); var remote = _remotes.Find(x => x.Name == branch.Remote);
if (remote != null) MakeBranchNode(branch, remote.Children, $"remote/{remote.Name}"); if (remote != null) MakeBranchNode(branch, remote.Children, $"remote/{remote.Name}", isFiltered);
} }
} }
@ -70,6 +73,10 @@ namespace SourceGit.Models {
SortNodes(_remotes); SortNodes(_remotes);
} }
public void SetFilters(AvaloniaList<string> filters) {
_filters.AddRange(filters);
}
public void CollectExpandedNodes(List<BranchTreeNode> nodes, bool isLocal) { public void CollectExpandedNodes(List<BranchTreeNode> nodes, bool isLocal) {
CollectExpandedNodes(nodes, isLocal ? "local" : "remote"); CollectExpandedNodes(nodes, isLocal ? "local" : "remote");
} }
@ -82,7 +89,7 @@ namespace SourceGit.Models {
} }
} }
private void MakeBranchNode(Branch branch, List<BranchTreeNode> roots, string prefix) { private void MakeBranchNode(Branch branch, List<BranchTreeNode> roots, string prefix, bool isFiltered) {
var subs = branch.Name.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); var subs = branch.Name.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (subs.Length == 1) { if (subs.Length == 1) {
@ -91,6 +98,7 @@ namespace SourceGit.Models {
Type = BranchTreeNodeType.Branch, Type = BranchTreeNodeType.Branch,
Backend = branch, Backend = branch,
IsExpanded = false, IsExpanded = false,
IsFiltered = isFiltered,
}; };
roots.Add(node); roots.Add(node);
return; return;
@ -127,6 +135,7 @@ namespace SourceGit.Models {
Type = BranchTreeNodeType.Branch, Type = BranchTreeNodeType.Branch,
Backend = branch, Backend = branch,
IsExpanded = false, IsExpanded = false,
IsFiltered = isFiltered,
}; };
lastFolder.Children.Add(last); lastFolder.Children.Add(last);
} }
@ -146,6 +155,7 @@ namespace SourceGit.Models {
private List<BranchTreeNode> _locals = new List<BranchTreeNode>(); private List<BranchTreeNode> _locals = new List<BranchTreeNode>();
private List<BranchTreeNode> _remotes = new List<BranchTreeNode>(); private List<BranchTreeNode> _remotes = new List<BranchTreeNode>();
private HashSet<string> _expanded = new HashSet<string>(); private HashSet<string> _expanded = new HashSet<string>();
private List<string> _filters = new List<string>();
private Dictionary<string, BranchTreeNode> _maps = new Dictionary<string, BranchTreeNode>(); private Dictionary<string, BranchTreeNode> _maps = new Dictionary<string, BranchTreeNode>();
} }
} }

View file

@ -2,5 +2,6 @@
public class Tag { public class Tag {
public string Name { get; set; } public string Name { get; set; }
public string SHA { get; set; } public string SHA { get; set; }
public bool IsFiltered { get; set; }
} }
} }

View file

@ -23,12 +23,12 @@ namespace SourceGit.ViewModels {
public AvaloniaList<string> Filters { public AvaloniaList<string> Filters {
get; get;
private set; set;
} = new AvaloniaList<string>(); } = new AvaloniaList<string>();
public AvaloniaList<string> CommitMessages { public AvaloniaList<string> CommitMessages {
get; get;
private set; set;
} = new AvaloniaList<string>(); } = new AvaloniaList<string>();
[JsonIgnore] [JsonIgnore]
@ -401,6 +401,7 @@ namespace SourceGit.ViewModels {
var remotes = new Commands.QueryRemotes(FullPath).Result(); var remotes = new Commands.QueryRemotes(FullPath).Result();
var builder = new Models.BranchTreeNode.Builder(); var builder = new Models.BranchTreeNode.Builder();
builder.SetFilters(Filters);
builder.CollectExpandedNodes(_localBranchTrees, true); builder.CollectExpandedNodes(_localBranchTrees, true);
builder.CollectExpandedNodes(_remoteBranchTrees, false); builder.CollectExpandedNodes(_remoteBranchTrees, false);
builder.Run(branches, remotes); builder.Run(branches, remotes);
@ -418,6 +419,7 @@ namespace SourceGit.ViewModels {
public void RefreshTags() { public void RefreshTags() {
var tags = new Commands.QueryTags(FullPath).Result(); var tags = new Commands.QueryTags(FullPath).Result();
foreach (var tag in tags) tag.IsFiltered = Filters.Contains(tag.Name);
Dispatcher.UIThread.Invoke(() => { Dispatcher.UIThread.Invoke(() => {
Tags = tags; Tags = tags;
}); });

View file

@ -178,7 +178,14 @@
<TextBlock Classes="monospace" FontSize="10" HorizontalAlignment="Center" Margin="9,0" Text="{Binding UpstreamTrackStatus}"/> <TextBlock Classes="monospace" FontSize="10" HorizontalAlignment="Center" Margin="9,0" Text="{Binding UpstreamTrackStatus}"/>
</Border> </Border>
<ToggleButton Grid.Column="3" Classes="filter" Margin="0,0,8,0" Background="Transparent" IsVisible="{Binding IsBranch}" Checked="OnToggleFilter" Unchecked="OnToggleFilter"/> <ToggleButton Grid.Column="3"
Classes="filter"
Margin="0,0,8,0"
Background="Transparent"
IsVisible="{Binding IsBranch}"
Checked="OnToggleFilter"
Unchecked="OnToggleFilter"
IsChecked="{Binding IsFiltered}"/>
</Grid> </Grid>
</TreeDataTemplate> </TreeDataTemplate>
</TreeView.ItemTemplate> </TreeView.ItemTemplate>
@ -204,8 +211,17 @@
<Path Grid.Column="0" Classes="folder_icon" Width="10" Height="10" Margin="0,2,0,0" IsVisible="{Binding IsFolder}" VerticalAlignment="Center"/> <Path Grid.Column="0" Classes="folder_icon" Width="10" Height="10" Margin="0,2,0,0" IsVisible="{Binding IsFolder}" VerticalAlignment="Center"/>
<Path Grid.Column="0" Width="10" Height="10" Margin="0,4,0,0" Data="{StaticResource Icons.Remote}" IsVisible="{Binding IsRemote}" VerticalAlignment="Center"/> <Path Grid.Column="0" Width="10" Height="10" Margin="0,4,0,0" Data="{StaticResource Icons.Remote}" IsVisible="{Binding IsRemote}" VerticalAlignment="Center"/>
<Path Grid.Column="0" Width="10" Height="10" Data="{StaticResource Icons.Branch}" IsVisible="{Binding IsBranch}" VerticalAlignment="Center"/> <Path Grid.Column="0" Width="10" Height="10" Data="{StaticResource Icons.Branch}" IsVisible="{Binding IsBranch}" VerticalAlignment="Center"/>
<TextBlock Grid.Column="1" Text="{Binding Name}" Classes="monospace" FontSize="12" Margin="8,0,4,0"/> <TextBlock Grid.Column="1" Text="{Binding Name}" Classes="monospace" FontSize="12" Margin="8,0,4,0"/>
<ToggleButton Grid.Column="2" Classes="filter" Margin="0,0,8,0" Background="Transparent" Checked="OnToggleFilter" Unchecked="OnToggleFilter" IsVisible="{Binding IsBranch}"/>
<ToggleButton Grid.Column="2"
Classes="filter"
Margin="0,0,8,0"
Background="Transparent"
Checked="OnToggleFilter"
Unchecked="OnToggleFilter"
IsVisible="{Binding IsBranch}"
IsChecked="{Binding IsFiltered}"/>
</Grid> </Grid>
</TreeDataTemplate> </TreeDataTemplate>
</TreeView.ItemTemplate> </TreeView.ItemTemplate>
@ -251,7 +267,7 @@
<DataGridTemplateColumn Width="*" Header="NAME"> <DataGridTemplateColumn Width="*" Header="NAME">
<DataGridTemplateColumn.CellTemplate> <DataGridTemplateColumn.CellTemplate>
<DataTemplate x:DataType="{x:Type m:Tag}"> <DataTemplate x:DataType="{x:Type m:Tag}">
<TextBlock Text="{Binding Name}" Classes="monospace" FontSize="12" /> <TextBlock Text="{Binding Name}" Classes="monospace" FontSize="12" TextTrimming="CharacterEllipsis" />
</DataTemplate> </DataTemplate>
</DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn> </DataGridTemplateColumn>
@ -259,7 +275,12 @@
<DataGridTemplateColumn Header="FILTER"> <DataGridTemplateColumn Header="FILTER">
<DataGridTemplateColumn.CellTemplate> <DataGridTemplateColumn.CellTemplate>
<DataTemplate x:DataType="{x:Type m:Tag}"> <DataTemplate x:DataType="{x:Type m:Tag}">
<ToggleButton Grid.Column="2" Classes="filter" Margin="0,0,8,0" Background="Transparent" Checked="OnToggleFilter" Unchecked="OnToggleFilter"/> <ToggleButton Classes="filter"
Margin="0,0,8,0"
Background="Transparent"
Checked="OnToggleFilter"
Unchecked="OnToggleFilter"
IsChecked="{Binding IsFiltered}"/>
</DataTemplate> </DataTemplate>
</DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn> </DataGridTemplateColumn>