[geary/wip/account-editor-refinements-v1: 2/7] Tidy up account editor's undo/redo action state management



commit 7ed289899a1b9a53d82ab11e095fc5d8836c1770
Author: Michael Gratton <mike vee net>
Date:   Wed Jan 9 10:13:11 2019 +1100

    Tidy up account editor's undo/redo action state management
    
    Move update_command_actions() from CommandPane to Editor, call that when
    updating state internally and make the implementation details of it
    private. Make more use of the generic command management code in the
    list pane.

 src/client/accounts/accounts-editor-edit-pane.vala |  2 +-
 src/client/accounts/accounts-editor-list-pane.vala | 16 +------
 .../accounts/accounts-editor-servers-pane.vala     |  2 +-
 src/client/accounts/accounts-editor.vala           | 52 ++++++++++------------
 4 files changed, 27 insertions(+), 45 deletions(-)
---
diff --git a/src/client/accounts/accounts-editor-edit-pane.vala 
b/src/client/accounts/accounts-editor-edit-pane.vala
index 66439e62..a86ff117 100644
--- a/src/client/accounts/accounts-editor-edit-pane.vala
+++ b/src/client/accounts/accounts-editor-edit-pane.vala
@@ -154,7 +154,7 @@ internal class Accounts.EditorEditPane :
 
     /** {@inheritDoc} */
     protected void command_executed() {
-        update_command_actions();
+        this.editor.update_command_actions();
 
         Application.Command next_undo = this.commands.peek_undo();
         this.undo_button.set_tooltip_text(
diff --git a/src/client/accounts/accounts-editor-list-pane.vala 
b/src/client/accounts/accounts-editor-list-pane.vala
index 41e737a4..db8092d8 100644
--- a/src/client/accounts/accounts-editor-list-pane.vala
+++ b/src/client/accounts/accounts-editor-list-pane.vala
@@ -110,7 +110,7 @@ internal class Accounts.EditorListPane : Gtk.Grid, EditorPane, CommandPane {
         this.commands.executed.connect(on_execute);
         this.commands.undone.connect(on_undo);
         this.commands.redone.connect(on_execute);
-
+        connect_command_signals();
         update_welcome_panel();
     }
 
@@ -118,6 +118,7 @@ internal class Accounts.EditorListPane : Gtk.Grid, EditorPane, CommandPane {
         this.commands.executed.disconnect(on_execute);
         this.commands.undone.disconnect(on_undo);
         this.commands.redone.disconnect(on_execute);
+        disconnect_command_signals();
 
         this.accounts.account_added.disconnect(on_account_added);
         this.accounts.account_status_changed.disconnect(on_account_status_changed);
@@ -169,15 +170,6 @@ internal class Accounts.EditorListPane : Gtk.Grid, EditorPane, CommandPane {
         notification.show();
     }
 
-    private void update_actions() {
-        this.editor.get_action(GearyController.ACTION_UNDO).set_enabled(
-            this.commands.can_undo
-        );
-        this.editor.get_action(GearyController.ACTION_REDO).set_enabled(
-            this.commands.can_redo
-        );
-    }
-
     private void update_welcome_panel() {
         if (this.show_welcome) {
             // No accounts are available, so show only the welcome
@@ -251,8 +243,6 @@ internal class Accounts.EditorListPane : Gtk.Grid, EditorPane, CommandPane {
             ian.set_button(_("Undo"), "win." + GearyController.ACTION_UNDO);
             add_notification(ian);
         }
-
-        update_actions();
     }
 
     private void on_undo(Application.Command command) {
@@ -261,8 +251,6 @@ internal class Accounts.EditorListPane : Gtk.Grid, EditorPane, CommandPane {
             ian.set_button(_("Redo"), "win." + GearyController.ACTION_REDO);
             add_notification(ian);
         }
-
-        update_actions();
     }
 
     [GtkCallback]
diff --git a/src/client/accounts/accounts-editor-servers-pane.vala 
b/src/client/accounts/accounts-editor-servers-pane.vala
index 2502196a..6c4e410a 100644
--- a/src/client/accounts/accounts-editor-servers-pane.vala
+++ b/src/client/accounts/accounts-editor-servers-pane.vala
@@ -179,7 +179,7 @@ internal class Accounts.EditorServersPane :
 
     /** {@inheritDoc} */
     protected void command_executed() {
-        update_command_actions();
+        this.editor.update_command_actions();
         this.apply_button.set_sensitive(this.commands.can_undo);
     }
 
diff --git a/src/client/accounts/accounts-editor.vala b/src/client/accounts/accounts-editor.vala
index 4080164d..30eee914 100644
--- a/src/client/accounts/accounts-editor.vala
+++ b/src/client/accounts/accounts-editor.vala
@@ -61,11 +61,10 @@ public class Accounts.Editor : Gtk.Dialog {
         this.actions.add_action_entries(ACTION_ENTRIES, this);
         insert_action_group("win", this.actions);
 
-        get_action(GearyController.ACTION_UNDO).set_enabled(false);
-        get_action(GearyController.ACTION_REDO).set_enabled(false);
-
         this.editor_list_pane = new EditorListPane(this);
         push(this.editor_list_pane);
+
+        update_command_actions();
     }
 
     public override bool key_press_event(Gdk.EventKey event) {
@@ -111,9 +110,6 @@ public class Accounts.Editor : Gtk.Dialog {
             this.editor_panes.remove(old);
         }
 
-        get_action(GearyController.ACTION_UNDO).set_enabled(false);
-        get_action(GearyController.ACTION_REDO).set_enabled(false);
-
         // Now push the new pane on
         this.editor_pane_stack.add(pane);
         this.editor_panes.add(pane);
@@ -131,19 +127,33 @@ public class Accounts.Editor : Gtk.Dialog {
         this.editor_panes.set_visible_child(prev);
     }
 
-    internal GLib.SimpleAction get_action(string name) {
-        return (GLib.SimpleAction) this.actions.lookup_action(name);
-    }
-
     internal void remove_account(Geary.AccountInformation account) {
         this.editor_panes.set_visible_child(this.editor_list_pane);
         this.editor_list_pane.remove_account(account);
     }
 
+    /** Updates the state of the editor's undo and redo actions. */
+    internal void update_command_actions() {
+        bool can_undo = false;
+        bool can_redo = false;
+        CommandPane? pane = get_current_pane() as CommandPane;
+        if (pane != null) {
+            can_undo = pane.commands.can_undo;
+            can_redo = pane.commands.can_redo;
+        }
+
+        get_action(GearyController.ACTION_UNDO).set_enabled(can_undo);
+        get_action(GearyController.ACTION_REDO).set_enabled(can_redo);
+    }
+
     private inline EditorPane? get_current_pane() {
         return this.editor_panes.get_visible_child() as EditorPane;
     }
 
+    private inline GLib.SimpleAction get_action(string name) {
+        return (GLib.SimpleAction) this.actions.lookup_action(name);
+    }
+
     private void on_undo() {
         CommandPane? pane = get_current_pane() as CommandPane;
         if (pane != null) {
@@ -171,11 +181,7 @@ public class Accounts.Editor : Gtk.Dialog {
             header = visible.get_header();
         }
         set_titlebar(header);
-
-        CommandPane? commands = visible as CommandPane;
-        if (commands != null) {
-            commands.update_command_actions();
-        }
+        update_command_actions();
     }
 
 }
@@ -279,18 +285,6 @@ internal interface Accounts.CommandPane : EditorPane {
         this.commands.redo.begin(null);
     }
 
-    /**
-     * Updates the state of the editor's undo and redo actions.
-     */
-    internal virtual void update_command_actions() {
-        this.editor.get_action(GearyController.ACTION_UNDO).set_enabled(
-            this.commands.can_undo
-        );
-        this.editor.get_action(GearyController.ACTION_REDO).set_enabled(
-            this.commands.can_redo
-        );
-    }
-
     /**
      * Connects to command stack signals.
      *
@@ -316,10 +310,10 @@ internal interface Accounts.CommandPane : EditorPane {
     /**
      * Called when a command is executed, undone or redone.
      *
-     * By default, calls {@link update_command_actions}.
+     * By default, calls {@link Accounts.Editor.update_command_actions}.
      */
     protected virtual void command_executed() {
-        update_command_actions();
+        this.editor.update_command_actions();
     }
 
     private void on_command() {


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