hdy-preferences-window.c 25.9 KB
Newer Older
Adrien Plazas's avatar
Adrien Plazas committed
1
2
3
/*
 * Copyright (C) 2019 Purism SPC
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
Adrien Plazas's avatar
Adrien Plazas committed
5
6
7
8
9
10
11
 */

#include "config.h"
#include <glib/gi18n-lib.h>

#include "hdy-preferences-window.h"

12
#include "hdy-animation.h"
Adrien Plazas's avatar
Adrien Plazas committed
13
#include "hdy-action-row.h"
14
#include "hdy-deck.h"
Adrien Plazas's avatar
Adrien Plazas committed
15
16
17
18
#include "hdy-preferences-group-private.h"
#include "hdy-preferences-page-private.h"
#include "hdy-view-switcher.h"
#include "hdy-view-switcher-bar.h"
19
#include "hdy-view-switcher-title.h"
Adrien Plazas's avatar
Adrien Plazas committed
20
21

/**
22
 * HdyPreferencesWindow:
Adrien Plazas's avatar
Adrien Plazas committed
23
 *
24
25
26
 * A window to present an application's preferences.
 *
 * The `HdyPreferencesWindow` widget presents an application's preferences
Adrien Plazas's avatar
Adrien Plazas committed
27
28
 * gathered into pages and groups. The preferences are searchable by the user.
 *
29
 * ## CSS nodes
30
 *
31
32
 * `HdyPreferencesWindow` has a main CSS node with the name `window` and the
 * style class `.preferences`.
33
 *
34
 * Since: 1.0
Adrien Plazas's avatar
Adrien Plazas committed
35
36
37
38
 */

typedef struct
{
39
40
  HdyDeck *subpages_deck;
  GtkWidget *preferences;
Adrien Plazas's avatar
Adrien Plazas committed
41
42
43
44
45
46
47
48
  GtkStack *content_stack;
  GtkStack *pages_stack;
  GtkToggleButton *search_button;
  GtkSearchEntry *search_entry;
  GtkListBox *search_results;
  GtkStack *search_stack;
  GtkStack *title_stack;
  HdyViewSwitcherBar *view_switcher_bar;
49
  HdyViewSwitcherTitle *view_switcher_title;
Adrien Plazas's avatar
Adrien Plazas committed
50

51
  gboolean search_enabled;
52
  gboolean can_swipe_back;
Adrien Plazas's avatar
Adrien Plazas committed
53
  gint n_last_search_results;
54
  GtkWidget *subpage;
Adrien Plazas's avatar
Adrien Plazas committed
55
56
} HdyPreferencesWindowPrivate;

57
G_DEFINE_TYPE_WITH_PRIVATE (HdyPreferencesWindow, hdy_preferences_window, HDY_TYPE_WINDOW)
Adrien Plazas's avatar
Adrien Plazas committed
58

59
60
61
enum {
  PROP_0,
  PROP_SEARCH_ENABLED,
62
  PROP_CAN_SWIPE_BACK,
63
64
65
66
67
  LAST_PROP,
};

static GParamSpec *props[LAST_PROP];

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
100
101
102
103
104
105
106
107
108
109
/* Copied and modified from gtklabel.c, separate_uline_pattern() */
static gchar *
strip_mnemonic (const gchar *src)
{
  g_autofree gchar *new_str = g_new (gchar, strlen (src) + 1);
  gchar *dest = new_str;
  gboolean underscore = FALSE;

  while (*src) {
    gunichar c;
    const gchar *next_src;

    c = g_utf8_get_char (src);
    if (c == (gunichar) -1) {
      g_warning ("Invalid input string");

      return NULL;
    }

    next_src = g_utf8_next_char (src);

    if (underscore) {
      while (src < next_src)
        *dest++ = *src++;

      underscore = FALSE;
    } else {
      if (c == '_'){
        underscore = TRUE;
        src = next_src;
      } else {
        while (src < next_src)
          *dest++ = *src++;
      }
    }
  }

  *dest = 0;

  return g_steal_pointer (&new_str);
}

