Skip to content

Commit c8eee8f

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 c8eee8f

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

src/ViewModels/Histories.cs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ public List<Models.Commit> Commits
6363
get => _commits;
6464
set
6565
{
66-
var lastSelected = SelectedCommit;
66+
// LastSelectedCommit = SelectedCommit;
6767
if (SetProperty(ref _commits, value))
6868
{
69-
if (value.Count > 0 && lastSelected != null)
70-
SelectedCommit = value.Find(x => x.SHA == lastSelected.SHA);
69+
// if (value.Count > 0 && _lastSelectedCommit != null)
70+
// SelectedCommit = value.Find(x => x.SHA == _lastSelectedCommit.SHA);
7171
}
7272
}
7373
}
@@ -84,6 +84,12 @@ public Models.Commit SelectedCommit
8484
set => SetProperty(ref _selectedCommit, value);
8585
}
8686

87+
public Models.Commit LastSelectedCommit
88+
{
89+
get => _lastSelectedCommit;
90+
set => SetProperty(ref _lastSelectedCommit, value);
91+
}
92+
8793
public List<Models.Commit> LastSelectedCommits
8894
{
8995
get => _lastSelectedCommits;
@@ -144,6 +150,8 @@ public void Dispose()
144150
_repo = null;
145151
_graph = null;
146152
_selectedCommit = null;
153+
_lastSelectedCommits = null;
154+
_lastSelectedCommit = null;
147155
_detailContext?.Dispose();
148156
_detailContext = null;
149157
}
@@ -216,7 +224,14 @@ public void NavigateTo(string commitSHA)
216224
});
217225
}
218226

219-
public void Select(IList commits)
227+
/// <summary>
228+
///
229+
/// notes: if multi selects, `AutoSelectedCommit` which `Binding Mode=OneWay` will not be updated,
230+
/// so that we could not get the lastSelectedCommit automatically
231+
///
232+
/// </summary>
233+
/// <param name="commits"></param>
234+
public void Select(IList commits, object selectedCommit)
220235
{
221236
if (_ignoreSelectionChange)
222237
return;
@@ -225,13 +240,15 @@ public void Select(IList commits)
225240
{
226241
_repo.SearchCommitContext.Selected = null;
227242
DetailContext = null;
243+
return;
228244
}
229245
else if (commits.Count == 1)
230246
{
231247
var commit = (commits[0] as Models.Commit)!;
232248
if (_repo.SearchCommitContext.Selected == null || _repo.SearchCommitContext.Selected.SHA != commit.SHA)
233249
_repo.SearchCommitContext.Selected = _repo.SearchCommitContext.Results?.Find(x => x.SHA == commit.SHA);
234250

251+
// MarkLastSelectedCommitsAsSelected();
235252
SelectedCommit = commit;
236253
NavigationId = _navigationId + 1;
237254

@@ -260,12 +277,24 @@ public void Select(IList commits)
260277
DetailContext = new Models.Count(commits.Count);
261278
}
262279

263-
_repo.SelectedCommits = commits.Cast<Models.Commit>().ToList();
280+
LastSelectedCommit = selectedCommit as Models.Commit;
281+
LastSelectedCommits = commits.Cast<Models.Commit>().ToList();
264282
}
265283

266-
public void MarkCommitsAsSelected(IList<Models.Commit> commits)
284+
public void MarkLastSelectedCommitsAsSelected(params IList<Models.Commit> commits)
267285
{
268-
LastSelectedCommits = _commits.Where(x => commits.Any(y => y.SHA == x.SHA)).ToList();
286+
var availableCommits = Commits.Where(x => commits.Any(y => y.SHA == x.SHA)).ToList();
287+
288+
// if LastSelectedCommits is not empty, find the AutoSelectedCommit or get last one
289+
if (availableCommits.Count > 0 && LastSelectedCommit != null)
290+
{
291+
// find in oldAvailable or get last one
292+
SelectedCommit = availableCommits.Find(x => x.SHA == LastSelectedCommit.SHA)
293+
?? availableCommits.FirstOrDefault();
294+
}
295+
// set selectItem above(1item), now select others too
296+
LastSelectedCommits = availableCommits;
297+
NavigationId = _navigationId + 1;
269298
}
270299

271300
public async Task<Models.Commit> GetCommitAsync(string sha)
@@ -481,6 +510,7 @@ public void CompareWithWorktree(Models.Commit commit)
481510
private bool _isLoading = true;
482511
private List<Models.Commit> _commits = new List<Models.Commit>();
483512
private List<Models.Commit> _lastSelectedCommits = [];
513+
private Models.Commit _lastSelectedCommit = null;
484514
private Models.CommitGraph _graph = null;
485515
private Models.Commit _selectedCommit = null;
486516
private Models.Bisect _bisect = null;

src/ViewModels/Repository.cs

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

@@ -1255,10 +1256,12 @@ public void RefreshCommits()
12551256

12561257
BisectState = _histories.UpdateBisectInfo();
12571258

1258-
_histories.MarkCommitsAsSelected(oldSelectedCommits);
1259+
_histories.MarkLastSelectedCommitsAsSelected(_histories.LastSelectedCommits);
12591260

12601261
if (!string.IsNullOrEmpty(_navigateToCommitDelayed))
12611262
NavigateToCommit(_navigateToCommitDelayed);
1263+
// else if (_histories.AutoSelectedCommit != null)
1264+
// NavigateToCommit(_histories.AutoSelectedCommit.SHA);
12621265
}
12631266

12641267
_navigateToCommitDelayed = string.Empty;

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)