From 9428807c57e5f49ccc9d99ec82aed9ae51fd1293 Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Thu, 16 Aug 2018 05:43:37 +0200 Subject: [PATCH] label: Implement mnemonics using shortcuts --- gtk/gtklabel.c | 62 ++++++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/gtk/gtklabel.c b/gtk/gtklabel.c index cfdb4ffe92..a8a242e481 100644 --- a/gtk/gtklabel.c +++ b/gtk/gtklabel.c @@ -41,6 +41,9 @@ #include "gtknotebook.h" #include "gtkpango.h" #include "gtkprivate.h" +#include "gtkshortcut.h" +#include "gtkshortcutcontroller.h" +#include "gtkshortcuttrigger.h" #include "gtkshow.h" #include "gtksnapshot.h" #include "gtkstylecontextprivate.h" @@ -275,7 +278,7 @@ struct _GtkLabelPrivate { GtkLabelSelectionInfo *select_info; GtkWidget *mnemonic_widget; - GtkWindow *mnemonic_window; + GtkEventController *mnemonic_controller; PangoAttrList *attrs; PangoAttrList *markup_attrs; @@ -413,7 +416,6 @@ static GParamSpec *label_props[NUM_PROPERTIES] = { NULL, }; static guint signals[LAST_SIGNAL] = { 0 }; static GQuark quark_shortcuts_connected; -static GQuark quark_mnemonic_menu; static GQuark quark_mnemonics_visible_connected; static GQuark quark_gtk_signal; static GQuark quark_link; @@ -498,9 +500,7 @@ static void gtk_label_update_active_link (GtkWidget *widget, static gboolean gtk_label_mnemonic_activate (GtkWidget *widget, gboolean group_cycling); -static void gtk_label_setup_mnemonic (GtkLabel *label, - GtkWidget *toplevel, - guint last_key); +static void gtk_label_setup_mnemonic (GtkLabel *label); static void gtk_label_buildable_interface_init (GtkBuildableIface *iface); static gboolean gtk_label_buildable_custom_tag_start (GtkBuildable *buildable, @@ -1132,7 +1132,6 @@ gtk_label_class_init (GtkLabelClass *class) gtk_widget_class_set_css_name (widget_class, I_("label")); quark_shortcuts_connected = g_quark_from_static_string ("gtk-label-shortcuts-connected"); - quark_mnemonic_menu = g_quark_from_static_string ("gtk-mnemonic-menu"); quark_mnemonics_visible_connected = g_quark_from_static_string ("gtk-label-mnemonics-visible-connected"); quark_gtk_signal = g_quark_from_static_string ("gtk-signal"); quark_link = g_quark_from_static_string ("link"); @@ -1335,7 +1334,6 @@ gtk_label_init (GtkLabel *label) priv->attrs = NULL; priv->mnemonic_widget = NULL; - priv->mnemonic_window = NULL; priv->mnemonics_visible = FALSE; } @@ -1803,30 +1801,42 @@ gtk_label_mnemonic_activate (GtkWidget *widget, } static void -gtk_label_setup_mnemonic (GtkLabel *label, - GtkWidget *toplevel, - guint last_key) +gtk_label_setup_mnemonic (GtkLabel *label) { GtkLabelPrivate *priv = gtk_label_get_instance_private (label); GtkWidget *widget = GTK_WIDGET (label); + GtkShortcut *shortcut; - if (last_key != GDK_KEY_VoidSymbol) + if (priv->mnemonic_keyval == GDK_KEY_VoidSymbol) { - if (priv->mnemonic_window) - { - gtk_window_remove_mnemonic (priv->mnemonic_window, - last_key, - widget); - priv->mnemonic_window = NULL; - } + if (priv->mnemonic_controller) + { + gtk_widget_remove_controller (widget, priv->mnemonic_controller); + priv->mnemonic_controller = NULL; + } + return; } - if (priv->mnemonic_keyval == GDK_KEY_VoidSymbol) - goto done; + if (priv->mnemonic_controller == NULL) + { + priv->mnemonic_controller = gtk_shortcut_controller_new (); + gtk_event_controller_set_propagation_phase (priv->mnemonic_controller, GTK_PHASE_CAPTURE); + gtk_shortcut_controller_set_scope (GTK_SHORTCUT_CONTROLLER (priv->mnemonic_controller), GTK_SHORTCUT_SCOPE_MANAGED); + shortcut = gtk_shortcut_new (); + gtk_shortcut_set_trigger (shortcut, gtk_mnemonic_trigger_new (priv->mnemonic_keyval)); + gtk_shortcut_set_mnemonic_activate (shortcut, TRUE); + gtk_shortcut_controller_add_shortcut (GTK_SHORTCUT_CONTROLLER (priv->mnemonic_controller), shortcut); + gtk_widget_add_controller (GTK_WIDGET (label), priv->mnemonic_controller); + g_object_unref (shortcut); + } + else + { + shortcut = g_list_model_get_item (G_LIST_MODEL (priv->mnemonic_controller), 0); + gtk_shortcut_set_trigger (shortcut, gtk_mnemonic_trigger_new (priv->mnemonic_keyval)); + g_object_unref (shortcut); + } connect_mnemonics_visible_notify (GTK_LABEL (widget)); - - done:; } static void @@ -1869,13 +1879,12 @@ static void gtk_label_root (GtkWidget *widget) { GtkLabel *label = GTK_LABEL (widget); - GtkLabelPrivate *priv = gtk_label_get_instance_private (label); GtkSettings *settings; gboolean shortcuts_connected; GTK_WIDGET_CLASS (gtk_label_parent_class)->root (widget); - gtk_label_setup_mnemonic (label, GTK_WIDGET (gtk_widget_get_root (widget)), priv->mnemonic_keyval); + gtk_label_setup_mnemonic (label); /* The PangoContext is replaced when the display changes, so clear the layouts */ gtk_label_clear_layout (GTK_LABEL (widget)); @@ -1902,9 +1911,8 @@ static void gtk_label_unroot (GtkWidget *widget) { GtkLabel *label = GTK_LABEL (widget); - GtkLabelPrivate *priv = gtk_label_get_instance_private (label); - gtk_label_setup_mnemonic (label, NULL, priv->mnemonic_keyval); + gtk_label_setup_mnemonic (label); GTK_WIDGET_CLASS (gtk_label_parent_class)->unroot (widget); } @@ -2152,7 +2160,7 @@ gtk_label_recalculate (GtkLabel *label) if (keyval != priv->mnemonic_keyval) { - gtk_label_setup_mnemonic (label, GTK_WIDGET (gtk_widget_get_root (GTK_WIDGET (label))), keyval); + gtk_label_setup_mnemonic (label); g_object_notify_by_pspec (G_OBJECT (label), label_props[PROP_MNEMONIC_KEYVAL]); }