Adrien Plazas's avatar
Adrien Plazas committed
110
111
112
113
114
115
static gboolean
filter_search_results (HdyActionRow         *row,
                       HdyPreferencesWindow *self)
{
  HdyPreferencesWindowPrivate *priv = hdy_preferences_window_get_instance_private (self);
  g_autofree gchar *text = g_utf8_casefold (gtk_entry_get_text (GTK_ENTRY (priv->search_entry)), -1);
116
  g_autofree gchar *title = g_utf8_casefold (hdy_preferences_row_get_title (HDY_PREFERENCES_ROW (row)), -1);
Adrien Plazas's avatar
Adrien Plazas committed
117
118
  g_autofree gchar *subtitle = NULL;

119
120
121
122
123
  /* The CSS engine works in such a way that invisible children are treated as
   * visible widgets, which breaks the expectations of the .preferences  style
   * class when filtering a row, leading to straight corners when the first row
   * or last row are filtered out.
   *
Adrien Plazas's avatar
Adrien Plazas committed
124
   * This works around it by explicitly toggling the row's visibility, while
125
126
   * keeping GtkListBox's filtering logic.
   *
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
127
   * See https://gitlab.gnome.org/GNOME/libhandy/-/merge_requests/424
128
129
   */

130
131
132
133
134
135
136
137
138
139
  if (hdy_preferences_row_get_use_underline (HDY_PREFERENCES_ROW (row))) {
    gchar *stripped_title = strip_mnemonic (title);

    if (stripped_title) {
      g_free (title);
      title = stripped_title;
    }
  }

  if (!!strstr (title, text)) {
Adrien Plazas's avatar
Adrien Plazas committed
140
    priv->n_last_search_results++;
141
    gtk_widget_show (GTK_WIDGET (row));
Adrien Plazas's avatar
Adrien Plazas committed
142
143
144
145
146
147
148
149

    return TRUE;
  }

  subtitle = g_utf8_casefold (hdy_action_row_get_subtitle (row), -1);

  if (!!strstr (subtitle, text)) {
    priv->n_last_search_results++;
150
    gtk_widget_show (GTK_WIDGET (row));
Adrien Plazas's avatar
Adrien Plazas committed
151
152
153
154

    return TRUE;
  }

155
156
  gtk_widget_hide (GTK_WIDGET (row));

Adrien Plazas's avatar
Adrien Plazas committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  return FALSE;
}

static GtkWidget *
new_search_row_for_preference (HdyPreferencesRow    *row,
                               HdyPreferencesWindow *self)
{
  HdyPreferencesWindowPrivate *priv = hdy_preferences_window_get_instance_private (self);
  HdyActionRow *widget;
  HdyPreferencesGroup *group;
  HdyPreferencesPage *page;
  const gchar *group_title, *page_title;
  GtkWidget *parent;

  g_assert (HDY_IS_PREFERENCES_ROW (row));

Ujjwal Kumar's avatar
Ujjwal Kumar committed
173
  widget = HDY_ACTION_ROW (hdy_action_row_new ());
174
  gtk_list_box_row_set_activatable (GTK_LIST_BOX_ROW (widget), TRUE);
Adrien Plazas's avatar
Adrien Plazas committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
  g_object_bind_property (row, "title", widget, "title", G_BINDING_SYNC_CREATE);
  g_object_bind_property (row, "use-underline", widget, "use-underline", G_BINDING_SYNC_CREATE);

  for (parent = gtk_widget_get_parent (GTK_WIDGET (row));
       parent != NULL && !HDY_IS_PREFERENCES_GROUP (parent);
       parent = gtk_widget_get_parent (parent));
  group = parent != NULL ? HDY_PREFERENCES_GROUP (parent) : NULL;
  group_title = group != NULL ? hdy_preferences_group_get_title (group) : NULL;
  if (g_strcmp0 (group_title, "") == 0)
    group_title = NULL;

  for (parent = gtk_widget_get_parent (GTK_WIDGET (group));
       parent != NULL && !HDY_IS_PREFERENCES_PAGE (parent);
       parent = gtk_widget_get_parent (parent));
  page = parent != NULL ? HDY_PREFERENCES_PAGE (parent) : NULL;
  page_title = page != NULL ? hdy_preferences_page_get_title (page) : NULL;
  if (g_strcmp0 (page_title, "") == 0)
    page_title = NULL;

194
  if (group_title && !hdy_view_switcher_title_get_title_visible (priv->view_switcher_title))
Adrien Plazas's avatar
Adrien Plazas committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
    hdy_action_row_set_subtitle (widget, group_title);
  if (group_title) {
    g_autofree gchar *subtitle = g_strdup_printf ("%s → %s", page_title != NULL ? page_title : _("Untitled page"), group_title);
    hdy_action_row_set_subtitle (widget, subtitle);
  } else if (page_title)
    hdy_action_row_set_subtitle (widget, page_title);

  gtk_widget_show (GTK_WIDGET (widget));

  g_object_set_data (G_OBJECT (widget), "page", page);
  g_object_set_data (G_OBJECT (widget), "row", row);

  return GTK_WIDGET (widget);
}

