Commit 254296fa authored by Alexander Mikhaylenko's avatar Alexander Mikhaylenko
Browse files

swipeable: Provide swipe direction when preparing



This will allow to restrict the swipe to only one direction for leaflet.
Signed-off-by: Alexander Mikhaylenko's avatarAlexander Mikhaylenko <alexm@gnome.org>
parent b4158a23
......@@ -118,7 +118,8 @@ hdy_paginator_switch_child (HdySwipeable *swipeable,
}
static void
hdy_paginator_begin_swipe (HdySwipeable *swipeable)
hdy_paginator_begin_swipe (HdySwipeable *swipeable,
gint direction)
{
HdyPaginator *self = HDY_PAGINATOR (swipeable);
gdouble distance, position, closest_point;
......
......@@ -118,6 +118,7 @@ switch_child_cb (HdySwipeGroup *self,
static void
begin_swipe_cb (HdySwipeGroup *self,
gint direction,
HdySwipeable *swipeable)
{
GSList *swipeables;
......@@ -129,7 +130,7 @@ begin_swipe_cb (HdySwipeGroup *self,
for (swipeables = self->swipeables; swipeables != NULL; swipeables = swipeables->next)
if (swipeables->data != swipeable)
hdy_swipeable_begin_swipe (swipeables->data);
hdy_swipeable_begin_swipe (swipeables->data, direction);
}
static void
......
......@@ -140,14 +140,13 @@ reset (HdySwipeTracker *self)
static void
gesture_prepare (HdySwipeTracker *self,
gdouble x,
gdouble y)
gint direction)
{
if (self->state != HDY_SWIPE_TRACKER_STATE_NONE)
return;
self->state = HDY_SWIPE_TRACKER_STATE_PREPARING;
hdy_swipeable_begin_swipe (self->swipeable);
hdy_swipeable_begin_swipe (self->swipeable, direction);
}
static void
......@@ -290,12 +289,8 @@ drag_begin_cb (HdySwipeTracker *self,
gdouble start_y,
GtkGestureDrag *gesture)
{
if (self->state != HDY_SWIPE_TRACKER_STATE_NONE) {
if (self->state != HDY_SWIPE_TRACKER_STATE_NONE)
gtk_gesture_set_state (self->touch_gesture, GTK_EVENT_SEQUENCE_DENIED);
return;
}
gesture_prepare (self, start_x, start_y);
}
static void
......@@ -305,7 +300,7 @@ drag_update_cb (HdySwipeTracker *self,
GtkGestureDrag *gesture)
{
gdouble offset;
gboolean is_vertical;
gboolean is_vertical, is_offset_vertical;
is_vertical = (self->orientation == GTK_ORIENTATION_VERTICAL);
if (is_vertical)
......@@ -316,16 +311,25 @@ drag_update_cb (HdySwipeTracker *self,
if (self->reversed)
offset = -offset;
is_offset_vertical = (ABS (offset_y) > ABS (offset_x));
if (self->state == HDY_SWIPE_TRACKER_STATE_NONE) {
if (is_vertical == is_offset_vertical)
gesture_prepare (self, offset > 0 ? 1 : -1);
else
gtk_gesture_set_state (self->touch_gesture, GTK_EVENT_SEQUENCE_DENIED);
return;
}
if (self->state == HDY_SWIPE_TRACKER_STATE_PENDING) {
gdouble distance;
gdouble first_point, last_point;
gboolean is_offset_vertical, is_overshooting;
gboolean is_overshooting;
first_point = self->snap_points[0];
last_point = self->snap_points[self->n_snap_points - 1];
distance = sqrt (offset_x * offset_x + offset_y * offset_y);
is_offset_vertical = (ABS (offset_y) > ABS (offset_x));
is_overshooting = (offset < 0 && self->progress <= first_point) ||
(offset > 0 && self->progress >= last_point);
......@@ -373,11 +377,11 @@ static gboolean
captured_scroll_event (HdySwipeTracker *self,
GdkEvent *event)
{
GtkWidget *widget;
GdkDevice *source_device;
GdkInputSource input_source;
gdouble dx, dy, delta;
gboolean is_vertical;
gboolean is_delta_vertical;
if (gdk_event_get_scroll_direction (event, NULL))
return GDK_EVENT_PROPAGATE;
......@@ -387,18 +391,23 @@ captured_scroll_event (HdySwipeTracker *self,
if (input_source != GDK_SOURCE_TOUCHPAD)
return GDK_EVENT_PROPAGATE;
if (self->state == HDY_SWIPE_TRACKER_STATE_NONE) {
gdouble root_x, root_y;
gint x, y;
GtkWidget *toplevel;
is_vertical = (self->orientation == GTK_ORIENTATION_VERTICAL);
gdk_event_get_scroll_deltas (event, &dx, &dy);
delta = is_vertical ? dy : dx;
if (self->reversed)
delta = -delta;
gdk_event_get_root_coords (event, &root_x, &root_y);
is_delta_vertical = (ABS (dy) > ABS (dx));
widget = GTK_WIDGET (self->swipeable);
toplevel = gtk_widget_get_toplevel (widget);
gtk_widget_translate_coordinates (toplevel, widget, root_x, root_y, &x, &y);
if (self->state == HDY_SWIPE_TRACKER_STATE_NONE) {
if (gdk_event_is_scroll_stop_event (event))
return GDK_EVENT_PROPAGATE;
gesture_prepare (self, x, y);
if (is_vertical == is_delta_vertical)
gesture_prepare (self, delta > 0 ? 1 : -1);
else
return GDK_EVENT_PROPAGATE;
}
if (self->state == HDY_SWIPE_TRACKER_STATE_PREPARING) {
......@@ -408,21 +417,13 @@ captured_scroll_event (HdySwipeTracker *self,
return GDK_EVENT_PROPAGATE;
}
is_vertical = (self->orientation == GTK_ORIENTATION_VERTICAL);
gdk_event_get_scroll_deltas (event, &dx, &dy);
delta = is_vertical ? dy : dx;
if (self->reversed)
delta = -delta;
if (self->state == HDY_SWIPE_TRACKER_STATE_PENDING) {
gboolean is_delta_vertical, is_overshooting;
gboolean is_overshooting;
gdouble first_point, last_point;
first_point = self->snap_points[0];
last_point = self->snap_points[self->n_snap_points - 1];
is_delta_vertical = (ABS (dy) > ABS (dx));
is_overshooting = (delta < 0 && self->progress <= first_point) ||
(delta > 0 && self->progress >= last_point);
......
......@@ -14,7 +14,8 @@ G_BEGIN_DECLS
void hdy_swipeable_switch_child (HdySwipeable *self,
guint index,
gint64 duration);
void hdy_swipeable_begin_swipe (HdySwipeable *self);
void hdy_swipeable_begin_swipe (HdySwipeable *self,
gint direction);
void hdy_swipeable_update_swipe (HdySwipeable *self,
gdouble value);
void hdy_swipeable_end_swipe (HdySwipeable *self,
......
......@@ -61,9 +61,12 @@ hdy_swipeable_default_init (HdySwipeableInterface *iface)
/**
* HdySwipeable::begin:
* @self: The #HdySwipeable instance
* @direction: The direction of the swipe, can be 1 or -1
*
* This signal is emitted when a possible swipe is detected. This is used by
* #HdySwipeGroup, applications should not connect to it.
* The @direction value can be used to restrict the swipe to a certain
* direction.
*
* Since: 0.0.12
*/
......@@ -74,7 +77,8 @@ hdy_swipeable_default_init (HdySwipeableInterface *iface)
0,
NULL, NULL, NULL,
G_TYPE_NONE,
0);
1,
G_TYPE_INT);
/**
* HdySwipeable::update:
......@@ -146,16 +150,19 @@ hdy_swipeable_switch_child (HdySwipeable *self,
/**
* hdy_swipeable_begin_swipe:
* @self: a #HdySwipeable
* @direction: The direction of the swipe, can be 1 or -1
*
* This function is called by #HdySwipeTracker when a possible swipe is detected.
* The implementation should check whether a swipe is possible, and if it is,
* it must call hdy_swipe_tracker_confirm_swipe() to provide details about the
* swipe, see that function for details.
* The @direction value can be used to restrict the swipe to a certain direction.
*
* Since: 0.0.12
*/
void
hdy_swipeable_begin_swipe (HdySwipeable *self)
hdy_swipeable_begin_swipe (HdySwipeable *self,
gint direction)
{
HdySwipeableInterface *iface;
......@@ -164,9 +171,9 @@ hdy_swipeable_begin_swipe (HdySwipeable *self)
iface = HDY_SWIPEABLE_GET_IFACE (self);
g_return_if_fail (iface->begin_swipe != NULL);
(* iface->begin_swipe) (self);
(* iface->begin_swipe) (self, direction);
g_signal_emit (self, signals[SIGNAL_BEGIN_SWIPE], 0);
g_signal_emit (self, signals[SIGNAL_BEGIN_SWIPE], 0, direction);
}
/**
......
......@@ -33,7 +33,8 @@ struct _HdySwipeableInterface
void (*switch_child) (HdySwipeable *self,
guint index,
gint64 duration);
void (*begin_swipe) (HdySwipeable *self);
void (*begin_swipe) (HdySwipeable *self,
gint direction);
void (*update_swipe) (HdySwipeable *self,
gdouble value);
void (*end_swipe) (HdySwipeable *self,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment