Skip to content

Commit 1fd8108

Browse files
committed
refactor: history filter icon
- Show a folder icon instead of branch icon for local/remote branch folder filter - Show a warning icon if the branch/tag filter is not existed - Fix the issue that wrong filter added after renaming a local branch Signed-off-by: leo <longshuang@msn.cn>
1 parent 3b9f55f commit 1fd8108

5 files changed

Lines changed: 143 additions & 6 deletions

File tree

src/Models/HistoryFilter.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using CommunityToolkit.Mvvm.ComponentModel;
1+
using System.Text.Json.Serialization;
2+
using CommunityToolkit.Mvvm.ComponentModel;
23

34
namespace SourceGit.Models
45
{
@@ -38,9 +39,11 @@ public FilterMode Mode
3839
set => SetProperty(ref _mode, value);
3940
}
4041

41-
public bool IsBranch
42+
[JsonIgnore]
43+
public bool IsValid
4244
{
43-
get => Type != FilterType.Tag;
45+
get => _isValid;
46+
set => SetProperty(ref _isValid, value);
4447
}
4548

4649
public HistoryFilter()
@@ -56,5 +59,6 @@ public HistoryFilter(string pattern, FilterType type, FilterMode mode)
5659

5760
private string _pattern = string.Empty;
5861
private FilterMode _mode = FilterMode.None;
62+
private bool _isValid = true;
5963
}
6064
}

src/Models/RepositoryUIStates.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ public void RenameBranchFilter(string oldName, string newName)
376376
if (filter.Type == FilterType.LocalBranch &&
377377
filter.Pattern.Equals(oldName, StringComparison.Ordinal))
378378
{
379-
filter.Pattern = $"refs/heads/{newName}";
379+
filter.Pattern = newName;
380380
break;
381381
}
382382
}

src/ViewModels/Repository.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,8 @@ public void RefreshBranches()
11691169
LocalBranchTrees = builder.Locals;
11701170
RemoteBranchTrees = builder.Remotes;
11711171

1172+
ValidateHistoryFilters(true);
1173+
11721174
var localBranchesCount = 0;
11731175
foreach (var b in branches)
11741176
{
@@ -1214,6 +1216,7 @@ public void RefreshTags()
12141216

12151217
Tags = tags;
12161218
VisibleTags = BuildVisibleTags();
1219+
ValidateHistoryFilters(false);
12171220
});
12181221
}, token);
12191222
}
@@ -1841,6 +1844,37 @@ private void ResetTagFilterMode()
18411844
}
18421845
}
18431846

