Skip to content

Commit 2cfcd43

Browse files
committed
fix: selection lost when switch from repo to welcome page
Signed-off-by: leo <longshuang@msn.cn>
1 parent 7bca6c9 commit 2cfcd43

2 files changed

Lines changed: 35 additions & 17 deletions

File tree

src/Views/Histories.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
ScrollViewer.AllowAutoHide="False"
4040
Background="{DynamicResource Brush.DataGridHeaderBG}"
4141
ItemsSource="{Binding Commits, Mode=OneWay}"
42+
TotalCommits="{Binding Commits.Count, Mode=OneWay}"
4243
SelectedCommits="{Binding SelectedCommits, Mode=TwoWay}"
4344
ColumnHeaderHeight="24"
4445
RowHeight="26"

src/Views/Histories.axaml.cs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ private void RefreshLayout()
7979

8080
public class HistoriesCommitList : DataGrid
8181
{
82-
public static readonly StyledProperty<List<Models.Commit>> CommitsProperty =
83-
AvaloniaProperty.Register<HistoriesCommitList, List<Models.Commit>>(nameof(Commits), []);
82+
public static readonly StyledProperty<int> TotalCommitsProperty =
83+
AvaloniaProperty.Register<HistoriesCommitList, int>(nameof(TotalCommits), 0);
8484

85-
public List<Models.Commit> Commits
85+
public int TotalCommits
8686
{
87-
get => GetValue(CommitsProperty);
88-
set => SetValue(CommitsProperty, value);
87+
get => GetValue(TotalCommitsProperty);
88+
set => SetValue(TotalCommitsProperty, value);
8989
}
9090

9191
public static readonly StyledProperty<List<Models.Commit>> SelectedCommitsProperty =
@@ -114,23 +114,18 @@ public HistoriesCommitList()
114114
VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
115115
}
116116

117+
protected override void OnLoaded(RoutedEventArgs e)
118+
{
119+
base.OnLoaded(e);
120+
ApplySelection();
121+
}
122+
117123
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
118124
{
119125
base.OnPropertyChanged(change);
120126

121127
if (change.Property == SelectedCommitsProperty && IsLoaded && !_ignoreSelectionChanged)
122-
{
123-
_ignoreSelectionChanged = true;
124-
125-
SelectedItems.Clear();
126-
if (SelectedCommits is { Count: > 0 })
127-
{
128-
foreach (var c in SelectedCommits)
129-
SelectedItems.Add(c);
130-
}
131-
132-
_ignoreSelectionChanged = false;
133-
}
128+
ApplySelection();
134129
}
135130

136131
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
@@ -180,6 +175,28 @@ protected override void OnSelectionChanged(SelectionChangedEventArgs e)
180175
}
181176
}
182177

178+
private void ApplySelection()
179+
{
180+
_ignoreSelectionChanged = true;
181+
182+
if (SelectedCommits == null || SelectedCommits.Count == 0)
183+
{
184+
SelectedItems.Clear();
185+
}
186+
else if (SelectedCommits.Count == TotalCommits)
187+
{
188+
SelectAll();
189+
}
190+
else
191+
{
192+
SelectedItems.Clear();
193+
foreach (var c in SelectedCommits)
194+
SelectedItems.Add(c);
195+
}
196+
197+
_ignoreSelectionChanged = false;
198+
}
199+
183200
private bool _ignoreSelectionChanged = false;
184201
}
185202

0 commit comments

Comments
 (0)