[gnome-text-editor] sidebar-row: use custom tooltip for sidebar rows



commit 51906b445e0b94ce584f9f8c0db07ea283a948b1
Author: Christian Hergert <chergert redhat com>
Date:   Fri Aug 12 12:04:28 2022 -0700

    sidebar-row: use custom tooltip for sidebar rows
    
    We should show the full path for files so that we can see through any
    ellipsized text. However, tooltips by default wrap and so we want to use
    our own label to display so we avoid the "natural word" line breaks.

 src/editor-sidebar-row.c  | 31 +++++++++++++++++++++++++++++++
 src/editor-sidebar-row.ui |  6 ++++++
 2 files changed, 37 insertions(+)
---
diff --git a/src/editor-sidebar-row.c b/src/editor-sidebar-row.c
index 30e7c35..f3e9bdd 100644
--- a/src/editor-sidebar-row.c
+++ b/src/editor-sidebar-row.c
@@ -44,6 +44,7 @@ struct _EditorSidebarRow
   GtkLabel          *age;
   GtkStack          *stack;
   GtkButton         *remove;
+  GtkLabel          *tooltip;
 
   GBinding          *title_binding;
   GBinding          *subtitle_binding;
@@ -125,6 +126,34 @@ on_remove_clicked_cb (EditorSidebarRow *self,
                               draft_id ? draft_id : "");
 }
 
+static gboolean
+query_tooltip_cb (GtkWidget  *widget,
+                  int         x,
+                  int         y,
+                  gboolean    keyboard,
+                  GtkTooltip *tooltip)
+{
+  EditorSidebarRow *self = (EditorSidebarRow *)widget;
+  g_autofree char *text = NULL;
+  GFile *file;
+
+  g_assert (EDITOR_IS_SIDEBAR_ROW (self));
+
+  if (!(file = _editor_sidebar_item_get_file (self->item)))
+    return FALSE;
+
+  if (g_file_is_native (file))
+    text = g_file_get_path (file);
+  else
+    text = g_file_get_uri (file);
+
+  gtk_label_set_text (self->tooltip, text);
+
+  gtk_tooltip_set_custom (tooltip, GTK_WIDGET (self->tooltip));
+
+  return TRUE;
+}
+
 static void
 editor_sidebar_row_dispose (GObject *object)
 {
@@ -199,7 +228,9 @@ editor_sidebar_row_class_init (EditorSidebarRowClass *klass)
   gtk_widget_class_bind_template_child (widget_class, EditorSidebarRow, stack);
   gtk_widget_class_bind_template_child (widget_class, EditorSidebarRow, subtitle);
   gtk_widget_class_bind_template_child (widget_class, EditorSidebarRow, title);
+  gtk_widget_class_bind_template_child (widget_class, EditorSidebarRow, tooltip);
   gtk_widget_class_bind_template_callback (widget_class, on_remove_clicked_cb);
+  gtk_widget_class_bind_template_callback (widget_class, query_tooltip_cb);
 
   properties [PROP_ITEM] =
     g_param_spec_object ("item",
diff --git a/src/editor-sidebar-row.ui b/src/editor-sidebar-row.ui
index 00f1499..56ffea9 100644
--- a/src/editor-sidebar-row.ui
+++ b/src/editor-sidebar-row.ui
@@ -2,6 +2,8 @@
 <interface>
   <requires lib="gtk" version="4.0"/>
   <template class="EditorSidebarRow" parent="GtkListBoxRow">
+    <property name="has-tooltip">true</property>
+    <signal name="query-tooltip" handler="query_tooltip_cb"/>
     <child>
       <object class="GtkGrid" id="grid">
         <property name="margin-top">3</property>
@@ -95,4 +97,8 @@
       </object>
     </child>
   </template>
+  <object class="GtkLabel" id="tooltip">
+    <property name="ellipsize">end</property>
+    <property name="wrap">false</property>
+  </object>
 </interface>


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