1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
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
346
347
348
349
350
351
352
353
354
355
356
357
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
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
502
503
504
505
506
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
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
673
674
675
676
677
678
679
680
681
682
683
684
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
|
/* cc-keyboard-shortcut-dialog.c
*
* Copyright (C) 2010 Intel, Inc
* Copyright (C) 2016 Endless, Inc
* Copyright (C) 2020 System76, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Thomas Wood <thomas.wood@intel.com>
* Georges Basile Stavracas Neto <gbsneto@gnome.org>
* Ian Douglas Scott <idscott@system76.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <config.h>
#include <glib/gi18n.h>
#include <adwaita.h>
#include "cc-keyboard-shortcut-dialog.h"
#include "cc-keyboard-item.h"
#include "cc-keyboard-manager.h"
#include "cc-keyboard-shortcut-editor.h"
#include "cc-keyboard-shortcut-row.h"
#include "cc-list-row.h"
#include "cc-util.h"
#include "keyboard-shortcuts.h"
#define SHORTCUT_DELIMITERS "+ "
typedef struct {
gchar *section_title;
gchar *section_id;
guint modified_count;
GtkLabel *modified_label;
} SectionRowData;
typedef struct {
CcKeyboardItem *item;
gchar *section_title;
gchar *section_id;
SectionRowData *section_data;
} ShortcutRowData;
struct _CcKeyboardShortcutDialog
{
GtkDialog parent_instance;
GtkSizeGroup *accelerator_sizegroup;
GtkWidget *back_button;
GtkListBoxRow *custom_shortcut_add_row;
guint custom_shortcut_count;
GtkWidget *empty_custom_shortcuts_placeholder;
GtkWidget *empty_search_placeholder;
GtkHeaderBar *headerbar;
GtkStack *header_stack;
GtkWidget *reset_all_button;
GtkWidget *section_box;
GtkSearchEntry *search_entry;
GtkListBox *section_listbox;
GtkListBoxRow *section_row;
GtkWidget *shortcut_box;
GtkListBox *shortcut_listbox;
GtkStack *stack;
CcKeyboardManager *manager;
GtkWidget *shortcut_editor;
GHashTable *sections;
};
G_DEFINE_TYPE (CcKeyboardShortcutDialog, cc_keyboard_shortcut_dialog, GTK_TYPE_DIALOG)
static gboolean
is_matched_shortcut_present (GtkListBox *listbox,
gpointer user_data);
static SectionRowData*
section_row_data_new (const gchar *section_id,
const gchar *section_title,
GtkLabel *modified_label)
{
SectionRowData *data;
data = g_new0 (SectionRowData, 1);
data->section_id = g_strdup (section_id);
data->section_title = g_strdup (section_title);
data->modified_count = 0;
data->modified_label = modified_label;
return data;
}
static void
section_row_data_free (SectionRowData *data)
{
g_free (data->section_id);
g_free (data->section_title);
g_free (data);
}
static ShortcutRowData*
shortcut_row_data_new (CcKeyboardItem *item,
const gchar *section_id,
const gchar *section_title,
SectionRowData *section_data)
{
ShortcutRowData *data;
data = g_new0 (ShortcutRowData, 1);
data->item = g_object_ref (item);
data->section_id = g_strdup (section_id);
data->section_title = g_strdup (section_title);
data->section_data = section_data;
return data;
}
static void
shortcut_row_data_free (ShortcutRowData *data)
{
g_object_unref (data->item);
g_free (data->section_id);
g_free (data->section_title);
g_free (data);
}
static GtkListBoxRow*
add_section (CcKeyboardShortcutDialog *self,
const gchar *section_id,
const gchar *section_title)
{
GtkWidget *icon, *modified_label, *row;
icon = gtk_image_new_from_icon_name ("go-next-symbolic");
gtk_widget_add_css_class (icon, "dim-label");
modified_label = gtk_label_new (NULL);
gtk_widget_add_css_class (modified_label, "dim-label");
row = adw_action_row_new ();
gtk_list_box_row_set_activatable (GTK_LIST_BOX_ROW (row), TRUE);
adw_preferences_row_set_title (ADW_PREFERENCES_ROW (row), _(section_title));
//TODO gtk_container_add (GTK_CONTAINER (row), modified_label);
//TODO gtk_container_add (GTK_CONTAINER (row), icon);
g_object_set_data_full (G_OBJECT (row),
"data",
section_row_data_new (section_id, section_title, GTK_LABEL (modified_label)),
(GDestroyNotify)section_row_data_free);
g_hash_table_insert (self->sections, g_strdup (section_id), row);
gtk_list_box_append (self->section_listbox, row);
return GTK_LIST_BOX_ROW (row);
}
static void
set_custom_shortcut_placeholder_visibility (CcKeyboardShortcutDialog *self)
{
SectionRowData *section_data;
gboolean is_custom_shortcuts = FALSE;
if (self->section_row != NULL)
{
section_data = g_object_get_data (G_OBJECT (self->section_row), "data");
is_custom_shortcuts = (strcmp (section_data->section_id, "custom") == 0);
gtk_stack_set_transition_type (self->stack, GTK_STACK_TRANSITION_TYPE_CROSSFADE);
if (is_custom_shortcuts && (self->custom_shortcut_count == 0))
gtk_stack_set_visible_child (self->stack, GTK_WIDGET (self->empty_custom_shortcuts_placeholder));
else
gtk_stack_set_visible_child (self->stack, self->shortcut_box);
}
}
static void
add_item (CcKeyboardShortcutDialog *self,
CcKeyboardItem *item,
const gchar *section_id,
const gchar *section_title)
{
GtkWidget *row;
GtkListBoxRow *section_row;
SectionRowData *section_data;
section_row = g_hash_table_lookup (self->sections, section_id);
if (section_row == NULL)
section_row = add_section (self, section_id, section_title);
section_data = g_object_get_data (G_OBJECT (section_row), "data");
row = GTK_WIDGET (cc_keyboard_shortcut_row_new (item,
self->manager,
CC_KEYBOARD_SHORTCUT_EDITOR (self->shortcut_editor),
self->accelerator_sizegroup));
g_object_set_data_full (G_OBJECT (row),
"data",
shortcut_row_data_new (item, section_id, section_title, section_data),
(GDestroyNotify)shortcut_row_data_free);
if (strcmp (section_id, "custom") == 0)
{
self->custom_shortcut_count++;
set_custom_shortcut_placeholder_visibility (self);
}
gtk_list_box_append (self->shortcut_listbox, row);
}
static void
remove_item (CcKeyboardShortcutDialog *self,
CcKeyboardItem *item)
{
GtkWidget *child;
for (child = gtk_widget_get_first_child (GTK_WIDGET (self->shortcut_listbox));
child;
child = gtk_widget_get_next_sibling (child))
{
ShortcutRowData *row_data;
if (!GTK_IS_LIST_BOX_ROW (child))
continue;
row_data = g_object_get_data (G_OBJECT (child), "data");
if (row_data && row_data->item == item)
{
if (strcmp (row_data->section_id, "custom") == 0)
{
self->custom_shortcut_count--;
set_custom_shortcut_placeholder_visibility (self);
}
gtk_list_box_remove (self->shortcut_listbox, child);
break;
}
}
}
static void
update_modified_counts (CcKeyboardShortcutDialog *self)
{
SectionRowData *section_data;
ShortcutRowData *shortcut_data;
g_autofree gchar *modified_text = NULL;
GtkWidget *child;
for (child = gtk_widget_get_first_child (GTK_WIDGET (self->section_listbox));
child;
child = gtk_widget_get_next_sibling (child))
{
if (!GTK_IS_LIST_BOX_ROW (child))
continue;
section_data = g_object_get_data (G_OBJECT (child), "data");
section_data->modified_count = 0;
}
for (child = gtk_widget_get_first_child (GTK_WIDGET (self->shortcut_listbox));
child;
child = gtk_widget_get_next_sibling (child))
{
if (!GTK_IS_LIST_BOX_ROW (child))
continue;
if (GTK_LIST_BOX_ROW (child) == self->custom_shortcut_add_row)
continue;
shortcut_data = g_object_get_data (G_OBJECT (child), "data");
if (!cc_keyboard_item_is_value_default (shortcut_data->item))
shortcut_data->section_data->modified_count++;
}
for (child = gtk_widget_get_first_child (GTK_WIDGET (self->section_listbox));
child;
child = gtk_widget_get_next_sibling (child))
{
if (!GTK_IS_LIST_BOX_ROW (child))
continue;
section_data = g_object_get_data (G_OBJECT (child), "data");
if (section_data->modified_count > 0)
{
modified_text = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE,
"%d modified",
"%d modified",
section_data->modified_count),
section_data->modified_count);
gtk_label_set_text (section_data->modified_label, modified_text);
}
else
{
gtk_label_set_text (section_data->modified_label, "");
}
}
}
static void
show_section_list (CcKeyboardShortcutDialog *self)
{
if (self->section_row != NULL)
gtk_stack_set_transition_type (self->stack, GTK_STACK_TRANSITION_TYPE_SLIDE_RIGHT);
else
gtk_stack_set_transition_type (self->stack, GTK_STACK_TRANSITION_TYPE_NONE);
self->section_row = NULL;
gtk_stack_set_visible_child (self->stack, self->section_box);
gtk_window_set_title (GTK_WINDOW (self), _("Keyboard Shortcuts"));
gtk_editable_set_text (GTK_EDITABLE (self->search_entry), "");
gtk_stack_set_visible_child (self->header_stack, self->reset_all_button);
gtk_widget_set_visible (GTK_WIDGET (self->search_entry), TRUE);
update_modified_counts (self);
}
static void
show_shortcut_list (CcKeyboardShortcutDialog *self)
{
SectionRowData *section_data;
gchar *title;
gboolean is_custom_shortcuts = FALSE;
title = _("Keyboard Shortcuts");
gtk_stack_set_transition_type (self->stack, GTK_STACK_TRANSITION_TYPE_NONE);
if (self->section_row != NULL)
{
section_data = g_object_get_data (G_OBJECT (self->section_row), "data");
title = _(section_data->section_title);
is_custom_shortcuts = (strcmp (section_data->section_id, "custom") == 0);
gtk_stack_set_transition_type (self->stack, GTK_STACK_TRANSITION_TYPE_SLIDE_LEFT);
}
gtk_list_box_invalidate_filter (self->shortcut_listbox);
if (is_custom_shortcuts && (self->custom_shortcut_count == 0))
gtk_stack_set_visible_child (self->stack, GTK_WIDGET (self->empty_custom_shortcuts_placeholder));
else
gtk_stack_set_visible_child (self->stack, self->shortcut_box);
gtk_window_set_title (GTK_WINDOW (self), title);
set_custom_shortcut_placeholder_visibility (self);
gtk_stack_set_visible_child (self->header_stack, self->back_button);
gtk_widget_set_visible (GTK_WIDGET (self->search_entry), self->section_row == NULL);
}
static void
add_custom_shortcut_clicked_cb (CcKeyboardShortcutDialog *self)
{
CcKeyboardShortcutEditor *editor;
editor = CC_KEYBOARD_SHORTCUT_EDITOR (self->shortcut_editor);
cc_keyboard_shortcut_editor_set_mode (editor, CC_SHORTCUT_EDITOR_CREATE);
cc_keyboard_shortcut_editor_set_item (editor, NULL);
gtk_widget_show (self->shortcut_editor);
}
static void
section_row_activated (GtkWidget *button,
GtkListBoxRow *row,
CcKeyboardShortcutDialog *self)
{
self->section_row = row;
show_shortcut_list (self);
}
static void
shortcut_row_activated (GtkWidget *button,
GtkListBoxRow *row,
CcKeyboardShortcutDialog *self)
{
CcKeyboardShortcutEditor *editor;
if (row == self->custom_shortcut_add_row)
{
add_custom_shortcut_clicked_cb (self);
return;
}
editor = CC_KEYBOARD_SHORTCUT_EDITOR (self->shortcut_editor);
ShortcutRowData *data = g_object_get_data (G_OBJECT (row), "data");
cc_keyboard_shortcut_editor_set_mode (editor, CC_SHORTCUT_EDITOR_EDIT);
cc_keyboard_shortcut_editor_set_item (editor, data->item);
gtk_widget_show (self->shortcut_editor);
}
static void
back_button_clicked_cb (CcKeyboardShortcutDialog *self)
{
show_section_list (self);
}
static void
reset_shortcut (CcKeyboardShortcutDialog *self,
GtkWidget *row)
{
ShortcutRowData *data;
if (row == GTK_WIDGET (self->custom_shortcut_add_row))
return;
data = g_object_get_data (G_OBJECT (row), "data");
/* Don't reset custom shortcuts */
if (cc_keyboard_item_get_item_type (data->item) == CC_KEYBOARD_ITEM_TYPE_GSETTINGS_PATH)
return;
/* cc_keyboard_manager_reset_shortcut() already resets conflicting shortcuts,
* so no other check is needed here. */
cc_keyboard_manager_reset_shortcut (self->manager, data->item);
}
static void
on_reset_all_dialog_response_cb (GtkDialog *dialog,
gint response,
CcKeyboardShortcutDialog *self)
{
if (response == GTK_RESPONSE_ACCEPT)
{
GtkWidget *child;
for (child = gtk_widget_get_first_child (GTK_WIDGET (self->shortcut_listbox));
child;
child = gtk_widget_get_next_sibling (child))
{
if (!GTK_IS_LIST_BOX_ROW (child))
continue;
if (GTK_LIST_BOX_ROW (child) == self->custom_shortcut_add_row)
continue;
reset_shortcut (self, child);
}
}
gtk_window_destroy (GTK_WINDOW (dialog));
update_modified_counts (self);
}
static void
reset_all_clicked_cb (CcKeyboardShortcutDialog *self)
{
GtkWidget *dialog, *button;
dialog = gtk_message_dialog_new (GTK_WINDOW (self),
GTK_DIALOG_MODAL | GTK_DIALOG_USE_HEADER_BAR | GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_NONE,
_("Reset All Shortcuts?"));
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
_("Resetting the shortcuts may affect your custom shortcuts. "
"This cannot be undone."));
gtk_dialog_add_buttons (GTK_DIALOG (dialog),
_("Cancel"), GTK_RESPONSE_CANCEL,
_("Reset All"), GTK_RESPONSE_ACCEPT,
NULL);
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
/* Make the "Reset All" button destructive */
button = gtk_dialog_get_widget_for_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
gtk_widget_add_css_class (button, "destructive-action");
g_signal_connect (dialog, "response", G_CALLBACK (on_reset_all_dialog_response_cb), self);
gtk_window_present (GTK_WINDOW (dialog));
}
static void
search_entry_cb (CcKeyboardShortcutDialog *self)
{
gboolean is_shortcut = is_matched_shortcut_present (self->shortcut_listbox, self);
const gchar *search_text = gtk_editable_get_text (GTK_EDITABLE (self->search_entry));
if (!is_shortcut)
gtk_stack_set_visible_child (self->stack, self->empty_search_placeholder);
else if (g_utf8_strlen (search_text, -1) == 0 && self->section_row == NULL)
show_section_list (self);
else if (gtk_stack_get_visible_child (self->stack) != self->shortcut_box)
show_shortcut_list (self);
else
gtk_list_box_invalidate_filter (self->shortcut_listbox);
}
static gboolean
strv_contains_prefix_or_match (gchar **strv,
const gchar *prefix)
{
const struct {
const gchar *key;
const gchar *untranslated;
const gchar *synonym;
} key_aliases[] =
{
{ "ctrl", "Ctrl", "ctrl" },
{ "win", "Super", "super" },
{ "option", NULL, "alt" },
{ "command", NULL, "super" },
{ "apple", NULL, "super" },
};
for (guint i = 0; strv[i]; i++)
{
if (g_str_has_prefix (strv[i], prefix))
return TRUE;
}
for (guint i = 0; i < G_N_ELEMENTS (key_aliases); i++)
{
g_autofree gchar *alias = NULL;
const gchar *synonym;
if (!g_str_has_prefix (key_aliases[i].key, prefix))
continue;
if (key_aliases[i].untranslated)
{
const gchar *translated_label;
/* Steal GTK+'s translation */
translated_label = g_dpgettext2 ("gtk40", "keyboard label", key_aliases[i].untranslated);
alias = g_utf8_strdown (translated_label, -1);
}
synonym = key_aliases[i].synonym;
/* If a translation or synonym of the key is in the accelerator, and we typed
* the key, also consider that a prefix */
if ((alias && g_strv_contains ((const gchar * const *) strv, alias)) ||
(synonym && g_strv_contains ((const gchar * const *) strv, synonym)))
{
return TRUE;
}
}
return FALSE;
}
static gboolean
search_match_shortcut (CcKeyboardItem *item,
const gchar *search)
{
g_auto(GStrv) shortcut_tokens = NULL, search_tokens = NULL;
g_autofree gchar *normalized_accel = NULL;
g_autofree gchar *accel = NULL;
gboolean match;
GList *key_combos;
CcKeyCombo *combo;
key_combos = cc_keyboard_item_get_key_combos (item);
for (GList *l = key_combos; l != NULL; l = l->next)
{
combo = l->data;
if (is_empty_binding (combo))
continue;
match = TRUE;
accel = convert_keysym_state_to_string (combo);
normalized_accel = cc_util_normalize_casefold_and_unaccent (accel);
shortcut_tokens = g_strsplit_set (normalized_accel, SHORTCUT_DELIMITERS, -1);
search_tokens = g_strsplit_set (search, SHORTCUT_DELIMITERS, -1);
for (guint i = 0; search_tokens[i] != NULL; i++)
{
const gchar *token;
/* Strip leading and trailing whitespaces */
token = g_strstrip (search_tokens[i]);
if (g_utf8_strlen (token, -1) == 0)
continue;
match = match && strv_contains_prefix_or_match (shortcut_tokens, token);
if (!match)
break;
}
if (match)
return TRUE;
}
return FALSE;
}
static gint
section_sort_function (GtkListBoxRow *a,
GtkListBoxRow *b,
gpointer user_data)
{
SectionRowData *a_data, *b_data;
a_data = g_object_get_data (G_OBJECT (a), "data");
b_data = g_object_get_data (G_OBJECT (b), "data");
/* Put custom shortcuts below everything else */
if (g_strcmp0 (a_data->section_id, "custom") == 0)
return 1;
return g_strcmp0 (a_data->section_title, b_data->section_title);
}
static gint
shortcut_sort_function (GtkListBoxRow *a,
GtkListBoxRow *b,
gpointer user_data)
{
CcKeyboardShortcutDialog *self = user_data;
ShortcutRowData *a_data, *b_data;
gint retval;
if (a == self->custom_shortcut_add_row)
return 1;
else if (b == self->custom_shortcut_add_row)
return -1;
a_data = g_object_get_data (G_OBJECT (a), "data");
b_data = g_object_get_data (G_OBJECT (b), "data");
retval = g_strcmp0 (a_data->section_title, b_data->section_title);
if (retval != 0)
return retval;
return g_strcmp0 (cc_keyboard_item_get_description (a_data->item), cc_keyboard_item_get_description (b_data->item));
}
static gboolean
shortcut_filter_function (GtkListBoxRow *row,
gpointer userdata)
{
CcKeyboardShortcutDialog *self = userdata;
SectionRowData *section_data;
ShortcutRowData *data;
CcKeyboardItem *item;
gboolean retval;
g_autofree gchar *search = NULL;
g_autofree gchar *name = NULL;
g_auto(GStrv) terms = NULL;
gboolean is_custom_shortcuts = FALSE;
const gchar *search_text;
if (self->section_row != NULL)
{
section_data = g_object_get_data (G_OBJECT (self->section_row), "data");
is_custom_shortcuts = (strcmp (section_data->section_id, "custom") == 0);
data = g_object_get_data (G_OBJECT (row), "data");
if (data && strcmp (data->section_id, section_data->section_id) != 0)
return FALSE;
}
if (row == self->custom_shortcut_add_row)
return is_custom_shortcuts;
search_text = gtk_editable_get_text (GTK_EDITABLE (self->search_entry));
if (g_utf8_strlen (search_text, -1) == 0)
return TRUE;
data = g_object_get_data (G_OBJECT (row), "data");
item = data->item;
name = cc_util_normalize_casefold_and_unaccent (cc_keyboard_item_get_description (item));
search = cc_util_normalize_casefold_and_unaccent (search_text);
terms = g_strsplit (search, " ", -1);
for (guint i = 0; terms && terms[i]; i++)
{
retval = strstr (name, terms[i]) || search_match_shortcut (item, terms[i]);
if (!retval)
break;
}
return retval;
}
static gboolean
is_matched_shortcut_present (GtkListBox* listbox,
gpointer user_data)
{
for (gint i = 0; ; i++)
{
GtkListBoxRow *current = gtk_list_box_get_row_at_index (listbox, i);
if (!current)
return FALSE;
if (shortcut_filter_function (current, user_data))
return TRUE;
}
}
static void
shortcut_header_function (GtkListBoxRow *row,
GtkListBoxRow *before,
gpointer user_data)
{
CcKeyboardShortcutDialog *self = user_data;
gboolean add_header;
ShortcutRowData *data, *before_data;
data = g_object_get_data (G_OBJECT (row), "data");
if (row == self->custom_shortcut_add_row)
{
add_header = FALSE;
}
else if (before && before != self->custom_shortcut_add_row)
{
before_data = g_object_get_data (G_OBJECT (before), "data");
add_header = g_strcmp0 (before_data->section_id, data->section_id) != 0;
}
else
{
add_header = TRUE;
}
if (self->section_row != NULL)
add_header = FALSE;
if (add_header)
{
GtkWidget *box, *label, *separator;
g_autofree gchar *markup = NULL;
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
if (!before)
gtk_widget_set_margin_top (box, 6);
markup = g_strdup_printf ("<b>%s</b>", _(data->section_title));
label = g_object_new (GTK_TYPE_LABEL,
"label", markup,
"use-markup", TRUE,
"xalign", 0.0,
"margin-start", 6,
NULL);
gtk_box_append (GTK_BOX (box), label);
separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
gtk_box_append (GTK_BOX (box), separator);
gtk_list_box_row_set_header (row, box);
}
else
{
gtk_list_box_row_set_header (row, NULL);
}
}
static void
cc_keyboard_shortcut_dialog_constructed (GObject *object)
{
CcKeyboardShortcutDialog *self = CC_KEYBOARD_SHORTCUT_DIALOG (object);
G_OBJECT_CLASS (cc_keyboard_shortcut_dialog_parent_class)->constructed (object);
/* Setup the dialog's transient parent */
gtk_window_set_transient_for (GTK_WINDOW (self->shortcut_editor), GTK_WINDOW (self));
}
static void
cc_keyboard_shortcut_dialog_finalize (GObject *object)
{
CcKeyboardShortcutDialog *self = CC_KEYBOARD_SHORTCUT_DIALOG (object);
g_clear_object (&self->manager);
g_clear_pointer (&self->sections, g_hash_table_destroy);
g_clear_pointer ((GtkWindow**)&self->shortcut_editor, gtk_window_destroy);
}
static void
cc_keyboard_shortcut_dialog_class_init (CcKeyboardShortcutDialogClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->constructed = cc_keyboard_shortcut_dialog_constructed;
object_class->finalize = cc_keyboard_shortcut_dialog_finalize;
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/keyboard/cc-keyboard-shortcut-dialog.ui");
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutDialog, accelerator_sizegroup);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutDialog, back_button);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutDialog, custom_shortcut_add_row);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutDialog, empty_custom_shortcuts_placeholder);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutDialog, empty_search_placeholder);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutDialog, headerbar);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutDialog, header_stack);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutDialog, reset_all_button);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutDialog, search_entry);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutDialog, section_listbox);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutDialog, section_box);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutDialog, shortcut_listbox);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutDialog, shortcut_box);
gtk_widget_class_bind_template_child (widget_class, CcKeyboardShortcutDialog, stack);
gtk_widget_class_bind_template_callback (widget_class, add_custom_shortcut_clicked_cb);
gtk_widget_class_bind_template_callback (widget_class, back_button_clicked_cb);
gtk_widget_class_bind_template_callback (widget_class, reset_all_clicked_cb);
gtk_widget_class_bind_template_callback (widget_class, search_entry_cb);
gtk_widget_class_bind_template_callback (widget_class, section_row_activated);
gtk_widget_class_bind_template_callback (widget_class, shortcut_row_activated);
}
static void
cc_keyboard_shortcut_dialog_init (CcKeyboardShortcutDialog *self)
{
gtk_widget_init_template (GTK_WIDGET (self));
gtk_search_entry_set_key_capture_widget (self->search_entry, GTK_WIDGET (self));
self->manager = cc_keyboard_manager_new ();
self->shortcut_editor = cc_keyboard_shortcut_editor_new (self->manager);
self->sections = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
self->section_row = NULL;
g_signal_connect_object (self->manager,
"shortcut-added",
G_CALLBACK (add_item),
self,
G_CONNECT_SWAPPED);
g_signal_connect_object (self->manager,
"shortcut-removed",
G_CALLBACK (remove_item),
self,
G_CONNECT_SWAPPED);
add_section(self, "custom", "Custom Shortcuts");
cc_keyboard_manager_load_shortcuts (self->manager);
gtk_list_box_set_sort_func (GTK_LIST_BOX (self->section_listbox),
section_sort_function,
self,
NULL);
gtk_list_box_set_filter_func (self->shortcut_listbox,
shortcut_filter_function,
self,
NULL);
gtk_list_box_set_header_func (self->shortcut_listbox,
shortcut_header_function,
self,
NULL);
gtk_list_box_set_sort_func (GTK_LIST_BOX (self->shortcut_listbox),
shortcut_sort_function,
self,
NULL);
show_section_list (self);
}
GtkWidget*
cc_keyboard_shortcut_dialog_new (void)
{
return g_object_new (CC_TYPE_KEYBOARD_SHORTCUT_DIALOG,
"use-header-bar", 1,
NULL);
}
|