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