This repository has been archived by the owner on Jan 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.vala
306 lines (233 loc) · 7.33 KB
/
demo.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
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
const int NUM = 6;
// Model {{{
class TweetModel : GLib.ListModel, GLib.Object {
private Gee.ArrayList<SampleModelItem> items = new Gee.ArrayList<SampleModelItem> ();
public GLib.Type get_item_type () {
return typeof (SampleModelItem);
}
public uint get_n_items () {
return items.size;
}
public void clear () {
int size = this.items.size;
items.clear ();
this.items_changed (0, size - 1, 0);
}
public GLib.Object? get_item (uint position) {
return items.get ((int)position);
}
public void append (SampleModelItem item) {
this.items.add (item);
}
public void insert (int pos, SampleModelItem item) {
items.insert (pos, item);
this.items_changed (pos, 0, 1);
}
public void shuffle () {
int s = items.size;
// Don't reverse the order,
for (int i = 0; i < s / 2; i ++) {
var k = this.items.remove_at (i);
this.items.insert ((int)(GLib.Random.next_int () % (this.items.size - 1)),
k);
}
// Everything changed
this.items_changed (0, get_n_items (), get_n_items ());
}
}
// }}}
// UTIL {{{
string random_text () {
const int MAX_LENGTH = 600;
const int MIN_LENGTH = 20;
StringBuilder b = new StringBuilder ();
// XXX Should just use a char[] m( m( m(
int length = GLib.Random.int_range (MIN_LENGTH, MAX_LENGTH);
for (int i = 0; i < length; i ++) {
char r_c = (char) (96 + GLib.Random.int_range (0, 128-97));
b.append_c (r_c);
}
return b.str;
//return "bbb";
}
// }}}
class SampleModelItem : GLib.Object {
public uint num;
public int size;
public bool checked = false;
public string text;
public SampleModelItem (uint num, int size) {
this.num = num;
this.size = size;
this.text = random_text ();
}
}
[GtkTemplate (ui = "/org/baedert/listbox/tweet-row.ui")]
class TweetRow : Gtk.ListBoxRow {
[GtkChild]
private Gtk.Label text_label;
[GtkChild]
private Gtk.Label time_delta_label;
public uint num = 0;
public TweetRow () {
//text_label.label = "asdkfjsdahfsdakjf sdafhsda fgsdag fhgsajkfhsga dhfsga df <a href=\"foobar\">hihi</a>
//asdfsadfsadf asjkdf sajkdfhl asdfjsak df";
}
public void assign (SampleModelItem item) {
this.num = item.num;
this.time_delta_label.label = this.num.to_string ();
//this.text_label.label = item.text;
if (item.num < NUM-2)
this.set_size_request (-1, 200);
else
this.set_size_request (-1, 80);
}
public override bool draw (Cairo.Context ct) {
ct.set_source_rgba (1, 1, 1, 1);
ct.rectangle (0, 0, get_allocated_width (), get_allocated_height ());
ct.fill ();
return base.draw (ct);
}
}
void main (string[] args) {
Gtk.init (ref args);
new ModelListBox ();
var win = new DemoWindow ();
win.show ();
Gtk.main ();
}
[GtkTemplate (ui = "/org/baedert/listbox/demo.ui")]
class DemoWindow : Gtk.Window {
[GtkChild]
private Gtk.Label used_widgets_label;
[GtkChild]
private ModelListBox list_box;
[GtkChild]
private Gtk.Label model_size_label;
[GtkChild]
private Gtk.Label visible_items_label;
[GtkChild]
private Gtk.Label estimated_height_label;
[GtkChild]
private Gtk.Entry filter_entry;
[GtkChild]
private Gtk.Switch filter_switch;
[GtkChild]
private Gtk.ScrolledWindow scroller;
private TweetModel model = new TweetModel ();
public DemoWindow () {
this.delete_event.connect (() => { Gtk.main_quit (); return false; });
list_box.notify["cur-widgets"].connect (() => {
used_widgets_label.label = "%'u (%'u cached, %'u total)"
.printf (list_box.cur_widgets, list_box.cached_widgets, list_box.total_widgets);
});
list_box.notify["model-from"].connect (() => {
visible_items_label.label = "%'d - %'d".printf (list_box.model_from, list_box.model_to);
});
list_box.notify["model-to"].connect (() => {
visible_items_label.label = "%'d - %'d".printf (list_box.model_from, list_box.model_to);
});
list_box.notify["estimated-height"].connect (() => {
estimated_height_label.label = "%'dpx".printf (list_box.estimated_height);
});
filter_entry.notify["text"].connect (filter_text_changed_cb);
list_box.fill_func = (item, widget) => {
TweetRow? row = (TweetRow) widget;
assert (item != null);
if (row == null)
row = new TweetRow ();
row.assign ((SampleModelItem)item);
row.show ();
return row;
};
try {
var provider = new Gtk.CssProvider ();
provider.load_from_data (".list-row { border-bottom: 1px solid alpha(grey, 0.3);}",-1);
Gtk.StyleContext.add_provider_for_screen (Gdk.Screen.get_default (),
provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
} catch (GLib.Error e) {
error (e.message);
}
//for (int i = 0; i < 5000; i ++)
for (int i = 0; i < NUM; i ++)
model.append (new SampleModelItem (i, 20 + (i * 10)));
list_box.set_model (model);
model_size_label.label = "%'u".printf (model.get_n_items ());
model.items_changed.connect (() => {
model_size_label.label = "%'u".printf (model.get_n_items ());
});
}
private void filter_text_changed_cb () {
string new_text = filter_entry.text;
message (new_text);
}
[GtkCallback]
private void remove_selected_cb () {
for (int i = 0; i < model.get_n_items (); i ++) {
var item = (SampleModelItem)model.get_object (i);
if (item.checked) {
//model.remove (i);
i --;
}
}
}
[GtkCallback]
private void reverse_order_button_clicked_cb () {
this.model.shuffle ();
}
[GtkCallback]
private void add_start_button_clicked_cb () {
int index = 0;
var item = new SampleModelItem (index, 20 + (int)(GLib.Random.next_int () % 100));
this.model.insert (index, item);
}
[GtkCallback]
private void add_middle_button_clicked_cb () {
uint index = this.list_box.model_from +
(this.list_box.model_to - this.list_box.model_from) / 2;
var item = new SampleModelItem (index, 20 + (int)(GLib.Random.next_int () % 100));
this.model.insert ((int)index, item);
}
[GtkCallback]
private void add_end_button_clicked_cb () {
int index = (int)this.model.get_n_items ();
var item = new SampleModelItem (index, 20 + (int)(GLib.Random.next_int () % 100));
this.model.insert (index, item);
}
[GtkCallback]
private void filter_cb () {
//if (this.filter_switch.active) {
//list_box.filter_func = (item) => {
//assert (item != null);
//var sample = (SampleModelItem) item;
//return sample.num % 2 == 0;
//};
//list_box.refilter ();
//} else {
//list_box.filter_func = null;
//list_box.refilter ();
//}
}
[GtkCallback]
private void remove_all_cb () {
this.model.clear ();
}
[GtkCallback]
private void debug_cb () {
//this.list_box.print_debug_info ();
GLib.Timeout.add (10, () => {
list_box.vadjustment.value += 4;
return list_box.vadjustment.value < list_box.vadjustment.upper - list_box.vadjustment.page_size;
});
}
[GtkCallback]
private void scroll_up_cb () {
scroller.get_vadjustment ().value --;
}
[GtkCallback]
private void scroll_down_cb () {
message ("========================= SCROLL DOWN ==================");
scroller.get_vadjustment ().value ++;
}
}