From db2e8c078dad459770bb3ece2c509490d25a1918 Mon Sep 17 00:00:00 2001 From: HoLLy Date: Sun, 8 Nov 2020 21:29:55 +0100 Subject: [PATCH] Don't force focus change after rename if renamed node was not selected (#1595) * Remove unneeded SelectItems calls * Don't force focus change after rename if renamed node was not selected --- .../dnSpy.AsmEditor/Assembly/AssemblyCommands.cs | 1 - .../dnSpy.AsmEditor/Event/EventDefCommands.cs | 8 ++++++-- .../dnSpy.AsmEditor/Field/FieldDefCommands.cs | 8 ++++++-- .../dnSpy.AsmEditor/Method/MethodDefCommands.cs | 8 ++++++-- .../dnSpy.AsmEditor/Module/ModuleCommands.cs | 1 - .../Namespace/NamespaceCommands.cs | 15 +++++++++++++-- .../Property/PropertyDefCommands.cs | 14 +++++++++++--- .../dnSpy.AsmEditor/Resources/ResourceCommands.cs | 14 +++++++++++--- .../dnSpy.AsmEditor/Types/TypeDefCommands.cs | 14 ++++++++++++-- 9 files changed, 65 insertions(+), 18 deletions(-) diff --git a/Extensions/dnSpy.AsmEditor/Assembly/AssemblyCommands.cs b/Extensions/dnSpy.AsmEditor/Assembly/AssemblyCommands.cs index 5ed73d3701..df0fb96a7a 100644 --- a/Extensions/dnSpy.AsmEditor/Assembly/AssemblyCommands.cs +++ b/Extensions/dnSpy.AsmEditor/Assembly/AssemblyCommands.cs @@ -337,7 +337,6 @@ public void Execute() { info.AssemblyRef.PublicKeyOrToken = newOptions.PublicKey; } } - asmNode.TreeNode.TreeView.SelectItems(new[] { asmNode }); asmNode.TreeNode.RefreshUI(); } diff --git a/Extensions/dnSpy.AsmEditor/Event/EventDefCommands.cs b/Extensions/dnSpy.AsmEditor/Event/EventDefCommands.cs index ecc3e0a9f1..2019ece712 100644 --- a/Extensions/dnSpy.AsmEditor/Event/EventDefCommands.cs +++ b/Extensions/dnSpy.AsmEditor/Event/EventDefCommands.cs @@ -414,11 +414,15 @@ public void Execute() { Debug.Assert(b); if (!b) throw new InvalidOperationException(); + + var isNodeSelected = eventNode.TreeNode.TreeView.SelectedItem == eventNode; + origParentNode.TreeNode.Children.RemoveAt(origParentChildIndex); newOptions.CopyTo(eventNode.EventDef); - origParentNode.TreeNode.AddChild(eventNode.TreeNode); - origParentNode.TreeNode.TreeView.SelectItems(new[] { eventNode }); + + if (isNodeSelected) + origParentNode.TreeNode.TreeView.SelectItems(new[] { eventNode }); } else newOptions.CopyTo(eventNode.EventDef); diff --git a/Extensions/dnSpy.AsmEditor/Field/FieldDefCommands.cs b/Extensions/dnSpy.AsmEditor/Field/FieldDefCommands.cs index 91f893f933..c7342b19e9 100644 --- a/Extensions/dnSpy.AsmEditor/Field/FieldDefCommands.cs +++ b/Extensions/dnSpy.AsmEditor/Field/FieldDefCommands.cs @@ -424,11 +424,15 @@ public void Execute() { Debug.Assert(b); if (!b) throw new InvalidOperationException(); + + var isNodeSelected = fieldNode.TreeNode.TreeView.SelectedItem == fieldNode; + origParentNode.TreeNode.Children.RemoveAt(origParentChildIndex); newOptions.CopyTo(fieldNode.FieldDef); - origParentNode.TreeNode.AddChild(fieldNode.TreeNode); - origParentNode.TreeNode.TreeView.SelectItems(new[] { fieldNode }); + + if (isNodeSelected) + origParentNode.TreeNode.TreeView.SelectItems(new[] { fieldNode }); } else newOptions.CopyTo(fieldNode.FieldDef); diff --git a/Extensions/dnSpy.AsmEditor/Method/MethodDefCommands.cs b/Extensions/dnSpy.AsmEditor/Method/MethodDefCommands.cs index 28b17d6245..2b8ec92d3a 100644 --- a/Extensions/dnSpy.AsmEditor/Method/MethodDefCommands.cs +++ b/Extensions/dnSpy.AsmEditor/Method/MethodDefCommands.cs @@ -512,11 +512,15 @@ public void Execute() { Debug.Assert(b); if (!b) throw new InvalidOperationException(); + + var isNodeSelected = methodNode.TreeNode.TreeView.SelectedItem == methodNode; + origParentNode.TreeNode.Children.RemoveAt(origParentChildIndex); newOptions.CopyTo(methodNode.MethodDef); - origParentNode.TreeNode.AddChild(methodNode.TreeNode); - origParentNode.TreeNode.TreeView.SelectItems(new[] { methodNode }); + + if (isNodeSelected) + origParentNode.TreeNode.TreeView.SelectItems(new[] { methodNode }); } else newOptions.CopyTo(methodNode.MethodDef); diff --git a/Extensions/dnSpy.AsmEditor/Module/ModuleCommands.cs b/Extensions/dnSpy.AsmEditor/Module/ModuleCommands.cs index d73eb6c4e3..f87f82967f 100644 --- a/Extensions/dnSpy.AsmEditor/Module/ModuleCommands.cs +++ b/Extensions/dnSpy.AsmEditor/Module/ModuleCommands.cs @@ -766,7 +766,6 @@ static void Execute(Lazy undoCommandService, IAppService ap void WriteModuleOptions(ModuleOptions theOptions) { theOptions.CopyTo(modNode.Document.ModuleDef!); - modNode.TreeNode.TreeView.SelectItems(new[] { modNode }); modNode.TreeNode.RefreshUI(); } diff --git a/Extensions/dnSpy.AsmEditor/Namespace/NamespaceCommands.cs b/Extensions/dnSpy.AsmEditor/Namespace/NamespaceCommands.cs index 36faddbe16..99e5bc5570 100644 --- a/Extensions/dnSpy.AsmEditor/Namespace/NamespaceCommands.cs +++ b/Extensions/dnSpy.AsmEditor/Namespace/NamespaceCommands.cs @@ -450,6 +450,9 @@ public void Execute() { Debug.Assert(b); if (!b) throw new InvalidOperationException(); + + var isNodeSelected = nsNode.TreeNode.TreeView.SelectedItem == nsNode; + origParentNode.TreeNode.Children.RemoveAt(origParentChildIndex); nsNode.Name = newName; @@ -457,7 +460,10 @@ public void Execute() { typeNode.TypeDef.Namespace = newNamespace; origParentNode.TreeNode.AddChild(nsNode.TreeNode); - origParentNode.TreeNode.TreeView.SelectItems(new[] { nsNode }); + + if (isNodeSelected) + origParentNode.TreeNode.TreeView.SelectItems(new[] { nsNode }); + nsNode.TreeNode.RefreshUI(); } @@ -481,6 +487,8 @@ public void Undo() { } } else { + var isNodeSelected = nsNode.TreeNode.TreeView.SelectedItem == nsNode; + bool b = origParentNode.TreeNode.Children.Remove(nsNode.TreeNode); Debug.Assert(b); if (!b) @@ -491,7 +499,10 @@ public void Undo() { nsNode.Name = origName; origParentNode.TreeNode.Children.Insert(origParentChildIndex, nsNode.TreeNode); - origParentNode.TreeNode.TreeView.SelectItems(new[] { nsNode }); + + if (isNodeSelected) + origParentNode.TreeNode.TreeView.SelectItems(new[] { nsNode }); + nsNode.TreeNode.RefreshUI(); } diff --git a/Extensions/dnSpy.AsmEditor/Property/PropertyDefCommands.cs b/Extensions/dnSpy.AsmEditor/Property/PropertyDefCommands.cs index 9ada7213d4..268ab5ed93 100644 --- a/Extensions/dnSpy.AsmEditor/Property/PropertyDefCommands.cs +++ b/Extensions/dnSpy.AsmEditor/Property/PropertyDefCommands.cs @@ -411,11 +411,15 @@ public void Execute() { Debug.Assert(b); if (!b) throw new InvalidOperationException(); + + var isNodeSelected = propNode.TreeNode.TreeView.SelectedItem == propNode; + origParentNode.TreeNode.Children.RemoveAt(origParentChildIndex); newOptions.CopyTo(propNode.PropertyDef); - origParentNode.TreeNode.AddChild(propNode.TreeNode); - origParentNode.TreeNode.TreeView.SelectItems(new[] { propNode }); + + if (isNodeSelected) + origParentNode.TreeNode.TreeView.SelectItems(new[] { propNode }); } else newOptions.CopyTo(propNode.PropertyDef); @@ -424,6 +428,8 @@ public void Execute() { public void Undo() { if (nameChanged) { + var isNodeSelected = propNode.TreeNode.TreeView.SelectedItem == propNode; + bool b = origParentNode.TreeNode.Children.Remove(propNode.TreeNode); Debug.Assert(b); if (!b) @@ -431,7 +437,9 @@ public void Undo() { origOptions.CopyTo(propNode.PropertyDef); origParentNode.TreeNode.Children.Insert(origParentChildIndex, propNode.TreeNode); - origParentNode.TreeNode.TreeView.SelectItems(new[] { propNode }); + + if (isNodeSelected) + origParentNode.TreeNode.TreeView.SelectItems(new[] { propNode }); } else origOptions.CopyTo(propNode.PropertyDef); diff --git a/Extensions/dnSpy.AsmEditor/Resources/ResourceCommands.cs b/Extensions/dnSpy.AsmEditor/Resources/ResourceCommands.cs index 9f6e9f6131..14592f58b8 100644 --- a/Extensions/dnSpy.AsmEditor/Resources/ResourceCommands.cs +++ b/Extensions/dnSpy.AsmEditor/Resources/ResourceCommands.cs @@ -1075,11 +1075,15 @@ public void Execute() { Debug.Assert(b); if (!b) throw new InvalidOperationException(); + + var isNodeSelected = rsrcNode.Node.TreeNode.TreeView.SelectedItem == rsrcNode.Node; + origParentNode.TreeNode.Children.RemoveAt(origParentChildIndex); newOptions.CopyTo(rsrcNode.Resource); - origParentNode.TreeNode.AddChild(rsrcNode.Node.TreeNode); - origParentNode.TreeNode.TreeView.SelectItems(new[] { rsrcNode.Node }); + + if (isNodeSelected) + origParentNode.TreeNode.TreeView.SelectItems(new[] { rsrcNode.Node }); } else newOptions.CopyTo(rsrcNode.Resource); @@ -1088,6 +1092,8 @@ public void Execute() { public void Undo() { if (nameChanged) { + var isNodeSelected = rsrcNode.Node.TreeNode.TreeView.SelectedItem == rsrcNode.Node; + bool b = origParentNode.TreeNode.Children.Remove(rsrcNode.Node.TreeNode); Debug.Assert(b); if (!b) @@ -1095,7 +1101,9 @@ public void Undo() { origOptions.CopyTo(rsrcNode.Resource); origParentNode.TreeNode.Children.Insert(origParentChildIndex, rsrcNode.Node.TreeNode); - origParentNode.TreeNode.TreeView.SelectItems(new[] { rsrcNode.Node }); + + if (isNodeSelected) + origParentNode.TreeNode.TreeView.SelectItems(new[] { rsrcNode.Node }); } else origOptions.CopyTo(rsrcNode.Resource); diff --git a/Extensions/dnSpy.AsmEditor/Types/TypeDefCommands.cs b/Extensions/dnSpy.AsmEditor/Types/TypeDefCommands.cs index ed2e48e04e..01f099bc0d 100644 --- a/Extensions/dnSpy.AsmEditor/Types/TypeDefCommands.cs +++ b/Extensions/dnSpy.AsmEditor/Types/TypeDefCommands.cs @@ -530,22 +530,32 @@ public void Execute() { Debug.Assert(b); if (!b) throw new InvalidOperationException(); + + var isNodeSelected = typeNode.TreeNode.TreeView.SelectedItem == typeNode; + origParentNode.TreeNode.Children.RemoveAt(origParentChildIndex); newOptions.CopyTo(typeNode.TypeDef, module); nsNodeCreator.Add(); nsNodeCreator.NamespaceNode.TreeNode.AddChild(typeNode.TreeNode); + + if (isNodeSelected) + origParentNode.TreeNode.TreeView.SelectItems(new[] { typeNode }); } else if (nameChanged) { bool b = origParentChildIndex < origParentNode.TreeNode.Children.Count && origParentNode.TreeNode.Children[origParentChildIndex] == typeNode.TreeNode; Debug.Assert(b); if (!b) throw new InvalidOperationException(); + + var isNodeSelected = typeNode.TreeNode.TreeView.SelectedItem == typeNode; + origParentNode.TreeNode.Children.RemoveAt(origParentChildIndex); newOptions.CopyTo(typeNode.TypeDef, module); - origParentNode.TreeNode.AddChild(typeNode.TreeNode); - origParentNode.TreeNode.TreeView.SelectItems(new[] { typeNode }); + + if (isNodeSelected) + origParentNode.TreeNode.TreeView.SelectItems(new[] { typeNode }); } else newOptions.CopyTo(typeNode.TypeDef, module);