Fix main treeview selecting wrong asset after rebuild(#36)

This commit is contained in:
miku-666
2024-06-24 19:10:35 +02:00
parent 795ad9f9d1
commit d620759de8
3 changed files with 30 additions and 4 deletions

View File

@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PckStudio.Internal.App;
namespace PckStudio.Extensions
{
internal static class TreeViewExtensions
{
public static TreeNode[] FindPath(this TreeView treeView, string path)
{
if (string.IsNullOrWhiteSpace(path))
return Array.Empty<TreeNode>();
string segment = path.Substring(0, path.IndexOf(treeView.PathSeparator));
if (treeView.Nodes.ContainsKey(segment))
{
var res = treeView.Nodes[segment].GetChildNodes().Where(node => node.FullPath == path).ToArray();
return res;
}
return Array.Empty<TreeNode>();
}
}
}

View File

@@ -423,7 +423,7 @@ namespace PckStudio
private void BuildMainTreeView()
{
// In case the Rename function was just used and the selected node name no longer matches the file name
string selectedNodeText = treeViewMain.SelectedNode is TreeNode node ? node.Text : string.Empty;
string selectedNodeText = treeViewMain.SelectedNode is TreeNode node ? node.FullPath : string.Empty;
previewPictureBox.Image = Resources.NoImageFound;
treeMeta.Nodes.Clear();
treeViewMain.Nodes.Clear();
@@ -440,9 +440,8 @@ namespace PckStudio
}
treeViewMain.Sort();
TreeNode[] selectedNodes;
if (!string.IsNullOrEmpty(selectedNodeText) &&
(selectedNodes = treeViewMain.Nodes.Find(selectedNodeText, true)).Length > 0)
TreeNode[] selectedNodes = treeViewMain.FindPath(selectedNodeText);
if (selectedNodes.Length > 0)
{
treeViewMain.SelectedNode = selectedNodes[0];
}

View File

@@ -134,6 +134,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Extensions\MaterialContainerExtensions.cs" />
<Compile Include="Extensions\TreeViewExtensions.cs" />
<Compile Include="Forms\Additional-Popups\FilterPrompt.cs">
<SubType>Form</SubType>
</Compile>