[libadwaita/wip/cdavis/fold-threshold-policy: 6/6] flap: Add fold-threshold-policy-property




commit a527ad474d8d271259311caccdf72f9fd0f34f61
Author: Christopher Davis <christopherdavis gnome org>
Date:   Wed Jun 23 17:25:26 2021 -0700

    flap: Add fold-threshold-policy-property
    
    We use AdwFoldThresholdPolicy to determine the size
    at which the flap should fold.

 src/adw-flap.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++-------
 src/adw-flap.h |   7 ++++
 2 files changed, 102 insertions(+), 12 deletions(-)
---
diff --git a/src/adw-flap.c b/src/adw-flap.c
index c5362ada..c3c2323e 100644
--- a/src/adw-flap.c
+++ b/src/adw-flap.c
@@ -119,6 +119,7 @@ struct _AdwFlap
   GtkWidget *shield;
 
   AdwFlapFoldPolicy fold_policy;
+  AdwFoldThresholdPolicy fold_threshold_policy;
   AdwFlapTransitionType transition_type;
   GtkPackType flap_position;
   gboolean reveal_flap;
@@ -168,6 +169,7 @@ enum {
   PROP_REVEAL_DURATION,
   PROP_REVEAL_PROGRESS,
   PROP_FOLD_POLICY,
+  PROP_FOLD_THRESHOLD_POLICY,
   PROP_FOLD_DURATION,
   PROP_FOLDED,
   PROP_LOCKED,
@@ -897,21 +899,30 @@ adw_flap_size_allocate (GtkWidget *widget,
   AdwFlap *self = ADW_FLAP (widget);
 
   if (self->fold_policy == ADW_FLAP_FOLD_POLICY_AUTO) {
-    GtkRequisition flap_min = { 0, 0 };
-    GtkRequisition content_min = { 0, 0 };
-    GtkRequisition separator_min = { 0, 0 };
-
-    if (self->flap.widget)
-      gtk_widget_get_preferred_size (self->flap.widget, &flap_min, NULL);
-    if (self->content.widget)
-      gtk_widget_get_preferred_size (self->content.widget, &content_min, NULL);
-    if (self->separator.widget)
-      gtk_widget_get_preferred_size (self->separator.widget, &separator_min, NULL);
+    GtkRequisition flap_size = { 0, 0 };
+    GtkRequisition content_size = { 0, 0 };
+    GtkRequisition separator_size = { 0, 0 };
+
+    if (self->fold_threshold_policy == ADW_FOLD_THRESHOLD_POLICY_MINIMUM) {
+      if (self->flap.widget)
+        gtk_widget_get_preferred_size (self->flap.widget, &flap_size, NULL);
+      if (self->content.widget)
+        gtk_widget_get_preferred_size (self->content.widget, &content_size, NULL);
+      if (self->separator.widget)
+        gtk_widget_get_preferred_size (self->separator.widget, &separator_size, NULL);
+    } else {
+      if (self->flap.widget)
+        gtk_widget_get_preferred_size (self->flap.widget, NULL, &flap_size);
+      if (self->content.widget)
+        gtk_widget_get_preferred_size (self->content.widget, NULL, &content_size);
+      if (self->separator.widget)
+        gtk_widget_get_preferred_size (self->separator.widget, NULL, &separator_size);
+    }
 
     if (self->orientation == GTK_ORIENTATION_HORIZONTAL)
-      set_folded (self, width < content_min.width + flap_min.width + separator_min.width);
+      set_folded (self, width < content_size.width + flap_size.width + separator_size.width);
     else
-      set_folded (self, height < content_min.height + flap_min.height + separator_min.height);
+      set_folded (self, height < content_size.height + flap_size.height + separator_size.height);
   }
 
   compute_allocation (self,
@@ -1126,6 +1137,9 @@ adw_flap_get_property (GObject    *object,
   case PROP_FOLD_POLICY:
     g_value_set_enum (value, adw_flap_get_fold_policy (self));
     break;
+  case PROP_FOLD_THRESHOLD_POLICY:
+    g_value_set_enum (value, adw_flap_get_fold_threshold_policy (self));
+    break;
   case PROP_FOLD_DURATION:
     g_value_set_uint (value, adw_flap_get_fold_duration (self));
     break;
@@ -1185,6 +1199,9 @@ adw_flap_set_property (GObject      *object,
   case PROP_FOLD_POLICY:
     adw_flap_set_fold_policy (self, g_value_get_enum (value));
     break;
+  case PROP_FOLD_THRESHOLD_POLICY:
+    adw_flap_set_fold_threshold_policy (self, g_value_get_enum (value));
+    break;
   case PROP_FOLD_DURATION:
     adw_flap_set_fold_duration (self, g_value_get_uint (value));
     break;
@@ -1377,6 +1394,28 @@ adw_flap_class_init (AdwFlapClass *klass)
                        ADW_FLAP_FOLD_POLICY_AUTO,
                        G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
 
+  /**
+   * AdwFlap:fold-threshold-policy: (attributes org.gtk.Property.get=adw_flap_get_fold_threshold_policy 
org.gtk.Property.set=adw_flap_set_fold_threshold_policy)
+   *
+   * Determines when the flap will fold.
+   *
+   * If set to `ADW_FOLD_THRESHOLD_POLICY_MINIMUM`, flap will only fold when
+   * the children cannot fit anymore. With `ADW_FOLD_THRESHOLD_POLICY_NATURAL`,
+   * it will fold as soon as children don't get their natural size.
+   *
+   * This can be useful if you have a long ellipsizing label and want to let it
+   * ellipsize instead of immediately folding.
+   *
+   * Since: 1.0
+   */
+  props[PROP_FOLD_THRESHOLD_POLICY] =
+    g_param_spec_enum ("fold-threshold-policy",
+                       "Fold Threshold Policy",
+                       "Determines when the flap will fold",
+                       ADW_TYPE_FOLD_THRESHOLD_POLICY,
+                       ADW_FOLD_THRESHOLD_POLICY_MINIMUM,
+                       G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
+
   /**
    * AdwFlap:fold-duration: (attributes org.gtk.Property.get=adw_flap_get_fold_duration 
org.gtk.Property.set=adw_flap_set_fold_duration)
    *
@@ -1527,6 +1566,7 @@ adw_flap_init (AdwFlap *self)
   self->orientation = GTK_ORIENTATION_HORIZONTAL;
   self->flap_position = GTK_PACK_START;
   self->fold_policy = ADW_FLAP_FOLD_POLICY_AUTO;
+  self->fold_threshold_policy = ADW_FOLD_THRESHOLD_POLICY_MINIMUM;
   self->transition_type = ADW_FLAP_TRANSITION_TYPE_OVER;
   self->reveal_flap = TRUE;
   self->locked = FALSE;
@@ -2406,3 +2446,46 @@ adw_flap_set_swipe_to_close (AdwFlap  *self,
 
   g_object_notify_by_pspec (G_OBJECT (self), props[PROP_SWIPE_TO_CLOSE]);
 }
+
+/**
+ * adw_flap_get_fold_threshold_policy: (attributes org.gtk.Method.get_property=fold-threshold-policy)
+ * @self: a `AdwFlap`
+ *
+ * Gets the fold threshold policy for @self.
+ *
+ * Since: 1.0
+ */
+AdwFoldThresholdPolicy
+adw_flap_get_fold_threshold_policy (AdwFlap *self)
+{
+  g_return_val_if_fail (ADW_IS_FLAP (self), ADW_FOLD_THRESHOLD_POLICY_MINIMUM);
+
+  return self->fold_threshold_policy;
+}
+
+
+/**
+ * adw_flap_set_fold_threshold_policy: (attributes org.gtk.Method.set_property=fold-threshold-policy)
+ * @self: a `AdwFlap`
+ * @policy: the policy to use
+ *
+ * Sets the fold threshold policy for @self.
+ *
+ * Since: 1.0
+ */
+void
+adw_flap_set_fold_threshold_policy (AdwFlap                *self,
+                                    AdwFoldThresholdPolicy  policy)
+{
+  g_return_if_fail (ADW_IS_FLAP (self));
+  g_return_if_fail (policy <= ADW_FOLD_THRESHOLD_POLICY_NATURAL);
+
+  if (self->fold_threshold_policy == policy)
+    return;
+
+  self->fold_threshold_policy = policy;
+
+  gtk_widget_queue_allocate (GTK_WIDGET (self));
+
+  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_FOLD_THRESHOLD_POLICY]);
+}
diff --git a/src/adw-flap.h b/src/adw-flap.h
index db8d28bd..bd184894 100644
--- a/src/adw-flap.h
+++ b/src/adw-flap.h
@@ -14,6 +14,7 @@
 
 #include <gtk/gtk.h>
 #include "adw-enums.h"
+#include "adw-fold-threshold-policy.h"
 
 G_BEGIN_DECLS
 
@@ -121,4 +122,10 @@ ADW_AVAILABLE_IN_ALL
 void     adw_flap_set_swipe_to_close (AdwFlap  *self,
                                       gboolean  swipe_to_close);
 
+ADW_AVAILABLE_IN_ALL
+AdwFoldThresholdPolicy adw_flap_get_fold_threshold_policy (AdwFlap                *self);
+ADW_AVAILABLE_IN_ALL
+void                   adw_flap_set_fold_threshold_policy (AdwFlap                *self,
+                                                           AdwFoldThresholdPolicy  policy);
+
 G_END_DECLS


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