[geary/mjog/account-command-stacks: 40/77] Allow Command instances to denote whether they support undoing



commit 17ddb451501a72eb2eb47a9ab0f0c2494be263c8
Author: Michael Gratton <mike vee net>
Date:   Sun Oct 6 10:30:43 2019 +1100

    Allow Command instances to denote whether they support undoing
    
    Update CommanddStack to discard commands and the undo stack after
    executing a command that is not undoable.

 src/client/application/application-command.vala | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
---
diff --git a/src/client/application/application-command.vala b/src/client/application/application-command.vala
index 796b0102..65dfcf15 100644
--- a/src/client/application/application-command.vala
+++ b/src/client/application/application-command.vala
@@ -12,6 +12,20 @@
 public abstract class Application.Command : GLib.Object {
 
 
+    /**
+     * Determines if a command can be undone.
+     *
+     * When passed to {@link CommandStack}, the stack will check this
+     * property after the command has been executed, and if false undo
+     * stack will be emptied and the non-undo-able command dropped.
+     *
+     * Returns true by default, derived classes may override if their
+     * {@link undo} method should not be called.
+     */
+    public virtual bool can_undo {
+        get { return true; }
+    }
+
     /**
      * A human-readable label describing the effect of calling {@link undo}.
      *
@@ -279,8 +293,13 @@ public class Application.CommandStack : GLib.Object {
         debug("Executing: %s", target.to_string());
         yield target.execute(cancellable);
 
-        this.undo_stack.insert(0, target);
-        this.can_undo = true;
+        if (target.can_undo) {
+            this.undo_stack.insert(0, target);
+            this.can_undo = true;
+        } else {
+            this.undo_stack.clear();
+            this.can_undo = false;
+        }
 
         this.redo_stack.clear();
         this.can_redo = false;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]