[gnome-notes/wip/igaldino/menu-popover] toolbar: Primary menu popover change



commit 5058fcc00f934b20286d735e397ef0d725e94f10
Author: Isaque Galdino <igaldino gmail com>
Date:   Wed Oct 17 18:48:03 2018 -0300

    toolbar: Primary menu popover change
    
    Change primary popup menu to a popover menu and move related code from
    main-toolbar to application source code.

 data/resources/main-toolbar.ui | 211 ++++++++++++++++++++++-------------------
 src/bjb-application.c          | 118 ++++++++++++++++++++++-
 src/bjb-main-toolbar.c         |  90 ------------------
 3 files changed, 230 insertions(+), 189 deletions(-)
---
diff --git a/data/resources/main-toolbar.ui b/data/resources/main-toolbar.ui
index 9d99ebf..767fd63 100644
--- a/data/resources/main-toolbar.ui
+++ b/data/resources/main-toolbar.ui
@@ -170,13 +170,16 @@
 
     <child>
       <object class="GtkMenuButton" id="main_button">
-        <property name="visible">1</property>
-        <property name="direction">none</property>
-        <property name="tooltip-text" translatable="yes">Open menu</property>
-        <property name="popup">main_menu</property>
-        <style>
-         <class name="image-button"/>
-        </style>
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="popover">main_menu</property>
+        <property name="receives_default">True</property>
+        <child>
+          <object class="GtkImage" id="main_button_image">
+            <property name="visible">True</property>
+            <property name="icon_name">open-menu-symbolic</property>
+          </object>
+        </child>
       </object>
       <packing>
         <property name="position">0</property>
@@ -209,115 +212,127 @@
 
   </template>
 
-  <object class="GtkMenu" id="main_menu">
-    <property name="visible">1</property>
-
+  <object class="GtkPopoverMenu" id="main_menu">
+    <property name="can_focus">False</property>
     <child>
-      <object class="GtkMenuItem" id="import_notes_item">
-        <property name="visible">1</property>
-        <property name="label" translatable="yes">Import Notes</property>
-        <signal name="activate" handler="on_import_notes_cb" swapped="yes"/>
-      </object>
-    </child>
+      <object class="GtkBox">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="margin">12</property>
+        <property name="orientation">vertical</property>
 
-    <child>
-      <object class="GtkMenuItem" id="view_trash_item">
-        <property name="visible">1</property>
-        <property name="label" translatable="yes">View Trash</property>
-        <signal name="activate" handler="on_view_trash_cb" swapped="yes"/>
-      </object>
-    </child>
+        <child>
+          <object class="GtkModelButton">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="text" translatable="yes">Import Notes</property>
+            <property name="action-name">app.import-notes</property>
+          </object>
+        </child>
 
-    <child>
-      <object class="GtkSeparatorMenuItem">
-        <property name="visible">1</property>
-      </object>
-    </child>
+        <child>
+          <object class="GtkModelButton">
+            <property name="visible">True</property>
+            <property name="text" translatable="yes">View Trash</property>
+            <property name="action-name">app.view-trash</property>
+          </object>
+        </child>
 
-    <child>
-      <object class="GtkMenuItem">
-        <property name="visible">True</property>
-        <property name="label" translatable="yes">_Text Sizes</property>
-        <property name="use_underline">True</property>
+        <child>
+          <object class="GtkSeparator">
+            <property name="margin-top">6</property>
+            <property name="margin-bottom">6</property>
+            <property name="visible">True</property>
+          </object>
+        </child>
 
-        <child type="submenu">
-          <object class="GtkMenu">
+        <child>
+          <object class="GtkLabel">
             <property name="visible">True</property>
+            <property name="sensitive">False</property>
+            <property name="label" translatable="yes">Text Sizes</property>
+            <property name="margin-left">6</property>
+            <property name="xalign">0</property>
+          </object>
+        </child>
 
