Automatically select next change after (un)stage

This commit is contained in:
Hannes Braun 2024-09-06 15:47:58 +02:00
parent b1457fe39d
commit 07be7d7d0f

View file

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia.Controls; using Avalonia.Controls;
@ -322,8 +323,9 @@ namespace SourceGit.ViewModels
public void StageSelected() public void StageSelected()
{ {
var nextSelection = CalculateNextSelection(_selectedUnstaged, Unstaged);
StageChanges(_selectedUnstaged); StageChanges(_selectedUnstaged);
SelectedUnstaged = []; SelectedUnstaged = nextSelection;
} }
public void StageAll() public void StageAll()
@ -359,8 +361,9 @@ namespace SourceGit.ViewModels
public void UnstageSelected() public void UnstageSelected()
{ {
var nextSelection = CalculateNextSelection(_selectedStaged, Staged);
UnstageChanges(_selectedStaged); UnstageChanges(_selectedStaged);
SelectedStaged = []; SelectedStaged = nextSelection;
} }
public void UnstageAll() public void UnstageAll()
@ -1357,6 +1360,19 @@ namespace SourceGit.ViewModels
return false; return false;
} }
private static List<Models.Change> CalculateNextSelection(List<Models.Change> selected, List<Models.Change> all)
{
var indices = selected.Select(x => all.IndexOf(x)).ToArray();
var min = indices.Min();
var next = min + 1;
while (indices.Contains(next))
next++;
if (next >= all.Count)
next = min - 1;
List<Models.Change> nextSelection = next >= 0 ? [all[next]] : [];
return nextSelection;
}
private Repository _repo = null; private Repository _repo = null;
private bool _isLoadingData = false; private bool _isLoadingData = false;
private bool _isStaging = false; private bool _isStaging = false;