From 6a09d692de73c00e7fef8655ceaf7da24b390d3e Mon Sep 17 00:00:00 2001 From: Andreas Weizel Date: Fri, 28 Jun 2013 00:55:12 +0200 Subject: [PATCH] Part II of: Closing of AbstractInlineRefactorDialog-based dialogs on Undo command is now handled without faking input. --- .../AbstractInlineRefactorDialog.cs | 75 +++++++++++-------- .../Src/Refactoring/CreateProperties.cs | 13 ++-- .../CreatePropertiesDialog.xaml.cs | 8 -- 3 files changed, 52 insertions(+), 44 deletions(-) diff --git a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/AbstractInlineRefactorDialog.cs b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/AbstractInlineRefactorDialog.cs index c343f0a2a0..fb91d6bf66 100644 --- a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/AbstractInlineRefactorDialog.cs +++ b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/AbstractInlineRefactorDialog.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; -using System.ComponentModel; using System.Threading; using System.Windows; using System.Windows.Controls; @@ -17,7 +16,6 @@ using ICSharpCode.NRefactory.CSharp; using ICSharpCode.NRefactory.CSharp.Refactoring; using ICSharpCode.NRefactory.Editor; using ICSharpCode.NRefactory.TypeSystem; -using ICSharpCode.SharpDevelop; using CSharpBinding.Refactoring; using ICSharpCode.SharpDevelop.Editor; @@ -25,6 +23,36 @@ namespace CSharpBinding.Refactoring { public abstract class AbstractInlineRefactorDialog : GroupBox, IOptionBindingContainer, IActiveElement { + private class UndoableInlineDialogCreation : IUndoableOperation + { + private AbstractInlineRefactorDialog _dialog; + + public UndoableInlineDialogCreation(AbstractInlineRefactorDialog dialog) + { + _dialog = dialog; + } + + public void Reset() + { + // Remove dialog reference + _dialog = null; + } + + public void Undo() + { + if (_dialog != null) { + // Close the dialog + _dialog.Deactivate(); + Reset(); + } + } + + public void Redo() + { + // We don't react to Redo command here... + } + } + protected ITextAnchor anchor; protected ITextAnchor insertionEndAnchor; protected ITextEditor editor; @@ -32,6 +60,8 @@ namespace CSharpBinding.Refactoring protected SDRefactoringContext refactoringContext; protected InsertionContext insertionContext; + private UndoableInlineDialogCreation undoableCreationOperation; + public IInlineUIElement Element { get; set; } protected AbstractInlineRefactorDialog(InsertionContext context, ITextEditor editor, ITextAnchor anchor) @@ -44,6 +74,16 @@ namespace CSharpBinding.Refactoring this.insertionContext = context; this.Background = SystemColors.ControlBrush; + + undoableCreationOperation = new UndoableInlineDialogCreation(this); + } + + public IUndoableOperation UndoableCreationOperation + { + get + { + return undoableCreationOperation; + } } protected virtual void FocusFirstElement() @@ -127,29 +167,6 @@ namespace CSharpBinding.Refactoring protected virtual void Initialize() { this.refactoringContext = SDRefactoringContext.Create(editor, CancellationToken.None); - - TextDocument textDocument = editor.Document as TextDocument; - if (textDocument != null) { - textDocument.UndoStack.PropertyChanged += UndoStackPropertyChanged; - } - } - - void UndoStackPropertyChanged(object sender, PropertyChangedEventArgs e) - { - if (e.PropertyName == "CanRedo") { - // Undo command has been triggered? - OnUndoTriggered(); - - // Unregister from event, again - TextDocument textDocument = editor.Document as TextDocument; - if (textDocument != null) { - textDocument.UndoStack.PropertyChanged -= UndoStackPropertyChanged; - } - } - } - - protected virtual void OnUndoTriggered() - { } protected virtual void OnInsertionCompleted() @@ -165,11 +182,6 @@ namespace CSharpBinding.Refactoring return; } - TextDocument textDocument = editor.Document as TextDocument; - if (textDocument != null) { - textDocument.UndoStack.PropertyChanged -= UndoStackPropertyChanged; - } - if (e.Reason == DeactivateReason.ReturnPressed) OKButtonClick(null, null); @@ -191,6 +203,9 @@ namespace CSharpBinding.Refactoring deactivated = true; Element.Remove(); + // Cut connection with UndoableInlineDialogCreation + undoableCreationOperation.Reset(); + insertionContext.Deactivate(null); } diff --git a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CreateProperties.cs b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CreateProperties.cs index 6a9b7b000c..3cdcacf6ca 100644 --- a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CreateProperties.cs +++ b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CreateProperties.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; +using ICSharpCode.AvalonEdit.Document; using ICSharpCode.AvalonEdit.Snippets; using ICSharpCode.NRefactory.Editor; using ICSharpCode.SharpDevelop.Editor; @@ -37,16 +38,16 @@ namespace CSharpBinding.Refactoring ITextAnchor anchor = textEditor.Document.CreateAnchor(context.InsertionPosition); anchor.MovementType = AnchorMovementType.BeforeInsertion; - // Since this snippet doesn't insert anything, fake insertion of 1 character to allow proper Ctrl+Z reaction -// if (context.StartPosition == context.InsertionPosition) { -// textEditor.Document.Insert(context.InsertionPosition, " "); -// context.InsertionPosition++; -// } - CreatePropertiesDialog dialog = new CreatePropertiesDialog(context, textEditor, anchor); dialog.Element = uiService.CreateInlineUIElement(anchor, dialog); + // Add creation of this inline dialog as undoable operation + TextDocument document = textEditor.Document as TextDocument; + if (document != null) { + document.UndoStack.Push(dialog.UndoableCreationOperation); + } + return dialog; } diff --git a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CreatePropertiesDialog.xaml.cs b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CreatePropertiesDialog.xaml.cs index 0bab18417e..b37a6d8b64 100644 --- a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CreatePropertiesDialog.xaml.cs +++ b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CreatePropertiesDialog.xaml.cs @@ -56,14 +56,6 @@ namespace CSharpBinding.Refactoring SelectAllUnchecked(); } - protected override void OnUndoTriggered() - { - base.OnUndoTriggered(); - - // Close this dialog on Undo - Deactivate(); - } - static IEnumerable FindFields(IType sourceClass) { int i = 0;