[gtk/wip/ebassi/shortcut: 178/203] shortcutcontroller: Add gtk_shortcut_controller_new_for_model()



commit 659494faaebaaf8906d712ece8e80ad70c281933
Author: Benjamin Otte <otte redhat com>
Date:   Sun Aug 19 07:12:00 2018 +0200

    shortcutcontroller: Add gtk_shortcut_controller_new_for_model()
    
    This is mainly for internal use, but I can't see a reason to not have it
    public for people who want to maintain their own lists.
    
    I'm sure gnome-builder will never ever find a way to misuse it.

 docs/reference/gtk/gtk4-sections.txt |  1 +
 gtk/gtkshortcutcontroller.c          | 67 ++++++++++++++++++++++++++++++++----
 gtk/gtkshortcutcontroller.h          |  2 ++
 3 files changed, 64 insertions(+), 6 deletions(-)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index 87f4e924a5..d3682f41a8 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -6197,6 +6197,7 @@ gtk_shortcut_get_type
 <TITLE>GtkShortcutController</TITLE>
 GtkShortcutController
 gtk_shortcut_controller_new
+gtk_shortcut_controller_new_with_model
 GtkShortcutScope
 GtkShortcutManager
 GtkShortcutManagerInterface
diff --git a/gtk/gtkshortcutcontroller.c b/gtk/gtkshortcutcontroller.c
index 099802d456..a995cc0d24 100644
--- a/gtk/gtkshortcutcontroller.c
+++ b/gtk/gtkshortcutcontroller.c
@@ -52,6 +52,7 @@ struct _GtkShortcutController
   GtkShortcutScope scope;
   GdkModifierType mnemonics_modifiers;
 
+  guint custom_shortcuts : 1;
   guint run_class : 1;
   guint run_managed : 1;
 };
