hdy-swipe-group.c 16 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
9
10
 */

#include "config.h"

#include "hdy-swipe-group.h"
#include <gtk/gtk.h>
11
#include "hdy-navigation-direction.h"
12
#include "hdy-swipe-tracker-private.h"
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
13
14
15
16
17
18

#define BUILDABLE_TAG_OBJECT "object"
#define BUILDABLE_TAG_SWIPEABLE "swipeable"
#define BUILDABLE_TAG_SWIPEABLES "swipeables"
#define BUILDABLE_TAG_TEMPLATE "template"

19
20
G_GNUC_BEGIN_IGNORE_DEPRECATIONS

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
21
/**
22
 * HdySwipeGroup:
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
23
 *
24
25
26
27
28
 * An object for syncing swipeable widgets.
 *
 * The `HdySwipeGroup` object can be used to sync multiple swipeable widgets
 * that implement the [iface@Swipeable] interface, such as [class@Carousel], so
 * that animating one of them also animates all the other widgets in the group.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
29
30
31
32
 *
 * This can be useful for syncing widgets between a window's titlebar and
 * content area.
 *
33
 * ## HdySwipeGroup as GtkBuildable
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
34
 *
35
 * `HdySwipeGroup` can be created in an UI definition. The list of swipeable
36
37
38
 * widgets is specified with a &lt;swipeables&gt; element containing multiple
 * &lt;swipeable&gt; elements with their ”name” attribute specifying the id of
 * the widgets.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
39
 *
40
 * ```xml
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
41
42
 * <object class="HdySwipeGroup">
 *   <swipeables>
43
44
 *     <swipeable name="carousel1"/>
 *     <swipeable name="carousel2"/>
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
45
46
 *   </swipeables>
 * </object>
47
 * ```
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
48
 *
49
50
51
 * `HdySwipeGroup` has been deprecated, [class@Window] and
 * [class@ApplicationWindow] allow using a single leaflet for both content and
 * header bar, without the need to sync them.
52
 *
53
 * Since: 1.0
54
55
 *
 * Deprecated: 1.4
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
56
57
58
59
60
61
62
63
 */

struct _HdySwipeGroup
{
  GObject parent_instance;

  GSList *swipeables;
  HdySwipeable *current;
64
  gboolean block;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
};

static void hdy_swipe_group_buildable_init (GtkBuildableIface *iface);

G_DEFINE_TYPE_WITH_CODE (HdySwipeGroup, hdy_swipe_group, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                         hdy_swipe_group_buildable_init))

static gboolean
contains (HdySwipeGroup *self,
          HdySwipeable  *swipeable)
{
  GSList *swipeables;

  for (swipeables = self->swipeables; swipeables != NULL; swipeables = swipeables->next)
    if (swipeables->data == swipeable)
      return TRUE;

  return FALSE;
}

static void
swipeable_destroyed (HdySwipeGroup *self,
                     HdySwipeable  *swipeable)
{
  g_return_if_fail (HDY_IS_SWIPE_GROUP (self));

  self->swipeables = g_slist_remove (self->swipeables, swipeable);

  g_object_unref (self);
}

/**
 * hdy_swipe_group_new:
 *
100
 * Creates a new `HdySwipeGroup`.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
101
 *
102
 * Returns: the newly created `HdySwipeGroup`
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
103
 *
104
 * Since: 1.0
105
106
 *
 * Deprecated: 1.4
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
107
108
109
110
111
112
113
114
 */
HdySwipeGroup *
hdy_swipe_group_new (void)
{
  return g_object_new (HDY_TYPE_SWIPE_GROUP, NULL);
}

static void
115
child_switched_cb (HdySwipeGroup *self,
116
                   guint          index,
117
118
                   gint64         duration,
                   HdySwipeable  *swipeable)
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
119
120
121
{
  GSList *swipeables;

122
123
124
  if (self->block)
    return;

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
125
126
127
  if (self->current != NULL && self->current != swipeable)
    return;

128
129
  self->block = TRUE;

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
130
131
132
  for (swipeables = self->swipeables; swipeables != NULL; swipeables = swipeables->next)
    if (swipeables->data != swipeable)
      hdy_swipeable_switch_child (swipeables->data, index, duration);
133
134

  self->block = FALSE;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
135
136
137
}

static void
138
begin_swipe_cb (HdySwipeGroup          *self,
139
                HdyNavigationDirection  direction,
140
141
                gboolean                direct,
                HdySwipeTracker        *tracker)
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
142
{
143
  HdySwipeable *swipeable;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
144
145
  GSList *swipeables;

146
147
148
  if (self->block)
    return;

149
150
  swipeable = hdy_swipe_tracker_get_swipeable (tracker);

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
151
152
153
154
155
  if (self->current != NULL && self->current != swipeable)
    return;

  self->current = swipeable;

156
157
  self->block = TRUE;

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
158
159
  for (swipeables = self->swipeables; swipeables != NULL; swipeables = swipeables->next)
    if (swipeables->data != swipeable)
160
      hdy_swipe_tracker_emit_begin_swipe (hdy_swipeable_get_swipe_tracker (swipeables->data),
161
                                          direction, FALSE);
162
163

  self->block = FALSE;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
164
165
166
}

