-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathIconListBox.vala
164 lines (130 loc) · 4.43 KB
/
IconListBox.vala
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
/*-
* Copyright 2021 Adam Bieńkowski <[email protected]>
*
* 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 3 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/.
*/
public class AppEditor.IconListBox : Gtk.ListBox {
private const int LOAD_BATCH = 20;
private static Gee.ArrayList<string> icons;
private static int max_index = -1;
private Gee.ArrayList<string> added;
private int current_index = 0;
private string search_query = "";
private Cancellable? search_cancellable = null;
static construct {
icons = new Gee.ArrayList<string> ();
var icon_theme = Gtk.IconTheme.get_default ();
var list = icon_theme.list_icons (null);
list.@foreach ((icon_name) => {
icons.add (icon_name);
});
max_index = icons.size - 1;
icons.sort ((a, b) => strcmp (a, b));
}
construct {
added = new Gee.ArrayList<string> ();
set_sort_func (sort_func);
set_filter_func (filter_func);
load_next_icons ();
}
public IconListBox () {
selection_mode = Gtk.SelectionMode.BROWSE;
activate_on_single_click = false;
}
public string? get_selected_icon_name () {
var row = get_selected_row ();
if (row == null) {
return null;
}
var icon_row = row as IconRow;
if (icon_row == null) {
return null;
}
return icon_row.icon_name;
}
public void add_icon_name (string icon_name) {
var row = new IconRow (icon_name);
add (row);
added.add (icon_name);
}
public void load_next_icons () {
int new_index = current_index + LOAD_BATCH;
int bound = new_index.clamp (0, max_index);
var slice = icons.slice (current_index, bound);
foreach (var icon_name in slice) {
add_icon_name (icon_name);
}
current_index = new_index;
show_all ();
}
public void search (string query) {
if (search_cancellable != null) {
search_cancellable.cancel ();
}
search_cancellable = new Cancellable ();
search_query = query;
search_internal.begin (search_query);
}
private async void search_internal (string query) {
new Thread<void*> ("search-internal", () => {
string[] matched = search_icons (query);
if (search_cancellable.is_cancelled ()) {
return null;
}
Idle.add (() => {
foreach (string icon_name in matched) {
add_icon_name (icon_name);
}
show_all ();
invalidate_filter ();
return false;
});
return null;
});
}
private string[] search_icons (string query) {
string[] matched = {};
for (int i = 0; i < icons.size; i++) {
string icon_name = icons[i];
if (!added.contains (icon_name) && query_matches_name (query, icon_name)) {
matched += icon_name;
}
}
return matched;
}
private int sort_func (Gtk.ListBoxRow row1, Gtk.ListBoxRow row2) {
var icon_row1 = row1 as IconRow;
if (icon_row1 == null) {
return 0;
}
var icon_row2 = row2 as IconRow;
if (icon_row2 == null) {
return 0;
}
return strcmp (icon_row1.icon_name, icon_row2.icon_name);
}
private bool filter_func (Gtk.ListBoxRow row) {
if (search_query.strip () == "") {
return true;
}
var icon_row = row as IconRow;
if (icon_row == null) {
return true;
}
return query_matches_name (search_query, icon_row.icon_name);
}
private static bool query_matches_name (string query, string icon_name) {
return query.down () in icon_name.down ();
}
}