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.
This commit is contained in:
Benjamin Otte 2018-08-19 07:12:00 +02:00 committed by Matthias Clasen
parent 538a1a0461
commit d14807b93d
3 changed files with 64 additions and 6 deletions

View File

@ -6102,6 +6102,7 @@ GtkShortcutManagerInterface
<TITLE>GtkShortcutController</TITLE> <TITLE>GtkShortcutController</TITLE>
GtkShortcutController GtkShortcutController
gtk_shortcut_controller_new gtk_shortcut_controller_new
gtk_shortcut_controller_new_with_model
GtkShortcutScope GtkShortcutScope
GtkShortcutManager GtkShortcutManager
GtkShortcutManagerInterface GtkShortcutManagerInterface

View File

@ -52,6 +52,7 @@ struct _GtkShortcutController
GtkShortcutScope scope; GtkShortcutScope scope;
GdkModifierType mnemonics_modifiers; GdkModifierType mnemonics_modifiers;
guint custom_shortcuts : 1;
guint run_class : 1; guint run_class : 1;
guint run_managed : 1; guint run_managed : 1;
}; };
@ -64,6 +65,7 @@ struct _GtkShortcutControllerClass
enum { enum {
PROP_0, PROP_0,
PROP_MNEMONICS_MODIFIERS, PROP_MNEMONICS_MODIFIERS,
PROP_MODEL,
PROP_SCOPE, PROP_SCOPE,
N_PROPS 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)); gtk_shortcut_controller_set_mnemonics_modifiers (self, g_value_get_flags (value));
break; 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: case PROP_SCOPE:
gtk_shortcut_controller_set_scope (self, g_value_get_enum (value)); gtk_shortcut_controller_set_scope (self, g_value_get_enum (value));
break; break;
@ -168,7 +193,8 @@ gtk_shortcut_controller_dispose (GObject *object)
{ {
GtkShortcutController *self = GTK_SHORTCUT_CONTROLLER (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); G_OBJECT_CLASS (gtk_shortcut_controller_parent_class)->dispose (object);
} }
@ -327,6 +353,18 @@ gtk_shortcut_controller_class_init (GtkShortcutControllerClass *klass)
GDK_MOD1_MASK, GDK_MOD1_MASK,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS); 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: * GtkShortcutController:scope:
* *
@ -347,9 +385,6 @@ static void
gtk_shortcut_controller_init (GtkShortcutController *self) gtk_shortcut_controller_init (GtkShortcutController *self)
{ {
self->mnemonics_modifiers = GDK_MOD1_MASK; 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 void
@ -449,6 +484,17 @@ gtk_shortcut_controller_new (void)
NULL); 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 void
gtk_shortcut_controller_set_run_class (GtkShortcutController *controller, gtk_shortcut_controller_set_run_class (GtkShortcutController *controller,
gboolean run_class) gboolean run_class)
@ -470,6 +516,9 @@ gtk_shortcut_controller_set_run_managed (GtkShortcutController *controller,
* *
* Adds @shortcut to the list of shortcuts handled by @self. * 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 * The shortcut is added to the list so that it is triggered before
* all existing shortcuts. * all existing shortcuts.
* *
@ -482,6 +531,9 @@ gtk_shortcut_controller_add_shortcut (GtkShortcutController *self,
g_return_if_fail (GTK_IS_SHORTCUT_CONTROLLER (self)); g_return_if_fail (GTK_IS_SHORTCUT_CONTROLLER (self));
g_return_if_fail (GTK_IS_SHORTCUT (shortcut)); g_return_if_fail (GTK_IS_SHORTCUT (shortcut));
if (!self->custom_shortcuts)
return;
g_list_store_append (G_LIST_STORE (self->shortcuts), shortcut); g_list_store_append (G_LIST_STORE (self->shortcuts), shortcut);
} }
@ -492,8 +544,8 @@ gtk_shortcut_controller_add_shortcut (GtkShortcutController *self,
* *
* Removes @shortcut from the list of shortcuts handled by @self. * Removes @shortcut from the list of shortcuts handled by @self.
* *
* If @shortcut had not been added to @controller, this function does * If @shortcut had not been added to @controller or this controller
* nothing. * uses an external shortcut list, this function does nothing.
**/ **/
void void
gtk_shortcut_controller_remove_shortcut (GtkShortcutController *self, gtk_shortcut_controller_remove_shortcut (GtkShortcutController *self,
@ -504,6 +556,9 @@ gtk_shortcut_controller_remove_shortcut (GtkShortcutController *self,
g_return_if_fail (GTK_IS_SHORTCUT_CONTROLLER (self)); g_return_if_fail (GTK_IS_SHORTCUT_CONTROLLER (self));
g_return_if_fail (GTK_IS_SHORTCUT (shortcut)); 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++) for (i = 0; i < g_list_model_get_n_items (self->shortcuts); i++)
{ {
GtkShortcut *item = g_list_model_get_item (self->shortcuts, i); GtkShortcut *item = g_list_model_get_item (self->shortcuts, i);

View File

@ -44,6 +44,8 @@ GType gtk_shortcut_controller_get_type (void) G
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
GtkEventController * gtk_shortcut_controller_new (void); GtkEventController * gtk_shortcut_controller_new (void);
GDK_AVAILABLE_IN_ALL
GtkEventController * gtk_shortcut_controller_new_for_model (GListModel *list);
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
void gtk_shortcut_controller_set_mnemonics_modifiers (GtkShortcutController *self, void gtk_shortcut_controller_set_mnemonics_modifiers (GtkShortcutController *self,