forked from elementary/files
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListView.vala
More file actions
289 lines (241 loc) · 10.5 KB
/
ListView.vala
File metadata and controls
289 lines (241 loc) · 10.5 KB
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
/***
Copyright (c) 2015-2020 elementary LLC <https://elementary.io>
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License version 3, as published
by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, 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/>.
Authors : Jeremy Wootten <jeremywootten@gmail.com>
***/
namespace Files {
public class ListView : AbstractTreeView {
/* We wait two seconds after row is collapsed to unload the subdirectory */
const int COLLAPSE_TO_UNLOAD_DELAY = 2;
static string [] column_titles = {
_("Filename"),
_("Size"),
_("Type"),
_("Modified")
};
/* ListView manages the loading and unloading of subdirectories displayed */
private uint unload_file_timeout_id = 0;
private GLib.List<Gtk.TreeRowReference> subdirectories_to_unload = null;
private GLib.List<Directory> loaded_subdirectories = null;
public ListView (View.Slot _slot) {
base (_slot);
}
protected override void set_up_icon_renderer () {
icon_renderer = new IconRenderer ();
}
private void connect_additional_signals () {
tree.row_expanded.connect (on_row_expanded);
tree.row_collapsed.connect (on_row_collapsed);
model.subdirectory_unloaded.connect (on_model_subdirectory_unloaded);
}
private void append_extra_tree_columns () {
int fnc = ListModel.ColumnID.FILENAME;
int preferred_column_width = Files.column_view_settings.get_int ("preferred-column-width");
for (int k = fnc; k < ListModel.ColumnID.NUM_COLUMNS; k++) {
if (k == fnc) {
/* name_column already created by AbstractTreeVIew */
name_column.set_title (column_titles [0]);
name_column.min_width = preferred_column_width;
} else {
var renderer = new Gtk.CellRendererText ();
var col = new Gtk.TreeViewColumn.with_attributes (column_titles [k - fnc],
renderer,
"text", k) {
sort_column_id = k,
resizable = false,
expand = false,
min_width = 24
};
if (k == ListModel.ColumnID.SIZE || k == ListModel.ColumnID.MODIFIED) {
renderer.@set ("xalign", 1.0f);
} else {
renderer.@set ("xalign", 0.0f);
}
tree.append_column (col);
}
}
}
protected override void enable_scroll (bool enable) {
base.enable_scroll (enable);
if (enable) {
scrolled_window.set_policy (AUTOMATIC, AUTOMATIC);
}
}
private void on_row_expanded (Gtk.TreeIter iter, Gtk.TreePath path) {
set_path_expanded (path, true);
add_subdirectory_at_path (path);
}
private void on_row_collapsed (Gtk.TreeIter iter, Gtk.TreePath path) {
set_path_expanded (path, false);
schedule_unload_subdirectory_at_path (path);
}
private void on_model_subdirectory_unloaded (Directory dir) {
/* ensure the model and our list of subdirectories are kept in sync */
remove_subdirectory (dir);
}
private void schedule_unload_subdirectory_at_path (Gtk.TreePath path) {
/* unload subdirectory from model and remove from our list of subdirectories
* after a delay, in case of rapid collapsing and re-expanding of rows */
subdirectories_to_unload.append (new Gtk.TreeRowReference (model, path));
schedule_model_unload_directories ();
}
private void set_path_expanded (Gtk.TreePath path, bool expanded) {
Files.File? file = model.file_for_path (path);
if (file != null) {
file.set_expanded (expanded);
}
}
private void schedule_model_unload_directories () {
cancel_file_timeout ();
unload_file_timeout_id = GLib.Timeout.add_seconds (COLLAPSE_TO_UNLOAD_DELAY,
unload_directories);
}
private bool unload_directories () {
foreach (unowned Gtk.TreeRowReference rowref in subdirectories_to_unload) {
Gtk.TreeIter? iter = null;
Gtk.TreePath path;
if (rowref.valid ()) {
path = rowref.get_path ();
} else {
warning ("TreeRowRef invalid when unloading subdirectory");
continue;
}
if (((Gtk.TreeView)tree).is_row_expanded (path)) {
continue;
}
if (model.get_iter (out iter, path) && iter != null) {
model.unload_subdirectory (iter);
} else {
warning ("Subdirectory to unload not found in model");
}
}
subdirectories_to_unload.@foreach ((rowref) => {
subdirectories_to_unload.remove (rowref);
});
unload_file_timeout_id = 0;
return false;
}
private void cancel_file_timeout () {
if (unload_file_timeout_id > 0) {
GLib.Source.remove (unload_file_timeout_id);
unload_file_timeout_id = 0;
}
}
protected override bool on_view_key_press_event (uint keyval, uint keycode, Gdk.ModifierType state) {
uint up_key;
uint down_key;
if (Gtk.StateFlags.DIR_RTL in get_style_context ().get_state ()) {
up_key = Gdk.Key.Right;
down_key = Gdk.Key.Left;
} else {
up_key = Gdk.Key.Left;
down_key = Gdk.Key.Right;
}
bool control_pressed = ((state & Gdk.ModifierType.CONTROL_MASK) != 0);
bool shift_pressed = ((state & Gdk.ModifierType.SHIFT_MASK) != 0);
if (!control_pressed && !shift_pressed) {
if (keyval == down_key) {
Gtk.TreePath? path = null;
tree.get_cursor (out path, null);
if (path != null) {
tree.expand_row (path, false);
}
return true;
} else if (keyval == up_key) {
Gtk.TreePath? path = null;
tree.get_cursor (out path, null);
if (path != null) {
if (tree.is_row_expanded (path)) {
tree.collapse_row (path);
} else if (path.up ()) {
tree.collapse_row (path);
}
}
return true;
}
}
return base.on_view_key_press_event (keyval, keycode, state);
}
protected override Gtk.Widget? create_view () {
model.has_child = true;
base.create_view ();
tree.set_show_expanders (true);
tree.set_headers_visible (true);
tree.set_rubber_banding (true);
append_extra_tree_columns ();
connect_additional_signals ();
return tree as Gtk.Widget;
}
public override Settings? get_view_settings () {
return Files.list_view_settings;
}
private void add_subdirectory_at_path (Gtk.TreePath path) {
/* If a new subdirectory is loaded, connect it, load it
* and add it to the list of subdirectories */
Files.Directory? dir = null;
if (model.get_subdirectory (path, out dir)) { // Returns true if dir non null
connect_directory_handlers (dir);
dir.init ();
/* Maintain our own reference on dir, independent of the model */
/* Also needed for updating show hidden status */
loaded_subdirectories.prepend (dir);
}
}
private void remove_subdirectory (Directory? dir) {
if (dir != null) {
disconnect_directory_handlers (dir);
/* Release our reference on dir */
loaded_subdirectories.remove (dir);
} else {
warning ("List View: directory null in remove_subdirectory");
}
}
protected override bool expand_collapse (Gtk.TreePath? path) {
if (tree.is_row_expanded (path)) {
tree.collapse_row (path);
} else {
tree.expand_row (path, false);
}
return true;
}
protected override bool get_next_visible_iter (ref Gtk.TreeIter iter, bool recurse = true) {
Gtk.TreePath? path = model.get_path (iter);
Gtk.TreeIter start = iter;
if (path == null) {
return false;
}
if (recurse && tree.is_row_expanded (path)) {
Gtk.TreeIter? child_iter = null;
if (model.iter_children (out child_iter, iter)) {
iter = child_iter;
return true;
}
}
if (model.iter_next (ref iter)) {
return true;
} else {
Gtk.TreeIter? parent = null;
if (model.iter_parent (out parent, start)) {
iter = parent;
return get_next_visible_iter (ref iter, false);
}
}
return false;
}
public override void cancel () {
cancel_file_timeout ();
base.cancel ();
loaded_subdirectories.@foreach ((dir) => {
remove_subdirectory (dir);
});
}
}
}