-            <child>
-              <object class="GtkRadioMenuItem" id="large_item">
-                <property name="visible">True</property>
-                <property name="label" translatable="yes">_Large</property>
-                <property name="use_underline">True</property>
-                <property name="draw_as_radio">True</property>
-                <signal name="activate" handler="on_text_size_cb" swapped="yes"/>
-              </object>
-            </child>
+        <child>
+          <object class="GtkModelButton">
+            <property name="visible">True</property>
+            <property name="text" translatable="yes">_Large</property>
+            <property name="action-name">app.text-size</property>
+            <property name="action-target">'large'</property>
+          </object>
+        </child>
 
-            <child>
-              <object class="GtkRadioMenuItem" id="medium_item">
-                <property name="visible">True</property>
-                <property name="label" translatable="yes">_Medium</property>
-                <property name="use_underline">True</property>
-                <property name="draw_as_radio">True</property>
-                <property name="group">large_item</property>
-                <signal name="activate" handler="on_text_size_cb" swapped="yes"/>
-              </object>
-            </child>
+        <child>
+          <object class="GtkModelButton">
+            <property name="visible">True</property>
+            <property name="text" translatable="yes">_Medium</property>
+            <property name="action-name">app.text-size</property>
+            <property name="action-target">'medium'</property>
+          </object>
+        </child>
 
-            <child>
-              <object class="GtkRadioMenuItem" id="small_item">
-                <property name="visible">True</property>
-                <property name="label" translatable="yes">_Small</property>
-                <property name="use_underline">True</property>
-                <property name="draw_as_radio">True</property>
-                <property name="group">large_item</property>
-                <signal name="activate" handler="on_text_size_cb" swapped="yes"/>
-              </object>
-            </child>
+        <child>
+          <object class="GtkModelButton">
+            <property name="visible">True</property>
+            <property name="text" translatable="yes">_Small</property>
+            <property name="action-name">app.text-size</property>
+            <property name="action-target">'small'</property>
           </object>
         </child>
-      </object>
-    </child>
 
-    <child>
-      <object class="GtkMenuItem" id="preferences_item">
-        <property name="visible">1</property>
-        <property name="label" translatable="yes">Preferences</property>
-        <signal name="activate" handler="on_preferences_cb" swapped="yes"/>
-      </object>
-    </child>
+        <child>
+          <object class="GtkSeparator">
+            <property name="margin-top">6</property>
+            <property name="margin-bottom">6</property>
+            <property name="visible">True</property>
+          </object>
+        </child>
 
-    <child>
-      <object class="GtkSeparatorMenuItem">
-        <property name="visible">1</property>
-      </object>
-    </child>
+        <child>
+          <object class="GtkModelButton">
+            <property name="visible">True</property>
+            <property name="text" translatable="yes">Preferences</property>
+            <property name="action-name">app.preferences</property>
+          </object>
+        </child>
 
-    <child>
-      <object class="GtkMenuItem" id="help_item">
-        <property name="visible">1</property>
-        <property name="label" translatable="yes">Help</property>
-        <signal name="activate" handler="on_help_cb" swapped="yes"/>
-      </object>
-    </child>
+        <child>
+          <object class="GtkSeparator">
+            <property name="margin-top">6</property>
+            <property name="margin-bottom">6</property>
+            <property name="visible">True</property>
+          </object>
+        </child>
 
-    <child>
-      <object class="GtkMenuItem" id="shortcuts_item">
-        <property name="visible">1</property>
-        <property name="label" translatable="yes">Keyboard Shortcuts</property>
-        <property name="action_name">win.show-help-overlay</property>
-      </object>
-    </child>
+        <child>
+          <object class="GtkModelButton">
+            <property name="visible">True</property>
+            <property name="text" translatable="yes">Help</property>
+            <property name="action_name">app.help</property>
+          </object>
+        </child>
+
+        <child>
+          <object class="GtkModelButton">
+            <property name="visible">True</property>
+            <property name="text" translatable="yes">Keyboard Shortcuts</property>
+            <property name="action_name">win.show-help-overlay</property>
+          </object>
+        </child>
+
+        <child>
+          <object class="GtkModelButton">
+            <property name="visible">True</property>
+            <property name="text" translatable="yes">About Notes</property>
+            <property name="action-name">app.about</property>
+          </object>
+        </child>
 
