[tepl] amtk: remove menu item factory function from ActionInfoStore



commit 34043a95de445fe9bc831c550fea96d08187338e
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Jul 16 18:11:50 2017 +0200

    amtk: remove menu item factory function from ActionInfoStore

 amtk/amtk-action-info-central-store.c   |   17 +--
 amtk/amtk-action-info-store.c           |  261 +-----------------------------
 amtk/amtk-action-info-store.h           |   11 +-
 docs/reference/tepl-3.0-sections.txt    |    2 -
 tepl/tepl-application.c                 |   21 +--
 tests/test-menu.c                       |   28 ++--
 testsuite/amtk/test-action-info-store.c |    2 +-
 7 files changed, 35 insertions(+), 307 deletions(-)
---
diff --git a/amtk/amtk-action-info-central-store.c b/amtk/amtk-action-info-central-store.c
index 6fa7937..d9ccf16 100644
--- a/amtk/amtk-action-info-central-store.c
+++ b/amtk/amtk-action-info-central-store.c
@@ -35,23 +35,18 @@
  *
  * Why both AmtkActionInfoStore and AmtkActionInfoCentralStore are needed?
  *
- * Advantages of AmtkActionInfoStore:
- * - amtk_action_info_store_new() takes an optional GtkApplication parameter. It
- *   doesn't rely on g_application_get_default() (calling
- *   g_application_get_default() in a library is not really a good practice I
- *   think. In theory an app can have several GApplication instances).
+ * Advantage of AmtkActionInfoStore:
  * - amtk_action_info_store_check_all_used()
  *
  * Advantages of AmtkActionInfoCentralStore:
  * - The central store checks if there are no duplicated action names
  *   (globally).
- * - [For the menu bar, easy to retrieve the tooltip to show it in the
- *   statusbar.] No longer relevant with amtk_menu_item_get_long_description().
+ * - Permits to write widget factory functions that don't require an
+ *   AmtkActionInfoStore parameter.
  *
- * If there was only one of the two classes, hacks would be needed to achieve
- * the above items. So by having the two classes, we have the best of both
- * worlds. We should not be afraid to create a lot of classes, and see things in
- * big.
+ * By having the two classes, we have the best of both worlds. We should not be
+ * afraid to create a lot of classes, and see things in big, even if it's a bit
+ * Java-ish.
  */
 
 struct _AmtkActionInfoCentralStorePrivate
diff --git a/amtk/amtk-action-info-store.c b/amtk/amtk-action-info-store.c
index 20189a2..873a368 100644
--- a/amtk/amtk-action-info-store.c
+++ b/amtk/amtk-action-info-store.c
@@ -20,7 +20,6 @@
 #include "amtk-action-info-store.h"
 #include "amtk-action-info.h"
 #include "amtk-action-info-central-store.h"
-#include "amtk-menu-item.h"
 
 /**
  * SECTION:amtk-action-info-store
@@ -28,17 +27,8 @@
  * @Title: AmtkActionInfoStore
  * @See_also: #AmtkActionInfo, #AmtkActionInfoCentralStore
  *
- * #AmtkActionInfoStore contains a set of #AmtkActionInfo's.
- *
- * #AmtkActionInfoStore is add-only, a #AmtkActionInfo cannot be removed. If
- * needed, the remove operation will be added in the future.
- *
- * A #GtkApplication can be associated so that when a widget is created,
- * gtk_application_set_accels_for_action() is called. See
- * amtk_action_info_store_create_menu_item() for more details. Note that this
- * happens on widget creation, not when adding an #AmtkActionInfo to the store,
- * so that the accelerator is bound to the application only if the
- * #AmtkActionInfo is actually used.
+ * #AmtkActionInfoStore contains a set of #AmtkActionInfo's. It is add-only, an
+ * #AmtkActionInfo cannot be removed.
  *
  * #AmtkActionInfoStore is designed so that libraries can provide their own
  * store, to share action information (with translations) and possibly the
@@ -52,104 +42,15 @@
 
 struct _AmtkActionInfoStorePrivate
 {
-       /* Weak ref, because usually GtkApplication owns (indirectly) a
-        * AmtkActionInfoStore.
-        */
-       GtkApplication *app;
-
        /* Key: owned gchar*: action name.
         * Value: owned AmtkActionInfo.
         */
        GHashTable *hash_table;
 };
 
