[libdazzle] shortcuts: allow overriding the accel for a tooltip



commit 943fdf821788ea6848396a43d9ee2f467e0b51cc
Author: Christian Hergert <chergert redhat com>
Date:   Tue Oct 2 21:44:54 2018 -0700

    shortcuts: allow overriding the accel for a tooltip

 src/shortcuts/dzl-shortcut-tooltip.c | 110 ++++++++++++++++++++++++++++-------
 src/shortcuts/dzl-shortcut-tooltip.h |   5 ++
 tests/test-shortcut-tooltip.c        |   1 +
 3 files changed, 96 insertions(+), 20 deletions(-)
---
diff --git a/src/shortcuts/dzl-shortcut-tooltip.c b/src/shortcuts/dzl-shortcut-tooltip.c
index 96df3d4..cb10d13 100644
--- a/src/shortcuts/dzl-shortcut-tooltip.c
+++ b/src/shortcuts/dzl-shortcut-tooltip.c
@@ -60,6 +60,11 @@ struct _DzlShortcutTooltip
    */
   gchar *title;
 
+  /* If no command-id is set, the accelerator can be specified
+   * directly to avoid looking up by command-id.
+   */
+  gchar *accel;
+
   /* The widget that we are watching the ::query-tooltip for. */
   GtkWidget *widget;
 
