[libhandy/wip/haecker-felix/flap-widget] Swipe to open/close API



commit d36f1aa1504b40d790e94591bda5140a83770119
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Mon Nov 23 20:58:11 2020 +0500

    Swipe to open/close API

 debian/libhandy-1-0.symbols |   4 +
 src/hdy-flap.c              | 192 ++++++++++++++++++++++++++++++++++++++++----
 src/hdy-flap.h              |  12 +++
 3 files changed, 191 insertions(+), 17 deletions(-)
---
diff --git a/debian/libhandy-1-0.symbols b/debian/libhandy-1-0.symbols
index 501b3016..2cb32898 100644
--- a/debian/libhandy-1-0.symbols
+++ b/debian/libhandy-1-0.symbols
@@ -131,6 +131,8 @@ libhandy-1.so.0 libhandy-1-0 #MINVER#
  hdy_flap_get_reveal_duration@LIBHANDY_1_0 1.1.0
  hdy_flap_get_reveal_flap@LIBHANDY_1_0 1.1.0
  hdy_flap_get_separator@LIBHANDY_1_0 1.1.0
+ hdy_flap_get_swipe_to_close@LIBHANDY_1_0 1.1.0
+ hdy_flap_get_swipe_to_open@LIBHANDY_1_0 1.1.0
  hdy_flap_get_transition_type@LIBHANDY_1_0 1.1.0
  hdy_flap_get_type@LIBHANDY_1_0 1.1.0
  hdy_flap_new@LIBHANDY_1_0 1.1.0
@@ -143,6 +145,8 @@ libhandy-1.so.0 libhandy-1-0 #MINVER#
  hdy_flap_set_reveal_duration@LIBHANDY_1_0 1.1.0
  hdy_flap_set_reveal_flap@LIBHANDY_1_0 1.1.0
  hdy_flap_set_separator@LIBHANDY_1_0 1.1.0
+ hdy_flap_set_swipe_to_close@LIBHANDY_1_0 1.1.0
+ hdy_flap_set_swipe_to_open@LIBHANDY_1_0 1.1.0
  hdy_flap_set_transition_type@LIBHANDY_1_0 1.1.0
  hdy_flap_transition_type_get_type@LIBHANDY_1_0 1.1.0
  hdy_get_enable_animations@LIBHANDY_1_0 0.0.11
diff --git a/src/hdy-flap.c b/src/hdy-flap.c
index 20b7bf19..817f7a19 100644
--- a/src/hdy-flap.c
+++ b/src/hdy-flap.c
@@ -82,6 +82,8 @@ struct _HdyFlap
 
   HdyShadowHelper *shadow_helper;
 
+  gboolean swipe_to_open;
+  gboolean swipe_to_close;
   HdySwipeTracker *tracker;
   gboolean swipe_active;
 
@@ -110,6 +112,8 @@ enum {
   PROP_REVEAL_DURATION,
   PROP_TRANSITION_TYPE,
   PROP_CLICK_TO_CLOSE,
+  PROP_SWIPE_TO_OPEN,
+  PROP_SWIPE_TO_CLOSE,
 
   /* Overridden properties */
   PROP_ORIENTATION,
@@ -124,10 +128,15 @@ update_swipe_tracker (HdyFlap *self)
 {
   gboolean reverse = self->flap_position == GTK_PACK_START;
 
+  if (!self->tracker)
+    return;
+
   if (self->orientation == GTK_ORIENTATION_HORIZONTAL &&
       gtk_widget_get_direction (GTK_WIDGET (self)) == GTK_TEXT_DIR_RTL)
     reverse = !reverse;
 
+  hdy_swipe_tracker_set_enabled (self->tracker, self->flap.widget &&
+                                 (self->swipe_to_open || self->swipe_to_close));
   hdy_swipe_tracker_set_reversed (self->tracker, reverse);
   gtk_orientable_set_orientation (GTK_ORIENTABLE (self->tracker),
                                   self->orientation);
@@ -319,12 +328,28 @@ set_folded (HdyFlap  *self,
   g_object_notify_by_pspec (G_OBJECT (self), props[PROP_FOLDED]);
 }
 
+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;
+  gboolean is_horiz = self->orientation == GTK_ORIENTATION_HORIZONTAL;
+
+  return (is_rtl && !is_horiz) ? GTK_PACK_END : GTK_PACK_START;
+}
+
 static void
 begin_swipe_cb (HdySwipeTracker        *tracker,
                 HdyNavigationDirection  direction,
                 gboolean                direct,
                 HdyFlap                *self)
 {
+  if (self->reveal_progress <= 0 && !self->swipe_to_open)
+    return;
+
+  if (self->reveal_progress >= 1 && !self->swipe_to_close)
+    return;
+
   if (self->reveal_animation)
     hdy_animation_stop (self->reveal_animation);
 
@@ -336,6 +361,9 @@ update_swipe_cb (HdySwipeTracker *tracker,
                  gdouble          progress,
                  HdyFlap         *self)
 {
+  if (!self->swipe_active)
+    return;
+
   set_reveal_progress (self, progress);
 }
 
@@ -345,6 +373,9 @@ end_swipe_cb (HdySwipeTracker *tracker,
               gdouble          to,
               HdyFlap         *self)
 {
+  if (!self->swipe_active)
+    return;
+
   self->swipe_active = FALSE;
 
   if ((to > 0) == self->reveal_flap)
@@ -495,16 +526,6 @@ restack_windows (HdyFlap *self)
   }
 }
 
-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;
-  gboolean is_horiz = self->orientation == GTK_ORIENTATION_HORIZONTAL;
-
-  return (is_rtl && !is_horiz) ? GTK_PACK_END : GTK_PACK_START;
-}
-
 static void
 add_child (HdyFlap   *self,
            ChildInfo *info)
