gtkshortcutswindow.c 31.1 KB
Newer Older
Matthias Clasen's avatar
Matthias Clasen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* gtkshortcutswindow.c
 *
 * Copyright (C) 2015 Christian Hergert <christian@hergert.me>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public License as
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

21
#include "gtkshortcutswindowprivate.h"
Benjamin Otte's avatar
Benjamin Otte committed
22
23

#include "gtkbox.h"
24
#include "gtkbuildable.h"
Benjamin Otte's avatar
Benjamin Otte committed
25
26
27
28
29
#include "gtkgrid.h"
#include "gtkheaderbar.h"
#include "gtkintl.h"
#include "gtklabel.h"
#include "gtklistbox.h"
30
#include "gtkmain.h"
Benjamin Otte's avatar
Benjamin Otte committed
31
32
33
#include "gtkmenubutton.h"
#include "gtkpopover.h"
#include "gtkprivate.h"
Matthias Clasen's avatar
Matthias Clasen committed
34
#include "gtkscrolledwindow.h"
Benjamin Otte's avatar
Benjamin Otte committed
35
36
#include "gtksearchbar.h"
#include "gtksearchentry.h"
Matthias Clasen's avatar
Matthias Clasen committed
37
38
#include "gtkshortcutssection.h"
#include "gtkshortcutsgroup.h"
39
#include "gtkshortcutsshortcutprivate.h"
Benjamin Otte's avatar
Benjamin Otte committed
40
41
42
43
#include "gtksizegroup.h"
#include "gtkstack.h"
#include "gtktogglebutton.h"
#include "gtktypebuiltins.h"
Matthias Clasen's avatar
Matthias Clasen committed
44
#include "gtkwidgetprivate.h"
Matthias Clasen's avatar
Matthias Clasen committed
45
46

/**
47
 * GtkShortcutsWindow:
Matthias Clasen's avatar
Matthias Clasen committed
48
 *
49
50
51
52
53
 * A `GtkShortcutsWindow` shows information about the keyboard shortcuts
 * and gestures of an application.
 *
 * The shortcuts can be grouped, and you can have multiple sections in this
 * window, corresponding to the major modes of your application.
Matthias Clasen's avatar
Matthias Clasen committed
54
55
56
57
 *
 * Additionally, the shortcuts can be filtered by the current view, to avoid
 * showing information that is not relevant in the current application context.
 *
58
59
60
61
 * The recommended way to construct a `GtkShortcutsWindow` is with
 * [class@Gtk.Builder], by populating a `GtkShortcutsWindow` with one or
 * more `GtkShortcutsSection` objects, which contain `GtkShortcutsGroups`
 * that in turn contain objects of class `GtkShortcutsShortcut`.
Matthias Clasen's avatar
Matthias Clasen committed
62
63
64
65
66
67
68
69
70
 *
 * # A simple example:
 *
 * ![](gedit-shortcuts.png)
 *
 * This example has as single section. As you can see, the shortcut groups
 * are arranged in columns, and spread across several pages if there are too
 * many to find on a single page.
 *
71
 * The .ui file for this example can be found [here](https://gitlab.gnome.org/GNOME/gtk/tree/master/demos/gtk-demo/shortcuts-gedit.ui).
Matthias Clasen's avatar
Matthias Clasen committed
72
73
74
75
76
 *
 * # An example with multiple views:
 *
 * ![](clocks-shortcuts.png)
 *
77
 * This example shows a `GtkShortcutsWindow` that has been configured to show only
Matthias Clasen's avatar
Matthias Clasen committed
78
79
 * the shortcuts relevant to the "stopwatch" view.
 *
80
 * The .ui file for this example can be found [here](https://gitlab.gnome.org/GNOME/gtk/tree/master/demos/gtk-demo/shortcuts-clocks.ui).
Matthias Clasen's avatar
Matthias Clasen committed
81
82
83
84
85
 *
 * # An example with multiple sections:
 *
 * ![](builder-shortcuts.png)
 *
86
 * This example shows a `GtkShortcutsWindow` with two sections, "Editor Shortcuts"
Matthias Clasen's avatar
Matthias Clasen committed
87
88
 * and "Terminal Shortcuts".
 *
89
 * The .ui file for this example can be found [here](https://gitlab.gnome.org/GNOME/gtk/tree/master/demos/gtk-demo/shortcuts-builder.ui).
Matthias Clasen's avatar
Matthias Clasen committed
90
91
 */

92
struct _GtkShortcutsWindow
93
{
94
  GtkWindow       parent_instance;
95

Matthias Clasen's avatar
Matthias Clasen committed
96
  GHashTable     *keywords;
Benjamin Otte's avatar
Benjamin Otte committed
97
98
99
  char           *initial_section;
  char           *last_section_name;
  char           *view_name;
Matthias Clasen's avatar
Matthias Clasen committed
100
101
102
103
104
105
106
107
108
109
  GtkSizeGroup   *search_text_group;
  GtkSizeGroup   *search_image_group;
  GHashTable     *search_items_hash;

  GtkStack       *stack;
  GtkStack       *title_stack;
  GtkMenuButton  *menu_button;
  GtkSearchBar   *search_bar;
  GtkSearchEntry *search_entry;
  GtkHeaderBar   *header_bar;
110
  GtkWidget      *main_box;
Matthias Clasen's avatar
Matthias Clasen committed
111
112
113
114
  GtkPopover     *popover;
  GtkListBox     *list_box;
  GtkBox         *search_gestures;
  GtkBox         *search_shortcuts;
115
116
117

  GtkWindow      *window;
  gulong          keys_changed_id;
118
119
120
121
122
123
124
125
126
};

