[libadwaita/wip/exalm/tooltips: 2/4] tab-view: Add AdwTabPage:indicator-tooltip




commit 9769766776846ecc001ea6ce95fe17a691006e79
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Sun Jul 31 06:16:15 2022 +0400

    tab-view: Add AdwTabPage:indicator-tooltip
    
    Since we have a tooltip on the close button now, only seems natural.

 demo/pages/tab-view/adw-tab-view-demo-window.c | 26 ++++++++-
 src/adw-tab-view.c                             | 78 ++++++++++++++++++++++++++
 src/adw-tab-view.h                             |  6 ++
 src/adw-tab.ui                                 |  5 ++
 tests/test-tab-view.c                          | 32 +++++++++++
 5 files changed, 146 insertions(+), 1 deletion(-)
---
diff --git a/demo/pages/tab-view/adw-tab-view-demo-window.c b/demo/pages/tab-view/adw-tab-view-demo-window.c
index 15078c24..adc4eef2 100644
--- a/demo/pages/tab-view/adw-tab-view-demo-window.c
+++ b/demo/pages/tab-view/adw-tab-view-demo-window.c
@@ -251,6 +251,20 @@ get_indicator_icon (AdwTabPage *page)
     return g_themed_icon_new ("tab-audio-playing-symbolic");
 }
 
