Skip to content

Commit d6e8759

Browse files
committed
refactor: Improve commit selection logic and handling
- Refactor commit selection logic to track last selected commit separately and improve multi-selection handling. - Refactor commit selection logic and remove commented auto-navigation code. - Pass the selected commit item to the histories view model when the selection changes.
1 parent b5d0f9d commit d6e8759

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

src/ViewModels/Histories.cs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,7 @@ public bool IsDateTimeColumnVisible
6161
public List<Models.Commit> Commits
6262
{
6363
get => _commits;
64-
set
65-
{
66-
var lastSelected = SelectedCommit;
67-
if (SetProperty(ref _commits, value))
68-
{
69-
if (value.Count > 0 && lastSelected != null)
70-
SelectedCommit = value.Find(x => x.SHA == lastSelected.SHA);
71-
}
72-
}
64+
set => SetProperty(ref _commits, value);
7365
}
7466

7567
public Models.CommitGraph Graph
@@ -84,6 +76,12 @@ public Models.Commit SelectedCommit
8476
set => SetProperty(ref _selectedCommit, value);
8577
}
8678

79+
public Models.Commit LastSelectedCommit
80+
{
81+
get => _lastSelectedCommit;
82+
set => SetProperty(ref _lastSelectedCommit, value);
83+
}
84+
8785
public List<Models.Commit> LastSelectedCommits
8886
{
8987
get => _lastSelectedCommits;
@@ -144,6 +142,8 @@ public void Dispose()
144142
_repo = null;
145143
_graph = null;
146144
_selectedCommit = null;
145+
_lastSelectedCommits = null;
146+
_lastSelectedCommit = null;
147147
_detailContext?.Dispose();
148148
_detailContext = null;
149149
}
@@ -216,7 +216,14 @@ public void NavigateTo(string commitSHA)
216216
});
217217
}
218218

219-
public void Select(IList commits)
219+
/// <summary>
220+
///
221+
/// notes: if multi selects, `AutoSelectedCommit` which `Binding Mode=OneWay` will not be updated,
222+
/// so that we could not get the lastSelectedCommit automatically
223+
///
224+
/// </summary>
225+
/// <param name="commits"></param>
226+
public void Select(IList commits, object selectedCommit)
220227
{
221228
if (_ignoreSelectionChange)
222229
return;
@@ -225,13 +232,15 @@ public void Select(IList commits)
225232
{
226233
_repo.SearchCommitContext.Selected = null;
227234
DetailContext = null;
235+
return;
228236
}
229237
else if (commits.Count == 1)
230238
{
231239
var commit = (commits[0] as Models.Commit)!;
232240
if (_repo.SearchCommitContext.Selected == null || _repo.SearchCommitContext.Selected.SHA != commit.SHA)
233241
_repo.SearchCommitContext.Selected = _repo.SearchCommitContext.Results?.Find(x => x.SHA == commit.SHA);
234242

243+
// MarkLastSelectedCommitsAsSelected();
235244
SelectedCommit = commit;
236245
NavigationId = _navigationId + 1;
237246

@@ -260,12 +269,24 @@ public void Select(IList commits)
260269
DetailContext = new Models.Count(commits.Count);
261270
}
262271

263-
_repo.SelectedCommits = commits.Cast<Models.Commit>().ToList();
272+
LastSelectedCommit = selectedCommit as Models.Commit;
273+
LastSelectedCommits = commits.Cast<Models.Commit>().ToList();
264274
}
265275

266-
public void MarkCommitsAsSelected(IList<Models.Commit> commits)
276+
public void MarkLastSelectedCommitsAsSelected(params IList<Models.Commit> commits)
267277
{
268-
LastSelectedCommits = _commits.Where(x => commits.Any(y => y.SHA == x.SHA)).ToList();
278+
var availableCommits = Commits.Where(x => commits.Any(y => y.SHA == x.SHA)).ToList();
279+
280+
// if LastSelectedCommits is not empty, find the AutoSelectedCommit or get last one
281+
if (availableCommits.Count > 0 && LastSelectedCommit != null)
282+
{
283+
// find in oldAvailable or get last one
284+
SelectedCommit = availableCommits.Find(x => x.SHA == LastSelectedCommit.SHA)
285+
?? availableCommits.FirstOrDefault();
286+
}
287+
// set selectItem above(1item), now select others too
288+
LastSelectedCommits = availableCommits;
289+
NavigationId = _navigationId + 1;
269290
}
270291

271292
public async Task<Models.Commit> GetCommitAsync(string sha)
@@ -481,6 +502,7 @@ public void CompareWithWorktree(Models.Commit commit)
481502
private bool _isLoading = true;
482503
private List<Models.Commit> _commits = new List<Models.Commit>();
483504
private List<Models.Commit> _lastSelectedCommits = [];
505+
private Models.Commit _lastSelectedCommit = null;
484506
private Models.CommitGraph _graph = null;
485507
private Models.Commit _selectedCommit = null;
486508
private Models.Bisect _bisect = null;

src/ViewModels/Repository.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Text;
56
using System.Threading;
67
using System.Threading.Tasks;
7-
88
using Avalonia.Collections;
99
using Avalonia.Threading;
10-
1110
using CommunityToolkit.Mvvm.ComponentModel;
1211

1312
namespace SourceGit.ViewModels
@@ -1255,7 +1254,7 @@ public void RefreshCommits()
12551254

12561255
BisectState = _histories.UpdateBisectInfo();
12571256

1258-
_histories.MarkCommitsAsSelected(oldSelectedCommits);
1257+
_histories.MarkLastSelectedCommitsAsSelected(_histories.LastSelectedCommits);
12591258

12601259
if (!string.IsNullOrEmpty(_navigateToCommitDelayed))
12611260
NavigateToCommit(_navigateToCommitDelayed);

src/Views/Histories.axaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ private void OnScrollToTopPointerPressed(object sender, PointerPressedEventArgs
322322
private void OnCommitListSelectionChanged(object _, SelectionChangedEventArgs e)
323323
{
324324
if (DataContext is ViewModels.Histories histories)
325-
histories.Select(CommitListContainer.SelectedItems);
325+
histories.Select(CommitListContainer.SelectedItems, CommitListContainer.SelectedItem);
326326

327327
e.Handled = true;
328328
}

0 commit comments

Comments
 (0)