enhance: keep selection after unstaging (#320)

This commit is contained in:
leo 2024-08-06 10:42:55 +08:00
parent 4ba7c879c5
commit 90e3abed8d
No known key found for this signature in database

View file

@ -218,7 +218,22 @@ namespace SourceGit.ViewModels
public bool SetData(List<Models.Change> changes) public bool SetData(List<Models.Change> changes)
{ {
if (!IsChanged(_cached, changes))
{
// Just force refresh selected changes.
Dispatcher.UIThread.Invoke(() =>
{
if (_selectedUnstaged.Count > 0)
SelectedUnstaged = new List<Models.Change>(_selectedUnstaged);
else if (_selectedStaged.Count > 0)
SelectedStaged = new List<Models.Change>(_selectedStaged);
});
return _cached.Find(x => x.IsConflit) != null;
}
_cached = changes; _cached = changes;
_count = _cached.Count;
var unstaged = new List<Models.Change>(); var unstaged = new List<Models.Change>();
var staged = new List<Models.Change>(); var staged = new List<Models.Change>();
@ -227,12 +242,12 @@ namespace SourceGit.ViewModels
var lastSelectedUnstaged = new HashSet<string>(); var lastSelectedUnstaged = new HashSet<string>();
var lastSelectedStaged = new HashSet<string>(); var lastSelectedStaged = new HashSet<string>();
if (_selectedUnstaged != null) if (_selectedUnstaged != null && _selectedUnstaged.Count > 0)
{ {
foreach (var c in _selectedUnstaged) foreach (var c in _selectedUnstaged)
lastSelectedUnstaged.Add(c.Path); lastSelectedUnstaged.Add(c.Path);
} }
else if (_selectedStaged != null) else if (_selectedStaged != null && _selectedStaged.Count > 0)
{ {
foreach (var c in _selectedStaged) foreach (var c in _selectedStaged)
lastSelectedStaged.Add(c.Path); lastSelectedStaged.Add(c.Path);
@ -258,8 +273,6 @@ namespace SourceGit.ViewModels
selectedStaged.Add(c); selectedStaged.Add(c);
} }
_count = changes.Count;
Dispatcher.UIThread.Invoke(() => Dispatcher.UIThread.Invoke(() =>
{ {
_isLoadingData = true; _isLoadingData = true;
@ -1273,6 +1286,24 @@ namespace SourceGit.ViewModels
}); });
} }
private bool IsChanged(List<Models.Change> old, List<Models.Change> cur)
{
if (old.Count != cur.Count)
return true;
var oldSet = new HashSet<string>();
foreach (var c in old)
oldSet.Add($"{c.Path}\n{c.WorkTree}\n{c.Index}");
foreach (var c in cur)
{
if (!oldSet.Contains($"{c.Path}\n{c.WorkTree}\n{c.Index}"))
return true;
}
return false;
}
private Repository _repo = null; private Repository _repo = null;
private bool _isLoadingData = false; private bool _isLoadingData = false;
private bool _isStaging = false; private bool _isStaging = false;