[amtk] Factory: add functions to create a GtkShortcutsShortcut



commit ce8b17806606ab9cbd43062df1bbc8438927f1c4
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Apr 13 20:50:43 2018 +0200

    Factory: add functions to create a GtkShortcutsShortcut

 amtk/amtk-factory.c                  |  115 ++++++++++++++++++++++++++++++++++
 amtk/amtk-factory.h                  |    7 ++
 docs/reference/amtk-5.0-sections.txt |    2 +
 tests/test-headerbar.c               |   28 +++-----
 4 files changed, 134 insertions(+), 18 deletions(-)
---
diff --git a/amtk/amtk-factory.c b/amtk/amtk-factory.c
index 84eed92..f85e0e0 100644
--- a/amtk/amtk-factory.c
+++ b/amtk/amtk-factory.c
@@ -970,3 +970,118 @@ amtk_factory_create_menu_tool_button_full (AmtkFactory      *factory,
 
        return GTK_MENU_TOOL_BUTTON (menu_tool_button);
 }
+
+/**
+ * amtk_factory_create_shortcut:
+ * @factory: an #AmtkFactory.
+ * @action_name: an action name.
+ *
+ * Calls amtk_factory_create_shortcut_full() with the
+ * #AmtkFactory:default-flags.
+ *
+ * Returns: (transfer floating): a new #GtkShortcutsShortcut for @action_name.
+ * Since: 5.0
+ */
+GtkWidget *
+amtk_factory_create_shortcut (AmtkFactory *factory,
+                             const gchar *action_name)
+{
+       g_return_val_if_fail (AMTK_IS_FACTORY (factory), NULL);
+       g_return_val_if_fail (action_name != NULL, NULL);
+
+       return amtk_factory_create_shortcut_full (factory,
+                                                 action_name,
+                                                 factory->priv->default_flags);
+}
+
+/**
+ * amtk_factory_create_shortcut_full:
+ * @factory: an #AmtkFactory.
+ * @action_name: an action name.
+ * @flags: #AmtkFactoryFlags.
+ *
+ * This function ignores the #AmtkFactory:default-flags property and takes the
+ * @flags argument instead.
+ *
+ * This function creates a new #GtkShortcutsShortcut for @action_name.
+ *
+ * For the #GtkShortcutsShortcut:title, the tooltip has the priorioty, with the
+ * label as fallback if the tooltip is %NULL. This can be controlled with the
+ * %AMTK_FACTORY_IGNORE_TOOLTIP and %AMTK_FACTORY_IGNORE_LABEL flags.
+ *
+ * The #GtkShortcutsShortcut:accelerator property is set with only the *first*
+ * accel returned by amtk_action_info_get_accels(). This step can be ignored
+ * with %AMTK_FACTORY_IGNORE_ACCELS or %AMTK_FACTORY_IGNORE_ACCELS_FOR_DOC.
+ *
+ * The #GtkShortcutsShortcut:action-name property is set to @action_name if the
+ * %AMTK_FACTORY_IGNORE_GACTION flag isn't set. Note that with
+ * #GtkShortcutsShortcut:action-name all accelerators are displayed (if set to
+ * the #GtkApplication).
+ *
+ * So depending on whether you want to show only the first accelerator or all
+ * accelerators, you need to set @flags appropriately.
+ *
+ * Returns: (transfer floating): a new #GtkShortcutsShortcut for @action_name.
+ * Since: 5.0
+ */
+GtkWidget *
+amtk_factory_create_shortcut_full (AmtkFactory      *factory,
+                                  const gchar      *action_name,
+                                  AmtkFactoryFlags  flags)
+{
+       AmtkActionInfo *action_info;
+       GtkWidget *shortcut;
+       const gchar *tooltip;
+       const gchar *label;
+
+       g_return_val_if_fail (AMTK_IS_FACTORY (factory), NULL);
+       g_return_val_if_fail (action_name != NULL, NULL);
+
+       action_info = common_create (factory, action_name, flags);
+       if (action_info == NULL)
+       {
+               return NULL;
+       }
+
+       shortcut = g_object_new (GTK_TYPE_SHORTCUTS_SHORTCUT, NULL);
+       gtk_widget_show (shortcut);
+
+       tooltip = amtk_action_info_get_tooltip (action_info);
+       label = amtk_action_info_get_label (action_info);
+
+       if ((flags & AMTK_FACTORY_IGNORE_TOOLTIP) == 0 &&
+           tooltip != NULL)
+       {
+               g_object_set (shortcut,
+                             "title", tooltip,
+                             NULL);
+       }
+       else if ((flags & AMTK_FACTORY_IGNORE_LABEL) == 0 &&
+                label != NULL)
+       {
+               /* TODO remove mnemonic. */
+               g_object_set (shortcut,
+                             "title", label,
+                             NULL);
+       }
+
+       if ((flags & AMTK_FACTORY_IGNORE_ACCELS) == 0 &&
+           (flags & AMTK_FACTORY_IGNORE_ACCELS_FOR_DOC) == 0)
+       {
+               const gchar * const *accels;
+
+               accels = amtk_action_info_get_accels (action_info);
+               g_object_set (shortcut,
+                             "accelerator", accels[0],
+                             NULL);
+       }
+
+       if ((flags & AMTK_FACTORY_IGNORE_GACTION) == 0)
+       {
+               g_object_set (shortcut,
+                             "action-name", action_name,
+                             NULL);
+       }
+
+       return shortcut;
+}
diff --git a/amtk/amtk-factory.h b/amtk/amtk-factory.h
index b7e2972..75b3124 100644
--- a/amtk/amtk-factory.h
+++ b/amtk/amtk-factory.h
@@ -143,6 +143,13 @@ GtkMenuToolButton *        amtk_factory_create_menu_tool_button_full       (AmtkFactory
                                                                         const gchar      *action_name,
                                                                         AmtkFactoryFlags  flags);
 
