This repository was archived by the owner on Nov 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathScrollableListView.vala
More file actions
217 lines (177 loc) · 6.7 KB
/
Copy pathScrollableListView.vala
File metadata and controls
217 lines (177 loc) · 6.7 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
/*
* Copyright © 2013–2014 Philipp Emanuel Weidmann <pew@worldwidemann.com>
*
* Nemo vir est qui mundum non reddat meliorem.
*
*
* This file is part of Final Term.
*
* Final Term 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.
*
* Final Term 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 Final Term. If not, see <http://www.gnu.org/licenses/>.
*/
public class ScrollableListView<T, E> : Clutter.Actor {
private Mx.ScrollView scroll_view;
private Mx.ListView list_view;
private Clutter.Model list_model;
public ScrollableListView(NotifyingList<T> list, Type item_type, Type item_view_type, string item_property_name) {
scroll_view = new Mx.ScrollView();
add(scroll_view);
/*
* The goal here is to work around the fact that the Mx ScrollView "shadow"
* cannot be disabled by clipping the ScrollView and compensating for the clipping
* using padding (see style.css). However, multiple Mx bugs stand in the way.
* Notably, the ScrollView fails to calculate the child widget's height properly
* when padding is applied. The "solution" seen here was found by examining the
* Mx source code (https://github.com/clutter-project/mx/tree/master/mx)
* and currently only works with vertical scrolling.
*/
// TODO: The scrollbar's top and bottom ranges lie slightly outside the visible part of the view
scroll_view.add_constraint(new Clutter.BindConstraint(this, Clutter.BindCoordinate.X, 0));
scroll_view.add_constraint(new Clutter.BindConstraint(this, Clutter.BindCoordinate.Y, -15));
scroll_view.add_constraint(new Clutter.BindConstraint(this, Clutter.BindCoordinate.WIDTH, 0));
scroll_view.add_constraint(new Clutter.BindConstraint(this, Clutter.BindCoordinate.HEIGHT, 45));
clip_to_allocation = true;
list_view = new Mx.ListView();
list_view.factory = new ItemViewFactory<E>(item_view_type);
list_model = new Clutter.ListModel(1, item_type, null);
list_view.model = list_model;
list_view.add_attribute(item_property_name, 0);
scroll_view.add(list_view);
scroll_view.motion_event.connect(on_motion_event);
scroll_view.button_press_event.connect(on_button_press_event);;
// Synchronize model with list
foreach (var item in list) {
list_model.append(0, item, -1);
}
list.item_inserted.connect((index, item) => {
list_model.insert(index, 0, item, -1);
});
list.item_removed.connect((index, item) => {
list_model.remove(index);
});
list.item_modified.connect((index, item) => {
// TODO
});
on_settings_changed(null);
Settings.get_default().changed.connect(on_settings_changed);
}
// TODO: This does not work (Vala crash):
//public delegate bool FilterFunction(T item);
//public delegate int SortFunction(T item_1, T item_2);
public delegate bool FilterFunction<G>(G item);
public delegate int SortFunction<G>(G item_1, G item_2);
public void set_filter_function(FilterFunction<T> filter_function) {
list_model.set_filter((model, iter) => {
return filter_function((T)iter.get_value(0).get_object());
});
}
public void set_sort_function(SortFunction<T> sort_function) {
list_model.set_sort(0, (model, a, b) => {
return sort_function((T)a.get_object(), (T)b.get_object());
});
}
// Returns the number of items that are actually displayed
public int get_number_of_items() {
return (int)list_model.get_n_rows();
}
public bool is_valid_item_index(int item_index) {
return (item_index >= 0 && item_index < get_number_of_items());
}
// Returns the item at the specified index,
// taking into account filtering and sorting
// (i.e. considering the items as they are displayed)
public T? get_item(int item_index) {
if (!is_valid_item_index(item_index))
return null;
return (T)list_model.get_iter_at_row(item_index).get_value(0).get_object();
}
// TODO: This method should return an object of type E, but Vala
// does not support constraints on generic parameters ("E implements ItemView"),
// so it is more convenient to return an ItemView here
private ItemView? get_item_view(int item_index) {
if (!is_valid_item_index(item_index))
return null;
return (ItemView)list_view.get_child_at_index(item_index);
}
public void update_item(int item_index) {
if (!is_valid_item_index(item_index))
return;
get_item_view(item_index).update();
}
public void scroll_to_item(int item_index) {
if (!is_valid_item_index(item_index))
return;
// NOTE: item.get_geometry() does not work here
// because the layout manager takes over positioning
var geometry = Clutter.Geometry();
var allocation_box = get_item_view(item_index).get_allocation_box();
geometry.x = (int)allocation_box.get_x();
geometry.y = (int)allocation_box.get_y();
geometry.width = (uint)allocation_box.get_width();
geometry.height = (uint)allocation_box.get_height();
scroll_view.ensure_visible(geometry);
}
public int get_item_by_y(int y) {
var index = -1;
var height = 0;
for (int i = 0; i < get_number_of_items(); i++) {
var allocation_box = get_item_view(i).get_allocation_box();
height += (int)allocation_box.get_height();
if ((int)y < height) {
index = i;
break;
}
}
return index;
}
private void on_settings_changed(string? key) {
scroll_view.style = Settings.get_default().theme.style;
list_view.style = Settings.get_default().theme.style;
}
private bool on_motion_event(Clutter.MotionEvent event) {
var index = get_item_by_y((int)event.y);
if (index >= 0) {
item_hovered(index);
}
return true;
}
private bool on_button_press_event(Clutter.ButtonEvent event) {
var index = get_item_by_y((int)event.y);
if (index >= 0) {
item_clicked(index);
}
return true;
}
public signal void item_hovered(int index);
public signal void item_clicked(int index);
private class ItemViewFactory<G> : Object, Mx.ItemFactory {
private Type type;
public ItemViewFactory(Type type) {
this.type = type;
}
public Clutter.Actor create() {
var object = Object.new(type);
if (object is ItemView) {
((ItemView)object).construct();
} else {
critical(_("Object does not implement the ItemView interface"));
}
return (Clutter.Actor)object;
}
}
}
// TODO: Rename (Mx also provides a class called "ItemView")?
public interface ItemView : Clutter.Actor {
public abstract void construct();
public abstract void update();
}