adw-swipeable.c 4.05 KB
Newer Older
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
1
2
3
/*
 * Copyright (C) 2019 Alexander Mikhaylenko <exalm7659@gmail.com>
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
5
6
 */

7
8
#include "config.h"

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
9
#include "adw-swipeable.h"
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
10
11

/**
12
 * AdwSwipeable:
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
13
 *
14
 * An interface for swipeable widgets.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
15
 *
16
17
 * The `AdwSwipeable` interface is implemented by all swipeable widgets.
 *
18
 * See [class@SwipeTracker] for details about implementing it.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
19
 *
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
20
 * Since: 1.0
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
21
22
 */

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
23
G_DEFINE_INTERFACE (AdwSwipeable, adw_swipeable, GTK_TYPE_WIDGET)
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
24
25

static void
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
26
adw_swipeable_default_init (AdwSwipeableInterface *iface)
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
27
28
{
}
29
30

/**
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
31
 * adw_swipeable_get_distance:
Maximiliano's avatar
Maximiliano committed
32
 * @self: a swipeable
33
34
 *
 * Gets the swipe distance of @self.
35
 *
36
 * This corresponds to how many pixels 1 unit represents.
37
38
39
40
41
 *
 * Returns: the swipe distance in pixels
 *
 * Since: 1.0
 */
Christopher Davis's avatar
Christopher Davis committed
42
double
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
43
adw_swipeable_get_distance (AdwSwipeable *self)
44
{
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
45
  AdwSwipeableInterface *iface;
46

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
47
  g_return_val_if_fail (ADW_IS_SWIPEABLE (self), 0);
48

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
49
  iface = ADW_SWIPEABLE_GET_IFACE (self);
50
51
  g_return_val_if_fail (iface->get_distance != NULL, 0);

52
  return iface->get_distance (self);
53
54
55
}

/**
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
56
 * adw_swipeable_get_snap_points: (virtual get_snap_points)
Maximiliano's avatar
Maximiliano committed
57
 * @self: a swipeable
58
 * @n_snap_points: (out): location to return the number of the snap points
59
 *
60
 * Gets the snap points of @self.
61
 *
62
63
64
65
 * Each snap point represents a progress value that is considered acceptable to
 * end the swipe on.
 *
 * Returns: (array length=n_snap_points) (transfer full): the snap points
66
67
68
 *
 * Since: 1.0
 */
Christopher Davis's avatar
Christopher Davis committed
69
double *
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
70
adw_swipeable_get_snap_points (AdwSwipeable *self,
Christopher Davis's avatar
Christopher Davis committed
71
                               int          *n_snap_points)
72
{
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
73
  AdwSwipeableInterface *iface;
74

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
75
  g_return_val_if_fail (ADW_IS_SWIPEABLE (self), NULL);
76

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
77
  iface = ADW_SWIPEABLE_GET_IFACE (self);
78
  g_return_val_if_fail (iface->get_snap_points != NULL, NULL);
79

80
  return iface->get_snap_points (self, n_snap_points);
81
82
83
}

/**
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
84
 * adw_swipeable_get_progress:
Maximiliano's avatar
Maximiliano committed
85
 * @self: a swipeable
86
 *
87
 * Gets the current progress of @self.
88
89
90
91
92
 *
 * Returns: the current progress, unitless
 *
 * Since: 1.0
 */
Christopher Davis's avatar
Christopher Davis committed
93
double
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
94
adw_swipeable_get_progress (AdwSwipeable *self)
95
{
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
96
  AdwSwipeableInterface *iface;
97

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
98
  g_return_val_if_fail (ADW_IS_SWIPEABLE (self), 0);
99

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
100
  iface = ADW_SWIPEABLE_GET_IFACE (self);
101
102
  g_return_val_if_fail (iface->get_progress != NULL, 0);

103
  return iface->get_progress (self);
104
105
106
}

/**
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
107
 * adw_swipeable_get_cancel_progress:
Maximiliano's avatar
Maximiliano committed
108
 * @self: a swipeable
109
110
111
112
113
114
115
 *
 * Gets the progress @self will snap back to after the gesture is canceled.
 *
 * Returns: the cancel progress, unitless
 *
 * Since: 1.0
 */
Christopher Davis's avatar
Christopher Davis committed
116
double
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
117
adw_swipeable_get_cancel_progress (AdwSwipeable *self)
118
{
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
119
  AdwSwipeableInterface *iface;
120

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
121
  g_return_val_if_fail (ADW_IS_SWIPEABLE (self), 0);
122

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
123
  iface = ADW_SWIPEABLE_GET_IFACE (self);
124
125
  g_return_val_if_fail (iface->get_cancel_progress != NULL, 0);

126
  return iface->get_cancel_progress (self);
127
}
128
129

/**
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
130
 * adw_swipeable_get_swipe_area:
Maximiliano's avatar
Maximiliano committed
131
 * @self: a swipeable
132
133
 * @navigation_direction: the direction of the swipe
 * @is_drag: whether the swipe is caused by a dragging gesture
134
 * @rect: (out): a pointer to a rectangle to store the swipe area
135
 *
136
137
 * Gets the area @self can start a swipe from for the given direction and
 * gesture type.
138
 *
139
140
141
 * This can be used to restrict swipes to only be possible from a certain area,
 * for example, to only allow edge swipes, or to have a draggable element and
 * ignore swipes elsewhere.
142
143
144
145
146
147
148
 *
 * If not implemented, the default implementation returns the allocation of
 * @self, allowing swipes from anywhere.
 *
 * Since: 1.0
 */
void
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
149
150
adw_swipeable_get_swipe_area (AdwSwipeable           *self,
                              AdwNavigationDirection  navigation_direction,
151
152
                              gboolean                is_drag,
                              GdkRectangle           *rect)
153
{
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
154
  AdwSwipeableInterface *iface;
155

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
156
  g_return_if_fail (ADW_IS_SWIPEABLE (self));
157
158
  g_return_if_fail (rect != NULL);

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
159
  iface = ADW_SWIPEABLE_GET_IFACE (self);
160
161

  if (iface->get_swipe_area) {
162
    iface->get_swipe_area (self, navigation_direction, is_drag, rect);
163
164
165
166
167
    return;
  }

  rect->x = 0;
  rect->y = 0;
168
169
  rect->width = gtk_widget_get_width (GTK_WIDGET (self));
  rect->height = gtk_widget_get_height (GTK_WIDGET (self));
170
}