fix moving items in sln explorer

This commit is contained in:
Matt Parker
2025-10-31 19:39:54 +10:00
parent 7fa1fedd1e
commit 86035b4628
2 changed files with 12 additions and 10 deletions

View File

@@ -326,18 +326,9 @@ public partial class SolutionExplorerPanel : MarginContainer
newStartingIndex += folderCount; newStartingIndex += folderCount;
} }
var treeParent = treeItem.GetParent()!;
await this.InvokeAsync(() => await this.InvokeAsync(() =>
{ {
// The API for moving TreeItems is painful - we can only move an Item before or after another item treeItem.MoveToIndexInParent(oldStartingIndex, newStartingIndex);
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();
}); });
} }

View File

@@ -52,6 +52,17 @@ public static class NodeExtensions
} }
return null; 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) extension(Node node)
{ {