@@ -1210,6 +1231,12 @@ hdy_flap_get_property (GObject    *object,
   case PROP_CLICK_TO_CLOSE:
     g_value_set_boolean (value, hdy_flap_get_click_to_close (self));
     break;
+  case PROP_SWIPE_TO_OPEN:
+    g_value_set_boolean (value, hdy_flap_get_swipe_to_open (self));
+    break;
+  case PROP_SWIPE_TO_CLOSE:
+    g_value_set_boolean (value, hdy_flap_get_swipe_to_close (self));
+    break;
   case PROP_ORIENTATION:
     g_value_set_enum (value, self->orientation);
     break;
@@ -1257,6 +1284,12 @@ hdy_flap_set_property (GObject      *object,
   case PROP_CLICK_TO_CLOSE:
     hdy_flap_set_click_to_close (self, g_value_get_boolean (value));
     break;
+  case PROP_SWIPE_TO_OPEN:
+    hdy_flap_set_swipe_to_open (self, g_value_get_boolean (value));
+    break;
+  case PROP_SWIPE_TO_CLOSE:
+    hdy_flap_set_swipe_to_close (self, g_value_get_boolean (value));
+    break;
   case PROP_ORIENTATION:
     set_orientation (self, g_value_get_enum (value));
     break;
@@ -1439,6 +1472,30 @@ hdy_flap_class_init (HdyFlapClass *klass)
                           TRUE,
                           G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
 
+  /**
+   * HdyFlap:swipe-to-open:
+   *
+   * Since: 1.1
+   */
+  props[PROP_SWIPE_TO_OPEN] =
+    g_param_spec_boolean ("swipe-to-open",
+                          _("Swipe to Open"),
+                          _("Swipe to Open"),
+                          TRUE,
+                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
+
+  /**
+   * HdyFlap:swipe-to-close:
+   *
+   * Since: 1.1
+   */
+  props[PROP_SWIPE_TO_CLOSE] =
+    g_param_spec_boolean ("swipe-to-close",
+                          _("Swipe to Close"),
+                          _("Swipe to Close"),
+                          TRUE,
+                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
+
   g_object_class_install_properties (object_class, LAST_PROP, props);
 
   g_object_class_override_property (object_class,
@@ -1463,6 +1520,8 @@ hdy_flap_init (HdyFlap *self)
   self->fold_duration = 250;
   self->reveal_duration = 250;
   self->click_to_close = TRUE;
+  self->swipe_to_open = TRUE;
+  self->swipe_to_close = TRUE;
 
   self->shadow_helper = hdy_shadow_helper_new (GTK_WIDGET (self));
   self->tracker = hdy_swipe_tracker_new (HDY_SWIPEABLE (self));
@@ -1549,13 +1608,32 @@ static gdouble *
 hdy_flap_get_snap_points (HdySwipeable *swipeable,
                           gint         *n_snap_points)
 {
-  gdouble *points = g_new0 (gdouble, 2);
+  HdyFlap *self = HDY_FLAP (swipeable);
+  gboolean can_open = self->reveal_progress > 0 || self->swipe_to_open || self->swipe_active;
+  gboolean can_close = self->reveal_progress < 1 || self->swipe_to_close || self->swipe_active;
+  gdouble *points;
+
+  if (!can_open && !can_close)
+    return NULL;
+
+  if (can_open && can_close) {
+    points = g_new0 (gdouble, 2);
+
+    if (n_snap_points)
+      *n_snap_points = 2;
+
+    points[0] = 0;
+    points[1] = 1;
+
+    return points;
+  }
+
+  points = g_new0 (gdouble, 1);
 
   if (n_snap_points)
-    *n_snap_points = 2;
+    *n_snap_points = 1;
 
-  points[0] = 0;
-  points[1] = 1;
+  points[0] = can_open ? 1 : 0;
 
   return points;
 }
@@ -1716,9 +1794,7 @@ hdy_flap_set_flap (HdyFlap   *self,
   if (self->flap.widget)
     add_child (self, &self->flap);
 
-  if (self->tracker)
-    hdy_swipe_tracker_set_enabled (self->tracker, self->flap.widget != NULL);
-
+  update_swipe_tracker (self);
   update_child_visibility (self);
 
   g_object_notify_by_pspec (G_OBJECT (self), props[PROP_FLAP]);
@@ -2105,3 +2181,85 @@ hdy_flap_set_click_to_close (HdyFlap  *self,
 
   g_object_notify_by_pspec (G_OBJECT (self), props[PROP_CLICK_TO_CLOSE]);
 }
+
+/**
+ * hdy_flap_get_swipe_to_open:
+ * @self: a #HdyFlap
+ *
+ * Returns: %TRUE if flap widget is click to close.
+ *
+ * Since: 1.1
+ */
+gboolean
+hdy_flap_get_swipe_to_open (HdyFlap *self)
+{
+  g_return_val_if_fail (HDY_IS_FLAP (self), FALSE);
+
+  return self->swipe_to_open;
+}
+
+/**
+ * hdy_flap_set_swipe_to_open:
+ * @self: a #HdyFlap
+ * @swipe_to_open: Reveal flap
+ *
+ * Since: 1.1
+ */
+void
+hdy_flap_set_swipe_to_open (HdyFlap  *self,
+                            gboolean  swipe_to_open)
+{
+  g_return_if_fail (HDY_IS_FLAP (self));
+
+  swipe_to_open = !!swipe_to_open;
+
+  if (self->swipe_to_open == swipe_to_open)
+    return;
+
+  self->swipe_to_open = swipe_to_open;
+
+  update_swipe_tracker (self);
+
+  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_SWIPE_TO_OPEN]);
+}
+
+/**
+ * hdy_flap_get_swipe_to_close:
+ * @self: a #HdyFlap
+ *
+ * Returns: %TRUE if flap widget is click to close.
+ *
+ * Since: 1.1
+ */
+gboolean
+hdy_flap_get_swipe_to_close (HdyFlap *self)
+{
+  g_return_val_if_fail (HDY_IS_FLAP (self), FALSE);
+
+  return self->swipe_to_close;
+}
+
+/**
+ * hdy_flap_set_swipe_to_close:
+ * @self: a #HdyFlap
+ * @swipe_to_close: Reveal flap
+ *
+ * Since: 1.1
+ */
+void
+hdy_flap_set_swipe_to_close (HdyFlap  *self,
+                             gboolean  swipe_to_close)
+{
+  g_return_if_fail (HDY_IS_FLAP (self));
+
+  swipe_to_close = !!swipe_to_close;
+
+  if (self->swipe_to_close == swipe_to_close)
+    return;
+
+  self->swipe_to_close = swipe_to_close;
+
+  update_swipe_tracker (self);
+
+  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_SWIPE_TO_CLOSE]);
+}
diff --git a/src/hdy-flap.h b/src/hdy-flap.h
index 7d1d776d..59e880d0 100644
--- a/src/hdy-flap.h
+++ b/src/hdy-flap.h
@@ -100,4 +100,16 @@ HDY_AVAILABLE_IN_1_1
 void     hdy_flap_set_click_to_close (HdyFlap  *self,
                                       gboolean  click_to_close);
 
+HDY_AVAILABLE_IN_1_1
+gboolean hdy_flap_get_swipe_to_open (HdyFlap  *self);
+HDY_AVAILABLE_IN_1_1
+void     hdy_flap_set_swipe_to_open (HdyFlap  *self,
+                                     gboolean  swipe_to_open);
+
+HDY_AVAILABLE_IN_1_1
+gboolean hdy_flap_get_swipe_to_close (HdyFlap  *self);
+HDY_AVAILABLE_IN_1_1
+void     hdy_flap_set_swipe_to_close (HdyFlap  *self,
+                                      gboolean  swipe_to_close);
+
 G_END_DECLS


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