@@ -74,6 +79,7 @@ G_DEFINE_TYPE (DzlShortcutTooltip, dzl_shortcut_tooltip, G_TYPE_OBJECT)
 
 enum {
   PROP_0,
+  PROP_ACCEL,
   PROP_COMMAND_ID,
   PROP_TITLE,
   PROP_WIDGET,
@@ -94,6 +100,8 @@ dzl_shortcut_tooltip_query_cb (DzlShortcutTooltip *self,
   const DzlShortcutChord *chord;
   DzlShortcutManager *manager;
   DzlShortcutTheme *theme;
+  DzlShortcutSimpleLabel *label;
+  g_autofree gchar *accel = NULL;
   const gchar *title = NULL;
   const gchar *subtitle = NULL;
 
@@ -108,31 +116,32 @@ dzl_shortcut_tooltip_query_cb (DzlShortcutTooltip *self,
       !(theme = dzl_shortcut_manager_get_theme (manager)))
     return FALSE;
 
-  /* If we find a matching chord for the command-id, display it to the user */
-  if ((chord = dzl_shortcut_theme_get_chord_for_command (theme, self->command_id)) &&
-      (self->title != NULL ||
-       _dzl_shortcut_manager_get_command_info (manager, self->command_id, &title, &subtitle)))
-    {
-      g_autofree gchar *accel = dzl_shortcut_chord_to_string (chord);
-      DzlShortcutSimpleLabel *label;
-
-      label = DZL_SHORTCUT_SIMPLE_LABEL (dzl_shortcut_simple_label_new ());
-      dzl_shortcut_simple_label_set_command (label, self->command_id);
-      dzl_shortcut_simple_label_set_accel (label, accel);
+  if (!(title = self->title))
+    _dzl_shortcut_manager_get_command_info (manager, self->command_id, &title, &subtitle);
+  if (title == NULL)
+    return FALSE;
 
-      if (self->title != NULL)
-        dzl_shortcut_simple_label_set_title (label, self->title);
-      else
-        dzl_shortcut_simple_label_set_title (label, title);
+  if (!(accel = g_strdup (self->accel)))
+    {
+      if ((chord = dzl_shortcut_theme_get_chord_for_command (theme, self->command_id)))
+        accel = dzl_shortcut_chord_to_string (chord);
+      if (accel == NULL)
+        return FALSE;
+    }
 
-      gtk_widget_show (GTK_WIDGET (label));
+  g_assert (accel != NULL);
+  g_assert (title != NULL);
 
-      gtk_tooltip_set_custom (tooltip, GTK_WIDGET (label));
+  label = DZL_SHORTCUT_SIMPLE_LABEL (dzl_shortcut_simple_label_new ());
+  if (self->command_id != NULL)
+    dzl_shortcut_simple_label_set_command (label, self->command_id);
+  dzl_shortcut_simple_label_set_accel (label, accel);
+  dzl_shortcut_simple_label_set_title (label, title);
+  gtk_widget_show (GTK_WIDGET (label));
 
-      return TRUE;
-    }
+  gtk_tooltip_set_custom (tooltip, GTK_WIDGET (label));
 
-  return FALSE;
+  return TRUE;
 }
 
 /**
@@ -163,6 +172,9 @@ dzl_shortcut_tooltip_finalize (GObject *object)
 
   self->widget = NULL;
 
+  g_clear_pointer (&self->title, g_free);
+  g_clear_pointer (&self->accel, g_free);
+
   G_OBJECT_CLASS (dzl_shortcut_tooltip_parent_class)->finalize (object);
 }
 
@@ -176,6 +188,10 @@ dzl_shortcut_tooltip_get_property (GObject    *object,
 
   switch (prop_id)
     {
+    case PROP_ACCEL:
+      g_value_set_string (value, dzl_shortcut_tooltip_get_accel (self));
+      break;
+
     case PROP_WIDGET:
       g_value_set_object (value, dzl_shortcut_tooltip_get_widget (self));
       break;
@@ -203,6 +219,10 @@ dzl_shortcut_tooltip_set_property (GObject      *object,
 
   switch (prop_id)
     {
+    case PROP_ACCEL:
+      dzl_shortcut_tooltip_set_accel (self, g_value_get_object (value));
+      break;
+
     case PROP_WIDGET:
       dzl_shortcut_tooltip_set_widget (self, g_value_get_object (value));
       break;
@@ -229,6 +249,13 @@ dzl_shortcut_tooltip_class_init (DzlShortcutTooltipClass *klass)
   object_class->get_property = dzl_shortcut_tooltip_get_property;
   object_class->set_property = dzl_shortcut_tooltip_set_property;
 
+  properties [PROP_ACCEL] =
+    g_param_spec_string ("accel",
+                         "Accel",
+                         "The accel for the label, if overriding the discovered accel",
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
   properties [PROP_COMMAND_ID] =
     g_param_spec_string ("command-id",
                          "Command Id",
@@ -417,3 +444,46 @@ dzl_shortcut_tooltip_set_widget (DzlShortcutTooltip *self,
       g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_WIDGET]);
     }
 }
+
+/**
+ * dzl_shortcut_tooltip_get_accel:
+ * @self: a #DzlShortcutTooltip
+ *
+ * Gets the #DzlShortcutTooltip:accel property, which can be used to override
+ * the commands accel.
+ *
+ * Returns: (nullable): an override accel, or %NULL
+ *
+ * Since: 3.320
+ */
+const gchar *
+dzl_shortcut_tooltip_get_accel (DzlShortcutTooltip *self)
+{
+  g_return_val_if_fail (DZL_IS_SHORTCUT_TOOLTIP (self), NULL);
+
+  return self->accel;
+}
+
+/**
+ * dzl_shortcut_tooltip_set_accel:
+ * @self: #DzlShortcutTooltip
+ * @accel: (nullable): Sets the accelerator to use, or %NULL to unset
+ *   and use the default
+ *
+ * Allows overriding the accel that is used.
+ *
+ * Since: 3.32
+ */
+void
+dzl_shortcut_tooltip_set_accel (DzlShortcutTooltip *self,
+                                const gchar        *accel)
+{
+  g_return_if_fail (DZL_IS_SHORTCUT_TOOLTIP (self));
+
+  if (!dzl_str_equal0 (self->accel, accel))
+    {
+      g_free (self->accel);
+      self->accel = g_strdup (accel);
+      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_ACCEL]);
+    }
+}
diff --git a/src/shortcuts/dzl-shortcut-tooltip.h b/src/shortcuts/dzl-shortcut-tooltip.h
index 0ca5e2c..ec44770 100644
--- a/src/shortcuts/dzl-shortcut-tooltip.h
+++ b/src/shortcuts/dzl-shortcut-tooltip.h
@@ -34,6 +34,11 @@ G_DECLARE_FINAL_TYPE (DzlShortcutTooltip, dzl_shortcut_tooltip, DZL, SHORTCUT_TO
 DZL_AVAILABLE_IN_3_32
 DzlShortcutTooltip *dzl_shortcut_tooltip_new            (void);
 DZL_AVAILABLE_IN_3_32
+const gchar        *dzl_shortcut_tooltip_get_accel      (DzlShortcutTooltip *self);
+DZL_AVAILABLE_IN_3_32
+void                dzl_shortcut_tooltip_set_accel      (DzlShortcutTooltip *self,
+                                                         const gchar        *accel);
+DZL_AVAILABLE_IN_3_32
 GtkWidget          *dzl_shortcut_tooltip_get_widget     (DzlShortcutTooltip *self);
 DZL_AVAILABLE_IN_3_32
 void                dzl_shortcut_tooltip_set_widget     (DzlShortcutTooltip *self,
diff --git a/tests/test-shortcut-tooltip.c b/tests/test-shortcut-tooltip.c
index c507123..eb0fbca 100644
--- a/tests/test-shortcut-tooltip.c
+++ b/tests/test-shortcut-tooltip.c
@@ -40,6 +40,7 @@ button_clicked_cb (GtkButton          *button,
 
   count++;
 
+  dzl_shortcut_tooltip_set_accel (tooltip, count % 2 ? "<Shift>F11" : "F11");
   dzl_shortcut_tooltip_set_title (tooltip, count % 2 ? "Unfullscreen window" : "Fullscreen window");
 }
 


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