enhance: prevent property changed event if it is unchanged

This commit is contained in:
leo 2024-07-26 10:22:42 +08:00
parent aae1c41dcd
commit 85ce2f9cab
No known key found for this signature in database
2 changed files with 27 additions and 31 deletions

View file

@ -137,6 +137,7 @@ namespace SourceGit.ViewModels
}
else
{
if (_selectedStaged != null && _selectedStaged.Count > 0)
SelectedStaged = [];
if (value.Count == 1)
@ -162,6 +163,7 @@ namespace SourceGit.ViewModels
}
else
{
if (_selectedUnstaged != null && _selectedUnstaged.Count > 0)
SelectedUnstaged = [];
if (value.Count == 1)

View file

@ -174,8 +174,9 @@ namespace SourceGit.Views
return;
_disableSelectionChangingEvent = true;
var selected = new List<Models.Change>();
if (sender is ListBox { SelectedItems: not null } list)
if (sender is ListBox list)
{
foreach (var item in list.SelectedItems)
{
@ -186,7 +187,27 @@ namespace SourceGit.Views
}
}
TrySetSelected(selected);
var old = SelectedChanges ?? [];
if (old.Count != selected.Count)
{
SetCurrentValue(SelectedChangesProperty, selected);
}
else
{
bool allEquals = true;
foreach (var c in old)
{
if (!selected.Contains(c))
{
allEquals = false;
break;
}
}
if (!allEquals)
SetCurrentValue(SelectedChangesProperty, selected);
}
_disableSelectionChangingEvent = false;
}
@ -330,33 +351,6 @@ namespace SourceGit.Views
}
}
private void TrySetSelected(List<Models.Change> changes)
{
var old = SelectedChanges;
if (old == null && changes.Count == 0)
return;
if (old != null && old.Count == changes.Count)
{
bool allEquals = true;
foreach (var c in old)
{
if (!changes.Contains(c))
{
allEquals = false;
break;
}
}
if (allEquals)
return;
}
_disableSelectionChangingEvent = true;
SetCurrentValue(SelectedChangesProperty, changes);
_disableSelectionChangingEvent = false;
}
private bool _disableSelectionChangingEvent = false;
}
}