[libadwaita/wip/exalm/action-row-expand] action-row: Allow suffixes to expand




commit 01e907f90a8d12170f28b3ba52436c787ab19574
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Fri Nov 12 21:03:04 2021 +0500

    action-row: Allow suffixes to expand
    
    GtkBox packs children from the start to end. Unfortunately, here we want
    to have the opposite so we need to set halign=end on the suffixes box.
    
    However, this also breaks hexpand for its children. There doesn't seem to
    be a clean way to accomplish it without changing GtkBox, so instead do a
    little hack: wrap the box into another widget and that would check if the
    child is expanded and control its halign.

 src/adw-action-row.c  | 44 ++++++++++++++++++++++++++++++++++++++++++--
 src/adw-action-row.ui | 10 +++++++---
 2 files changed, 49 insertions(+), 5 deletions(-)
---
diff --git a/src/adw-action-row.c b/src/adw-action-row.c
index 9f4ff983..6600c3b1 100644
--- a/src/adw-action-row.c
+++ b/src/adw-action-row.c
@@ -7,6 +7,7 @@
 #include "config.h"
 #include "adw-action-row.h"
 
+#include "adw-gizmo-private.h"
 #include "adw-macros-private.h"
 
 /**
@@ -51,6 +52,7 @@ typedef struct
   GtkImage *image;
   GtkBox *prefixes;
   GtkLabel *subtitle;
+  GtkWidget *suffixes_bin;
   GtkBox *suffixes;
   GtkLabel *title;
   GtkBox *title_box;
@@ -124,6 +126,38 @@ parent_cb (AdwActionRow *self)
   g_signal_connect_swapped (parent, "row-activated", G_CALLBACK (row_activated_cb), self);
 }
 
+static void
+suffixes_measure (GtkWidget      *widget,
+                  GtkOrientation  orientation,
+                  int             for_size,
+                  int            *minimum,
+                  int            *natural,
+                  int            *minimum_baseline,
+                  int            *natural_baseline)
+{
+  GtkWidget *child = gtk_widget_get_first_child (widget);
+
+  gtk_widget_measure (child, orientation, for_size, minimum, natural,
+                      minimum_baseline, natural_baseline);
+}
+
+
+static void
+suffixes_allocate (GtkWidget *widget,
+                   int        width,
+                   int        height,
+                   int        baseline)
+{
+  GtkWidget *child = gtk_widget_get_first_child (widget);
+
+  if (gtk_widget_compute_expand (child, GTK_ORIENTATION_HORIZONTAL)) {
+    gtk_widget_set_halign (child, GTK_ALIGN_FILL);
+  } else
+    gtk_widget_set_halign (child, GTK_ALIGN_END);
+
+  gtk_widget_allocate (child, width, height, baseline, NULL);
+}
+
 static void
 adw_action_row_get_property (GObject    *object,
                              guint       prop_id,
@@ -331,6 +365,7 @@ adw_action_row_class_init (AdwActionRowClass *klass)
   gtk_widget_class_bind_template_child_private (widget_class, AdwActionRow, image);
   gtk_widget_class_bind_template_child_private (widget_class, AdwActionRow, prefixes);
   gtk_widget_class_bind_template_child_private (widget_class, AdwActionRow, subtitle);
+  gtk_widget_class_bind_template_child_private (widget_class, AdwActionRow, suffixes_bin);
   gtk_widget_class_bind_template_child_private (widget_class, AdwActionRow, suffixes);
   gtk_widget_class_bind_template_child_private (widget_class, AdwActionRow, title);
   gtk_widget_class_bind_template_child_private (widget_class, AdwActionRow, title_box);
@@ -340,10 +375,15 @@ adw_action_row_class_init (AdwActionRowClass *klass)
 static void
 adw_action_row_init (AdwActionRow *self)
 {
+  AdwActionRowPrivate *priv = adw_action_row_get_instance_private (self);
+
+  g_type_ensure (ADW_TYPE_GIZMO);
+
   gtk_widget_init_template (GTK_WIDGET (self));
 
-  g_signal_connect (self, "notify::parent", G_CALLBACK (parent_cb), NULL);
+  gtk_widget_set_layout_manager (priv->suffixes_bin, gtk_custom_layout_new (NULL, suffixes_measure, 
suffixes_allocate));
 
+  g_signal_connect (self, "notify::parent", G_CALLBACK (parent_cb), NULL);
 }
 
 static void
@@ -728,7 +768,7 @@ adw_action_row_add_suffix (AdwActionRow *self,
   priv = adw_action_row_get_instance_private (self);
 
   gtk_box_append (priv->suffixes, widget);
-  gtk_widget_show (GTK_WIDGET (priv->suffixes));
+  gtk_widget_show (GTK_WIDGET (priv->suffixes_bin));
 }
 
 /**
diff --git a/src/adw-action-row.ui b/src/adw-action-row.ui
index 840df424..f1a80e1f 100644
--- a/src/adw-action-row.ui
+++ b/src/adw-action-row.ui
@@ -38,7 +38,6 @@
             <property name="halign">start</property>
             <property name="orientation">vertical</property>
             <property name="valign">center</property>
-            <property name="hexpand">True</property>
             <style>
               <class name="title"/>
             </style>
@@ -86,9 +85,14 @@
           </object>
         </child>
         <child>
-          <object class="GtkBox" id="suffixes">
+          <object class="AdwGizmo" id="suffixes_bin">
             <property name="visible">False</property>
-            <property name="spacing">12</property>
+            <property name="hexpand">True</property>
+            <child>
+              <object class="GtkBox" id="suffixes">
+                <property name="spacing">12</property>
+              </object>
+            </child>
           </object>
         </child>
       </object>


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