static void
update_search_results (HdyPreferencesWindow *self)
{
  HdyPreferencesWindowPrivate *priv = hdy_preferences_window_get_instance_private (self);
  g_autoptr (GListStore) model;

  model = g_list_store_new (HDY_TYPE_PREFERENCES_ROW);
  gtk_container_foreach (GTK_CONTAINER (priv->pages_stack), (GtkCallback) hdy_preferences_page_add_preferences_to_model, model);
  gtk_container_foreach (GTK_CONTAINER (priv->search_results), (GtkCallback) gtk_widget_destroy, NULL);
  for (guint i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (model)); i++)
    gtk_container_add (GTK_CONTAINER (priv->search_results),
                       new_search_row_for_preference ((HdyPreferencesRow *) g_list_model_get_item (G_LIST_MODEL (model), i), self));
}

static void
225
226
search_result_activated_cb (HdyPreferencesWindow *self,
                            HdyActionRow         *widget)
Adrien Plazas's avatar
Adrien Plazas committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
{
  HdyPreferencesWindowPrivate *priv = hdy_preferences_window_get_instance_private (self);
  HdyPreferencesPage *page;
  HdyPreferencesRow *row;

  gtk_toggle_button_set_active (priv->search_button, FALSE);
  page = HDY_PREFERENCES_PAGE (g_object_get_data (G_OBJECT (widget), "page"));
  row = HDY_PREFERENCES_ROW (g_object_get_data (G_OBJECT (widget), "row"));

  g_assert (page != NULL);
  g_assert (row != NULL);

  gtk_stack_set_visible_child (priv->pages_stack, GTK_WIDGET (page));
  gtk_widget_set_can_focus (GTK_WIDGET (row), TRUE);
  gtk_widget_grab_focus (GTK_WIDGET (row));
}

static gboolean
245
246
247
key_press_event_cb (GtkWidget            *sender,
                    GdkEvent             *event,
                    HdyPreferencesWindow *self)
Adrien Plazas's avatar
Adrien Plazas committed
248
249
250
251
252
{
  HdyPreferencesWindowPrivate *priv = hdy_preferences_window_get_instance_private (self);
  GdkModifierType default_modifiers = gtk_accelerator_get_default_mod_mask ();
  guint keyval;
  GdkModifierType state;
253
254
  GdkKeymap *keymap;
  GdkEventKey *key_event = (GdkEventKey *) event;
Adrien Plazas's avatar
Adrien Plazas committed
255

256
  gdk_event_get_state (event, &state);
Adrien Plazas's avatar
Adrien Plazas committed
257

258
259
260
261
262
263
264
265
  keymap = gdk_keymap_get_for_display (gtk_widget_get_display (sender));

  gdk_keymap_translate_keyboard_state (keymap,
                                       key_event->hardware_keycode,
                                       state,
                                       key_event->group,
                                       &keyval, NULL, NULL, NULL);

266
267
268
269
270
271
272
273
274
275
276
  if (priv->subpage) {
    if (keyval == GDK_KEY_Escape &&
        hdy_preferences_window_get_can_swipe_back (self)) {
      hdy_preferences_window_close_subpage (self);

      return GDK_EVENT_STOP;
    }

    return GDK_EVENT_PROPAGATE;
  }

277
278
  if (priv->search_enabled &&
      (keyval == GDK_KEY_f || keyval == GDK_KEY_F) &&
Adrien Plazas's avatar
Adrien Plazas committed
279
280
281
      (state & default_modifiers) == GDK_CONTROL_MASK) {
    gtk_toggle_button_set_active (priv->search_button, TRUE);

282
    return GDK_EVENT_STOP;
Adrien Plazas's avatar
Adrien Plazas committed
283
284
  }

285
  if (priv->search_enabled &&
286
      gtk_search_entry_handle_event (priv->search_entry, event)) {
Adrien Plazas's avatar
Adrien Plazas committed
287
288
    gtk_toggle_button_set_active (priv->search_button, TRUE);

289
    return GDK_EVENT_STOP;
Adrien Plazas's avatar
Adrien Plazas committed
290
291
  }

292
293
294
295
296
297
  if (keyval == GDK_KEY_Escape) {
    gtk_window_close (GTK_WINDOW (self));

    return GDK_EVENT_STOP;
  }

298
  return GDK_EVENT_PROPAGATE;
Adrien Plazas's avatar
Adrien Plazas committed
299
300
}