typedef struct
{
  GtkWindowClass parent_class;

  void (*close)  (GtkShortcutsWindow *self);
  void (*search) (GtkShortcutsWindow *self);
} GtkShortcutsWindowClass;
Matthias Clasen's avatar
Matthias Clasen committed
127
128
129
130
131
132

typedef struct
{
  GtkShortcutsWindow *self;
  GtkBuilder        *builder;
  GQueue            *stack;
Benjamin Otte's avatar
Benjamin Otte committed
133
  char              *property_name;
Matthias Clasen's avatar
Matthias Clasen committed
134
135
136
  guint              translatable : 1;
} ViewsParserData;

137
static void gtk_shortcuts_window_buildable_iface_init (GtkBuildableIface *iface);
Matthias Clasen's avatar
Matthias Clasen committed
138

139
140
141
142

G_DEFINE_TYPE_WITH_CODE (GtkShortcutsWindow, gtk_shortcuts_window, GTK_TYPE_WINDOW,
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                                                gtk_shortcuts_window_buildable_iface_init))
Matthias Clasen's avatar
Matthias Clasen committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161


enum {
  CLOSE,
  SEARCH,
  LAST_SIGNAL
};

enum {
  PROP_0,
  PROP_SECTION_NAME,
  PROP_VIEW_NAME,
  LAST_PROP
};

static GParamSpec *properties[LAST_PROP];
static guint signals[LAST_SIGNAL];


162
163
static gboolean
more_than_three_children (GtkWidget *widget)
Matthias Clasen's avatar
Matthias Clasen committed
164
{
165
166
  GtkWidget *child;
  int i;
Matthias Clasen's avatar
Matthias Clasen committed
167

168
169
170
171
172
173
174
  for (child = gtk_widget_get_first_child (widget), i = 0;
       child != NULL;
       child = gtk_widget_get_next_sibling (child), i++)
    {
      if (i == 3)
        return TRUE;
    }
Matthias Clasen's avatar
Matthias Clasen committed
175

176
  return FALSE;
Matthias Clasen's avatar
Matthias Clasen committed
177
178
179
180
181
182
183
}

static void
update_title_stack (GtkShortcutsWindow *self)
{
  GtkWidget *visible_child;

184
  visible_child = gtk_stack_get_visible_child (self->stack);
Matthias Clasen's avatar
Matthias Clasen committed
185
186
187

  if (GTK_IS_SHORTCUTS_SECTION (visible_child))
    {
188
      if (more_than_three_children (GTK_WIDGET (self->stack)))
Matthias Clasen's avatar
Matthias Clasen committed
189
        {
190
          char *title;
Matthias Clasen's avatar
Matthias Clasen committed
191

192
          gtk_stack_set_visible_child_name (self->title_stack, "sections");
Matthias Clasen's avatar
Matthias Clasen committed
193
          g_object_get (visible_child, "title", &title, NULL);
194
          gtk_menu_button_set_label (self->menu_button, title);
Matthias Clasen's avatar
Matthias Clasen committed
195
196
197
198
          g_free (title);
        }
      else
        {
199
          gtk_stack_set_visible_child_name (self->title_stack, "title");
Matthias Clasen's avatar
Matthias Clasen committed
200
201
202
203
        }
    }
  else if (visible_child != NULL)
    {
204
      gtk_stack_set_visible_child_name (self->title_stack, "search");
Matthias Clasen's avatar
Matthias Clasen committed
205
206
207
208
209
210
211
212
    }
}

static void
gtk_shortcuts_window_add_search_item (GtkWidget *child, gpointer data)
{
  GtkShortcutsWindow *self = data;
  GtkWidget *item;
Benjamin Otte's avatar
Benjamin Otte committed
213
214
215
  char *accelerator = NULL;
  char *title = NULL;
  char *hash_key = NULL;
216
217
218
  GIcon *icon = NULL;
  gboolean icon_set = FALSE;
  gboolean subtitle_set = FALSE;
219
  GtkTextDirection direction;
220
  GtkShortcutType shortcut_type;
Benjamin Otte's avatar
Benjamin Otte committed
221
222
223
224
  char *action_name = NULL;
  char *subtitle;
  char *str;
  char *keywords;
Matthias Clasen's avatar
Matthias Clasen committed
225
226
227

  if (GTK_IS_SHORTCUTS_SHORTCUT (child))
    {
228
229
230
      GEnumClass *class;
      GEnumValue *value;

Matthias Clasen's avatar
Matthias Clasen committed
231
232
233
      g_object_get (child,
                    "accelerator", &accelerator,
                    "title", &title,
234
                    "direction", &direction,
235
236
237
                    "icon-set", &icon_set,
                    "subtitle-set", &subtitle_set,
                    "shortcut-type", &shortcut_type,
238
                    "action-name", &action_name,
Matthias Clasen's avatar
Matthias Clasen committed
239
240
                    NULL);

241
242
243
244
245
246
247
      class = G_ENUM_CLASS (g_type_class_ref (GTK_TYPE_SHORTCUT_TYPE));
      value = g_enum_get_value (class, shortcut_type);

      hash_key = g_strdup_printf ("%s-%s-%s", title, value->value_nick, accelerator);

      g_type_class_unref (class);

248
      if (g_hash_table_contains (self->search_items_hash, hash_key))
Matthias Clasen's avatar
Matthias Clasen committed
249
250
251
252
253
254
255
        {
          g_free (hash_key);
          g_free (title);
          g_free (accelerator);
          return;
        }

256
      g_hash_table_insert (self->search_items_hash, hash_key, GINT_TO_POINTER (1));
Matthias Clasen's avatar
Matthias Clasen committed
257
258
259
260

      item = g_object_new (GTK_TYPE_SHORTCUTS_SHORTCUT,
                           "accelerator", accelerator,
                           "title", title,
261
                           "direction", direction,
262
                           "shortcut-type", shortcut_type,
263
264
                           "accel-size-group", self->search_image_group,
                           "title-size-group", self->search_text_group,
265
                           "action-name", action_name,
Matthias Clasen's avatar
Matthias Clasen committed
266
                           NULL);
267
268
269
270
271
272
273
274
275
276
277
278
      if (icon_set)
        {
          g_object_get (child, "icon", &icon, NULL);
          g_object_set (item, "icon", icon, NULL);
          g_clear_object (&icon);
        }
      if (subtitle_set)
        {
          g_object_get (child, "subtitle", &subtitle, NULL);
          g_object_set (item, "subtitle", subtitle, NULL);
          g_free (subtitle);
        }
Matthias Clasen's avatar
Matthias Clasen committed
279
280
281
      str = g_strdup_printf ("%s %s", accelerator, title);
      keywords = g_utf8_strdown (str, -1);

282
      g_hash_table_insert (self->keywords, item, keywords);
283
      if (shortcut_type == GTK_SHORTCUT_ACCELERATOR)
284
        gtk_box_append (GTK_BOX (self->search_shortcuts), item);
285
      else
286
        gtk_box_append (GTK_BOX (self->search_gestures), item);
Matthias Clasen's avatar
Matthias Clasen committed
287
288
289
290

      g_free (title);
      g_free (accelerator);
      g_free (str);
291
      g_free (action_name);
Matthias Clasen's avatar
Matthias Clasen committed
292
    }
293
  else
Matthias Clasen's avatar
Matthias Clasen committed
294
    {
295
296
297
298
299
300
      GtkWidget *widget;

      for (widget = gtk_widget_get_first_child (child);
           widget != NULL;
           widget = gtk_widget_get_next_sibling (widget))
        gtk_shortcuts_window_add_search_item (widget, self);
Matthias Clasen's avatar
Matthias Clasen committed
301
302
303
    }
}

