fix: select item in tree not work

This commit is contained in:
leo 2024-06-05 13:21:01 +08:00
parent ce9a3dad2f
commit 1870dcd468
No known key found for this signature in database
GPG key ID: B528468E49CD0E58

View file

@ -42,26 +42,18 @@ namespace SourceGit.Models
public void Select(IEnumerable<TModel> items)
{
var sets = new HashSet<TModel>();
foreach (var item in items)
sets.Add(item);
using (BatchUpdate())
{
Clear();
int num = _source.Rows.Count;
for (int i = 0; i < num; ++i)
foreach (var selected in items)
{
var m = _source.Rows[i].Model as TModel;
if (m != null && sets.Contains(m))
{
var idx = _source.Rows.RowIndexToModelIndex(i);
var idx = GetModelIndex(_source.Items, selected, IndexPath.Unselected);
if (!idx.Equals(IndexPath.Unselected))
Select(idx);
}
}
}
}
event EventHandler ITreeDataGridSelectionInteraction.SelectionChanged
{
@ -431,6 +423,30 @@ namespace SourceGit.Models
return _childrenGetter?.Invoke(node);
}
private IndexPath GetModelIndex(IEnumerable<TModel> collection, TModel model, IndexPath parent)
{
int i = 0;
foreach (var item in collection)
{
var index = parent.Append(i);
if (item != null && item == model)
return index;
var children = GetChildren(item);
if (children != null)
{
var findInChildren = GetModelIndex(children, model, index);
if (!findInChildren.Equals(IndexPath.Unselected))
return findInChildren;
}
i++;
}
return IndexPath.Unselected;
}
private bool HasChildren(IRow row)
{
var children = GetChildren(row.Model as TModel);