1847+
private void ValidateHistoryFilters(bool forBranch)
1848+
{
1849+
if (_historyFilterMode == Models.FilterMode.None)
1850+
return;
1851+
1852+
var set = new HashSet<string>();
1853+
1854+
if (forBranch)
1855+
{
1856+
foreach (var b in _branches)
1857+
set.Add(b.FullName);
1858+
1859+
foreach (var f in _uiStates.HistoryFilters)
1860+
{
1861+
if (f.Type is Models.FilterType.LocalBranch or Models.FilterType.RemoteBranch)
1862+
f.IsValid = set.Contains(f.Pattern);
1863+
}
1864+
}
1865+
else
1866+
{
1867+
foreach (var t in _tags)
1868+
set.Add(t.Name);
1869+
1870+
foreach (var f in _uiStates.HistoryFilters)
1871+
{
1872+
if (f.Type is Models.FilterType.Tag)
1873+
f.IsValid = set.Contains(f.Pattern);
1874+
}
1875+
}
1876+
}
1877+
18441878
private BranchTreeNode FindBranchNode(List<BranchTreeNode> nodes, string path)
18451879
{
18461880
if (string.IsNullOrEmpty(path))

src/Views/HistoryFilterIcon.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using Avalonia;
2+
using Avalonia.Controls;
3+
using Avalonia.Controls.Shapes;
4+
using Avalonia.Media;
5+
6+
namespace SourceGit.Views
7+
{
8+
public class HistoryFilterIcon : ContentControl
9+
{
10+
public static readonly DirectProperty<HistoryFilterIcon, Models.HistoryFilter> FilterProperty =
11+
AvaloniaProperty.RegisterDirect<HistoryFilterIcon, Models.HistoryFilter>(
12+
nameof(Filter),
13+
static o => o.Filter,
14+
static (o, v) => o.Filter = v);
15+
16+
public Models.HistoryFilter Filter
17+
{
18+
get => _filter;
19+
set => SetAndRaise(FilterProperty, ref _filter, value);
20+
}
21+
22+
public static readonly DirectProperty<HistoryFilterIcon, bool> IsFilterValidProperty =
23+
AvaloniaProperty.RegisterDirect<HistoryFilterIcon, bool>(
24+
nameof(IsFilterValid),
25+
static o => o.IsFilterValid,
26+
static (o, v) => o.IsFilterValid = v);
27+
28+
public bool IsFilterValid
29+
{
30+
get => _isFilterValid;
31+
set => SetAndRaise(IsFilterValidProperty, ref _isFilterValid, value);
32+
}
33+
34+
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
35+
{
36+
base.OnPropertyChanged(change);
37+
38+
if (change.Property == FilterProperty || change.Property == IsFilterValidProperty)
39+
UpdateContent();
40+
}
41+
42+
private void UpdateContent()
43+
{
44+
if (_filter == null)
45+
{
46+
Content = null;
47+
return;
48+
}
49+
50+
if (_filter.Type is Models.FilterType.LocalBranch or Models.FilterType.RemoteBranch)
51+
{
52+
Padding = new Thickness(0);
53+
54+
if (_isFilterValid)
55+
CreateContent("Icons.Branch");
56+
else
57+
CreateContent("Icons.Error", Brushes.DarkOrange);
58+
}
59+
else if (_filter.Type is Models.FilterType.Tag)
60+
{
61+
Padding = new Thickness(0);
62+
63+
if (_isFilterValid)
64+
CreateContent("Icons.Tag");
65+
else
66+
CreateContent("Icons.Error", Brushes.DarkOrange);
67+
}
68+
else
69+
{
70+
Padding = new Thickness(0, 1, 0, 0);
71+
CreateContent("Icons.Folder");
72+
}
73+
}
74+
75+
private void CreateContent(string iconKey, IBrush fill = null)
76+
{
77+
if (this.FindResource(iconKey) is not StreamGeometry geo)
78+
return;
79+
80+
var path = new Path()
81+
{
82+
Width = 10,
83+
Height = 10,
84+
Data = geo,
85+
};
86+
87+
if (fill != null)
88+
path.Fill = fill;
89+
90+
Content = path;
91+
}
92+
93+
private Models.HistoryFilter _filter = null;
94+
private bool _isFilterValid = true;
95+
}
96+
}

src/Views/Repository.axaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -960,8 +960,11 @@
960960
<ColumnDefinition Width="Auto" MaxWidth="480"/>
961961
<ColumnDefinition Width="Auto"/>
962962
</Grid.ColumnDefinitions>
963-
<Path Grid.Column="0" Width="10" Height="10" Data="{StaticResource Icons.Branch}" IsVisible="{Binding IsBranch}"/>
964-
<Path Grid.Column="0" Width="10" Height="10" Data="{StaticResource Icons.Tag}" IsVisible="{Binding !IsBranch}"/>
963+
<v:HistoryFilterIcon Grid.Column="0"
964+
Width="12" Height="12"
965+
VerticalAlignment="Center"
966+
Filter="{Binding, Mode=OneWay}"
967+
IsFilterValid="{Binding IsValid, Mode=OneWay}"/>
965968

966969
<TextBlock Grid.Column="1"
967970
Text="{Binding Pattern, Converter={x:Static c:StringConverters.TrimRefsPrefix}}"

0 commit comments

Comments
 (0)