301
static void
302
try_remove_subpages (HdyPreferencesWindow *self)
303
304
305
306
307
308
{
  HdyPreferencesWindowPrivate *priv = hdy_preferences_window_get_instance_private (self);

  if (hdy_deck_get_transition_running (priv->subpages_deck))
    return;

309
310
311
  if (hdy_deck_get_visible_child (priv->subpages_deck) == priv->preferences)
    priv->subpage = NULL;

312
313
314
315
316
317
318
  for (GList *child = gtk_container_get_children (GTK_CONTAINER (priv->subpages_deck));
       child;
       child = child->next)
    if (child->data != priv->preferences && child->data != priv->subpage)
      gtk_container_remove (GTK_CONTAINER (priv->subpages_deck), child->data);
}

319
320
321
322
323
324
325
326
327
328
329
330
static void
subpages_deck_transition_running_cb (HdyPreferencesWindow *self)
{
  try_remove_subpages (self);
}

static void
subpages_deck_visible_child_cb (HdyPreferencesWindow *self)
{
  try_remove_subpages (self);
}

Adrien Plazas's avatar
Adrien Plazas committed
331
static void
332
333
header_bar_size_allocate_cb (HdyPreferencesWindow *self,
                             GdkRectangle         *allocation)
Adrien Plazas's avatar
Adrien Plazas committed
334
335
336
{
  HdyPreferencesWindowPrivate *priv = hdy_preferences_window_get_instance_private (self);

337
  hdy_view_switcher_title_set_view_switcher_enabled (priv->view_switcher_title, allocation->width > 360);
Adrien Plazas's avatar
Adrien Plazas committed
338
339
}

340
341
342
343
344
345
static void
title_stack_notify_transition_running_cb (HdyPreferencesWindow *self)
{
  HdyPreferencesWindowPrivate *priv = hdy_preferences_window_get_instance_private (self);

  if (gtk_stack_get_transition_running (priv->title_stack) ||
346
      gtk_stack_get_visible_child (priv->title_stack) != GTK_WIDGET (priv->view_switcher_title))
347
348
349
350
351
352
353
354
355
356
357
    return;

  gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
}

static void
title_stack_notify_visible_child_cb (HdyPreferencesWindow *self)
{
  HdyPreferencesWindowPrivate *priv = hdy_preferences_window_get_instance_private (self);

  if (hdy_get_enable_animations (GTK_WIDGET (priv->title_stack)) ||
358
      gtk_stack_get_visible_child (priv->title_stack) != GTK_WIDGET (priv->view_switcher_title))
359
360
361
362
363
364
    return;

  gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
}


Adrien Plazas's avatar
Adrien Plazas committed
365
static void
366
search_button_notify_active_cb (HdyPreferencesWindow *self)
Adrien Plazas's avatar
Adrien Plazas committed
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
{
  HdyPreferencesWindowPrivate *priv = hdy_preferences_window_get_instance_private (self);

  if (gtk_toggle_button_get_active (priv->search_button)) {
    update_search_results (self);
    gtk_stack_set_visible_child_name (priv->title_stack, "search");
    gtk_stack_set_visible_child_name (priv->content_stack, "search");
    gtk_entry_grab_focus_without_selecting (GTK_ENTRY (priv->search_entry));
    /* Grabbing without selecting puts the cursor at the start of the buffer, so
     * for "type to search" to work we must move the cursor at the end. We can't
     * use GTK_MOVEMENT_BUFFER_ENDS because it causes a sound to be played.
     */
    g_signal_emit_by_name (priv->search_entry, "move-cursor",
                           GTK_MOVEMENT_LOGICAL_POSITIONS, G_MAXINT, FALSE, NULL);
  } else {
    gtk_stack_set_visible_child_name (priv->title_stack, "pages");
    gtk_stack_set_visible_child_name (priv->content_stack, "pages");
  }
}

