From 312b286e2b198812866148233dc6a304c3351540 Mon Sep 17 00:00:00 2001 From: Hari Rana Date: Sat, 11 Apr 2026 23:58:33 -0400 Subject: [PATCH] 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: --- src/utils/gcal-utils.c | 41 +++++++++++++++++++++++++++++++++++++++++ src/utils/gcal-utils.h | 3 +++ 2 files changed, 44 insertions(+) diff --git a/src/utils/gcal-utils.c b/src/utils/gcal-utils.c index 2456a079..f2124fb1 100644 --- a/src/utils/gcal-utils.c +++ b/src/utils/gcal-utils.c @@ -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; +} diff --git a/src/utils/gcal-utils.h b/src/utils/gcal-utils.h index 7f66aa8a..41243eba 100644 --- a/src/utils/gcal-utils.h +++ b/src/utils/gcal-utils.h @@ -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__ */