diff --git a/src/SharpIDE.Godot/Features/SolutionExplorer/SolutionExplorerPanel.cs b/src/SharpIDE.Godot/Features/SolutionExplorer/SolutionExplorerPanel.cs index 4034c87..b5f74c0 100644 --- a/src/SharpIDE.Godot/Features/SolutionExplorer/SolutionExplorerPanel.cs +++ b/src/SharpIDE.Godot/Features/SolutionExplorer/SolutionExplorerPanel.cs @@ -326,18 +326,9 @@ public partial class SolutionExplorerPanel : MarginContainer newStartingIndex += folderCount; } - var treeParent = treeItem.GetParent()!; await this.InvokeAsync(() => { - // The API for moving TreeItems is painful - we can only move an Item before or after another item - treeParent.RemoveChild(treeItem); - var newItem = tree.CreateItem(treeParent, newStartingIndex); - newItem.SetText(0, treeItem.GetText(0)); - newItem.SetIcon(0, treeItem.GetIcon(0)); - newItem.SetMetadata(0, treeItem.GetMetadata(0)); - if (isFile) newItem.SetCustomColor(0, treeItem.GetCustomColor(0)); - treeItemContainer.Value = newItem; - treeItem.Free(); + treeItem.MoveToIndexInParent(oldStartingIndex, newStartingIndex); }); } diff --git a/src/SharpIDE.Godot/NodeExtensions.cs b/src/SharpIDE.Godot/NodeExtensions.cs index 5a4af87..ef5dce1 100644 --- a/src/SharpIDE.Godot/NodeExtensions.cs +++ b/src/SharpIDE.Godot/NodeExtensions.cs @@ -52,6 +52,17 @@ public static class NodeExtensions } return null; } + public void MoveToIndexInParent(int currentIndex, int newIndex) + { + var parent = treeItem.GetParent()!; + if (newIndex == currentIndex) throw new ArgumentException("New index is the same as current index", nameof(newIndex)); + + var target = parent.GetChild(newIndex); + if (newIndex < currentIndex) + treeItem.MoveBefore(target); + else + treeItem.MoveAfter(target); + } } extension(Node node) {