Skip to content

Commit b02d01f

Browse files
committed
feat: remember the multi selected commit avalible(olds in new)
- it is usefull for multi selection. like two revision compare, it will keep those selection for compare while filter changed (if avaliable)
1 parent 6764fc7 commit b02d01f

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

src/ViewModels/Histories.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.IO;
5+
using System.Linq;
56
using System.Threading.Tasks;
67

78
using Avalonia.Controls;
@@ -44,6 +45,12 @@ public Models.Commit AutoSelectedCommit
4445
set => SetProperty(ref _autoSelectedCommit, value);
4546
}
4647

48+
public List<Models.Commit> LastSelectedCommits
49+
{
50+
get => _lastSelectedCommits;
51+
private set => SetProperty(ref _lastSelectedCommits, value);
52+
}
53+
4754
public long NavigationId
4855
{
4956
get => _navigationId;
@@ -191,6 +198,20 @@ public void Select(IList commits)
191198
_repo.SelectedSearchedCommit = null;
192199
DetailContext = new Models.Count(commits.Count);
193200
}
201+
202+
_repo.SelectedCommits = commits.Cast<Models.Commit>().ToList();
203+
}
204+
205+
public void MarkCommitsAsSelected(IList<Models.Commit> commits)
206+
{
207+
// 提取_commits中存在于commits中的元素的索引
208+
List<int> selectedIndices = _commits
209+
.Select((commit, index) => new { commit, index })
210+
.Where(item => commits.Any(c => c.SHA == item.commit.SHA))
211+
.Select(item => item.index)
212+
.ToList();
213+
214+
LastSelectedCommits = _commits.Where(x => commits.Any(y => y.SHA == x.SHA)).ToList();
194215
}
195216

196217
public bool CheckoutBranchByDecorator(Models.Decorator decorator)
@@ -402,6 +423,7 @@ private void NavigateTo(Models.Commit commit)
402423
private Repository _repo = null;
403424
private bool _isLoading = true;
404425
private List<Models.Commit> _commits = new List<Models.Commit>();
426+
private List<Models.Commit> _lastSelectedCommits = [];
405427
private Models.CommitGraph _graph = null;
406428
private Models.Commit _autoSelectedCommit = null;
407429
private Models.Bisect _bisect = null;

src/ViewModels/Repository.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,12 @@ public List<Models.Commit> SearchedCommits
349349
set => SetProperty(ref _searchedCommits, value);
350350
}
351351

352+
public List<Models.Commit> SelectedCommits
353+
{
354+
get => _selectCommits;
355+
set => SetProperty(ref _selectCommits, value);
356+
}
357+
352358
public Models.Commit SelectedSearchedCommit
353359
{
354360
get => _selectedSearchedCommit;
@@ -1307,6 +1313,8 @@ public void RefreshTags()
13071313

13081314
public void RefreshCommits()
13091315
{
1316+
var oldSelectedCommits = _selectCommits.ToList();
1317+
13101318
Dispatcher.UIThread.Invoke(() => _histories.IsLoading = true);
13111319

13121320
var builder = new StringBuilder();
@@ -1344,6 +1352,8 @@ public void RefreshCommits()
13441352
_histories.Graph = graph;
13451353

13461354
BisectState = _histories.UpdateBisectInfo();
1355+
1356+
_histories.MarkCommitsAsSelected(oldSelectedCommits);
13471357

13481358
if (!string.IsNullOrEmpty(_navigateToCommitDelayed))
13491359
NavigateToCommit(_navigateToCommitDelayed);
@@ -2031,6 +2041,7 @@ private async void AutoFetchImpl(object sender)
20312041
private bool _onlySearchCommitsInCurrentBranch = false;
20322042
private string _searchCommitFilter = string.Empty;
20332043
private List<Models.Commit> _searchedCommits = new List<Models.Commit>();
2044+
private List<Models.Commit> _selectCommits = new List<Models.Commit>();
20342045
private Models.Commit _selectedSearchedCommit = null;
20352046
private bool _requestingWorktreeFiles = false;
20362047
private List<string> _worktreeFiles = null;

src/Views/Histories.axaml.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ public long NavigationId
122122
set => SetValue(NavigationIdProperty, value);
123123
}
124124

125+
public static readonly StyledProperty<List<Models.Commit>> LastSelectedCommitsProperty =
126+
AvaloniaProperty.Register<Histories, List<Models.Commit>>(nameof(LastSelectedCommits));
127+
128+
public List<Models.Commit> LastSelectedCommits
129+
{
130+
get => GetValue(LastSelectedCommitsProperty);
131+
set => SetValue(LastSelectedCommitsProperty, value);
132+
}
133+
125134
public static readonly StyledProperty<bool> IsScrollToTopVisibleProperty =
126135
AvaloniaProperty.Register<Histories, bool>(nameof(IsScrollToTopVisible));
127136

@@ -145,6 +154,18 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
145154
if (CommitListContainer is { SelectedItems.Count: 1, IsLoaded: true } dataGrid)
146155
dataGrid.ScrollIntoView(dataGrid.SelectedItem, null);
147156
}
157+
if (change.Property == LastSelectedCommitsProperty)
158+
{
159+
if (LastSelectedCommits?.Count > 1 &&
160+
(CommitListContainer is DataGrid { IsLoaded: true } dataGrid))
161+
{
162+
foreach (var c in LastSelectedCommits)
163+
{
164+
dataGrid.SelectedItems.Add(c);
165+
}
166+
}
167+
}
168+
148169
}
149170

150171
private void OnCommitListLoaded(object sender, RoutedEventArgs e)

src/Views/Repository.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,7 @@
838838
Bisect="{Binding Bisect}"
839839
IssueTrackers="{Binding $parent[v:Repository].((vm:Repository)DataContext).IssueTrackers}"
840840
OnlyHighlightCurrentBranch="{Binding $parent[v:Repository].((vm:Repository)DataContext).OnlyHighlightCurrentBranchInHistories}"
841+
LastSelectedCommits="{Binding LastSelectedCommits}"
841842
NavigationId="{Binding NavigationId}"/>
842843
</DataTemplate>
843844

0 commit comments

Comments
 (0)