static void
388
search_changed_cb (HdyPreferencesWindow *self)
Adrien Plazas's avatar
Adrien Plazas committed
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
{
  HdyPreferencesWindowPrivate *priv = hdy_preferences_window_get_instance_private (self);

  priv->n_last_search_results = 0;
  gtk_list_box_invalidate_filter (priv->search_results);
  gtk_stack_set_visible_child_name (priv->search_stack,
                                    priv->n_last_search_results > 0 ? "results" : "no-results");
}

static void
on_page_icon_name_changed (HdyPreferencesPage   *page,
                           GParamSpec           *pspec,
                           HdyPreferencesWindow *self)
{
  HdyPreferencesWindowPrivate *priv = hdy_preferences_window_get_instance_private (self);

  gtk_container_child_set (GTK_CONTAINER (priv->pages_stack), GTK_WIDGET (page),
                           "icon-name", hdy_preferences_page_get_icon_name (page),
                           NULL);
}

410
411
412
413
414
415
416
417
static void
stop_search_cb (HdyPreferencesWindow *self)
{
  HdyPreferencesWindowPrivate *priv = hdy_preferences_window_get_instance_private (self);

  gtk_toggle_button_set_active (priv->search_button, FALSE);
}

Adrien Plazas's avatar
Adrien Plazas committed
418
419
420
421
422
423
424
425
426
427
428
429
static void
on_page_title_changed (HdyPreferencesPage   *page,
                       GParamSpec           *pspec,
                       HdyPreferencesWindow *self)
{
  HdyPreferencesWindowPrivate *priv = hdy_preferences_window_get_instance_private (self);

  gtk_container_child_set (GTK_CONTAINER (priv->pages_stack), GTK_WIDGET (page),
                           "title", hdy_preferences_page_get_title (page),
                           NULL);
}

