Debian Codegraph @

src/KeywordList.vala

raw
1 /* KeywordList.vala
2 *
3 * Copyright 2023 Diego Iván <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 [GtkTemplate (ui = "/io/github/diegoivan/pdf_metadata_editor/gtk/keyword-list.ui")]
22 public class PaperClip.KeywordList : Adw.PreferencesGroup {
23 [GtkChild]
24 private unowned Gtk.ListBox listbox;
25
26 public bool is_empty { get; private set; }
27
28 private Gtk.Widget? empty_state_row = null;
29
30 private unowned Document _document;
31 public unowned Document document {
32 get {
33 return _document;
34 }
35 set {
36 _document = value;
37 listbox.bind_model (document.keywords, create_keyword_row);
38 document.keywords.items_changed.connect (on_items_changed);
39 on_items_changed ();
40 }
41 }
42
43 construct {
44 var action_group = new SimpleActionGroup ();
45 var action = new SimpleAction ("add-keyword", null);
46 action.activate.connect (() => document.add_keyword (""));
47
48 action_group.add_action (action);
49
50 insert_action_group ("keyword-list", action_group);
51 }
52
53 private Gtk.Widget create_keyword_row (Object item) {
54 var row = new KeywordRow () {
55 title = _("Keyword"),
56 document = document,
57 };
58 item.bind_property ("str", row, "text", SYNC_CREATE | BIDIRECTIONAL);
59
60 return row;
61 }
62
63 private void on_items_changed () {
64 if (document.keywords.get_n_items () == 0) {
65 create_default_row ();
66 is_empty = true;
67 return;
68 }
69
70 int i = 0;
71 Gtk.ListBoxRow? current_row;
72 while ((current_row = listbox.get_row_at_index (i)) != null) {
73 if (current_row == empty_state_row) {
74 listbox.remove (empty_state_row);
75 is_empty = false;
76 break;
77 }
78 if (listbox.get_row_at_index (i+1) == null) {
79 current_row.grab_focus ();
80 }
81 i++;
82 }
83
84 if (document.keywords.get_n_items () == 1) {
85 listbox.get_first_child ().grab_focus ();
86 }
87 }
88
89 private void create_default_row () {
90 var center_box = new Gtk.CenterBox ();
91
92 var label = new Gtk.Label (_("Add Keyword")) {
93 justify = CENTER,
94 wrap = true,
95 margin_top = 12,
96 margin_bottom = 12
97 };
98
99 center_box.center_widget = label;
100 center_box.start_widget = new Gtk.Image.from_icon_name ("list-add-symbolic") {
101 halign = END,
102 hexpand = true,
103 margin_end = 12
104 };
105
106 empty_state_row = new Adw.PreferencesRow () {
107 child = center_box,
108 activatable = true,
109 selectable = false
110 };
111
112 listbox.append (empty_state_row);
113 }
114
115 [GtkCallback]
116 private void on_row_activated (Gtk.ListBoxRow selected_row) {
117 if (selected_row != empty_state_row) {
118 return;
119 }
120 document.add_keyword ("");
121 }
122 }