[geary/bug/728002-webkit2: 94/140] Ensure composer selection actions are not enabled when first opened.



commit ceb6400e1c00a6c1109a749f631797da99281011
Author: Michael James Gratton <mike vee net>
Date:   Tue Jan 17 17:09:20 2017 +1100

    Ensure composer selection actions are not enabled when first opened.
    
    * src/client/composer/composer-widget.vala (ComposerWidget): Rename
      on_selection_changed to update_selection_actions, call as needed. Tidy
      action init methods a bit.
    
    * src/client/components/client-web-view.vala (ClientWebView): Add new
      has_selection property and keep it updated.

 src/client/components/client-web-view.vala |   13 +++++-
 src/client/composer/composer-widget.vala   |   62 ++++++++++++++++------------
 2 files changed, 47 insertions(+), 28 deletions(-)
---
diff --git a/src/client/components/client-web-view.vala b/src/client/components/client-web-view.vala
index 2a9c265..e6e94c3 100644
--- a/src/client/components/client-web-view.vala
+++ b/src/client/components/client-web-view.vala
@@ -148,8 +148,11 @@ public class ClientWebView : WebKit.WebView {
     /** Delegate for UserContentManager message callbacks. */
     public delegate void JavaScriptMessageHandler(WebKit.JavascriptResult js_result);
 
+    /** Determines if the view has any selected text */
+    public bool has_selection { get; private set; default = false; }
+
     /** Determines if the view has started rendering the HTML */
-    public bool has_valid_height { get; set; default = false; }
+    public bool has_valid_height { get; private set; default = false; }
 
     private string _document_font;
     public string document_font {
@@ -465,7 +468,13 @@ public class ClientWebView : WebKit.WebView {
 
     private void on_selection_changed(WebKit.JavascriptResult result) {
         try {
-            selection_changed(WebKitUtil.to_bool(result));
+            bool has_selection = WebKitUtil.to_bool(result);
+            // Avoid firing multiple notifies if the value hasn't
+            // changed
+            if (this.has_selection != has_selection) {
+                this.has_selection = has_selection;
+            }
+            selection_changed(has_selection);
         } catch (Geary.JS.Error err) {
             debug("Could not get selection content: %s", err.message);
         }
diff --git a/src/client/composer/composer-widget.vala b/src/client/composer/composer-widget.vala
index b889754..ff881b5 100644
--- a/src/client/composer/composer-widget.vala
+++ b/src/client/composer/composer-widget.vala
@@ -81,10 +81,12 @@ public class ComposerWidget : Gtk.EventBox {
     private const string ACTION_SELECT_DICTIONARY = "select-dictionary";
     private const string ACTION_OPEN_INSPECTOR = "open_inspector";
 
+    // ACTION_INSERT_LINK and ACTION_REMOVE_FORMAT are missing from
+    // here since they are handled in update_selection_actions
     private const string[] html_actions = {
-        ACTION_BOLD, ACTION_ITALIC, ACTION_UNDERLINE, ACTION_STRIKETHROUGH, ACTION_FONT_SIZE,
-        ACTION_FONT_FAMILY, ACTION_REMOVE_FORMAT, ACTION_COLOR, ACTION_JUSTIFY,
-        ACTION_INSERT_IMAGE, ACTION_INSERT_LINK, ACTION_COPY_LINK, ACTION_PASTE_WITH_FORMATTING
+        ACTION_BOLD, ACTION_ITALIC, ACTION_UNDERLINE, ACTION_STRIKETHROUGH,
+        ACTION_FONT_SIZE, ACTION_FONT_FAMILY, ACTION_COLOR, ACTION_JUSTIFY,
+        ACTION_INSERT_IMAGE, ACTION_COPY_LINK, ACTION_PASTE_WITH_FORMATTING
     };
 
     private const ActionEntry[] action_entries = {
@@ -504,7 +506,9 @@ public class ComposerWidget : Gtk.EventBox {
         this.editor.key_press_event.connect(on_editor_key_press_event);
         this.editor.load_changed.connect(on_load_changed);
         this.editor.mouse_target_changed.connect(on_mouse_target_changed);
-        this.editor.selection_changed.connect(on_selection_changed);
+        this.editor.selection_changed.connect((has_selection) => {
+                update_selection_actions(has_selection);
+            });
 
         this.editor.load_html(this.body_html, this.signature_html, this.top_posting);
 
@@ -579,20 +583,6 @@ public class ComposerWidget : Gtk.EventBox {
         }
     }
 
-    // Initializes all actions and adds them to the action group
-    private void initialize_actions() {
-        this.actions.add_action_entries(action_entries, this);
-
-        // for some reason, we can't use the same prefix.
-        insert_action_group("cmp", this.actions);
-        this.header.insert_action_group("cmh", this.actions);
-
-        get_action(ACTION_CLOSE_AND_SAVE).set_enabled(false);
-
-        get_action(ACTION_UNDO).set_enabled(false);
-        get_action(ACTION_REDO).set_enabled(false);
-    }
-
     /**
      * Loads and sets contact auto-complete data for the current account.
      */
@@ -789,6 +779,32 @@ public class ComposerWidget : Gtk.EventBox {
             this.editor.grab_focus();
     }
 
+    // Initializes all actions and adds them to the action group
+    private void initialize_actions() {
+        this.actions.add_action_entries(action_entries, this);
+
+        // for some reason, we can't use the same prefix.
+        insert_action_group("cmp", this.actions);
+        this.header.insert_action_group("cmh", this.actions);
+
+        get_action(ACTION_CLOSE_AND_SAVE).set_enabled(false);
+
+        get_action(ACTION_UNDO).set_enabled(false);
+        get_action(ACTION_REDO).set_enabled(false);
+
+        // No initial selection
+        update_selection_actions(false);
+    }
+
+    private void update_selection_actions(bool has_selection) {
+        get_action(ACTION_CUT).set_enabled(has_selection);
+        get_action(ACTION_COPY).set_enabled(has_selection);
+
+        bool rich_text_selected = has_selection && this.editor.is_rich_text;
+        get_action(ACTION_INSERT_LINK).set_enabled(rich_text_selected);
+        get_action(ACTION_REMOVE_FORMAT).set_enabled(rich_text_selected);
+    }
+
     private bool check_preferred_from_address(Gee.List<Geary.RFC822.MailboxAddress> account_addresses,
         Geary.RFC822.MailboxAddresses? referred_addresses) {
         if (referred_addresses != null) {
@@ -1748,6 +1764,8 @@ public class ComposerWidget : Gtk.EventBox {
         foreach (string html_action in html_actions)
             get_action(html_action).set_enabled(compose_as_html);
 
+        update_selection_actions(this.editor.has_selection);
+
         this.insert_buttons.visible = compose_as_html;
         this.font_style_buttons.visible = compose_as_html;
         this.remove_format_button.visible = compose_as_html;
@@ -2236,14 +2254,6 @@ public class ComposerWidget : Gtk.EventBox {
         }
     }
 
-    private void on_selection_changed(bool has_selection) {
-        get_action(ACTION_CUT).set_enabled(has_selection);
-        get_action(ACTION_COPY).set_enabled(has_selection);
-        get_action(ACTION_REMOVE_FORMAT).set_enabled(
-            has_selection && this.editor.is_rich_text
-        );
-    }
-
     private void on_cursor_style_changed(string font_family, uint font_size) {
         this.actions.change_action_state(ACTION_FONT_FAMILY, font_family);
 


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