-    <child>
-      <object class="GtkMenuItem" id="about_item">
-        <property name="visible">1</property>
-        <property name="label" translatable="yes">About Notes</property>
-        <signal name="activate" handler="on_about_cb" swapped="yes"/>
       </object>
     </child>
-
   </object>
 
   <object class="GtkMenu" id="menu">
diff --git a/src/bjb-application.c b/src/bjb-application.c
index 4a98c1a..b84ae88 100644
--- a/src/bjb-application.c
+++ b/src/bjb-application.c
@@ -43,7 +43,7 @@ struct _BjbApplication
   BjbSettings *settings;
 
   /* Controls. to_open is for startup */
-
+  GAction     *text_size;
   gboolean     first_run;
   gboolean     is_loaded;
   gboolean     new_note;
@@ -58,6 +58,34 @@ static gboolean bijiben_open_path           (BjbApplication *self,
                                              gchar          *path,
                                              BjbWindowBase *window);
 
+void            on_import_notes_cb          (GSimpleAction      *action,
+                                             GVariant           *parameter,
+                                             gpointer            user_data);
+
+void            on_view_trash_cb            (GSimpleAction      *action,
+                                             GVariant           *parameter,
+                                             gpointer            user_data);
+
+void            on_preferences_cb           (GSimpleAction      *action,
+                                             GVariant           *parameter,
+                                             gpointer            user_data);
+
+void            on_help_cb                  (GSimpleAction      *action,
+                                             GVariant           *parameter,
+                                             gpointer            user_data);
+
+void            on_about_cb                 (GSimpleAction      *action,
+                                             GVariant           *parameter,
+                                             gpointer            user_data);
+
+gboolean        text_size_mapping_get       (GValue             *value,
+                                             GVariant           *variant,
+                                             gpointer            user_data);
+
+GVariant       *text_size_mapping_set       (const GValue       *value,
+                                             const GVariantType *expected_type,
+                                             gpointer            user_data);
+
 static void
 on_window_activated_cb (BjbWindowBase  *window,
                         gboolean        available,
@@ -195,6 +223,69 @@ bijiben_import_notes (BjbApplication *self, gchar *uri)
                            uri);
 }
 