static void
167
168
169
update_swipe_cb (HdySwipeGroup   *self,
                 gdouble          progress,
                 HdySwipeTracker *tracker)
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
170
{
171
  HdySwipeable *swipeable;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
172
173
  GSList *swipeables;

174
175
176
  if (self->block)
    return;

177
178
  swipeable = hdy_swipe_tracker_get_swipeable (tracker);

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
179
180
181
  if (swipeable != self->current)
    return;

182
183
  self->block = TRUE;

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
184
185
  for (swipeables = self->swipeables; swipeables != NULL; swipeables = swipeables->next)
    if (swipeables->data != swipeable)
186
      hdy_swipe_tracker_emit_update_swipe (hdy_swipeable_get_swipe_tracker (swipeables->data),
187
                                           progress);
188
189

  self->block = FALSE;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
190
191
192
}

static void
193
194
195
196
end_swipe_cb (HdySwipeGroup   *self,
              gint64           duration,
              gdouble          to,
              HdySwipeTracker *tracker)
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
197
{
198
  HdySwipeable *swipeable;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
199
200
  GSList *swipeables;

201
202
203
  if (self->block)
    return;

204
205
  swipeable = hdy_swipe_tracker_get_swipeable (tracker);

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
206
207
208
  if (swipeable != self->current)
    return;

209
210
  self->block = TRUE;

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
211
212
  for (swipeables = self->swipeables; swipeables != NULL; swipeables = swipeables->next)
    if (swipeables->data != swipeable)
213
      hdy_swipe_tracker_emit_end_swipe (hdy_swipeable_get_swipe_tracker (swipeables->data),
214
                                        duration, to);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
215
216

  self->current = NULL;
217
218

  self->block = FALSE;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
219
220
221
222
}

/**
 * hdy_swipe_group_add_swipeable:
223
224
225
226
 * @self: a swipe group
 * @swipeable: the [iface@Swipeable] to add
 *
 * Adds a swipeable to @self.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
227
 *
228
229
 * When the widget is destroyed or no longer referenced elsewhere, it will be
 * removed from the swipe group.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
230
 *
231
 * Since: 1.0
232
233
 *
 * Deprecated: 1.4
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
234
235
236
237
238
 */
void
hdy_swipe_group_add_swipeable (HdySwipeGroup *self,
                               HdySwipeable  *swipeable)
{
239
240
  HdySwipeTracker *tracker;

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
241
242
243
  g_return_if_fail (HDY_IS_SWIPE_GROUP (self));
  g_return_if_fail (HDY_IS_SWIPEABLE (swipeable));

244
  tracker = hdy_swipeable_get_swipe_tracker (swipeable);
245
246
247

  g_return_if_fail (HDY_IS_SWIPE_TRACKER (tracker));

248
  g_signal_connect_swapped (swipeable, "child-switched", G_CALLBACK (child_switched_cb), self);
249
250
251
  g_signal_connect_swapped (tracker, "begin-swipe", G_CALLBACK (begin_swipe_cb), self);
  g_signal_connect_swapped (tracker, "update-swipe", G_CALLBACK (update_swipe_cb), self);
  g_signal_connect_swapped (tracker, "end-swipe", G_CALLBACK (end_swipe_cb), self);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
252
253
254
255
256
257
258
259
260
261
262

  self->swipeables = g_slist_prepend (self->swipeables, swipeable);

  g_object_ref (self);

  g_signal_connect_swapped (swipeable, "destroy", G_CALLBACK (swipeable_destroyed), self);
}


/**
 * hdy_swipe_group_remove_swipeable:
263
264
 * @self: a swipe group
 * @swipeable: the [iface@Swipeable] to remove
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
265
 *
266
 * Removes a widget from a [class@SwipeGroup].
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
267
 *
268
 * Since: 1.0
269
270
 *
 * Deprecated: 1.4
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
271
272
273
274
275
 **/
