diff --git a/Extensions/dnSpy.AsmEditor/Assembly/AssemblyCommands.cs b/Extensions/dnSpy.AsmEditor/Assembly/AssemblyCommands.cs index 811e37ada0..1e458dc808 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 199a44b01d..f07e27d29d 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 dfecce7ca7..fd13e9f64e 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 640508d5a7..9023200098 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 dbcca9f18a..2601df02c4 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 a4247fdbf7..9019b956ce 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 8de0c08761..fa12b09cca 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 68d43c2ab7..691767aa46 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 f9a8c7b8d8..dc8eb84892 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);