[geary/mjog/493-undo-send: 17/37] Let Application.Command instances specify if they support redo



commit 455eb770fd3553b1dcabbd7a9b677074c5f5ba34
Author: Michael Gratton <mike vee net>
Date:   Fri Nov 8 10:11:25 2019 +1100

    Let Application.Command instances specify if they support redo
    
    This adds simmilar support as already exists for undo.

 src/client/application/application-command.vala | 34 ++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 4 deletions(-)
---
diff --git a/src/client/application/application-command.vala b/src/client/application/application-command.vala
index ed831cb5..24ee9f3b 100644
--- a/src/client/application/application-command.vala
+++ b/src/client/application/application-command.vala
@@ -16,8 +16,8 @@ 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.
+     * property after the command has been executed, and if false the
+     * non-undo-able command will not be put on the undo stack.
      *
      * Returns true by default, derived classes may override if their
      * {@link undo} method should not be called.
@@ -26,6 +26,20 @@ public abstract class Application.Command : GLib.Object {
         get { return true; }
     }
 
+    /**
+     * Determines if a command can be redone.
+     *
+     * After this command has been undone by the stack, it will check
+     * this property and if false the non-redo-able command will not
+     * be put on the redo stack.
+     *
+     * Returns true by default, derived classes may override if their
+     * {@link undo} method should not be called.
+     */
+    public virtual bool can_redo {
+        get { return true; }
+    }
+
     /**
      * A human-readable label describing the effect of calling {@link undo}.
      *
@@ -412,8 +426,8 @@ public class Application.CommandStack : GLib.Object {
                 throw err;
             }
 
-            this.redo_stack.offer_head(target);
-            this.can_redo = true;
+            update_redo_stack(target);
+            this.can_redo = !this.redo_stack.is_empty;
 
             undone(target);
             target.undone();
@@ -484,4 +498,16 @@ public class Application.CommandStack : GLib.Object {
         }
     }
 
+    /**
+     * Updates the redo stack when a command is undone.
+     *
+     * By default, this pushes the command to the head of the redo
+     * stack if {@link Command.can_undo} is true.
+     */
+    protected virtual void update_redo_stack(Command target) {
+        if (target.can_redo) {
+            this.redo_stack.offer_head(target);
+        }
+    }
+
 }


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