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
/
no-scroll.vala
225 lines (166 loc) · 4.96 KB
/
no-scroll.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
// vim: set noexpandtab tabstop=4 shiftwidth=4 softtabstop=4:
class FontData : GLib.Object {
public Pango.FontFace face;
public Pango.FontDescription? desc = null;
public string display_text;
public int index;
}
class FontModel : GLib.Object, GLib.ListModel {
private Gee.ArrayList<FontData> fonts = new Gee.ArrayList<FontData> ();
private Gee.ArrayList<int> filtered_view = new Gee.ArrayList<int> ();
private string? filter = null;
public GLib.Type get_item_type ()
{
return typeof (Pango.FontDescription);
}
public uint get_n_items ()
{
if (this.filter != null)
return this.filtered_view.size;
else
return this.fonts.size;
}
public GLib.Object? get_item (uint index)
{
if (this.filter != null) {
return this.fonts.get (this.filtered_view.get ((int)index));
} else {
return this.fonts.get ((int)index);
}
}
/* XXX We don't support changing the model while a filter is set here. */
public void apply_filter (string? filter)
{
this.filter = filter;
if (filter == null || filter.length == 0) {
this.filtered_view.clear ();
this.filter = null;
this.items_changed (0, this.filtered_view.size, this.fonts.size);
return;
}
int filtered_before = this.filtered_view.size;
this.filtered_view.clear ();
for (int i = 0, p = this.fonts.size; i < p; i ++) {
if (fonts.get (i).display_text.down ().contains (filter.down())) {
filtered_view.add (i);
}
}
if (filtered_before > 0) {
this.items_changed (0, filtered_before, this.filtered_view.size);
} else {
this.items_changed (0, this.fonts.size, this.filtered_view.size);
}
}
private const int LIMIT = 5;
public void load_fonts ()
{
var font_map = Pango.CairoFontMap.get_default ();
Pango.FontFamily[] families;
font_map.list_families (out families);
int n = 0;
foreach (var family in families) {
Pango.FontFace?[] faces;
family.list_faces (out faces);
if (faces != null && faces.length == 0)
continue;
string family_name = family.get_name ();
for (int i = 0; i < faces.length; i ++) {
var data = new FontData ();
data.face = faces[i];
data.display_text = family_name + " " + faces[i].get_face_name ();
data.index = n;
this.fonts.add (data);
if (n >= LIMIT)
break;
n ++;
}
if (n >= LIMIT)
break;
}
this.items_changed (0, 0, n);
}
}
/*
XXX XXX XXX XXX XXX XXX XXX XXX
Make sure the pango.vapi is fixed!
XXX XXX XXX XXX XXX XXX XXX XXX
*/
class Row : Gtk.ListBoxRow {
public Gtk.Label sample_label;
public Gtk.Label name_label;
public Row () {
var box = new Gtk.Box (Gtk.Orientation.VERTICAL, 6);
this.sample_label = new Gtk.Label ("");
this.sample_label.halign = Gtk.Align.START;
this.sample_label.xalign = 0.0f;
this.sample_label.ellipsize = Pango.EllipsizeMode.END;
this.name_label = new Gtk.Label ("");
this.name_label.halign = Gtk.Align.START;
this.name_label.get_style_context ().add_class ("dim-label");
box.add (sample_label);
box.add (name_label);
box.margin = 6;
this.add (box);
}
public void assign (FontData data) {
assert (data != null);
assert (data.face != null);
assert (data.desc != null);
var attrs = new Pango.AttrList ();
attrs.insert (new Pango.AttrFontDesc (data.desc));
attrs.insert (new Pango.AttrSize (14 * Pango.SCALE));
this.sample_label.set_attributes (attrs);
this.sample_label.set_label ("The quick brown fox jumps over the lazy dog.");
this.name_label.set_label ("%d: %s".printf (data.index, data.display_text));
}
}
void main (string[] args)
{
Gtk.init (ref args);
var window = new Gtk.Window ();
var list = new ModelListBox ();
var model = new FontModel ();
var box = new Gtk.Box (Gtk.Orientation.VERTICAL, 6);
var filter_entry = new Gtk.SearchEntry ();
list.set_model (model);
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);
}
list.fill_func = (item, old_widget) => {
Row? row = (Row)old_widget;
if (row == null)
row = new Row ();
else
message ("Recycling...");
FontData data = (FontData) item;
if (data.desc == null)
data.desc = data.face.describe ();
row.assign (data);
row.show_all ();
return row;
};
filter_entry.search_changed.connect (() => {
model.apply_filter (filter_entry.get_text ());
});
list.vexpand = true;
box.add (list);
var bbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
bbox.hexpand = true;
bbox.get_style_context ().add_class ("linked");
filter_entry.hexpand = true;
bbox.add (filter_entry);
box.add (bbox);
box.margin = 6;
window.add (box);
window.resize (400, 400);
window.delete_event.connect (() => { Gtk.main_quit (); return true;});
window.show_all ();
model.load_fonts ();
Gtk.main ();
}