-enum
-{
-       PROP_0,
-       PROP_APPLICATION,
-       N_PROPERTIES
-};
-
-static GParamSpec *properties[N_PROPERTIES];
-
 G_DEFINE_TYPE_WITH_PRIVATE (AmtkActionInfoStore, amtk_action_info_store, G_TYPE_OBJECT)
 
 static void
-set_application (AmtkActionInfoStore *store,
-                GtkApplication      *app)
-{
-       g_return_if_fail (app == NULL || GTK_IS_APPLICATION (app));
-
-       g_assert (store->priv->app == NULL);
-
-       if (app == NULL)
-       {
-               return;
-       }
-
-       store->priv->app = app;
-       g_object_add_weak_pointer (G_OBJECT (store->priv->app),
-                                  (gpointer *) &store->priv->app);
-
-       g_object_notify_by_pspec (G_OBJECT (store), properties[PROP_APPLICATION]);
-}
-
-static void
-amtk_action_info_store_get_property (GObject    *object,
-                                    guint       prop_id,
-                                    GValue     *value,
-                                    GParamSpec *pspec)
-{
-       AmtkActionInfoStore *store = AMTK_ACTION_INFO_STORE (object);
-
-       switch (prop_id)
-       {
-               case PROP_APPLICATION:
-                       g_value_set_object (value, amtk_action_info_store_get_application (store));
-                       break;
-
-               default:
-                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-                       break;
-       }
-}
-
-static void
-amtk_action_info_store_set_property (GObject      *object,
-                                    guint         prop_id,
-                                    const GValue *value,
-                                    GParamSpec   *pspec)
-{
-       AmtkActionInfoStore *store = AMTK_ACTION_INFO_STORE (object);
-
-       switch (prop_id)
-       {
-               case PROP_APPLICATION:
-                       set_application (store, g_value_get_object (value));
-                       break;
-
-               default:
-                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-                       break;
-       }
-}
-
-static void
-amtk_action_info_store_dispose (GObject *object)
-{
-       AmtkActionInfoStore *store = AMTK_ACTION_INFO_STORE (object);
-
-       if (store->priv->app != NULL)
-       {
-               g_object_remove_weak_pointer (G_OBJECT (store->priv->app),
-                                             (gpointer *) &store->priv->app);
-               store->priv->app = NULL;
-       }
-
-       G_OBJECT_CLASS (amtk_action_info_store_parent_class)->dispose (object);
-}
-
-static void
 amtk_action_info_store_finalize (GObject *object)
 {
        AmtkActionInfoStore *store = AMTK_ACTION_INFO_STORE (object);
@@ -164,29 +65,7 @@ amtk_action_info_store_class_init (AmtkActionInfoStoreClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-       object_class->get_property = amtk_action_info_store_get_property;
-       object_class->set_property = amtk_action_info_store_set_property;
-       object_class->dispose = amtk_action_info_store_dispose;
        object_class->finalize = amtk_action_info_store_finalize;
-
-       /**
-        * AmtkActionInfoStore:application:
-        *
-        * The associated #GtkApplication. #AmtkActionInfoStore has a weak
-        * reference to the #GtkApplication.
-        *
-        * Since: 2.0
-        */
-       properties[PROP_APPLICATION] =
-               g_param_spec_object ("application",
-                                    "GtkApplication",
-                                    "",
-                                    GTK_TYPE_APPLICATION,
-                                    G_PARAM_READWRITE |
-                                    G_PARAM_CONSTRUCT_ONLY |
-                                    G_PARAM_STATIC_STRINGS);
-
-       g_object_class_install_properties (object_class, N_PROPERTIES, properties);
 }
 
 static void
@@ -202,37 +81,14 @@ amtk_action_info_store_init (AmtkActionInfoStore *store)
 
 /**
  * amtk_action_info_store_new:
- * @application: (nullable): a #GtkApplication, or %NULL.
- *
- * Creates a new #AmtkActionInfoStore object. Associating a #GtkApplication is
- * optional.
  *
  * Returns: a new #AmtkActionInfoStore.
- * Since: 2.0
+ * Since: 3.0
  */
 AmtkActionInfoStore *
-amtk_action_info_store_new (GtkApplication *application)
+amtk_action_info_store_new (void)
 {
-       g_return_val_if_fail (application == NULL || GTK_IS_APPLICATION (application), NULL);
-
-       return g_object_new (AMTK_TYPE_ACTION_INFO_STORE,
-                            "application", application,
-                            NULL);
-}
-
-/**
- * amtk_action_info_store_get_application:
- * @store: an #AmtkActionInfoStore.
- *
- * Returns: (transfer none) (nullable): the associated #GtkApplication, or
- * %NULL.
- */
-GtkApplication *
-amtk_action_info_store_get_application (AmtkActionInfoStore *store)
-{
-       g_return_val_if_fail (AMTK_IS_ACTION_INFO_STORE (store), NULL);
-
-       return store->priv->app;
+       return g_object_new (AMTK_TYPE_ACTION_INFO_STORE, NULL);
 }
 
 /**
@@ -334,106 +190,6 @@ amtk_action_info_store_lookup (AmtkActionInfoStore *store,
        return g_hash_table_lookup (store->priv->hash_table, action_name);
 }
 
-/**
- * amtk_action_info_store_create_menu_item:
- * @store: an #AmtkActionInfoStore.
- * @action_name: an action name.
- *
- * Creates a new #GtkMenuItem for @action_name. The @store must contain an
- * #AmtkActionInfo for @action_name.
- *
- * gtk_actionable_set_action_name() is called on the menu item with
- * @action_name. The label is set with the #GtkMenuItem:use-underline property
- * enabled. The first accelerator is set to the #GtkAccelLabel of the menu item.
- * The icon is set. And the tooltip is set with
- * amtk_menu_item_set_long_description().
- *
- * If #AmtkActionInfoStore:application is non-%NULL, this function also calls
- * gtk_application_set_accels_for_action() with the accelerators returned by
- * amtk_action_info_get_accels() (this will erase previously set accelerators
- * for that action, if any).
- *
- * Returns: (transfer floating): a new #GtkMenuItem for @action_name.
- * Since: 2.0
- */
-GtkWidget *
-amtk_action_info_store_create_menu_item (AmtkActionInfoStore *store,
-                                        const gchar         *action_name)
-{
-       GtkMenuItem *menu_item;
-       AmtkActionInfo *action_info;
-       const gchar * const *accels;
-       const gchar *icon_name;
-       const gchar *tooltip;
-
-       g_return_val_if_fail (AMTK_IS_ACTION_INFO_STORE (store), NULL);
-       g_return_val_if_fail (action_name != NULL, NULL);
-
-       action_info = g_hash_table_lookup (store->priv->hash_table, action_name);
-
-       if (action_info == NULL)
-       {
-               g_warning ("%s(): action name '%s' not found.",
-                          G_STRFUNC,
-                          action_name);
-
-               return NULL;
-       }
-
-       menu_item = GTK_MENU_ITEM (gtk_menu_item_new ());
-
-       gtk_actionable_set_action_name (GTK_ACTIONABLE (menu_item), action_name);
-
-       gtk_menu_item_set_use_underline (menu_item, TRUE);
-       gtk_menu_item_set_label (menu_item, amtk_action_info_get_label (action_info));
-
-       /* Set accel before setting icon, because
-        * amtk_menu_item_set_icon_name() adds a GtkBox.
-        */
-       accels = amtk_action_info_get_accels (action_info);
-       if (accels != NULL && accels[0] != NULL)
-       {
-               guint accel_key;
-               GdkModifierType accel_mods;
-
-               gtk_accelerator_parse (accels[0], &accel_key, &accel_mods);
-
-               if (accel_key != 0 || accel_mods != 0)
-               {
-                       GtkWidget *child;
-
-                       child = gtk_bin_get_child (GTK_BIN (menu_item));
-
-                       gtk_accel_label_set_accel (GTK_ACCEL_LABEL (child),
-                                                  accel_key,
-                                                  accel_mods);
-               }
-       }
-
-       icon_name = amtk_action_info_get_icon_name (action_info);
-       if (icon_name != NULL)
-       {
-               amtk_menu_item_set_icon_name (menu_item, icon_name);
-       }
-
-       tooltip = amtk_action_info_get_tooltip (action_info);
-       if (tooltip != NULL)
-       {
-               amtk_menu_item_set_long_description (menu_item, tooltip);
-       }
-
-       if (store->priv->app != NULL)
-       {
-               gtk_application_set_accels_for_action (store->priv->app,
-                                                      action_name,
-                                                      accels);
-       }
-
-       _amtk_action_info_set_used (action_info);
-
-       return GTK_WIDGET (menu_item);
-}
-
 static void
 check_used_cb (gpointer key,
               gpointer value,
@@ -453,9 +209,10 @@ check_used_cb (gpointer key,
  * amtk_action_info_store_check_all_used:
  * @store: an #AmtkActionInfoStore.
  *
- * Checks that all #AmtkActionInfo's of @store have been used by
- * amtk_action_info_store_create_menu_item(). If not, a warning is printed and
- * might indicate dead code.
+ * Checks for each #AmtkActionInfo of @store that it has been used by a factory
+ * to create a #GtkWidget (typically a menu or toolbar item). If an
+ * #AmtkActionInfo has not been used, a warning is printed and might indicate
+ * dead code.
  *
  * You probably want to call this function on the application store. But it can
  * also be useful for a store provided by a library, to easily see which actions
diff --git a/amtk/amtk-action-info-store.h b/amtk/amtk-action-info-store.h
index e57dbad..fa4d97f 100644
--- a/amtk/amtk-action-info-store.h
+++ b/amtk/amtk-action-info-store.h
@@ -24,7 +24,7 @@
 #error "Only <amtk/amtk.h> can be included directly."
 #endif
 
-#include <gtk/gtk.h>
+#include <glib-object.h>
 #include <amtk/amtk-types.h>
 
 G_BEGIN_DECLS
@@ -53,11 +53,9 @@ struct _AmtkActionInfoStoreClass
        gpointer padding[12];
 };
 
-GType                  amtk_action_info_store_get_type                 (void) G_GNUC_CONST;
+GType                  amtk_action_info_store_get_type                 (void);
 
-AmtkActionInfoStore *  amtk_action_info_store_new                      (GtkApplication *application);
-
-GtkApplication *       amtk_action_info_store_get_application          (AmtkActionInfoStore *store);
+AmtkActionInfoStore *  amtk_action_info_store_new                      (void);
 
 void                   amtk_action_info_store_add                      (AmtkActionInfoStore *store,
                                                                         AmtkActionInfo      *info);
@@ -70,9 +68,6 @@ void                  amtk_action_info_store_add_entries              (AmtkActionInfoStore  
     *store,
 const AmtkActionInfo * amtk_action_info_store_lookup                   (AmtkActionInfoStore *store,
                                                                         const gchar         *action_name);
 
-GtkWidget *            amtk_action_info_store_create_menu_item         (AmtkActionInfoStore *store,
-                                                                        const gchar         *action_name);
-
 void                   amtk_action_info_store_check_all_used           (AmtkActionInfoStore *store);
 
 G_END_DECLS
diff --git a/docs/reference/tepl-3.0-sections.txt b/docs/reference/tepl-3.0-sections.txt
index b358a4c..f70b9c5 100644
--- a/docs/reference/tepl-3.0-sections.txt
+++ b/docs/reference/tepl-3.0-sections.txt
@@ -50,11 +50,9 @@ AMTK_TYPE_ACTION_INFO
 <FILE>amtk-action-info-store</FILE>
 AmtkActionInfoStore
 amtk_action_info_store_new
-amtk_action_info_store_get_application
 amtk_action_info_store_add
 amtk_action_info_store_add_entries
 amtk_action_info_store_lookup
-amtk_action_info_store_create_menu_item
 amtk_action_info_store_check_all_used
 <SUBSECTION Standard>
 AMTK_ACTION_INFO_STORE
diff --git a/tepl/tepl-application.c b/tepl/tepl-application.c
index 97fd888..51fd1ec 100644
--- a/tepl/tepl-application.c
+++ b/tepl/tepl-application.c
@@ -55,15 +55,6 @@ static GParamSpec *properties[N_PROPERTIES];
 G_DEFINE_TYPE_WITH_PRIVATE (TeplApplication, tepl_application, G_TYPE_OBJECT)
 
 static void
-init_app_action_info_store (TeplApplication *tepl_app)
-{
-       g_return_if_fail (tepl_app->priv->app_action_info_store == NULL);
-       g_assert (tepl_app->priv->gtk_app != NULL);
-
-       tepl_app->priv->app_action_info_store = amtk_action_info_store_new (tepl_app->priv->gtk_app);
-}
-
-static void
 init_tepl_action_info_store (TeplApplication *tepl_app)
 {
        const AmtkActionInfoEntry entries[] =
@@ -86,10 +77,8 @@ init_tepl_action_info_store (TeplApplication *tepl_app)
                  N_("Select all the text") },
        };
 
-       g_return_if_fail (tepl_app->priv->tepl_action_info_store == NULL);
-       g_assert (tepl_app->priv->gtk_app != NULL);
-
-       tepl_app->priv->tepl_action_info_store = amtk_action_info_store_new (tepl_app->priv->gtk_app);
+       g_assert (tepl_app->priv->tepl_action_info_store == NULL);
+       tepl_app->priv->tepl_action_info_store = amtk_action_info_store_new ();
 
        amtk_action_info_store_add_entries (tepl_app->priv->tepl_action_info_store,
                                            entries,
@@ -130,9 +119,6 @@ tepl_application_set_property (GObject      *object,
                case PROP_APPLICATION:
                        g_assert (tepl_app->priv->gtk_app == NULL);
                        tepl_app->priv->gtk_app = g_value_get_object (value);
-
-                       init_app_action_info_store (tepl_app);
-                       init_tepl_action_info_store (tepl_app);
                        break;
 
                default:
@@ -185,6 +171,9 @@ static void
 tepl_application_init (TeplApplication *tepl_app)
 {
        tepl_app->priv = tepl_application_get_instance_private (tepl_app);
+
+       tepl_app->priv->app_action_info_store = amtk_action_info_store_new ();
+       init_tepl_action_info_store (tepl_app);
 }
 
 /**
diff --git a/tests/test-menu.c b/tests/test-menu.c
index 583867c..e4d49c9 100644
--- a/tests/test-menu.c
+++ b/tests/test-menu.c
@@ -86,26 +86,17 @@ startup_cb (GApplication *g_app,
        add_action_entries (g_app);
 }
 
-static AmtkActionInfoStore *
-get_action_info_store (void)
-{
-       TeplApplication *app;
-
-       app = tepl_application_get_default ();
-
-       return tepl_application_get_app_action_info_store (app);
-}
-
 static GtkWidget *
 create_file_submenu (void)
 {
-       AmtkActionInfoStore *store;
        GtkMenuShell *file_submenu;
+       AmtkMenuFactory *factory;
 
-       store = get_action_info_store ();
        file_submenu = GTK_MENU_SHELL (gtk_menu_new ());
 
-       gtk_menu_shell_append (file_submenu, amtk_action_info_store_create_menu_item (store, "app.quit"));
+       factory = amtk_menu_factory_new_with_default_application ();
+       gtk_menu_shell_append (file_submenu, amtk_menu_factory_create_menu_item (factory, "app.quit"));
+       g_object_unref (factory);
 
        return GTK_WIDGET (file_submenu);
 }
@@ -113,13 +104,14 @@ create_file_submenu (void)
 static GtkWidget *
 create_help_submenu (void)
 {
-       AmtkActionInfoStore *store;
        GtkMenuShell *help_submenu;
+       AmtkMenuFactory *factory;
 
-       store = get_action_info_store ();
        help_submenu = GTK_MENU_SHELL (gtk_menu_new ());
 
-       gtk_menu_shell_append (help_submenu, amtk_action_info_store_create_menu_item (store, "app.about"));
+       factory = amtk_menu_factory_new_with_default_application ();
+       gtk_menu_shell_append (help_submenu, amtk_menu_factory_create_menu_item (factory, "app.about"));
+       g_object_unref (factory);
 
        return GTK_WIDGET (help_submenu);
 }
@@ -130,6 +122,7 @@ create_menu_bar (void)
        GtkWidget *file_menu_item;
        GtkWidget *help_menu_item;
        GtkMenuBar *menu_bar;
+       TeplApplication *app;
        AmtkActionInfoStore *store;
 
        file_menu_item = gtk_menu_item_new_with_mnemonic ("_File");
@@ -144,7 +137,8 @@ create_menu_bar (void)
        gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), file_menu_item);
        gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), help_menu_item);
 
-       store = get_action_info_store ();
+       app = tepl_application_get_default ();
+       store = tepl_application_get_app_action_info_store (app);
        amtk_action_info_store_check_all_used (store);
 
        return menu_bar;
diff --git a/testsuite/amtk/test-action-info-store.c b/testsuite/amtk/test-action-info-store.c
index 98ad5d7..1bade3a 100644
--- a/testsuite/amtk/test-action-info-store.c
+++ b/testsuite/amtk/test-action-info-store.c
@@ -38,7 +38,7 @@ test_add_entries (void)
                { "win.open", "document-open", "_Open", "<Control>o" },
        };
 
-       store = amtk_action_info_store_new (NULL);
+       store = amtk_action_info_store_new ();
 
        amtk_action_info_store_add_entries (store,
                                            entries,


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