+GtkWidget *            amtk_factory_create_shortcut                    (AmtkFactory *factory,
+                                                                        const gchar *action_name);
+
+GtkWidget *            amtk_factory_create_shortcut_full               (AmtkFactory      *factory,
+                                                                        const gchar      *action_name,
+                                                                        AmtkFactoryFlags  flags);
+
 G_END_DECLS
 
 #endif /* AMTK_FACTORY_H */
diff --git a/docs/reference/amtk-5.0-sections.txt b/docs/reference/amtk-5.0-sections.txt
index f11981e..fcf435e 100644
--- a/docs/reference/amtk-5.0-sections.txt
+++ b/docs/reference/amtk-5.0-sections.txt
@@ -120,6 +120,8 @@ amtk_factory_create_tool_button
 amtk_factory_create_tool_button_full
 amtk_factory_create_menu_tool_button
 amtk_factory_create_menu_tool_button_full
+amtk_factory_create_shortcut
+amtk_factory_create_shortcut_full
 <SUBSECTION Standard>
 AMTK_FACTORY
 AMTK_FACTORY_CLASS
diff --git a/tests/test-headerbar.c b/tests/test-headerbar.c
index 1b7aabd..04f0714 100644
--- a/tests/test-headerbar.c
+++ b/tests/test-headerbar.c
@@ -66,9 +66,9 @@ shortcuts_window_activate_cb (GSimpleAction *action,
                              gpointer       user_data)
 {
        GtkShortcutsWindow *window;
-       GtkWidget *section;
-       GtkWidget *group;
-       GtkWidget *shortcut;
+       GtkContainer *section;
+       GtkContainer *group;
+       AmtkFactory *factory;
 
        /* TODO Add AmtkFactory function, and maybe some convenience functions
         * too.
@@ -80,28 +80,20 @@ shortcuts_window_activate_cb (GSimpleAction *action,
                              "title", "General",
                              NULL);
 
-       shortcut = g_object_new (GTK_TYPE_SHORTCUTS_SHORTCUT,
-                                "visible", TRUE,
-                                "title", "Find in current page",
-                                "accelerator", "<Control>f",
-                                NULL);
-       gtk_container_add (GTK_CONTAINER (group), shortcut);
-
-       shortcut = g_object_new (GTK_TYPE_SHORTCUTS_SHORTCUT,
-                                "visible", TRUE,
-                                "title", "Open a new tab",
-                                "accelerator", "<Control>t",
-                                NULL);
-       gtk_container_add (GTK_CONTAINER (group), shortcut);
+       factory = amtk_factory_new (NULL);
+       amtk_factory_set_default_flags (factory, AMTK_FACTORY_IGNORE_GACTION);
+       gtk_container_add (group, amtk_factory_create_shortcut (factory, "win.show-side-panel"));
+       gtk_container_add (group, amtk_factory_create_shortcut (factory, "win.print"));
+       g_object_unref (factory);
 
        /* Create section and window */
        section = g_object_new (GTK_TYPE_SHORTCUTS_SECTION,
                                "visible", TRUE,
                                NULL);
-       gtk_container_add (GTK_CONTAINER (section), group);
+       gtk_container_add (section, GTK_WIDGET (group));
 
        window = g_object_new (GTK_TYPE_SHORTCUTS_WINDOW, NULL);
-       gtk_container_add (GTK_CONTAINER (window), section);
+       gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (section));
 
        gtk_widget_show_all (GTK_WIDGET (window));
 }


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