select file in tree when selected externally

This commit is contained in:
Matt Parker
2025-09-22 18:56:13 +10:00
parent 0b770e3d02
commit 25b8accbbc
6 changed files with 55 additions and 12 deletions

View File

@@ -24,6 +24,7 @@ public partial class SolutionExplorerPanel : MarginContainer
{
_tree = GetNode<Tree>("Tree");
_tree.ItemMouseSelected += TreeOnItemMouseSelected;
GodotGlobalEvents.FileExternallySelected += OnFileExternallySelected;
}
private void TreeOnItemMouseSelected(Vector2 mousePosition, long mouseButtonIndex)
@@ -36,6 +37,40 @@ public partial class SolutionExplorerPanel : MarginContainer
Guard.Against.Null(sharpIdeFile, nameof(sharpIdeFile));
GodotGlobalEvents.InvokeFileSelected(sharpIdeFile);
}
private async Task OnFileExternallySelected(SharpIdeFile file)
{
GodotGlobalEvents.InvokeFileSelected(file);
var item = FindItemRecursive(_tree.GetRoot(), file);
if (item is not null)
{
await this.InvokeAsync(() =>
{
item.UncollapseTree();
_tree.SetSelected(item, 0);
_tree.ScrollToItem(item, true);
});
}
}
private static TreeItem? FindItemRecursive(TreeItem item, SharpIdeFile file)
{
var metadata = item.GetMetadata(0);
if (metadata.As<SharpIdeFileGodotContainer?>()?.File == file)
return item;
var child = item.GetFirstChild();
while (child != null)
{
var result = FindItemRecursive(child, file);
if (result != null)
return result;
child = child.GetNext();
}
return null;
}
public void RepopulateTree()
{