Skip to content

Commit c3cbc61

Browse files
committed
refactor: keep selection in HISTORY page if user only selected one or two commits (#2297)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 45127ba commit c3cbc61

6 files changed

Lines changed: 167 additions & 72 deletions

File tree

‎src/ViewModels/Fetch.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ public override async Task<bool> Sure()
6868
{
6969
using var lockWatcher = _repo.LockWatcher();
7070

71-
var navigateToUpstreamHEAD = _repo.SelectedView is Histories { SelectedCommit: { IsCurrentHead: true } };
71+
var navigateToUpstreamHEAD = _repo.SelectedView is Histories histories &&
72+
histories.SelectedCommits.Count == 1 &&
73+
histories.SelectedCommits[0].IsCurrentHead;
74+
7275
var notags = _repo.UIStates.FetchWithoutTags;
7376
var force = _repo.UIStates.EnableForceOnFetch;
7477
var log = _repo.CreateLog("Fetch");

‎src/ViewModels/Histories.cs‎

Lines changed: 92 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections;
32
using System.Collections.Generic;
43
using System.IO;
54
using System.Threading.Tasks;
@@ -62,12 +61,8 @@ public List<Models.Commit> Commits
6261
get => _commits;
6362
set
6463
{
65-
var lastSelected = SelectedCommit;
6664
if (SetProperty(ref _commits, value))
67-
{
68-
if (value.Count > 0 && lastSelected != null)
69-
SelectedCommit = value.Find(x => x.SHA.Equals(lastSelected.SHA, StringComparison.Ordinal));
70-
}
65+
PostCommitsChanged();
7166
}
7267
}
7368

@@ -77,16 +72,14 @@ public Models.CommitGraph Graph
7772
set => SetProperty(ref _graph, value);
7873
}
7974

80-
public Models.Commit SelectedCommit
81-
{
82-
get => _selectedCommit;
83-
set => SetProperty(ref _selectedCommit, value);
84-
}
85-
86-
public long NavigationId
75+
public List<Models.Commit> SelectedCommits
8776
{
88-
get => _navigationId;
89-
private set => SetProperty(ref _navigationId, value);
77+
get => _selectedCommits;
78+
set
79+
{
80+
if (SetProperty(ref _selectedCommits, value))
81+
PostSelectedCommitsChanged();
82+
}
9083
}
9184

9285
public object DetailContext
@@ -167,8 +160,7 @@ public void NavigateTo(string commitSHA)
167160
var commit = _commits.Find(x => x.SHA.StartsWith(commitSHA, StringComparison.Ordinal));
168161
if (commit != null)
169162
{
170-
SelectedCommit = commit;
171-
NavigationId = _navigationId + 1;
163+
SelectedCommits = [commit];
172164
return;
173165
}
174166

@@ -181,7 +173,7 @@ public void NavigateTo(string commitSHA)
181173
Dispatcher.UIThread.Post(() =>
182174
{
183175
_ignoreSelectionChange = true;
184-
SelectedCommit = null;
176+
SelectedCommits = [];
185177

186178
if (_detailContext is CommitDetail detail)
187179
{
@@ -199,49 +191,30 @@ public void NavigateTo(string commitSHA)
199191
});
200192
}
201193

