utils: Add utility function for creating activate signal and shortcuts

The current way of adding activation shortcuts is cumbersome,
as it is a for-loop pasted in the middle of class initializers.
The utility function addresses this problem by simply allowing us
to declaratively add these shortcuts with minimal effort.

Furthermore, the implementation is also simpler, thanks to the
`gtk_widget_class_add_binding_signal()` wrapper function.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-calendar/-/merge_requests/747>
This commit is contained in:
Hari Rana
2026-05-04 10:42:02 +00:00
committed by Marge Bot
parent 16a67b7f60
commit 312b286e2b
2 changed files with 44 additions and 0 deletions
+41
View File
@@ -1449,3 +1449,44 @@ gcal_get_email_from_mailto_uri (const gchar *mailto_uri)
return address;
}
/**
* gcal_create_activate_signal_shortcuts:
* @widget_class: the class to add the bindings to
* @widget_type: the type this signal pertains to
*
* Adds activate signal and shortcuts to the desired widget during class instantiation.
*
* The primary use case is for custom widgets desiring to have activation functionality.
*
* Returns: the signal ID
*/
guint
gcal_create_activate_signal_and_shortcuts (GtkWidgetClass *widget_class,
GType widget_type)
{
const guint activate_keyvals[] = {
GDK_KEY_space,
GDK_KEY_KP_Space,
GDK_KEY_Return,
GDK_KEY_ISO_Enter,
GDK_KEY_KP_Enter,
};
guint signal_id =
g_signal_new ("activate",
widget_type,
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
0,
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE,
0);
gtk_widget_class_set_activate_signal (widget_class, signal_id);
for (size_t i = 0; i < G_N_ELEMENTS (activate_keyvals); i++)
gtk_widget_class_add_binding_signal (widget_class, activate_keyvals[i], 0, "activate", NULL);
return signal_id;
}
+3
View File
@@ -141,4 +141,7 @@ SoupSession * gcal_create_soup_session (void);
GListModel * gcal_create_writable_calendars_model (GcalManager *manager);
const gchar * gcal_get_email_from_mailto_uri (const gchar *mailto_uri);
guint gcal_create_activate_signal_and_shortcuts (GtkWidgetClass *widget_class,
GType widget_type);
#endif /* __GCAL_UTILS_H__ */