@@ -64,6 +65,7 @@ struct _GtkShortcutControllerClass
 enum {
   PROP_0,
   PROP_MNEMONICS_MODIFIERS,
+  PROP_MODEL,
   PROP_SCOPE,
 
   N_PROPS
@@ -131,6 +133,29 @@ gtk_shortcut_controller_set_property (GObject      *object,
       gtk_shortcut_controller_set_mnemonics_modifiers (self, g_value_get_flags (value));
       break;
 
+    case PROP_MODEL:
+      {
+        GListModel *model = g_value_get_object (value);
+        if (model && g_list_model_get_item_type (model) != GTK_TYPE_SHORTCUT)
+          {
+            g_warning ("Setting a model with type '%s' on a shortcut controller that requires 'GtkShortcut'",
+                       g_type_name (g_list_model_get_item_type (model)));
+            model = NULL;
+          }
+        if (model == NULL)
+          {
+            self->shortcuts = G_LIST_MODEL (g_list_store_new (GTK_TYPE_SHORTCUT));
+            self->custom_shortcuts = TRUE;
+          }
+        else
+          {
+            self->shortcuts = g_object_ref (model);
+            self->custom_shortcuts = FALSE;
+          }
+        g_signal_connect_swapped (self->shortcuts, "items-changed", G_CALLBACK (g_list_model_items_changed), 
self);
+      }
+      break;
+
     case PROP_SCOPE:
       gtk_shortcut_controller_set_scope (self, g_value_get_enum (value));
       break;
@@ -168,7 +193,8 @@ gtk_shortcut_controller_dispose (GObject *object)
 {
   GtkShortcutController *self = GTK_SHORTCUT_CONTROLLER (object);
 
-  g_list_store_remove_all (G_LIST_STORE (self->shortcuts));
+  if (self->custom_shortcuts)
+    g_list_store_remove_all (G_LIST_STORE (self->shortcuts));
 
   G_OBJECT_CLASS (gtk_shortcut_controller_parent_class)->dispose (object);
 }
@@ -323,6 +349,18 @@ gtk_shortcut_controller_class_init (GtkShortcutControllerClass *klass)
                           GDK_MOD1_MASK,
                           G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
 
+  /**
+   * GtkShortcutController:model:
+   *
+   * A list model to take shortcuts from
+   */
+  properties[PROP_MODEL] =
+      g_param_spec_object ("model",
+                           P_("Model"),
+                           P_("A list model to take shortcuts from"),
+                           G_TYPE_LIST_MODEL,
+                           G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_EXPLICIT_NOTIFY | 
G_PARAM_STATIC_STRINGS);
+
   /**
    * GtkShortcutController:scope:
    *
@@ -343,9 +381,6 @@ static void
 gtk_shortcut_controller_init (GtkShortcutController *self)
 {
   self->mnemonics_modifiers = GDK_MOD1_MASK;
-
-  self->shortcuts = G_LIST_MODEL (g_list_store_new (GTK_TYPE_SHORTCUT));
-  g_signal_connect_swapped (self->shortcuts, "items-changed", G_CALLBACK (g_list_model_items_changed), self);
 }
 
 void
@@ -445,6 +480,17 @@ gtk_shortcut_controller_new (void)
                        NULL);
 }
 
+GtkEventController *
+gtk_shortcut_controller_new_for_model (GListModel *model)
+{
+  g_return_val_if_fail (G_IS_LIST_MODEL (model), NULL);
+  g_return_val_if_fail (g_list_model_get_item_type (model) == GTK_TYPE_SHORTCUT, NULL);
+
+  return g_object_new (GTK_TYPE_SHORTCUT_CONTROLLER,
+                       "model", model,
+                       NULL);
+}
+
 void
 gtk_shortcut_controller_set_run_class (GtkShortcutController  *controller,
                                        gboolean                run_class)
@@ -466,6 +512,9 @@ gtk_shortcut_controller_set_run_managed (GtkShortcutController  *controller,
  *
  * Adds @shortcut to the list of shortcuts handled by @self.
  *
+ * If this controller uses an external shortcut list, this function does
+ * nothing.
+ *
  * The shortcut is added to the list so that it is triggered before
  * all existing shortcuts.
  *
@@ -478,6 +527,9 @@ gtk_shortcut_controller_add_shortcut (GtkShortcutController *self,
   g_return_if_fail (GTK_IS_SHORTCUT_CONTROLLER (self));
   g_return_if_fail (GTK_IS_SHORTCUT (shortcut));
 
+  if (!self->custom_shortcuts)
+    return;
+
   g_list_store_append (G_LIST_STORE (self->shortcuts), shortcut);
 }
 
@@ -488,8 +540,8 @@ gtk_shortcut_controller_add_shortcut (GtkShortcutController *self,
  *
  * Removes @shortcut from the list of shortcuts handled by @self.
  *
- * If @shortcut had not been added to @controller, this function does
- * nothing.
+ * If @shortcut had not been added to @controller or this controller
+ * uses an external shortcut list, this function does nothing.
  **/
 void
 gtk_shortcut_controller_remove_shortcut (GtkShortcutController  *self,
@@ -500,6 +552,9 @@ gtk_shortcut_controller_remove_shortcut (GtkShortcutController  *self,
   g_return_if_fail (GTK_IS_SHORTCUT_CONTROLLER (self));
   g_return_if_fail (GTK_IS_SHORTCUT (shortcut));
 
+  if (!self->custom_shortcuts)
+    return;
+
   for (i = 0; i < g_list_model_get_n_items (self->shortcuts); i++)
     {
       GtkShortcut *item = g_list_model_get_item (self->shortcuts, i);
diff --git a/gtk/gtkshortcutcontroller.h b/gtk/gtkshortcutcontroller.h
index 28d3362756..6f311f142b 100644
--- a/gtk/gtkshortcutcontroller.h
+++ b/gtk/gtkshortcutcontroller.h
@@ -44,6 +44,8 @@ GType                   gtk_shortcut_controller_get_type                (void) G
 
 GDK_AVAILABLE_IN_ALL
 GtkEventController *    gtk_shortcut_controller_new                     (void);
+GDK_AVAILABLE_IN_ALL
+GtkEventController *    gtk_shortcut_controller_new_for_model           (GListModel             *list);
 
 GDK_AVAILABLE_IN_ALL
 void                    gtk_shortcut_controller_set_mnemonics_modifiers (GtkShortcutController  *self,


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