202-
public void Select(IList commits)
194+
public void SetSelectedCommitsDirectly(List<Models.Commit> commits)
203195
{
204-
if (_ignoreSelectionChange)
205-
return;
206-
207-
if (commits.Count == 0)
196+
if (_selectedCommits.Count != commits.Count)
208197
{
209-
_repo.SearchCommitContext.Selected = null;
210-
DetailContext = null;
198+
SelectedCommits = commits;
199+
return;
211200
}
212-
else if (commits.Count == 1)
213-
{
214-
var commit = (commits[0] as Models.Commit)!;
215-
if (_repo.SearchCommitContext.Selected == null || !_repo.SearchCommitContext.Selected.SHA.Equals(commit.SHA, StringComparison.Ordinal))
216-
_repo.SearchCommitContext.Selected = _repo.SearchCommitContext.Results?.Find(x => x.SHA.Equals(commit.SHA, StringComparison.Ordinal));
217201

218-
SelectedCommit = commit;
219-
NavigationId = _navigationId + 1;
202+
var hashes = new HashSet<string>();
203+
foreach (var c in _selectedCommits)
204+
hashes.Add(c.SHA);
220205

221-
if (_detailContext is CommitDetail detail)
222-
{
223-
detail.Commit = commit;
224-
}
225-
else
206+
var equals = true;
207+
foreach (var c in commits)
208+
{
209+
if (!hashes.Contains(c.SHA))
226210
{
227-
var commitDetail = new CommitDetail(_repo, _commitDetailSharedData);
228-
commitDetail.Commit = commit;
229-
DetailContext = commitDetail;
211+
equals = false;
212+
break;
230213
}
231214
}
232-
else if (commits.Count == 2)
233-
{
234-
_repo.SearchCommitContext.Selected = null;
235215

236-
var end = commits[0] as Models.Commit;
237-
var start = commits[1] as Models.Commit;
238-
DetailContext = new RevisionCompare(_repo, start, end);
239-
}
240-
else
241-
{
242-
_repo.SearchCommitContext.Selected = null;
243-
DetailContext = new Models.Count(commits.Count);
244-
}
216+
if (!equals)
217+
SelectedCommits = commits;
245218
}
246219

247220
public async Task<Models.Commit> GetCommitAsync(string sha)
@@ -436,14 +409,79 @@ public void CompareWithWorktree(Models.Commit commit)
436409
DetailContext = new RevisionCompare(_repo, commit, null);
437410
}
438411

412+
private void PostCommitsChanged()
413+
{
414+
if (_commits.Count == 0 ||
415+
_selectedCommits.Count == 0 ||
416+
_selectedCommits.Count > 2)
417+
{
418+
SelectedCommits = [];
419+
return;
420+
}
421+
422+
var set = new HashSet<string>();
423+
foreach (var c in _selectedCommits)
424+
set.Add(c.SHA);
425+
426+
var selected = new List<Models.Commit>();
427+
foreach (var c in _commits)
428+
{
429+
if (set.Contains(c.SHA))
430+
{
431+
selected.Add(c);
432+
set.Remove(c.SHA);
433+
if (set.Count == 0)
434+
break;
435+
}
436+
}
437+
438+
SelectedCommits = selected;
439+
}
440+
441+
private void PostSelectedCommitsChanged()
442+
{
443+
if (_ignoreSelectionChange)
444+
return;
445+
446+
if (_selectedCommits.Count == 0)
447+
{
448+
_repo.SearchCommitContext.Selected = null;
449+
DetailContext = null;
450+
}
451+
else if (_selectedCommits.Count == 1)
452+
{
453+
var c = _selectedCommits[0];
454+
if (_repo.SearchCommitContext.Selected == null || !_repo.SearchCommitContext.Selected.SHA.Equals(c.SHA, StringComparison.Ordinal))
455+
_repo.SearchCommitContext.Selected = _repo.SearchCommitContext.Results?.Find(x => x.SHA.Equals(c.SHA, StringComparison.Ordinal));
456+
457+
if (_detailContext is CommitDetail detail)
458+
detail.Commit = c;
459+
else
460+
DetailContext = new CommitDetail(_repo, _commitDetailSharedData) { Commit = c };
461+
}
462+
else if (_selectedCommits.Count == 2)
463+
{
464+
_repo.SearchCommitContext.Selected = null;
465+
466+
if (_detailContext is RevisionCompare compare)
467+
compare.SetTargets(_selectedCommits[0], _selectedCommits[1]);
468+
else
469+
DetailContext = new RevisionCompare(_repo, _selectedCommits[0], _selectedCommits[1]);
470+
}
471+
else
472+
{
473+
_repo.SearchCommitContext.Selected = null;
474+
DetailContext = new Models.Count(_selectedCommits.Count);
475+
}
476+
}
477+
439478
private Repository _repo = null;
440479
private CommitDetailSharedData _commitDetailSharedData = null;
441480
private bool _isLoading = true;
442481
private List<Models.Commit> _commits = new List<Models.Commit>();
443482
private Models.CommitGraph _graph = null;
444-
private Models.Commit _selectedCommit = null;
483+
private List<Models.Commit> _selectedCommits = [];
445484
private Models.Bisect _bisect = null;
446-
private long _navigationId = 0;
447485
private object _detailContext = null;
448486
private bool _ignoreSelectionChange = false;
449487