+static char *
+get_indicator_tooltip (AdwTabPage *page)
+{
+  gboolean muted;
+
+  muted = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (page),
+                                              "adw-tab-view-demo-muted"));
+
+  if (muted)
+    return g_strdup (_("Unmute Tab"));
+  else
+    return g_strdup (_("Mute Tab"));
+}
+
 static void
 tab_change_indicator (GSimpleAction *action,
                       GVariant      *parameter,
@@ -259,13 +273,18 @@ tab_change_indicator (GSimpleAction *action,
   AdwTabViewDemoWindow *self = ADW_TAB_VIEW_DEMO_WINDOW (user_data);
   gboolean indicator = g_variant_get_boolean (parameter);
   GIcon *icon = NULL;
+  char *tooltip = NULL;
 
-  if (indicator)
+  if (indicator) {
     icon = get_indicator_icon (get_current_page (self));
+    tooltip = get_indicator_tooltip (get_current_page (self));
+  }
 
   adw_tab_page_set_indicator_icon (get_current_page (self), icon);
+  adw_tab_page_set_indicator_tooltip (get_current_page (self), tooltip);
   g_simple_action_set_state (action, g_variant_new_boolean (indicator));
 
+  g_clear_pointer (&tooltip, g_free);
   g_clear_object (&icon);
 }
 
@@ -317,6 +336,7 @@ tab_duplicate (GSimpleAction *action,
                    adw_tab_page_get_icon (parent));
 
   adw_tab_page_set_indicator_icon (page, adw_tab_page_get_indicator_icon (parent));
+  adw_tab_page_set_indicator_tooltip (page, adw_tab_page_get_indicator_tooltip (parent));
   adw_tab_page_set_loading (page, adw_tab_page_get_loading (parent));
   adw_tab_page_set_needs_attention (page, adw_tab_page_get_needs_attention (parent));
 
@@ -445,6 +465,7 @@ indicator_activated_cb (AdwTabViewDemoWindow *self,
                         AdwTabPage           *page)
 {
   GIcon *icon;
+  char *tooltip;
   gboolean muted;
 
   muted = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (page),
@@ -455,10 +476,13 @@ indicator_activated_cb (AdwTabViewDemoWindow *self,
                      GINT_TO_POINTER (!muted));
 
   icon = get_indicator_icon (page);
+  tooltip = get_indicator_tooltip (page);
 
   adw_tab_page_set_indicator_icon (page, icon);
+  adw_tab_page_set_indicator_tooltip (page, tooltip);
 
   g_object_unref (icon);
+  g_free (tooltip);
 }
 
 static gboolean
diff --git a/src/adw-tab-view.c b/src/adw-tab-view.c
index c7d0da14..9edba541 100644
--- a/src/adw-tab-view.c
+++ b/src/adw-tab-view.c
@@ -124,6 +124,7 @@ struct _AdwTabPage
   GIcon *icon;
   gboolean loading;
   GIcon *indicator_icon;
+  char *indicator_tooltip;
   gboolean indicator_activatable;
   gboolean needs_attention;
 
@@ -143,6 +144,7 @@ enum {
   PAGE_PROP_ICON,
   PAGE_PROP_LOADING,
   PAGE_PROP_INDICATOR_ICON,
+  PAGE_PROP_INDICATOR_TOOLTIP,
   PAGE_PROP_INDICATOR_ACTIVATABLE,
   PAGE_PROP_NEEDS_ATTENTION,
   LAST_PAGE_PROP
@@ -297,6 +299,7 @@ adw_tab_page_finalize (GObject *object)
   g_clear_pointer (&self->tooltip, g_free);
   g_clear_object (&self->icon);
   g_clear_object (&self->indicator_icon);
+  g_clear_pointer (&self->indicator_tooltip, g_free);
 
   G_OBJECT_CLASS (adw_tab_page_parent_class)->finalize (object);
 }
@@ -346,6 +349,10 @@ adw_tab_page_get_property (GObject    *object,
     g_value_set_object (value, adw_tab_page_get_indicator_icon (self));
     break;
 
+  case PAGE_PROP_INDICATOR_TOOLTIP:
+    g_value_set_string (value, adw_tab_page_get_indicator_tooltip (self));
+    break;
+
   case PAGE_PROP_INDICATOR_ACTIVATABLE:
     g_value_set_boolean (value, adw_tab_page_get_indicator_activatable (self));
     break;
@@ -396,6 +403,10 @@ adw_tab_page_set_property (GObject      *object,
     adw_tab_page_set_indicator_icon (self, g_value_get_object (value));
     break;
 
+  case PAGE_PROP_INDICATOR_TOOLTIP:
+    adw_tab_page_set_indicator_tooltip (self, g_value_get_string (value));
+    break;
+
   case PAGE_PROP_INDICATOR_ACTIVATABLE:
     adw_tab_page_set_indicator_activatable (self, g_value_get_boolean (value));
     break;
@@ -550,6 +561,9 @@ adw_tab_page_class_init (AdwTabPageClass *klass)
    * If the page is pinned, the indicator will be shown instead of icon or
    * spinner.
    *
+   * [property@TabPage:indicator-tooltip] can be used to set the tooltip on the
+   * indicator icon.
+   *
    * If [property@TabPage:indicator-activatable] is set to `TRUE`, the
    * indicator icon can act as a button.
    *
@@ -560,6 +574,22 @@ adw_tab_page_class_init (AdwTabPageClass *klass)
                          G_TYPE_ICON,
                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
 
+  /**
+   * AdwTabPage:indicator-tooltip: (attributes org.gtk.Property.get=adw_tab_page_get_indicator_tooltip 
org.gtk.Property.set=adw_tab_page_set_indicator_tooltip)
+   *
+   * The tooltip of the indicator icon.
+   *
+   * The tooltip can be marked up with the Pango text markup language.
+   *
+   * See [property@TabPage:indicator-icon].
+   *
+   * Since: 1.2
+   */
+  page_props[PAGE_PROP_INDICATOR_TOOLTIP] =
+    g_param_spec_string ("indicator-tooltip", NULL, NULL,
+                         "",
+                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
+
   /**
    * AdwTabPage:indicator-activatable: (attributes 
org.gtk.Property.get=adw_tab_page_get_indicator_activatable 
org.gtk.Property.set=adw_tab_page_set_indicator_activatable)
    *
@@ -601,6 +631,7 @@ adw_tab_page_init (AdwTabPage *self)
 {
   self->title = g_strdup ("");
   self->tooltip = g_strdup ("");
+  self->indicator_tooltip = g_strdup ("");
 }
 
 #define ADW_TYPE_TAB_PAGES (adw_tab_pages_get_type ())
@@ -2039,6 +2070,53 @@ adw_tab_page_set_indicator_icon (AdwTabPage *self,
   g_object_notify_by_pspec (G_OBJECT (self), page_props[PAGE_PROP_INDICATOR_ICON]);
 }
 
+/**
+ * adw_tab_page_get_indicator_tooltip: (attributes org.gtk.Method.get_property=indicator-tooltip)
+ * @self: a tab page
+ *
+ * Gets the tooltip of the indicator icon of @self.
+ *
+ * Returns: (transfer none): the indicator tooltip of @self
+ *
+ * Since: 1.2
+ */
+const char *
+adw_tab_page_get_indicator_tooltip (AdwTabPage *self)
+{
+  g_return_val_if_fail (ADW_IS_TAB_PAGE (self), NULL);
+
+  return self->indicator_tooltip;
+}
+
+/**
+ * adw_tab_page_set_indicator_tooltip: (attributes org.gtk.Method.set_property=indicator-tooltip)
+ * @self: a tab page
+ * @tooltip: the indicator tooltip of @self
+ *
+ * Sets the tooltip of the indicator icon of @self.
+ *
+ * The tooltip can be marked up with the Pango text markup language.
+ *
+ * See [property@TabPage:indicator-icon].
+ *
+ * Since: 1.2
+ */
+void
+adw_tab_page_set_indicator_tooltip (AdwTabPage *self,
+                                    const char *tooltip)
+{
+  g_return_if_fail (ADW_IS_TAB_PAGE (self));
+  g_return_if_fail (tooltip != NULL);
+
+  if (!g_strcmp0 (tooltip, self->indicator_tooltip))
+    return;
+
+  g_clear_pointer (&self->indicator_tooltip, g_free);
+  self->indicator_tooltip = g_strdup (tooltip ? tooltip : "");
+
+  g_object_notify_by_pspec (G_OBJECT (self), page_props[PAGE_PROP_INDICATOR_TOOLTIP]);
+}
+
 /**
  * adw_tab_page_get_indicator_activatable: (attributes org.gtk.Method.get_property=indicator-activatable)
  * @self: a tab page
diff --git a/src/adw-tab-view.h b/src/adw-tab-view.h
index 39412044..4c1af2da 100644
--- a/src/adw-tab-view.h
+++ b/src/adw-tab-view.h
@@ -83,6 +83,12 @@ ADW_AVAILABLE_IN_ALL
 void   adw_tab_page_set_indicator_icon (AdwTabPage *self,
                                         GIcon      *indicator_icon);
 
+ADW_AVAILABLE_IN_1_2
+const char *adw_tab_page_get_indicator_tooltip (AdwTabPage *self);
+ADW_AVAILABLE_IN_1_2
+void        adw_tab_page_set_indicator_tooltip (AdwTabPage *self,
+                                                const char *tooltip);
+
 ADW_AVAILABLE_IN_ALL
 gboolean adw_tab_page_get_indicator_activatable (AdwTabPage *self);
 ADW_AVAILABLE_IN_ALL
diff --git a/src/adw-tab.ui b/src/adw-tab.ui
index d40fa818..2e9f67b4 100644
--- a/src/adw-tab.ui
+++ b/src/adw-tab.ui
@@ -58,6 +58,11 @@
       <object class="GtkButton" id="indicator_btn">
         <property name="can-focus">False</property>
         <property name="valign">center</property>
+        <binding name="tooltip-markup">
+          <lookup name="indicator-tooltip" type="AdwTabPage">
+            <lookup name="page">AdwTab</lookup>
+          </lookup>
+        </binding>
         <signal name="clicked" handler="indicator_clicked_cb" swapped="true"/>
         <style>
           <class name="flat"/>
diff --git a/tests/test-tab-view.c b/tests/test-tab-view.c
index 880e33a7..f1a0cc7f 100644
--- a/tests/test-tab-view.c
+++ b/tests/test-tab-view.c
@@ -1124,6 +1124,37 @@ test_adw_tab_page_indicator_icon (void)
   g_assert_finalize_object (icon2);
 }
 
+static void
+test_adw_tab_page_indicator_tooltip (void)
+{
+  AdwTabView *view = g_object_ref_sink (ADW_TAB_VIEW (adw_tab_view_new ()));
+  AdwTabPage *page;
+  char *tooltip;
+
+  g_assert_nonnull (view);
+
+  page = adw_tab_view_append (view, gtk_button_new ());
+  g_assert_nonnull (page);
+
+  notified = 0;
+  g_signal_connect (page, "notify::indicator-tooltip", G_CALLBACK (notify_cb), NULL);
+
+  g_object_get (page, "indicator-tooltip", &tooltip, NULL);
+  g_assert_cmpstr (tooltip, ==, "");
+  g_assert_cmpint (notified, ==, 0);
+
+  adw_tab_page_set_indicator_tooltip (page, "Some tooltip");
+  g_assert_cmpstr (adw_tab_page_get_indicator_tooltip (page), ==, "Some tooltip");
+  g_assert_cmpint (notified, ==, 1);
+
+  g_object_set (page, "indicator-tooltip", "Some other tooltip", NULL);
+  g_assert_cmpstr (adw_tab_page_get_indicator_tooltip (page), ==, "Some other tooltip");
+  g_assert_cmpint (notified, ==, 2);
+
+  g_free (tooltip);
+  g_assert_finalize_object (view);
+}
+
 static void
 test_adw_tab_page_indicator_activatable (void)
 {
@@ -1287,6 +1318,7 @@ main (int   argc,
   g_test_add_func ("/Adwaita/TabPage/icon", test_adw_tab_page_icon);
   g_test_add_func ("/Adwaita/TabPage/loading", test_adw_tab_page_loading);
   g_test_add_func ("/Adwaita/TabPage/indicator_icon", test_adw_tab_page_indicator_icon);
+  g_test_add_func ("/Adwaita/TabPage/indicator_tooltip", test_adw_tab_page_indicator_tooltip);
   g_test_add_func ("/Adwaita/TabPage/indicator_activatable", test_adw_tab_page_indicator_activatable);
   g_test_add_func ("/Adwaita/TabPage/needs_attention", test_adw_tab_page_needs_attention);
 


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