430
431
432
433
434
435
436
437
438
439
440
441
static void
hdy_preferences_window_get_property (GObject    *object,
                                     guint       prop_id,
                                     GValue     *value,
                                     GParamSpec *pspec)
{
  HdyPreferencesWindow *self = HDY_PREFERENCES_WINDOW (object);

  switch (prop_id) {
  case PROP_SEARCH_ENABLED:
    g_value_set_boolean (value, hdy_preferences_window_get_search_enabled (self));
    break;
442
443
444
  case PROP_CAN_SWIPE_BACK:
    g_value_set_boolean (value, hdy_preferences_window_get_can_swipe_back (self));
    break;
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

static void
hdy_preferences_window_set_property (GObject      *object,
                                     guint         prop_id,
                                     const GValue *value,
                                     GParamSpec   *pspec)
{
  HdyPreferencesWindow *self = HDY_PREFERENCES_WINDOW (object);

  switch (prop_id) {
  case PROP_SEARCH_ENABLED:
    hdy_preferences_window_set_search_enabled (self, g_value_get_boolean (value));
    break;
462
463
464
  case PROP_CAN_SWIPE_BACK:
    hdy_preferences_window_set_can_swipe_back (self, g_value_get_boolean (value));
    break;
465
466
467
468
469
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

Adrien Plazas's avatar
Adrien Plazas committed
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
static void
hdy_preferences_window_add (GtkContainer *container,
                            GtkWidget    *child)
{
  HdyPreferencesWindow *self = HDY_PREFERENCES_WINDOW (container);
  HdyPreferencesWindowPrivate *priv = hdy_preferences_window_get_instance_private (self);

  if (priv->content_stack == NULL)
    GTK_CONTAINER_CLASS (hdy_preferences_window_parent_class)->add (container, child);
  else if (HDY_IS_PREFERENCES_PAGE (child)) {
    gtk_container_add (GTK_CONTAINER (priv->pages_stack), child);
    on_page_icon_name_changed (HDY_PREFERENCES_PAGE (child), NULL, self);
    on_page_title_changed (HDY_PREFERENCES_PAGE (child), NULL, self);
    g_signal_connect (child, "notify::icon-name",
                      G_CALLBACK (on_page_icon_name_changed), self);
    g_signal_connect (child, "notify::title",
                      G_CALLBACK (on_page_title_changed), self);
  } else
    g_warning ("Can't add children of type %s to %s",
               G_OBJECT_TYPE_NAME (child),
               G_OBJECT_TYPE_NAME (container));
}

493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
static void
hdy_preferences_window_remove (GtkContainer *container,
                               GtkWidget    *child)
{
  HdyPreferencesWindow *self = HDY_PREFERENCES_WINDOW (container);
  HdyPreferencesWindowPrivate *priv = hdy_preferences_window_get_instance_private (self);

  if (child == GTK_WIDGET (priv->content_stack))
    GTK_CONTAINER_CLASS (hdy_preferences_window_parent_class)->remove (container, child);
  else
    gtk_container_remove (GTK_CONTAINER (priv->pages_stack), child);
}

static void
hdy_preferences_window_forall (GtkContainer *container,
                               gboolean      include_internals,
                               GtkCallback   callback,
                               gpointer      callback_data)
{
  HdyPreferencesWindow *self = HDY_PREFERENCES_WINDOW (container);
  HdyPreferencesWindowPrivate *priv = hdy_preferences_window_get_instance_private (self);

  if (include_internals)
    GTK_CONTAINER_CLASS (hdy_preferences_window_parent_class)->forall (container,
                                                                       include_internals,
                                                                       callback,
                                                                       callback_data);
  else if (priv->pages_stack)
    gtk_container_foreach (GTK_CONTAINER (priv->pages_stack), callback, callback_data);
}

Adrien Plazas's avatar
Adrien Plazas committed
524
525
526
static void
hdy_preferences_window_class_init (HdyPreferencesWindowClass *klass)
{
527
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
Adrien Plazas's avatar
Adrien Plazas committed
528
529
530
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);

531
532
533
  object_class->get_property = hdy_preferences_window_get_property;
  object_class->set_property = hdy_preferences_window_set_property;

Adrien Plazas's avatar
Adrien Plazas committed
534
  container_class->add = hdy_preferences_window_add;
535
536
  container_class->remove = hdy_preferences_window_remove;
  container_class->forall = hdy_preferences_window_forall;
Adrien Plazas's avatar
Adrien Plazas committed
537

538
  /**
539
   * HdyPreferencesWindow:search-enabled: (attributes org.gtk.Property.get=hdy_preferences_window_get_search_enabled org.gtk.Property.set=hdy_preferences_window_set_search_enabled)
540
541
542
543
544
545
546
547
548
549
550
551
   *
   * Whether search is enabled.
   *
   * Since: 1.0
   */
  props[PROP_SEARCH_ENABLED] =
    g_param_spec_boolean ("search-enabled",
                          _("Search enabled"),
                          _("Whether search is enabled"),
                          TRUE,
                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

552
  /**
553
   * HdyPreferencesWindow:can-swipe-back: (attributes org.gtk.Property.get=hdy_preferences_window_get_can_swipe_back org.gtk.Property.set=hdy_preferences_window_set_can_swipe_back)
554
   *
555
   * Whether the window allows closing the subpage via a swipe gesture.
556
557
558
559
560
561
562
563
564
565
   *
   * Since: 1.0
   */
  props[PROP_CAN_SWIPE_BACK] =
      g_param_spec_boolean ("can-swipe-back",
                            _("Can swipe back"),
                            _("Whether or not swipe gesture can be used to switch from a subpage to the preferences"),
                            FALSE,
                            G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);

566
567
  g_object_class_install_properties (object_class, LAST_PROP, props);

Adrien Plazas's avatar
Adrien Plazas committed
568
569
  gtk_widget_class_set_template_from_resource (widget_class,
                                               "/sm/puri/handy/ui/hdy-preferences-window.ui");
570
571
  gtk_widget_class_bind_template_child_private (widget_class, HdyPreferencesWindow, subpages_deck);
  gtk_widget_class_bind_template_child_private (widget_class, HdyPreferencesWindow, preferences);
Adrien Plazas's avatar
Adrien Plazas committed
572
573
574
575
576
577
578
579
  gtk_widget_class_bind_template_child_private (widget_class, HdyPreferencesWindow, content_stack);
  gtk_widget_class_bind_template_child_private (widget_class, HdyPreferencesWindow, pages_stack);
  gtk_widget_class_bind_template_child_private (widget_class, HdyPreferencesWindow, search_button);
  gtk_widget_class_bind_template_child_private (widget_class, HdyPreferencesWindow, search_entry);
  gtk_widget_class_bind_template_child_private (widget_class, HdyPreferencesWindow, search_results);
  gtk_widget_class_bind_template_child_private (widget_class, HdyPreferencesWindow, search_stack);
  gtk_widget_class_bind_template_child_private (widget_class, HdyPreferencesWindow, title_stack);
  gtk_widget_class_bind_template_child_private (widget_class, HdyPreferencesWindow, view_switcher_bar);
580
  gtk_widget_class_bind_template_child_private (widget_class, HdyPreferencesWindow, view_switcher_title);
581
  gtk_widget_class_bind_template_callback (widget_class, subpages_deck_transition_running_cb);
582
  gtk_widget_class_bind_template_callback (widget_class, subpages_deck_visible_child_cb);
583
  gtk_widget_class_bind_template_callback (widget_class, header_bar_size_allocate_cb);
584
585
  gtk_widget_class_bind_template_callback (widget_class, title_stack_notify_transition_running_cb);
  gtk_widget_class_bind_template_callback (widget_class, title_stack_notify_visible_child_cb);
586
587
588
589
  gtk_widget_class_bind_template_callback (widget_class, key_press_event_cb);
  gtk_widget_class_bind_template_callback (widget_class, search_button_notify_active_cb);
  gtk_widget_class_bind_template_callback (widget_class, search_changed_cb);
  gtk_widget_class_bind_template_callback (widget_class, search_result_activated_cb);
590
  gtk_widget_class_bind_template_callback (widget_class, stop_search_cb);
Adrien Plazas's avatar
Adrien Plazas committed
591
592
593
594
595
596
597
}

static void
hdy_preferences_window_init (HdyPreferencesWindow *self)
{
  HdyPreferencesWindowPrivate *priv = hdy_preferences_window_get_instance_private (self);

598
599
  priv->search_enabled = TRUE;

Adrien Plazas's avatar
Adrien Plazas committed
600
601
602
603
604
605
606
607
  gtk_widget_init_template (GTK_WIDGET (self));

  gtk_list_box_set_filter_func (priv->search_results, (GtkListBoxFilterFunc) filter_search_results, self, NULL);
}

/**
 * hdy_preferences_window_new:
 *
608
 * Creates a new `HdyPreferencesWindow`.
Adrien Plazas's avatar
Adrien Plazas committed
609
 *
610
 * Returns: the newly created `HdyPreferencesWindow`
Adrien Plazas's avatar
Adrien Plazas committed
611
 *
612
 * Since: 1.0
Adrien Plazas's avatar
Adrien Plazas committed
613
 */
Ujjwal Kumar's avatar
Ujjwal Kumar committed
614
GtkWidget *
Adrien Plazas's avatar
Adrien Plazas committed
615
616
617
618
hdy_preferences_window_new (void)
{
  return g_object_new (HDY_TYPE_PREFERENCES_WINDOW, NULL);
}
619
620

/**
621
622
 * hdy_preferences_window_get_search_enabled: (attributes org.gtk.Method.get_property=search-enabled)
 * @self: a preferences window
623
624
625
 *
 * Gets whether search is enabled for @self.
 *
626
 * Returns: whether search is enabled for @self
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
 *
 * Since: 1.0
 */
gboolean
hdy_preferences_window_get_search_enabled (HdyPreferencesWindow *self)
{
  HdyPreferencesWindowPrivate *priv;

  g_return_val_if_fail (HDY_IS_PREFERENCES_WINDOW (self), FALSE);

  priv = hdy_preferences_window_get_instance_private (self);

  return priv->search_enabled;
}

/**
643
644
645
 * hdy_preferences_window_set_search_enabled: (attributes org.gtk.Method.set_property=search-enabled)
 * @self: a preferences window
 * @search_enabled: `TRUE` to enable search, `FALSE` to disable it
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
 *
 * Sets whether search is enabled for @self.
 *
 * Since: 1.0
 */
void
hdy_preferences_window_set_search_enabled (HdyPreferencesWindow *self,
                                           gboolean              search_enabled)
{
  HdyPreferencesWindowPrivate *priv;

  g_return_if_fail (HDY_IS_PREFERENCES_WINDOW (self));

  priv = hdy_preferences_window_get_instance_private (self);

  search_enabled = !!search_enabled;

  if (priv->search_enabled == search_enabled)
    return;

  priv->search_enabled = search_enabled;
  gtk_widget_set_visible (GTK_WIDGET (priv->search_button), search_enabled);
  if (!search_enabled)
    gtk_toggle_button_set_active (priv->search_button, FALSE);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_SEARCH_ENABLED]);
}
673

674
/**
675
676
 * hdy_preferences_window_set_can_swipe_back: (attributes org.gtk.Method.set_property=can-swipe-back)
 * @self: a preferences window
677
678
 * @can_swipe_back: the new value
 *
679
680
 * Sets whether swipe gestures allow switching from a subpage to the
 * preferences.
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
 *
 * Since: 1.0
 */
void
hdy_preferences_window_set_can_swipe_back (HdyPreferencesWindow *self,
                                           gboolean              can_swipe_back)
{
  HdyPreferencesWindowPrivate *priv;

  g_return_if_fail (HDY_IS_PREFERENCES_WINDOW (self));

  priv = hdy_preferences_window_get_instance_private (self);

  can_swipe_back = !!can_swipe_back;

  if (priv->can_swipe_back == can_swipe_back)
    return;

  priv->can_swipe_back = can_swipe_back;

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_CAN_SWIPE_BACK]);
}