void
hdy_swipe_group_remove_swipeable (HdySwipeGroup *self,
                                  HdySwipeable  *swipeable)
{
276
277
  HdySwipeTracker *tracker;

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
278
279
280
281
  g_return_if_fail (HDY_IS_SWIPE_GROUP (self));
  g_return_if_fail (HDY_IS_SWIPEABLE (swipeable));
  g_return_if_fail (contains (self, swipeable));

282
  tracker = hdy_swipeable_get_swipe_tracker (swipeable);
283

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
284
285
286
  self->swipeables = g_slist_remove (self->swipeables, swipeable);

  g_signal_handlers_disconnect_by_data (swipeable, self);
287
  g_signal_handlers_disconnect_by_data (tracker, self);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
288
289
290
291
292
293
294

  g_object_unref (self);
}


/**
 * hdy_swipe_group_get_swipeables:
295
 * @self: a swipe group
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
296
 *
297
 * Gets the list of swipeables associated with @self.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
298
 *
299
 * Returns: (element-type HdySwipeable) (transfer none): a list of swipeables
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
300
 *
301
 * Since: 1.0
302
303
 *
 * Deprecated: 1.4
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
 **/
GSList *
hdy_swipe_group_get_swipeables (HdySwipeGroup *self)
{
  g_return_val_if_fail (HDY_IS_SWIPE_GROUP (self), NULL);

  return self->swipeables;
}

typedef struct {
  gchar *name;
  gint line;
  gint col;
} ItemData;

static void
item_data_free (gpointer data)
{
  ItemData *item_data = data;

  g_free (item_data->name);
  g_free (item_data);
}

typedef struct {
  GObject *object;
  GtkBuilder *builder;
  GSList *items;
} GSListSubParserData;

static void
hdy_swipe_group_dispose (GObject *object)
{
  HdySwipeGroup *self = (HdySwipeGroup *)object;

  g_slist_free_full (self->swipeables, (GDestroyNotify) g_object_unref);
  self->swipeables = NULL;

  G_OBJECT_CLASS (hdy_swipe_group_parent_class)->dispose (object);
}

/*< private >
346
347
 * @builder: a builder
 * @context: the markup parse context
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
348
349
350
 * @parent_name: the name of the expected parent element
 * @error: return location for an error
 *
351
352
 * Checks that the parent element of the currently handled start tag is
 * @parent_name and set @error if it isn't.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
353
 *
354
355
 * This is intended to be called in start_element vfuncs to ensure that element
 * nesting is as intended.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
356
 *
357
 * Returns: whether @parent_name is the parent element
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
 */
/* This has been copied and modified from gtkbuilder.c. */
static gboolean
_gtk_builder_check_parent (GtkBuilder           *builder,
                           GMarkupParseContext  *context,
                           const gchar          *parent_name,
                           GError              **error)
{
  const GSList *stack;
  gint line, col;
  const gchar *parent;
  const gchar *element;

  stack = g_markup_parse_context_get_element_stack (context);

  element = (const gchar *)stack->data;
  parent = stack->next ? (const gchar *)stack->next->data : "";

  if (g_str_equal (parent_name, parent) ||
      (g_str_equal (parent_name, BUILDABLE_TAG_OBJECT) &&
       g_str_equal (parent, BUILDABLE_TAG_TEMPLATE)))
    return TRUE;

  g_markup_parse_context_get_position (context, &line, &col);
  g_set_error (error,
               GTK_BUILDER_ERROR,
               GTK_BUILDER_ERROR_INVALID_TAG,
               ".:%d:%d Can't use <%s> here",
               line, col, element);

  return FALSE;
}

/*< private >
 * _gtk_builder_prefix_error:
393
394
 * @builder: a builder
 * @context: the markup parse context
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
395
396
 * @error: an error
 *
397
398
399
 * Calls g_prefix_error() to prepend a filename:line:column marker to the given
 * error. The filename is taken from @builder, and the line and column are
 * obtained by calling g_markup_parse_context_get_position().
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
 *
 * This is intended to be called on errors returned by
 * g_markup_collect_attributes() in a start_element vfunc.
 */
/* This has been copied and modified from gtkbuilder.c. */
static void
_gtk_builder_prefix_error (GtkBuilder           *builder,
                           GMarkupParseContext  *context,
                           GError              **error)
{
  gint line, col;

  g_markup_parse_context_get_position (context, &line, &col);
  g_prefix_error (error, ".:%d:%d ", line, col);
}

/*< private >
 * _gtk_builder_error_unhandled_tag:
418
419
 * @builder: a builder
 * @context: the [GLib.MarkupParseContext]
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
420
421
422
423
 * @object: name of the object that is being handled
 * @element_name: name of the element whose start tag is being handled
 * @error: return location for the error
 *
424
425
 * Sets @error to a suitable error indicating that an @element_name tag is not
 * expected in the custom markup for @object.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
 *
 * This is intended to be called in a start_element vfunc.
 */