‎src/ViewModels/RevisionCompare.cs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ public RevisionCompare(Repository repo, Models.Commit startPoint, Models.Commit
109109
Refresh();
110110
}
111111

112+
public void SetTargets(Models.Commit l, Models.Commit r)
113+
{
114+
var hashes = new HashSet<string>();
115+
hashes.Add(l.SHA);
116+
hashes.Add(r.SHA);
117+
118+
if (_startPoint is Models.Commit s &&
119+
_endPoint is Models.Commit e &&
120+
hashes.Contains(s.SHA) &&
121+
hashes.Contains(e.SHA))
122+
return;
123+
124+
_startPoint = l;
125+
_endPoint = r;
126+
Refresh();
127+
}
128+
112129
public void OpenChangeWithExternalDiffTool(Models.Change change)
113130
{
114131
var opt = new Models.DiffOption(GetSHA(_startPoint), GetSHA(_endPoint), change);

‎src/Views/Histories.axaml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
Background="{DynamicResource Brush.DataGridHeaderBG}"
4141
SelectionMode="Extended"
4242
ItemsSource="{Binding Commits, Mode=OneWay}"
43-
SelectedItem="{Binding SelectedCommit, Mode=OneWay}"
4443
CanUserReorderColumns="False"
4544
CanUserResizeColumns="True"
4645
CanUserSortColumns="False"

‎src/Views/Histories.axaml.cs‎

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ public bool OnlyHighlightCurrentBranch
115115
set => SetValue(OnlyHighlightCurrentBranchProperty, value);
116116
}
117117

118-
public static readonly StyledProperty<long> NavigationIdProperty =
119-
AvaloniaProperty.Register<Histories, long>(nameof(NavigationId));
118+
public static readonly StyledProperty<List<Models.Commit>> SelectedCommitsProperty =
119+
AvaloniaProperty.Register<Histories, List<Models.Commit>>(nameof(SelectedCommits), []);
120120

121-
public long NavigationId
121+
public List<Models.Commit> SelectedCommits
122122
{
123-
get => GetValue(NavigationIdProperty);
124-
set => SetValue(NavigationIdProperty, value);
123+
get => GetValue(SelectedCommitsProperty);
124+
set => SetValue(SelectedCommitsProperty, value);
125125
}
126126

127127
public static readonly StyledProperty<bool> IsScrollToTopVisibleProperty =
@@ -142,10 +142,21 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
142142
{
143143
base.OnPropertyChanged(change);
144144

145-
if (change.Property == NavigationIdProperty)
145+
if (change.Property == SelectedCommitsProperty &&
146+
CommitListContainer.IsLoaded &&
147+
!_ignoreSelectionChanged)
146148
{
147-
if (CommitListContainer is { SelectedItems.Count: 1, IsLoaded: true } dataGrid)
148-
dataGrid.ScrollIntoView(dataGrid.SelectedItem, null);
149+
_ignoreSelectionChanged = true;
150+
151+
var dataGrid = CommitListContainer;
152+
dataGrid.SelectedItems.Clear();
153+
if (SelectedCommits is { Count: > 0 })
154+
{
155+
foreach (var c in SelectedCommits)
156+
dataGrid.SelectedItems.Add(c);
157+
}
158+
159+
_ignoreSelectionChanged = false;
149160
}
150161
}
151162

