[gnome-control-center/wip/gbsneto/keyboard-improvements: 59/67] shortcut-editor: use states to handle headerbar mode



commit 07ae4d51326c988259aabff90e473aa23781f3f5
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Tue Aug 30 11:43:43 2016 -0300

    shortcut-editor: use states to handle headerbar mode
    
    Instead of manually handling every button in the headerbar,
    using states to set it is a much better approach because it's
    more semantic and easier to understand.
    
    This patch adds the headerbar mode handling code, and updates
    the current code to use it rather than by updating each individual
    button manually.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=769314

 panels/keyboard/cc-keyboard-shortcut-editor.c |   63 +++++++++++++++----------
 1 files changed, 38 insertions(+), 25 deletions(-)
---
diff --git a/panels/keyboard/cc-keyboard-shortcut-editor.c b/panels/keyboard/cc-keyboard-shortcut-editor.c
index e9f9005..6190376 100644
--- a/panels/keyboard/cc-keyboard-shortcut-editor.c
+++ b/panels/keyboard/cc-keyboard-shortcut-editor.c
@@ -83,6 +83,16 @@ enum
   N_PROPS
 };
 
+typedef enum
+{
+  NONE,
+  ADD,
+  SET,
+  REPLACE,
+  CUSTOM_CANCEL,
+  CUSTOM_EDIT
+} HeaderMode;
+
 static GParamSpec *properties [N_PROPS] = { NULL, };
 
 static void
@@ -128,8 +138,6 @@ clear_custom_entries (CcKeyboardShortcutEditor *self)
   gtk_entry_set_text (GTK_ENTRY (self->name_entry), "");
   gtk_entry_set_text (GTK_ENTRY (self->command_entry), "");
 
-  gtk_widget_set_sensitive (self->add_button, FALSE);
-
   gtk_shortcut_label_set_accelerator (GTK_SHORTCUT_LABEL (self->custom_shortcut_accel_label), "");
   gtk_label_set_label (GTK_LABEL (self->new_shortcut_conflict_label), "");
   gtk_label_set_label (GTK_LABEL (self->shortcut_conflict_label), "");
@@ -233,13 +241,15 @@ get_current_shortcut_label (CcKeyboardShortcutEditor *self)
 }
 
 static void
-set_collision_headerbar (CcKeyboardShortcutEditor *self,
-                         gboolean                  has_collision)
+set_header_mode (CcKeyboardShortcutEditor *self,
+                 HeaderMode                mode)
 {
-  gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (self->headerbar), !has_collision);
+  gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (self->headerbar), mode == CUSTOM_EDIT);
 
-  gtk_widget_set_visible (self->cancel_button, has_collision);
-  gtk_widget_set_visible (self->replace_button, has_collision);
+  gtk_widget_set_visible (self->add_button, mode == ADD);
+  gtk_widget_set_visible (self->cancel_button, mode != NONE && mode != CUSTOM_EDIT);
+  gtk_widget_set_visible (self->replace_button, mode == REPLACE);
+  gtk_widget_set_visible (self->set_button, mode == SET);
 }
 
 static void
@@ -247,22 +257,25 @@ setup_custom_shortcut (CcKeyboardShortcutEditor *self)
 {
   GtkShortcutLabel *shortcut_label;
   CcKeyboardItem *collision_item;
+  HeaderMode mode;
+  gboolean is_custom;
   gboolean valid;
   gchar *accel;
 
+  is_custom = is_custom_shortcut (self);
   valid = is_valid_binding (self->custom_keyval, self->custom_mask, self->custom_keycode) &&
           gtk_accelerator_valid (self->custom_keyval, self->custom_mask) &&
           !self->custom_is_modifier;
 
   /* Additional checks for custom shortcuts */
-  if (is_custom_shortcut (self))
+  if (is_custom)
     {
       valid = valid &&
               gtk_entry_get_text_length (GTK_ENTRY (self->name_entry)) > 0 &&
               gtk_entry_get_text_length (GTK_ENTRY (self->command_entry)) > 0;
     }
 
-  gtk_widget_set_sensitive (self->add_button, valid);
+  set_header_mode (self, valid ? ADD : is_custom ? CUSTOM_CANCEL : NONE);
 
   if (!valid)
     return;
@@ -328,8 +341,19 @@ setup_custom_shortcut (CcKeyboardShortcutEditor *self)
    * the headerbar to display "Cancel" and "Replace". Otherwise, make sure to set
    * only the close button again.
    */
-  if (self->mode == CC_SHORTCUT_EDITOR_EDIT)
-    set_collision_headerbar (self, collision_item != NULL);
+  if (collision_item)
+    {
+      mode = REPLACE;
+    }
+  else
+    {
+      if (self->mode == CC_SHORTCUT_EDITOR_EDIT)
+        mode = is_custom ? CUSTOM_EDIT : SET;
+      else
+        mode = is_custom ? ADD : SET;
+    }
+
+  set_header_mode (self, mode);
 
   self->collision_item = collision_item;
 
@@ -445,7 +469,8 @@ setup_keyboard_item (CcKeyboardShortcutEditor *self,
   /* Headerbar */
   gtk_header_bar_set_title (GTK_HEADER_BAR (self->headerbar),
                             is_custom ? _("Set Custom Shortcut") : _("Set Shortcut"));
-  gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (self->headerbar), TRUE);
+
+  set_header_mode (self, is_custom ? CUSTOM_EDIT : NONE);
 
   gtk_widget_hide (self->add_button);
   gtk_widget_hide (self->cancel_button);
@@ -792,9 +817,6 @@ cc_keyboard_shortcut_editor_set_mode (CcKeyboardShortcutEditor *self,
 
   g_return_if_fail (CC_IS_KEYBOARD_SHORTCUT_EDITOR (self));
 
-  if (self->mode == mode)
-    return;
-
   self->mode = mode;
   is_create_mode = mode == CC_SHORTCUT_EDITOR_CREATE;
 
@@ -805,18 +827,9 @@ cc_keyboard_shortcut_editor_set_mode (CcKeyboardShortcutEditor *self,
       /* Cleanup whatever was set before */
       clear_custom_entries (self);
 
-      /* The 'Add' button is only sensitive when the shortcut is valid */
-      gtk_widget_set_sensitive (self->add_button, FALSE);
-
-      gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (self->headerbar), FALSE);
+      set_header_mode (self, CUSTOM_CANCEL);
       gtk_header_bar_set_title (GTK_HEADER_BAR (self->headerbar), _("Add Custom Shortcut"));
 
       gtk_stack_set_visible_child_name (GTK_STACK (self->stack), "custom");
-
-      gtk_widget_show (self->add_button);
-      gtk_widget_show (self->cancel_button);
-
-      gtk_widget_hide (self->remove_button);
-      gtk_widget_hide (self->replace_button);
     }
 }


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