304
305
306
307
308
309
310
311
312
static void
section_notify_cb (GObject    *section,
                   GParamSpec *pspec,
                   gpointer    data)
{
  GtkShortcutsWindow *self = data;

  if (strcmp (pspec->name, "section-name") == 0)
    {
Benjamin Otte's avatar
Benjamin Otte committed
313
      char *name;
314
315

      g_object_get (section, "section-name", &name, NULL);
316
      g_object_set (gtk_stack_get_page (self->stack, GTK_WIDGET (section)), "name", name, NULL);
317
318
319
320
      g_free (name);
    }
  else if (strcmp (pspec->name, "title") == 0)
    {
Benjamin Otte's avatar
Benjamin Otte committed
321
      char *title;
322
323
324
325
326
327
328
329
330
      GtkWidget *label;

      label = g_object_get_data (section, "gtk-shortcuts-title");
      g_object_get (section, "title", &title, NULL);
      gtk_label_set_label (GTK_LABEL (label), title);
      g_free (title);
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
331
332
333
334
335
static void
gtk_shortcuts_window_add_section (GtkShortcutsWindow  *self,
                                  GtkShortcutsSection *section)
{
  GtkListBoxRow *row;
Benjamin Otte's avatar
Benjamin Otte committed
336
337
338
  char *title;
  char *name;
  const char *visible_section;
Matthias Clasen's avatar
Matthias Clasen committed
339
  GtkWidget *label;
340
  GtkWidget *child;
Matthias Clasen's avatar
Matthias Clasen committed
341

342
343
344
345
  for (child = gtk_widget_get_first_child (GTK_WIDGET (section));
       child != NULL;
       child = gtk_widget_get_next_sibling (child))
    gtk_shortcuts_window_add_search_item (child, self);
Matthias Clasen's avatar
Matthias Clasen committed
346
347
348
349
350
351

  g_object_get (section,
                "section-name", &name,
                "title", &title,
                NULL);

352
353
  g_signal_connect (section, "notify", G_CALLBACK (section_notify_cb), self);

Matthias Clasen's avatar
Matthias Clasen committed
354
355
356
  if (name == NULL)
    name = g_strdup ("shortcuts");

357
  gtk_stack_add_titled (self->stack, GTK_WIDGET (section), name, title);
Matthias Clasen's avatar
Matthias Clasen committed
358

359
  visible_section = gtk_stack_get_visible_child_name (self->stack);
Matthias Clasen's avatar
Matthias Clasen committed
360
  if (strcmp (visible_section, "internal-search") == 0 ||
361
362
      (self->initial_section && strcmp (self->initial_section, visible_section) == 0))
    gtk_stack_set_visible_child (self->stack, GTK_WIDGET (section));
Matthias Clasen's avatar
Matthias Clasen committed
363
364
365

  row = g_object_new (GTK_TYPE_LIST_BOX_ROW,
                      NULL);
366
  g_object_set_data (G_OBJECT (row), "gtk-shortcuts-section", section);
Matthias Clasen's avatar
Matthias Clasen committed
367
  label = g_object_new (GTK_TYPE_LABEL,
Matthias Clasen's avatar
Matthias Clasen committed
368
369
370
371
                        "margin-start", 6,
                        "margin-end", 6,
                        "margin-top", 6,
                        "margin-bottom", 6,
Matthias Clasen's avatar
Matthias Clasen committed
372
373
374
                        "label", title,
                        "xalign", 0.5f,
                        NULL);
375
  g_object_set_data (G_OBJECT (section), "gtk-shortcuts-title", label);
376
  gtk_list_box_row_set_child (GTK_LIST_BOX_ROW (row), GTK_WIDGET (label));
377
  gtk_list_box_insert (GTK_LIST_BOX (self->list_box), GTK_WIDGET (row), -1);
Matthias Clasen's avatar
Matthias Clasen committed
378
379
380
381
382
383
384

  update_title_stack (self);

  g_free (name);
  g_free (title);
}

385
static GtkBuildableIface *parent_buildable_iface;
Matthias Clasen's avatar
Matthias Clasen committed
386

387
static void
388
389
390
gtk_shortcuts_window_buildable_add_child (GtkBuildable *buildable,
                                          GtkBuilder   *builder,
                                          GObject      *child,
Benjamin Otte's avatar
Benjamin Otte committed
391
                                          const char   *type)
392
393
394
395
{
  if (GTK_IS_SHORTCUTS_SECTION (child))
    gtk_shortcuts_window_add_section (GTK_SHORTCUTS_WINDOW (buildable),
                                      GTK_SHORTCUTS_SECTION (child));
396
  else
397
    parent_buildable_iface->add_child (buildable, builder, child, type);
398
399
400
}

static void
401
gtk_shortcuts_window_buildable_iface_init (GtkBuildableIface *iface)
402
{
403
  parent_buildable_iface = g_type_interface_peek_parent (iface);
404

405
  iface->add_child = gtk_shortcuts_window_buildable_add_child;
406
407
}

Matthias Clasen's avatar
Matthias Clasen committed
408
409
static void
gtk_shortcuts_window_set_view_name (GtkShortcutsWindow *self,
Benjamin Otte's avatar
Benjamin Otte committed
410
                                    const char         *view_name)
Matthias Clasen's avatar
Matthias Clasen committed
411
{
412
  GtkWidget *section;
Matthias Clasen's avatar
Matthias Clasen committed
413

414
415
  g_free (self->view_name);
  self->view_name = g_strdup (view_name);
Matthias Clasen's avatar
Matthias Clasen committed
416

417
  for (section = gtk_widget_get_first_child (GTK_WIDGET (self->stack));
418
419
       section != NULL;
       section = gtk_widget_get_next_sibling (section))
Matthias Clasen's avatar
Matthias Clasen committed
420
421
    {
      if (GTK_IS_SHORTCUTS_SECTION (section))
422
        g_object_set (section, "view-name", self->view_name, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
423
424
425
426
427
    }
}

static void
gtk_shortcuts_window_set_section_name (GtkShortcutsWindow *self,
Benjamin Otte's avatar
Benjamin Otte committed
428
                                       const char         *section_name)
Matthias Clasen's avatar
Matthias Clasen committed
429
{
430
  GtkWidget *section = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
431

432
433
  g_free (self->initial_section);
  self->initial_section = g_strdup (section_name);
Matthias Clasen's avatar
Matthias Clasen committed
434

435
  if (section_name)
436
    section = gtk_stack_get_child_by_name (self->stack, section_name);
Matthias Clasen's avatar
Matthias Clasen committed
437
  if (section)
438
    gtk_stack_set_visible_child (self->stack, section);
Matthias Clasen's avatar
Matthias Clasen committed
439
440
}

441
442
443
444
445
446
447
static void
update_accels_cb (GtkWidget *widget,
                  gpointer   data)
{
  GtkShortcutsWindow *self = data;

  if (GTK_IS_SHORTCUTS_SHORTCUT (widget))
448
    gtk_shortcuts_shortcut_update_accel (GTK_SHORTCUTS_SHORTCUT (widget), self->window);
449
450
451
452
453
454
455
456
457
  else
    {
      GtkWidget *child;

      for (child = gtk_widget_get_first_child (GTK_WIDGET (widget));
           child != NULL;
           child = gtk_widget_get_next_sibling (child ))
        update_accels_cb (child, self);
    }
458
459
460
461
462
}

static void
update_accels_for_actions (GtkShortcutsWindow *self)
{
463
  if (self->window)
464
465
466
467
468
469
470
471
    {
      GtkWidget *child;

      for (child = gtk_widget_get_first_child (GTK_WIDGET (self));
           child != NULL;
           child = gtk_widget_get_next_sibling (child))
        update_accels_cb (child, self);
    }
472
473
474
475
476
477
478
479
480
481
482
483
484
}

static void
keys_changed_handler (GtkWindow          *window,
                      GtkShortcutsWindow *self)
{
  update_accels_for_actions (self);
}

void
gtk_shortcuts_window_set_window (GtkShortcutsWindow *self,
                                 GtkWindow          *window)
{
485
  if (self->keys_changed_id)
486
    {
487
488
      g_signal_handler_disconnect (self->window, self->keys_changed_id);
      self->keys_changed_id = 0;
489
490
    }

491
  self->window = window;
492

493
494
  if (self->window)
    self->keys_changed_id = g_signal_connect (window, "keys-changed",
495
496
497
498
499
500
                                              G_CALLBACK (keys_changed_handler),
                                              self);

  update_accels_for_actions (self);
}

Matthias Clasen's avatar
Matthias Clasen committed
501
502
503
504
505
static void
gtk_shortcuts_window__list_box__row_activated (GtkShortcutsWindow *self,
                                               GtkListBoxRow      *row,
                                               GtkListBox         *list_box)
{
506
  GtkWidget *section;
Matthias Clasen's avatar
Matthias Clasen committed
507

508
  section = g_object_get_data (G_OBJECT (row), "gtk-shortcuts-section");
509
510
  gtk_stack_set_visible_child (self->stack, section);
  gtk_popover_popdown (self->popover);
Matthias Clasen's avatar
Matthias Clasen committed
511
512
}

513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
static gboolean
hidden_by_direction (GtkWidget *widget)
{
  if (GTK_IS_SHORTCUTS_SHORTCUT (widget))
    {
      GtkTextDirection dir;

      g_object_get (widget, "direction", &dir, NULL);
      if (dir != GTK_TEXT_DIR_NONE &&
          dir != gtk_widget_get_direction (widget))
        return TRUE;
    }

  return FALSE;
}

Matthias Clasen's avatar
Matthias Clasen committed
529
530
531
532
static void
gtk_shortcuts_window__entry__changed (GtkShortcutsWindow *self,
                                     GtkSearchEntry      *search_entry)
{
Benjamin Otte's avatar
Benjamin Otte committed
533
  char *downcase = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
534
  GHashTableIter iter;
Benjamin Otte's avatar
Benjamin Otte committed
535
536
  const char *text;
  const char *last_section_name;
Matthias Clasen's avatar
Matthias Clasen committed
537
538
539
540
  gpointer key;
  gpointer value;
  gboolean has_result;

541
  text = gtk_editable_get_text (GTK_EDITABLE (search_entry));
Matthias Clasen's avatar
Matthias Clasen committed
542
543
544

  if (!text || !*text)
    {
545
      if (self->last_section_name != NULL)
Matthias Clasen's avatar
Matthias Clasen committed
546
        {
547
          gtk_stack_set_visible_child_name (self->stack, self->last_section_name);
Matthias Clasen's avatar
Matthias Clasen committed
548
          return;
549

Matthias Clasen's avatar
Matthias Clasen committed
550
551
552
        }
    }

553
  last_section_name = gtk_stack_get_visible_child_name (self->stack);
Matthias Clasen's avatar
Matthias Clasen committed
554
555
556
557

  if (g_strcmp0 (last_section_name, "internal-search") != 0 &&
      g_strcmp0 (last_section_name, "no-search-results") != 0)
    {
558
559
      g_free (self->last_section_name);
      self->last_section_name = g_strdup (last_section_name);
Matthias Clasen's avatar
Matthias Clasen committed
560
561
562
563
    }

  downcase = g_utf8_strdown (text, -1);

564
  g_hash_table_iter_init (&iter, self->keywords);
Matthias Clasen's avatar
Matthias Clasen committed
565
566
567
568
569

  has_result = FALSE;
  while (g_hash_table_iter_next (&iter, &key, &value))
    {
      GtkWidget *widget = key;
Benjamin Otte's avatar
Benjamin Otte committed
570
      const char *keywords = value;
Matthias Clasen's avatar
Matthias Clasen committed
571
572
      gboolean match;

573
574
575
576
577
      if (hidden_by_direction (widget))
        match = FALSE;
      else
        match = strstr (keywords, downcase) != NULL;

Matthias Clasen's avatar
Matthias Clasen committed
578
579
580
581
582
583
584
      gtk_widget_set_visible (widget, match);
      has_result |= match;
    }

  g_free (downcase);

  if (has_result)
585
    gtk_stack_set_visible_child_name (self->stack, "internal-search");
Matthias Clasen's avatar
Matthias Clasen committed
586
  else
587
    gtk_stack_set_visible_child_name (self->stack, "no-search-results");
Matthias Clasen's avatar
Matthias Clasen committed
588
589
590
591
592
}

static void
gtk_shortcuts_window__search_mode__changed (GtkShortcutsWindow *self)
{
593
  if (!gtk_search_bar_get_search_mode (self->search_bar))
Matthias Clasen's avatar
Matthias Clasen committed
594
    {
595
596
      if (self->last_section_name != NULL)
        gtk_stack_set_visible_child_name (self->stack, self->last_section_name);
Matthias Clasen's avatar
Matthias Clasen committed
597
598
599
600
601
602
603
604
605
606
607
608
    }
}

static void
gtk_shortcuts_window_close (GtkShortcutsWindow *self)
{
  gtk_window_close (GTK_WINDOW (self));
}

static void
gtk_shortcuts_window_search (GtkShortcutsWindow *self)
{
609
  gtk_search_bar_set_search_mode (self->search_bar, TRUE);
Matthias Clasen's avatar
Matthias Clasen committed
610
611
612
613
614
615
616
617
618
}

static void
gtk_shortcuts_window_constructed (GObject *object)
{
  GtkShortcutsWindow *self = (GtkShortcutsWindow *)object;

  G_OBJECT_CLASS (gtk_shortcuts_window_parent_class)->constructed (object);

619
620
  if (self->initial_section != NULL)
    gtk_stack_set_visible_child_name (self->stack, self->initial_section);
Matthias Clasen's avatar
Matthias Clasen committed
621
622
623
624
625
626
627
}

static void
gtk_shortcuts_window_finalize (GObject *object)
{
  GtkShortcutsWindow *self = (GtkShortcutsWindow *)object;

628
629
630
631
632
  g_clear_pointer (&self->keywords, g_hash_table_unref);
  g_clear_pointer (&self->initial_section, g_free);
  g_clear_pointer (&self->view_name, g_free);
  g_clear_pointer (&self->last_section_name, g_free);
  g_clear_pointer (&self->search_items_hash, g_hash_table_unref);
Matthias Clasen's avatar
Matthias Clasen committed
633

634
635
  g_clear_object (&self->search_image_group);
  g_clear_object (&self->search_text_group);
Matthias Clasen's avatar
Matthias Clasen committed
636
637
638
639

  G_OBJECT_CLASS (gtk_shortcuts_window_parent_class)->finalize (object);
}

640
641
642
643
644
static void
gtk_shortcuts_window_dispose (GObject *object)
{
  GtkShortcutsWindow *self = (GtkShortcutsWindow *)object;

645
646
  if (self->stack)
    g_signal_handlers_disconnect_by_func (self->stack, G_CALLBACK (update_title_stack), self);
647

648
649
  gtk_shortcuts_window_set_window (self, NULL);

650
651
652
  self->stack = NULL;
  self->search_bar = NULL;
  self->main_box = NULL;
653
654

  G_OBJECT_CLASS (gtk_shortcuts_window_parent_class)->dispose (object);
655
656
}

Matthias Clasen's avatar
Matthias Clasen committed
657
658
659
660
661
662
663
664
665
666
667
668
static void
gtk_shortcuts_window_get_property (GObject    *object,
                                  guint       prop_id,
                                  GValue     *value,
                                  GParamSpec *pspec)
{
  GtkShortcutsWindow *self = (GtkShortcutsWindow *)object;

  switch (prop_id)
    {
    case PROP_SECTION_NAME:
      {
669
        GtkWidget *child = gtk_stack_get_visible_child (self->stack);
Matthias Clasen's avatar
Matthias Clasen committed
670
671
672

        if (child != NULL)
          {
Benjamin Otte's avatar
Benjamin Otte committed
673
            char *name = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
674

675
            g_object_get (gtk_stack_get_page (self->stack, child),
Matthias Clasen's avatar
Matthias Clasen committed
676
677
678
679
680
681
682
683
                                     "name", &name,
                                     NULL);
            g_value_take_string (value, name);
          }
      }
      break;

    case PROP_VIEW_NAME:
684
      g_value_set_string (value, self->view_name);
Matthias Clasen's avatar
Matthias Clasen committed
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gtk_shortcuts_window_set_property (GObject      *object,
                                  guint         prop_id,
                                  const GValue *value,
                                  GParamSpec   *pspec)
{
  GtkShortcutsWindow *self = (GtkShortcutsWindow *)object;

  switch (prop_id)
    {
    case PROP_SECTION_NAME:
      gtk_shortcuts_window_set_section_name (self, g_value_get_string (value));
      break;

    case PROP_VIEW_NAME:
      gtk_shortcuts_window_set_view_name (self, g_value_get_string (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gtk_shortcuts_window_unmap (GtkWidget *widget)
{
  GtkShortcutsWindow *self = (GtkShortcutsWindow *)widget;

720
721
  gtk_search_bar_set_search_mode (self->search_bar, FALSE);
  gtk_editable_set_text (GTK_EDITABLE (self->search_entry), "");
Matthias Clasen's avatar
Matthias Clasen committed
722
723
724
725
726
727
728
729
730
731
732
733
734
735

  GTK_WIDGET_CLASS (gtk_shortcuts_window_parent_class)->unmap (widget);
}

static void
gtk_shortcuts_window_class_init (GtkShortcutsWindowClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  object_class->constructed = gtk_shortcuts_window_constructed;
  object_class->finalize = gtk_shortcuts_window_finalize;
  object_class->get_property = gtk_shortcuts_window_get_property;
  object_class->set_property = gtk_shortcuts_window_set_property;
736
  object_class->dispose = gtk_shortcuts_window_dispose;
Matthias Clasen's avatar
Matthias Clasen committed
737
738
739
740
741
742
743
744
745
746
747

  widget_class->unmap = gtk_shortcuts_window_unmap;

  klass->close = gtk_shortcuts_window_close;
  klass->search = gtk_shortcuts_window_search;

  /**
   * GtkShortcutsWindow:section-name:
   *
   * The name of the section to show.
   *
748
   * This should be the section-name of one of the `GtkShortcutsSection`
Matthias Clasen's avatar
Matthias Clasen committed
749
750
751
752
   * objects that are in this shortcuts window.
   */
  properties[PROP_SECTION_NAME] =
    g_param_spec_string ("section-name", P_("Section Name"), P_("Section Name"),
753
                         "internal-search",
Matthias Clasen's avatar
Matthias Clasen committed
754
755
756
757
758
759
760
                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  /**
   * GtkShortcutsWindow:view-name:
   *
   * The view name by which to filter the contents.
   *
761
762
763
   * This should correspond to the [property@Gtk.ShortcutsGroup:view]
   * property of some of the [class@Gtk.ShortcutsGroup] objects that
   * are inside this shortcuts window.
Matthias Clasen's avatar
Matthias Clasen committed
764
765
766
767
768
769
770
771
772
773
774
775
776
   *
   * Set this to %NULL to show all groups.
   */
  properties[PROP_VIEW_NAME] =
    g_param_spec_string ("view-name", P_("View Name"), P_("View Name"),
                         NULL,
                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_properties (object_class, LAST_PROP, properties);

  /**
   * GtkShortcutsWindow::close:
   *
777
778
779
   * Emitted when the user uses a keybinding to close the window.
   *
   * This is a [keybinding signal](class.SignalAction.html).
Matthias Clasen's avatar
Matthias Clasen committed
780
781
782
783
784
785
786
787
788
789
790
791
792
793
   *
   * The default binding for this signal is the Escape key.
   */
  signals[CLOSE] = g_signal_new (I_("close"),
                                 G_TYPE_FROM_CLASS (klass),
                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                                 G_STRUCT_OFFSET (GtkShortcutsWindowClass, close),
                                 NULL, NULL, NULL,
                                 G_TYPE_NONE,
                                 0);

  /**
   * GtkShortcutsWindow::search:
   *
794
795
796
   * Emitted when the user uses a keybinding to start a search.
   *
   * This is a [keybinding signal](class.SignalAction.html).
Matthias Clasen's avatar
Matthias Clasen committed
797
798
799
800
801
802
803
804
805
806
807
   *
   * The default binding for this signal is Control-F.
   */
  signals[SEARCH] = g_signal_new (I_("search"),
                                 G_TYPE_FROM_CLASS (klass),
                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                                 G_STRUCT_OFFSET (GtkShortcutsWindowClass, search),
                                 NULL, NULL, NULL,
                                 G_TYPE_NONE,
                                 0);

808
809
810
811
812
813
814
815
  gtk_widget_class_add_binding_signal (widget_class,
                                       GDK_KEY_Escape, 0,
                                       "close",
                                       NULL);
  gtk_widget_class_add_binding_signal (widget_class,
                                       GDK_KEY_f, GDK_CONTROL_MASK,
                                       "search",
                                       NULL);
Matthias Clasen's avatar
Matthias Clasen committed
816
817
818
819
820
821
822
823

  g_type_ensure (GTK_TYPE_SHORTCUTS_GROUP);
  g_type_ensure (GTK_TYPE_SHORTCUTS_SHORTCUT);
}

static void
gtk_shortcuts_window_init (GtkShortcutsWindow *self)
{
824
  GtkWidget *search_button;
Matthias Clasen's avatar
Matthias Clasen committed
825
826
827
828
829
830
831
832
  GtkBox *box;
  GtkWidget *scroller;
  GtkWidget *label;
  GtkWidget *empty;
  PangoAttrList *attributes;

  gtk_window_set_resizable (GTK_WINDOW (self), FALSE);

833
834
  self->keywords = g_hash_table_new_full (NULL, NULL, NULL, g_free);
  self->search_items_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
835

836
837
  self->search_text_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
  self->search_image_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
Matthias Clasen's avatar
Matthias Clasen committed
838

839
840
  self->header_bar = GTK_HEADER_BAR (gtk_header_bar_new ());
  gtk_window_set_titlebar (GTK_WINDOW (self), GTK_WIDGET (self->header_bar));
Matthias Clasen's avatar
Matthias Clasen committed
841

842
843
844
  search_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
                                "icon-name", "edit-find-symbolic",
                                NULL);
845
  gtk_header_bar_pack_start (GTK_HEADER_BAR (self->header_bar), search_button);
Matthias Clasen's avatar
Matthias Clasen committed
846

847
  self->main_box = g_object_new (GTK_TYPE_BOX,
Matthias Clasen's avatar
Matthias Clasen committed
848
849
                           "orientation", GTK_ORIENTATION_VERTICAL,
                           NULL);
850
  gtk_window_set_child (GTK_WINDOW (self), self->main_box);
Matthias Clasen's avatar
Matthias Clasen committed
851

852
853
  self->search_bar = g_object_new (GTK_TYPE_SEARCH_BAR, NULL);
  g_object_bind_property (self->search_bar, "search-mode-enabled",
Matthias Clasen's avatar
Matthias Clasen committed
854
855
                          search_button, "active",
                          G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
856
857
  gtk_box_append (GTK_BOX (self->main_box), GTK_WIDGET (self->search_bar));
  gtk_search_bar_set_key_capture_widget (GTK_SEARCH_BAR (self->search_bar),
858
                                         GTK_WIDGET (self));
Matthias Clasen's avatar
Matthias Clasen committed
859

860
  self->stack = g_object_new (GTK_TYPE_STACK,
Matthias Clasen's avatar
Matthias Clasen committed
861
862
                              "hexpand", TRUE,
                              "vexpand", TRUE,
Matthias Clasen's avatar
Matthias Clasen committed
863
864
                              "hhomogeneous", TRUE,
                              "vhomogeneous", TRUE,
Matthias Clasen's avatar
Matthias Clasen committed
865
866
                              "transition-type", GTK_STACK_TRANSITION_TYPE_CROSSFADE,
                              NULL);
867
  gtk_box_append (GTK_BOX (self->main_box), GTK_WIDGET (self->stack));
Matthias Clasen's avatar
Matthias Clasen committed
868

869
  self->title_stack = g_object_new (GTK_TYPE_STACK,
Matthias Clasen's avatar
Matthias Clasen committed
870
                                    NULL);
871
  gtk_header_bar_set_title_widget (self->header_bar, GTK_WIDGET (self->title_stack));
Matthias Clasen's avatar
Matthias Clasen committed
872

Matthias Clasen's avatar
Matthias Clasen committed
873
  /* Translators: This is the window title for the shortcuts window in normal mode */
Matthias Clasen's avatar
Matthias Clasen committed
874
  label = gtk_label_new (_("Shortcuts"));
Matthias Clasen's avatar
Matthias Clasen committed
875
  gtk_widget_add_css_class (label, "title");
876
  gtk_stack_add_named (self->title_stack, label, "title");
Matthias Clasen's avatar
Matthias Clasen committed
877

Matthias Clasen's avatar
Matthias Clasen committed
878
  /* Translators: This is the window title for the shortcuts window in search mode */
Matthias Clasen's avatar
Matthias Clasen committed
879
  label = gtk_label_new (_("Search Results"));
Matthias Clasen's avatar
Matthias Clasen committed
880
  gtk_widget_add_css_class (label, "title");
881
  gtk_stack_add_named (self->title_stack, label, "search");
Matthias Clasen's avatar
Matthias Clasen committed
882

883
  self->menu_button = g_object_new (GTK_TYPE_MENU_BUTTON,
Matthias Clasen's avatar
Matthias Clasen committed
884
885
                                    "focus-on-click", FALSE,
                                    NULL);
886
887
  gtk_widget_add_css_class (GTK_WIDGET (self->menu_button), "flat");
  gtk_stack_add_named (self->title_stack, GTK_WIDGET (self->menu_button), "sections");
Matthias Clasen's avatar
Matthias Clasen committed
888

889
  self->popover = g_object_new (GTK_TYPE_POPOVER,
Matthias Clasen's avatar
Matthias Clasen committed
890
891
                                "position", GTK_POS_BOTTOM,
                                NULL);
892
  gtk_menu_button_set_popover (self->menu_button, GTK_WIDGET (self->popover));
Matthias Clasen's avatar
Matthias Clasen committed
893

894
  self->list_box = g_object_new (GTK_TYPE_LIST_BOX,
Matthias Clasen's avatar
Matthias Clasen committed
895
896
                                 "selection-mode", GTK_SELECTION_NONE,
                                 NULL);
897
  g_signal_connect_object (self->list_box,
Matthias Clasen's avatar
Matthias Clasen committed
898
899
900
901
                           "row-activated",
                           G_CALLBACK (gtk_shortcuts_window__list_box__row_activated),
                           self,
                           G_CONNECT_SWAPPED);
902
  gtk_popover_set_child (GTK_POPOVER (self->popover), GTK_WIDGET (self->list_box));
Matthias Clasen's avatar
Matthias Clasen committed
903

904
905
  self->search_entry = GTK_SEARCH_ENTRY (gtk_search_entry_new ());
  gtk_search_bar_set_child (GTK_SEARCH_BAR (self->search_bar), GTK_WIDGET (self->search_entry));
906

907
  g_object_set (self->search_entry,
Matthias Clasen's avatar
Matthias Clasen committed
908
                /* Translators: This is placeholder text for the search entry in the shortcuts window */
Matthias Clasen's avatar
Matthias Clasen committed
909
910
911
                "placeholder-text", _("Search Shortcuts"),
                "width-chars", 40,
                NULL);
912
  g_signal_connect_object (self->search_entry,
Matthias Clasen's avatar
Matthias Clasen committed
913
914
915
916
                           "search-changed",
                           G_CALLBACK (gtk_shortcuts_window__entry__changed),
                           self,
                           G_CONNECT_SWAPPED);
917
  g_signal_connect_object (self->search_bar,
Matthias Clasen's avatar
Matthias Clasen committed
918
919
920
921
922
                           "notify::search-mode-enabled",
                           G_CALLBACK (gtk_shortcuts_window__search_mode__changed),
                           self,
                           G_CONNECT_SWAPPED);

923
  scroller = gtk_scrolled_window_new ();
Matthias Clasen's avatar
Matthias Clasen committed
924
925
926
927
  box = g_object_new (GTK_TYPE_BOX,
                      "halign", GTK_ALIGN_CENTER,
                      "orientation", GTK_ORIENTATION_VERTICAL,
                      NULL);
928
  gtk_widget_add_css_class (GTK_WIDGET (box), "shortcuts-search-results");
929
  gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scroller), GTK_WIDGET (box));
930
  gtk_stack_add_named (self->stack, scroller, "internal-search");
Matthias Clasen's avatar
Matthias Clasen committed
931

932
  self->search_shortcuts = g_object_new (GTK_TYPE_BOX,
Matthias Clasen's avatar
Matthias Clasen committed
933
934
935
936
                                         "halign", GTK_ALIGN_CENTER,
                                         "spacing", 6,
                                         "orientation", GTK_ORIENTATION_VERTICAL,
                                         NULL);
937
  gtk_box_append (GTK_BOX (box), GTK_WIDGET (self->search_shortcuts));
Matthias Clasen's avatar
Matthias Clasen committed
938

939
  self->search_gestures = g_object_new (GTK_TYPE_BOX,
Matthias Clasen's avatar
Matthias Clasen committed
940
941
942
943
                                        "halign", GTK_ALIGN_CENTER,
                                        "spacing", 6,
                                        "orientation", GTK_ORIENTATION_VERTICAL,
                                        NULL);
944
  gtk_box_append (GTK_BOX (box), GTK_WIDGET (self->search_gestures));
Matthias Clasen's avatar
Matthias Clasen committed
945
946
947

  empty = g_object_new (GTK_TYPE_GRID,
                        "row-spacing", 12,
Matthias Clasen's avatar
Matthias Clasen committed
948
949
950
951
                        "margin-start", 12,
                        "margin-end", 12,
                        "margin-top", 12,
                        "margin-bottom", 12,
Matthias Clasen's avatar
Matthias Clasen committed
952
953
954
955
956
                        "hexpand", TRUE,
                        "vexpand", TRUE,
                        "halign", GTK_ALIGN_CENTER,
                        "valign", GTK_ALIGN_CENTER,
                        NULL);
Matthias Clasen's avatar
Matthias Clasen committed
957
  gtk_widget_add_css_class (empty, "dim-label");
Matthias Clasen's avatar
Matthias Clasen committed
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
  gtk_grid_attach (GTK_GRID (empty),
                   g_object_new (GTK_TYPE_IMAGE,
                                 "icon-name", "edit-find-symbolic",
                                 "pixel-size", 72,
                                 NULL),
                   0, 0, 1, 1);
  attributes = pango_attr_list_new ();
  pango_attr_list_insert (attributes, pango_attr_weight_new (PANGO_WEIGHT_BOLD));
  pango_attr_list_insert (attributes, pango_attr_scale_new (1.44));
  label = g_object_new (GTK_TYPE_LABEL,
                        "label", _("No Results Found"),
                        "attributes", attributes,
                        NULL);
  pango_attr_list_unref (attributes);
  gtk_grid_attach (GTK_GRID (empty), label, 0, 1, 1, 1);
  label = g_object_new (GTK_TYPE_LABEL,
                        "label", _("Try a different search"),
                        NULL);
  gtk_grid_attach (GTK_GRID (empty), label, 0, 2, 1, 1);

978
  gtk_stack_add_named (self->stack, empty, "no-search-results");
979

980
  g_signal_connect_object (self->stack, "notify::visible-child",
981
982
                           G_CALLBACK (update_title_stack), self, G_CONNECT_SWAPPED);

Matthias Clasen's avatar
Matthias Clasen committed
983
}