@@ -156,8 +167,16 @@ private void OnCommitListLoaded(object sender, RoutedEventArgs e)
156167
if (rowsPresenter is { Children: { Count: > 0 } rows })
157168
CommitGraph.Layout = new(0, dataGrid.Columns[0].ActualWidth - 4, rows[0].Bounds.Height);
158169

159-
if (dataGrid.SelectedItems.Count == 1)
160-
dataGrid.ScrollIntoView(dataGrid.SelectedItem, null);
170+
_ignoreSelectionChanged = true;
171+
172+
dataGrid.SelectedItems.Clear();
173+
if (SelectedCommits is { Count: > 0 })
174+
{
175+
foreach (var c in SelectedCommits)
176+
dataGrid.SelectedItems.Add(c);
177+
}
178+
179+
_ignoreSelectionChanged = false;
161180
}
162181

163182
private async void OnGotoParent(object sender, RoutedEventArgs e)
@@ -299,10 +318,28 @@ private void OnScrollToTopPointerPressed(object sender, PointerPressedEventArgs
299318
CommitListContainer.ScrollIntoView(histories.Commits[0], null);
300319
}
301320

302-
private void OnCommitListSelectionChanged(object _, SelectionChangedEventArgs e)
321+
private void OnCommitListSelectionChanged(object sender, SelectionChangedEventArgs e)
303322
{
304-
if (DataContext is ViewModels.Histories histories)
305-
histories.Select(CommitListContainer.SelectedItems);
323+
if (sender is not DataGrid dataGrid)
324+
return;
325+
326+
var selected = dataGrid.SelectedItems;
327+
var commits = new List<Models.Commit>();
328+
foreach (var o in selected)
329+
{
330+
if (o is Models.Commit c)
331+
commits.Add(c);
332+
}
333+
334+
if (commits.Count > 0 && commits.Count < 3)
335+
dataGrid.ScrollIntoView(commits[^1], null);
336+
337+
if (DataContext is ViewModels.Histories histories && !_ignoreSelectionChanged)
338+
{
339+
_ignoreSelectionChanged = true;
340+
histories.SetSelectedCommitsDirectly(commits);
341+
_ignoreSelectionChanged = false;
342+
}
306343

307344
e.Handled = true;
308345
}
@@ -1437,5 +1474,6 @@ private async Task InteractiveRebaseWithPrefillActionAsync(ViewModels.Repository
14371474
private double _lastGraphStartY = 0;
14381475
private double _lastGraphClipWidth = 0;
14391476
private double _lastGraphRowHeight = 0;
1477+
private bool _ignoreSelectionChanged = false;
14401478
}
14411479
}

‎src/Views/Repository.axaml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -927,10 +927,10 @@
927927

928928
<DataTemplate DataType="vm:Histories">
929929
<v:Histories CurrentBranch="{Binding $parent[v:Repository].((vm:Repository)DataContext).CurrentBranch}"
930-
Bisect="{Binding Bisect}"
930+
Bisect="{Binding Bisect, Mode=OneWay}"
931+
SelectedCommits="{Binding SelectedCommits, Mode=OneWay}"
931932
IssueTrackers="{Binding $parent[v:Repository].((vm:Repository)DataContext).IssueTrackers}"
932-
OnlyHighlightCurrentBranch="{Binding $parent[v:Repository].((vm:Repository)DataContext).OnlyHighlightCurrentBranchInHistory}"
933-
NavigationId="{Binding NavigationId}"/>
933+
OnlyHighlightCurrentBranch="{Binding $parent[v:Repository].((vm:Repository)DataContext).OnlyHighlightCurrentBranchInHistory}"/>
934934
</DataTemplate>
935935

936936
<DataTemplate DataType="vm:WorkingCopy">

0 commit comments

Comments
 (0)