[libhandy/wip/haecker-felix/flap-widget] Refactor layout; respect min size



commit 5cf8b00075002564b033cece142aea45bbd9beea
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Wed Nov 18 15:45:05 2020 +0500

    Refactor layout; respect min size

 src/hdy-flap.c | 177 +++++++++++++++++++++++++++------------------------------
 1 file changed, 84 insertions(+), 93 deletions(-)
---
diff --git a/src/hdy-flap.c b/src/hdy-flap.c
index 3ffc63d0..cfbefd4a 100644
--- a/src/hdy-flap.c
+++ b/src/hdy-flap.c
@@ -127,7 +127,10 @@ set_reveal_progress (HdyFlap *self,
 {
   self->reveal_progress = progress;
 
-  gtk_widget_queue_allocate (GTK_WIDGET (self));
+  if (self->fold_policy == HDY_FLAP_FOLD_POLICY_NEVER)
+    gtk_widget_queue_resize (GTK_WIDGET (self));
+  else
+    gtk_widget_queue_allocate (GTK_WIDGET (self));
 }
 
 static void
@@ -146,7 +149,7 @@ fold_animation_done_cb (HdyFlap *self)
 }
 
 static void
-animate_overlay (HdyFlap *self)
+animate_fold (HdyFlap *self)
 {
   if (self->fold_animation)
     hdy_animation_stop (self->fold_animation);
@@ -209,7 +212,7 @@ set_folded (HdyFlap  *self,
 
   self->folded = folded;
 
-  animate_overlay (self);
+  animate_fold (self);
 
   if (!self->locked)
     hdy_flap_set_reveal_flap (self, !self->folded);
@@ -283,10 +286,19 @@ end_swipe_cb (HdySwipeTracker *tracker,
     set_reveal_flap (self, to > 0, duration, FALSE);
 }
 
+static inline GtkPackType
+get_start_or_end (HdyFlap *self)
+{
+  GtkTextDirection direction = gtk_widget_get_direction (GTK_WIDGET (self));
+  gboolean is_rtl = direction == GTK_TEXT_DIR_RTL;
+
+  return is_rtl ? GTK_PACK_END : GTK_PACK_START;
+}
+
 static gint
 get_flap_size (HdyFlap        *self,
-               GtkAllocation  *widget_alloc,
-               GtkOrientation  widget_orientation)
+               GtkAllocation  *alloc,
+               GtkOrientation  orientation)
 {
   GtkRequisition flap_min_size, flap_nat_size;
 
@@ -295,124 +307,110 @@ get_flap_size (HdyFlap        *self,
 
   gtk_widget_get_preferred_size (self->flap, &flap_min_size, &flap_nat_size);
 
-  if (widget_orientation == GTK_ORIENTATION_HORIZONTAL) {
-    if (self->orientation == GTK_ORIENTATION_HORIZONTAL)
+  if (orientation == GTK_ORIENTATION_HORIZONTAL) {
+    if (self->orientation == orientation)
       return flap_min_size.width;
 
-    return widget_alloc->width;
-  } else {
-    if (self->orientation == GTK_ORIENTATION_HORIZONTAL)
-      return widget_alloc->height;
-
-    return flap_min_size.height;
-  }
-}
-
-static gint
-get_content_size (HdyFlap        *self,
-                  GtkAllocation  *widget_alloc,
-                  GtkOrientation  widget_orientation)
-{
-  if (widget_orientation == GTK_ORIENTATION_HORIZONTAL) {
-    gint offset = 0;
-    if (self->orientation == GTK_ORIENTATION_HORIZONTAL)
-      offset = adjust_for_overlay (self, get_flap_size (self, widget_alloc, GTK_ORIENTATION_HORIZONTAL));
-
-    return widget_alloc->width - offset;
+    return alloc->width;
   } else {
-    if (self->orientation == GTK_ORIENTATION_HORIZONTAL)
-      return widget_alloc->height;
+    if (self->orientation == orientation)
+      return flap_min_size.height;
 
-    return widget_alloc->height - adjust_for_overlay (self, get_flap_size (self, widget_alloc, 
GTK_ORIENTATION_VERTICAL));
+    return alloc->height;
   }
 }
 
 static void
 allocate_flap (HdyFlap       *self,
-               GtkAllocation *widget_alloc)
+               GtkAllocation *alloc)
 {
-  gint flap_x = 0;
-  gint flap_y = 0;
-  gint flap_size = get_flap_size (self, widget_alloc, self->orientation);
+  GtkAllocation flap_alloc;
+  gint flap_size = get_flap_size (self, alloc, self->orientation);
 
   if (self->orientation == GTK_ORIENTATION_HORIZONTAL) {
-    GtkTextDirection direction = gtk_widget_get_direction (GTK_WIDGET (self));
-    gboolean is_rtl = direction == GTK_TEXT_DIR_RTL;
-    GtkPackType start_or_end = is_rtl ? GTK_PACK_END : GTK_PACK_START;
+    flap_alloc.y = 0;
+    flap_alloc.height = alloc->height;
+    flap_alloc.width = flap_size;
 
-    if (self->flap_position == start_or_end)
-      flap_x = (gint) round (flap_size * (self->reveal_progress - 1));
+    if (self->flap_position == get_start_or_end (self))
+      flap_alloc.x = (gint) round (flap_size * self->reveal_progress) - flap_size;
     else
-      flap_x = widget_alloc->width - (gint) round (flap_size * self->reveal_progress);
+      flap_alloc.x = alloc->width - (gint) round (flap_size * self->reveal_progress);
+
   } else {
+    flap_alloc.x = 0;
+    flap_alloc.width = alloc->width;
+    flap_alloc.height = flap_size;
+
     if (self->flap_position == GTK_PACK_START)
-      flap_y = (gint) round (flap_size * (self->reveal_progress - 1));
+      flap_alloc.y = (gint) round (flap_size * self->reveal_progress) - flap_size;
     else
-      flap_y = widget_alloc->height - (gint) round (flap_size * self->reveal_progress);
+      flap_alloc.y = alloc->height - (gint) round (flap_size * self->reveal_progress);
   }
 
   gtk_widget_set_child_visible (self->flap, self->reveal_progress > 0);
 
-  if (self->reveal_progress > 0) {
-    GtkAllocation alloc;
-
-    alloc.x = flap_x;
-    alloc.y = flap_y;
-    alloc.width = get_flap_size (self, widget_alloc, GTK_ORIENTATION_HORIZONTAL);
-    alloc.height = get_flap_size (self, widget_alloc, GTK_ORIENTATION_VERTICAL);
-
-    gtk_widget_size_allocate (self->flap, &alloc);
-  }
+  if (self->reveal_progress > 0)
+    gtk_widget_size_allocate (self->flap, &flap_alloc);
 }
 
 static void
 allocate_content (HdyFlap       *self,
                   GtkAllocation *alloc)
 {
-  gint content_x = 0, content_y = 0;
-  GtkRequisition flap_min, flap_nat;
-  GtkRequisition content_min, content_nat;
-  GtkAllocation child_alloc;
+  GtkRequisition content_min;
+  GtkAllocation content_alloc;
+  gint flap_size;
+  gint offset;
 
-  if (self->flap) {
-    gtk_widget_get_preferred_size (self->flap, &flap_min, &flap_nat);
-  } else {
-    flap_min.height = 0;
-    flap_min.width = 0;
+  if (!self->flap) {
+    gtk_widget_size_allocate (self->content, alloc);
+
+    return;
   }
 
-  gtk_widget_get_preferred_size (self->content, &content_min, &content_nat);
+  gtk_widget_get_preferred_size (self->content, &content_min, NULL);
 
-  content_x = 0;
-  content_y = 0;
+  flap_size = get_flap_size (self, alloc, self->orientation);
+  offset = adjust_for_overlay (self, flap_size);
 
   if (self->orientation == GTK_ORIENTATION_HORIZONTAL) {
-    GtkTextDirection direction = gtk_widget_get_direction (GTK_WIDGET (self));
-    gboolean is_rtl = direction == GTK_TEXT_DIR_RTL;
-    GtkPackType start_or_end = is_rtl ? GTK_PACK_END : GTK_PACK_START;
-    gint min = content_min.width + flap_min.width;
+    if (self->fold_policy == HDY_FLAP_FOLD_POLICY_AUTO) {
+      GtkRequisition flap_min;
 
-    if (self->fold_policy == HDY_FLAP_FOLD_POLICY_AUTO)
-      set_folded (self, alloc->width <= min);
+      gtk_widget_get_preferred_size (self->flap, &flap_min, NULL);
+
+      set_folded (self, alloc->width <= content_min.width + flap_min.width);
+    }
 
-    if (self->flap_position == start_or_end)
-      content_x += adjust_for_overlay (self, get_flap_size (self, alloc, self->orientation));
+    content_alloc.y = 0;
+    content_alloc.height = alloc->height;
+    content_alloc.width = MAX (alloc->width - offset, content_min.width);
+
+    if (self->flap_position == get_start_or_end (self))
+      content_alloc.x = alloc->width - content_alloc.width;
+    else
+      content_alloc.x = 0;
   } else {
-    gint min = content_min.height + flap_min.height;
+    if (self->fold_policy == HDY_FLAP_FOLD_POLICY_AUTO) {
+      GtkRequisition flap_min;
+
+      gtk_widget_get_preferred_size (self->flap, &flap_min, NULL);
+
+      set_folded (self, alloc->height <= content_min.height + flap_min.height);
+    }
 
-    if (self->fold_policy == HDY_FLAP_FOLD_POLICY_AUTO)
-      set_folded (self, alloc->height <= min);
+    content_alloc.x = 0;
+    content_alloc.width = alloc->width;
+    content_alloc.height = MAX (alloc->height - offset, content_min.height);
 
     if (self->flap_position == GTK_PACK_START)
-      content_y += adjust_for_overlay (self, get_flap_size (self, alloc, self->orientation));
+      content_alloc.y = alloc->height - content_alloc.height;
+    else
+      content_alloc.y = 0;
   }
 
-  child_alloc.x = content_x;
-  child_alloc.y = content_y;
-  child_alloc.width = get_content_size (self, alloc, GTK_ORIENTATION_HORIZONTAL);
-  child_alloc.height = get_content_size (self, alloc, GTK_ORIENTATION_VERTICAL);
-
-  gtk_widget_size_allocate (self->content, &child_alloc);
+  gtk_widget_size_allocate (self->content, &content_alloc);
 }
 
 static void
@@ -566,12 +564,9 @@ hdy_flap_draw (GtkWidget *widget,
       shadow_y = -flap_height * self->reveal_progress;
     }
   } else {
-    GtkTextDirection direction = gtk_widget_get_direction (GTK_WIDGET (self));
-    gboolean is_rtl = direction == GTK_TEXT_DIR_RTL;
-    GtkPackType start_or_end = is_rtl ? GTK_PACK_END : GTK_PACK_START;
     gint flap_width = gtk_widget_get_allocated_width (self->flap);
 
-    if (self->flap_position == start_or_end) {
+    if (self->flap_position == get_start_or_end (self)) {
       shadow_direction = GTK_PAN_DIRECTION_LEFT;
       shadow_x = flap_width * self->reveal_progress;
     } else {
@@ -1058,11 +1053,7 @@ hdy_flap_get_swipe_area (HdySwipeable           *swipeable,
   gtk_widget_get_allocation (self->flap, &alloc);
 
   if (self->orientation == GTK_ORIENTATION_HORIZONTAL) {
-    GtkTextDirection direction = gtk_widget_get_direction (GTK_WIDGET (self));
-    gboolean is_rtl = direction == GTK_TEXT_DIR_RTL;
-    GtkPackType start_or_end = is_rtl ? GTK_PACK_END : GTK_PACK_START;
-
-    if (self->flap_position == start_or_end) {
+    if (self->flap_position == get_start_or_end (self)) {
       rect->x = 0;
       rect->width = MAX (alloc.x + alloc.width, HDY_SWIPE_BORDER);
     } else {
@@ -1338,7 +1329,7 @@ hdy_flap_get_folded (HdyFlap *self)
  * hdy_flap_get_fold_duration:
  * @self: a #HdyFlap
  *
- * Returns: current overlay animation duration.
+ * Returns: current fold animation duration.
  *
  * Since: 1.1
  */
@@ -1353,7 +1344,7 @@ hdy_flap_get_fold_duration (HdyFlap *self)
 /**
  * hdy_flap_set_fold_duration:
  * @self: a #HdyFlap
- * @duration: Duration for overlay animation
+ * @duration: Duration for fold animation
  *
  * Since: 1.1
  */


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