/**
705
706
 * hdy_preferences_window_get_can_swipe_back: (attributes org.gtk.Method.get_property=can-swipe-back)
 * @self: a preferences window
707
 *
708
709
 * Gets whether swipe gestures allow switching from a subpage to the
 * preferences.
710
 *
711
 * Returns: `TRUE` if back swipe is enabled
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
 *
 * Since: 1.0
 */
gboolean
hdy_preferences_window_get_can_swipe_back (HdyPreferencesWindow *self)
{
  HdyPreferencesWindowPrivate *priv;

  g_return_val_if_fail (HDY_IS_PREFERENCES_WINDOW (self), FALSE);

  priv = hdy_preferences_window_get_instance_private (self);

  return priv->can_swipe_back;
}

727
728
/**
 * hdy_preferences_window_present_subpage:
729
 * @self: a preferences window
730
731
 * @subpage: the subpage
 *
732
733
 * Sets @subpage as the window's subpage and opens it.
 *
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
 * The transition can be cancelled by the user, in which case visible child will
 * change back to the previously visible child.
 *
 * Since: 1.0
 */
void
hdy_preferences_window_present_subpage (HdyPreferencesWindow *self,
                                        GtkWidget            *subpage)
{
  HdyPreferencesWindowPrivate *priv;

  g_return_if_fail (HDY_IS_PREFERENCES_WINDOW (self));
  g_return_if_fail (GTK_IS_WIDGET (subpage));

  priv = hdy_preferences_window_get_instance_private (self);

  if (priv->subpage == subpage)
    return;

  priv->subpage = subpage;

  /* The check below avoids a warning when re-entering a subpage during the
   * transition between the that subpage to the preferences.
   */
  if (gtk_widget_get_parent (subpage) != GTK_WIDGET (priv->subpages_deck))
    gtk_container_add (GTK_CONTAINER (priv->subpages_deck), subpage);

  hdy_deck_set_visible_child (priv->subpages_deck, subpage);
}

/**
 * hdy_preferences_window_close_subpage:
766
767
768
 * @self: a preferences window
 *
 * Closes the current subpage.
769
 *
770
 * If there is no presented subpage, this does nothing.
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
 *
 * Since: 1.0
 */
void
hdy_preferences_window_close_subpage (HdyPreferencesWindow *self)
{
  HdyPreferencesWindowPrivate *priv;

  g_return_if_fail (HDY_IS_PREFERENCES_WINDOW (self));

  priv = hdy_preferences_window_get_instance_private (self);

  if (priv->subpage == NULL)
    return;

  hdy_deck_set_visible_child (priv->subpages_deck, priv->preferences);
}