/* This has been copied and modified from gtkbuilder.c. */
static void
_gtk_builder_error_unhandled_tag (GtkBuilder           *builder,
                                  GMarkupParseContext  *context,
                                  const gchar          *object,
                                  const gchar          *element_name,
                                  GError              **error)
{
  gint line, col;

  g_markup_parse_context_get_position (context, &line, &col);
  g_set_error (error,
               GTK_BUILDER_ERROR,
               GTK_BUILDER_ERROR_UNHANDLED_TAG,
               ".:%d:%d Unsupported tag for %s: <%s>",
               line, col,
               object, element_name);
}

/* This has been copied and modified from gtksizegroup.c. */
static void
swipe_group_start_element (GMarkupParseContext  *context,
                           const gchar          *element_name,
                           const gchar         **names,
                           const gchar         **values,
                           gpointer              user_data,
                           GError              **error)
{
  GSListSubParserData *data = (GSListSubParserData*)user_data;

  if (strcmp (element_name, BUILDABLE_TAG_SWIPEABLE) == 0)
    {
      const gchar *name;
      ItemData *item_data;

      if (!_gtk_builder_check_parent (data->builder, context, BUILDABLE_TAG_SWIPEABLES, error))
        return;

      if (!g_markup_collect_attributes (element_name, names, values, error,
                                        G_MARKUP_COLLECT_STRING, "name", &name,
                                        G_MARKUP_COLLECT_INVALID))
        {
          _gtk_builder_prefix_error (data->builder, context, error);
          return;
        }

      item_data = g_new (ItemData, 1);
      item_data->name = g_strdup (name);
      g_markup_parse_context_get_position (context, &item_data->line, &item_data->col);
      data->items = g_slist_prepend (data->items, item_data);
    }
  else if (strcmp (element_name, BUILDABLE_TAG_SWIPEABLES) == 0)
    {
      if (!_gtk_builder_check_parent (data->builder, context, BUILDABLE_TAG_OBJECT, error))
        return;

      if (!g_markup_collect_attributes (element_name, names, values, error,
                                        G_MARKUP_COLLECT_INVALID, NULL, NULL,
                                        G_MARKUP_COLLECT_INVALID))
        _gtk_builder_prefix_error (data->builder, context, error);
    }
  else
    {
      _gtk_builder_error_unhandled_tag (data->builder, context,
                                        "HdySwipeGroup", element_name,
                                        error);
    }
}


/* This has been copied and modified from gtksizegroup.c. */
static const GMarkupParser swipe_group_parser =
  {
502
503
504
505
506
    swipe_group_start_element,
    NULL,
    NULL,
    NULL,
    NULL
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
  };

/* This has been copied and modified from gtksizegroup.c. */
static gboolean
hdy_swipe_group_buildable_custom_tag_start (GtkBuildable  *buildable,
                                            GtkBuilder    *builder,
                                            GObject       *child,
                                            const gchar   *tagname,
                                            GMarkupParser *parser,
                                            gpointer      *parser_data)
{
  GSListSubParserData *data;

  if (child)
    return FALSE;

  if (strcmp (tagname, BUILDABLE_TAG_SWIPEABLES) == 0)
    {
      data = g_slice_new0 (GSListSubParserData);
      data->items = NULL;
      data->object = G_OBJECT (buildable);
      data->builder = builder;

      *parser = swipe_group_parser;
      *parser_data = data;

      return TRUE;
    }

  return FALSE;
}

/* This has been copied and modified from gtksizegroup.c. */
static void
hdy_swipe_group_buildable_custom_finished (GtkBuildable *buildable,
                                           GtkBuilder   *builder,
                                           GObject      *child,
                                           const gchar  *tagname,
                                           gpointer      user_data)
{
  GSList *l;
  GSListSubParserData *data;
  GObject *object;

  if (strcmp (tagname, BUILDABLE_TAG_SWIPEABLES) != 0)
    return;

  data = (GSListSubParserData*)user_data;
  data->items = g_slist_reverse (data->items);

  for (l = data->items; l; l = l->next)
    {
      ItemData *item_data = l->data;
      object = gtk_builder_get_object (builder, item_data->name);
      if (!object)
        continue;
      hdy_swipe_group_add_swipeable (HDY_SWIPE_GROUP (data->object), HDY_SWIPEABLE (object));
    }
  g_slist_free_full (data->items, item_data_free);
  g_slice_free (GSListSubParserData, data);
}

static void
hdy_swipe_group_class_init (HdySwipeGroupClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->dispose = hdy_swipe_group_dispose;
}

static void
hdy_swipe_group_init (HdySwipeGroup *self)
{
}

static void
hdy_swipe_group_buildable_init (GtkBuildableIface *iface)
{
  iface->custom_tag_start = hdy_swipe_group_buildable_custom_tag_start;
  iface->custom_finished = hdy_swipe_group_buildable_custom_finished;
}
588
589

G_GNUC_END_IGNORE_DEPRECATIONS