+void
+on_import_notes_cb (GSimpleAction *action,
+                    GVariant      *parameter,
+                    gpointer       user_data)
+{
+  bjb_app_import_notes (BJB_APPLICATION (user_data));
+}
+
+void
+on_view_trash_cb (GSimpleAction *action,
+                  GVariant      *parameter,
+                  gpointer       user_data)
+{
+  GList *windows = gtk_application_get_windows (GTK_APPLICATION (user_data));
+  BjbController *controller = bjb_window_base_get_controller (BJB_WINDOW_BASE (windows->data));
+
+  bjb_controller_set_group (controller, BIJI_ARCHIVED_ITEMS);
+}
+
+void
+on_preferences_cb (GSimpleAction *action,
+                   GVariant      *parameter,
+                   gpointer       user_data)
+{
+  GtkApplication *app = GTK_APPLICATION (user_data);
+  GList *windows = gtk_application_get_windows (app);
+
+  show_bijiben_settings_window (g_list_nth_data (windows, 0));
+}
+
+void
+on_help_cb (GSimpleAction *action,
+            GVariant      *parameter,
+            gpointer       user_data)
+{
+  bjb_app_help (BJB_APPLICATION (user_data));
+}
+
+void
+on_about_cb (GSimpleAction *action,
+             GVariant      *parameter,
+             gpointer       user_data)
+{
+  bjb_app_about (BJB_APPLICATION (user_data));
+}
+
+gboolean
+text_size_mapping_get (GValue   *value,
+                       GVariant *variant,
+                       gpointer  user_data)
+{
+  g_value_set_variant (value, variant);
+  return TRUE;
+}
+
+GVariant *
+text_size_mapping_set (const GValue       *value,
+                       const GVariantType *expected_type,
+                       gpointer            user_data)
+{
+  return g_value_dup_variant (value);
+}
+
 static void
 theme_changed (GtkSettings *settings)
 {
@@ -280,6 +371,15 @@ manager_ready_cb (GObject *source,
   bijiben_new_window_internal (self, NULL);
 }
 
+static GActionEntry app_entries[] = {
+  { "import-notes", on_import_notes_cb, NULL, NULL, NULL },
+  { "view-trash", on_view_trash_cb, NULL, NULL, NULL },
+  { "text-size", NULL, "s", "'medium'", NULL },
+  { "preferences", on_preferences_cb, NULL, NULL, NULL },
+  { "help", on_help_cb, NULL, NULL, NULL },
+  { "about", on_about_cb, NULL, NULL, NULL },
+};
+
 static void
 bijiben_startup (GApplication *application)
 {
@@ -296,6 +396,11 @@ bijiben_startup (GApplication *application)
 
   bjb_apply_style ();
 
+  g_action_map_add_action_entries (G_ACTION_MAP (application),
+                                   app_entries,
+                                   G_N_ELEMENTS (app_entries),
+                                   application);
+
   storage_path = g_build_filename (g_get_user_data_dir (), "bijiben", NULL);
   storage = g_file_new_for_path (storage_path);
 
@@ -312,6 +417,17 @@ bijiben_startup (GApplication *application)
   g_object_get (self->settings, "color", &default_color, NULL);
   gdk_rgba_parse (&color, default_color);
 
+  self->text_size = g_action_map_lookup_action (G_ACTION_MAP (application), "text-size");
+  g_settings_bind_with_mapping (G_SETTINGS (self->settings),
+                                "text-size",
+                                self->text_size,
+                                "state",
+                                G_SETTINGS_BIND_DEFAULT,
+                                text_size_mapping_get,
+                                text_size_mapping_set,
+                                NULL,
+                                NULL);
+
   g_application_hold (application);
   biji_manager_new_async (storage, &color, manager_ready_cb, self);
 }
diff --git a/src/bjb-main-toolbar.c b/src/bjb-main-toolbar.c
index b5d96d3..1da5a97 100644
--- a/src/bjb-main-toolbar.c
+++ b/src/bjb-main-toolbar.c
@@ -69,11 +69,6 @@ struct _BjbMainToolbar
   GtkWidget *main_button;
   GtkWidget *menu_button;
 
-  /* Main menu items */
-  GtkWidget *large_item;
-  GtkWidget *medium_item;
-  GtkWidget *small_item;
-
   /* Menu items */
   GtkWidget *new_window_item;
   GtkWidget *undo_item;
@@ -107,70 +102,6 @@ static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
 
 G_DEFINE_TYPE (BjbMainToolbar, bjb_main_toolbar, GTK_TYPE_HEADER_BAR)
 
-static void
-on_about_cb (BjbMainToolbar *self)
-{
-  bjb_app_about (BJB_APPLICATION (g_application_get_default ()));
-}
-
-static void
-on_import_notes_cb (BjbMainToolbar *self)
-{
-  bjb_app_import_notes (BJB_APPLICATION (g_application_get_default ()));
-}
-
-static void
-on_view_trash_cb (BjbMainToolbar *self)
-{
-  GtkApplication *app = GTK_APPLICATION (g_application_get_default ());
-  GList *windows = gtk_application_get_windows (app);
-  BjbController *controller = bjb_window_base_get_controller (BJB_WINDOW_BASE (windows->data));
-
-  bjb_controller_set_group (controller, BIJI_ARCHIVED_ITEMS);
-}
-
-static void
-on_text_size_cb (BjbMainToolbar *self,
-                 GtkWidget      *item)
-{
-  BjbSettings *settings = bjb_app_get_settings (g_application_get_default ());
-  BjbTextSizeType text_size = g_settings_get_enum (G_SETTINGS (settings), "text-size");
-  BjbTextSizeType new_text_size = text_size;
-
-  if (item == self->large_item)
-    {
-      new_text_size = BJB_TEXT_SIZE_LARGE;
-    }
-  else if (item == self->medium_item)
-    {
-      new_text_size = BJB_TEXT_SIZE_MEDIUM;
-    }
-  else if (item == self->small_item)
-    {
-      new_text_size = BJB_TEXT_SIZE_SMALL;
-    }
-
-  if (text_size != new_text_size)
-    {
-      g_settings_set_enum (G_SETTINGS (settings), "text-size", new_text_size);
-    }
-}
-
-static void
-on_preferences_cb (BjbMainToolbar *self)
-{
-  GtkApplication *app = GTK_APPLICATION (g_application_get_default ());
-  GList *windows = gtk_application_get_windows (app);
-
-  show_bijiben_settings_window (g_list_nth_data (windows, 0));
-}
-
-static void
-on_help_cb (BjbMainToolbar *self)
-{
-  bjb_app_help (BJB_APPLICATION (g_application_get_default ()));
-}
-
 static void
 on_new_note_clicked (BjbMainToolbar *self)
 {
@@ -781,9 +712,6 @@ populate_main_toolbar(BjbMainToolbar *self)
 static void
 bjb_main_toolbar_setup_menu (BjbMainToolbar *self)
 {
-  BjbSettings *settings = bjb_app_get_settings (g_application_get_default ());
-  BjbTextSizeType text_size = g_settings_get_enum (G_SETTINGS (settings), "text-size");
-
   gtk_widget_add_accelerator (self->undo_item, "activate", self->accel, GDK_KEY_z,
                               GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
   gtk_widget_add_accelerator (self->redo_item, "activate", self->accel, GDK_KEY_z,
@@ -791,11 +719,6 @@ bjb_main_toolbar_setup_menu (BjbMainToolbar *self)
   gtk_widget_add_accelerator (self->trash_item, "activate", self->accel,
                               GDK_KEY_Delete, GDK_CONTROL_MASK,
                               GTK_ACCEL_VISIBLE);
-
-  gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (self->large_item), text_size == BJB_TEXT_SIZE_LARGE);
-  gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (self->medium_item), text_size == 
BJB_TEXT_SIZE_MEDIUM);
-  gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (self->small_item), text_size == BJB_TEXT_SIZE_SMALL);
-
 }
 
 static void
@@ -934,11 +857,6 @@ bjb_main_toolbar_class_init (BjbMainToolbarClass *klass)
   gtk_widget_class_bind_template_child (widget_class, BjbMainToolbar, main_button);
   gtk_widget_class_bind_template_child (widget_class, BjbMainToolbar, menu_button);
 
-  /* Main menu items */
-  gtk_widget_class_bind_template_child (widget_class, BjbMainToolbar, large_item);
-  gtk_widget_class_bind_template_child (widget_class, BjbMainToolbar, medium_item);
-  gtk_widget_class_bind_template_child (widget_class, BjbMainToolbar, small_item);
-
   /* Menu items */
   gtk_widget_class_bind_template_child (widget_class, BjbMainToolbar, new_window_item);
   gtk_widget_class_bind_template_child (widget_class, BjbMainToolbar, undo_item);
@@ -953,14 +871,6 @@ bjb_main_toolbar_class_init (BjbMainToolbarClass *klass)
   gtk_widget_class_bind_template_callback (widget_class, on_empty_clicked_callback);
   gtk_widget_class_bind_template_callback (widget_class, on_color_button_clicked);
 
-  /* Main menu items */
-  gtk_widget_class_bind_template_callback (widget_class, on_import_notes_cb);
-  gtk_widget_class_bind_template_callback (widget_class, on_view_trash_cb);
-  gtk_widget_class_bind_template_callback (widget_class, on_preferences_cb);
-  gtk_widget_class_bind_template_callback (widget_class, on_help_cb);
-  gtk_widget_class_bind_template_callback (widget_class, on_about_cb);
-  gtk_widget_class_bind_template_callback (widget_class, on_text_size_cb);
-
   /* Menu items */
   gtk_widget_class_bind_template_callback (widget_class, on_detached_clicked_cb);
   gtk_widget_class_bind_template_callback (widget_class, on_undo_or_redo_cb);


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