-
Notifications
You must be signed in to change notification settings - Fork 0
/
grrr.js
executable file
·430 lines (302 loc) · 11.4 KB
/
grrr.js
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/usr/bin/gjs
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const Gdk = imports.gi.Gdk;
const Gtk = imports.gi.Gtk;
const Notify = imports.gi.Notify;
const Lang = imports.lang;
const APP_NAME = "Grrr!";
Notify.init(APP_NAME);
const res_name_default = "custom.gresource";
const res_prefix_default = "/org/gnome/custom";
let res_name = res_name_default;
let res_prefix = res_prefix_default;
let config = {};
let config_file = Gio.File.new_for_path(GLib.get_user_data_dir() + "/grrr/config.json");
if (config_file.query_exists(null)) {
let size = config_file.query_info("standard::size",
Gio.FileQueryInfoFlags.NONE,
null).get_size();
try {
let data = config_file.read(null).read_bytes(size, null).get_data();
config = JSON.parse(data);
if (config.res_name) {
res_name = config.res_name;
}
if (config.res_prefix) {
res_prefix = config.res_prefix;
}
} catch (e) {
printerr(e);
}
}
function GResource() {
this._name = res_name;
this._prefix = res_prefix;
this._files = [];
}
GResource.prototype.set_name = function(name) {
this._name = name;
};
GResource.prototype.set_prefix = function(prefix) {
this._prefix = prefix;
};
GResource.prototype.add = function(dir) {
this._base = this._base || dir.get_parent();
if (dir.query_info("standard::*",
Gio.FileQueryInfoFlags.NONE,
null).get_file_type() !== Gio.FileType.DIRECTORY) {
this._files.push(dir);
return;
}
let fileEnum;
try {
fileEnum = dir.enumerate_children("standard::name,standard::type",
Gio.FileQueryInfoFlags.NONE, null);
} catch (e) {
fileEnum = null;
}
if (fileEnum !== null) {
let info;
while ((info = fileEnum.next_file(null)) !== null) {
let file = dir.resolve_relative_path(info.get_name());
if (info.get_file_type() === Gio.FileType.DIRECTORY) {
this.add(file);
} else {
this._files.push(file);
}
}
}
};
GResource.prototype.build = function() {
let xml = "<?xml version='1.0' encoding='UTF-8'?>\n";
xml += "<gresources>\n\t<gresource prefix='" + this._prefix + "'>\n";
for (let file of this._files) {
let info = file.query_info("standard::*", Gio.FileQueryInfoFlags.NONE, null);
xml += "\t\t";
let path = this._base.get_relative_path(file)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
if (/image\//.test(info.get_content_type())) {
xml += "<file preprocess='to-pixdata'>" + path + "</file>\n";
} else {
xml += "<file>" + path + "</file>\n";
}
}
xml += "\t</gresource>\n</gresources>\n";
let xmlfile = this._base.resolve_relative_path(this._name + ".xml");
if (xmlfile.query_exists(null)) {
xmlfile.delete(null);
}
let outputstream = xmlfile.create(Gio.FileCreateFlags.REPLACE_DESTINATION, null);
outputstream.write_all(xml, null);
outputstream.close(null);
};
GResource.prototype.compile = function(cb = () => {}) {
let ok, pid;
try {
[ ok, pid ] = GLib.spawn_async(this._base.get_path(),
[ "glib-compile-resources", this._name + ".xml" ],
GLib.get_environ(),
GLib.SpawnFlags.SEARCH_PATH_FROM_ENVP | GLib.SpawnFlags.DO_NOT_REAP_CHILD,
null);
} catch (e) {
printerr(e);
}
if (ok === false) {
return;
}
if (typeof pid === "number") {
GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, () => {
GLib.spawn_close_pid(pid);
try {
let notification = new Notify.Notification({
summary: "Gresource file generated!",
body: this._name + " generated at " + this._base.get_path(),
icon_name: "dialog-information"
});
notification.set_timeout(1000);
notification.show();
} catch (e) {
printerr(e);
}
cb();
});
}
};
const Application = new Lang.Class({
Name: APP_NAME,
_init: function() {
this.application = new Gtk.Application({
application_id: "org.ozonos.grrr",
flags: Gio.ApplicationFlags.FLAGS_NONE
});
this.application.connect("activate", Lang.bind(this, this._onActivate));
this.application.connect("startup", Lang.bind(this, this._onStartup));
},
_buildUI: function() {
this._window = new Gtk.ApplicationWindow({
application: this.application,
window_position: Gtk.WindowPosition.CENTER,
title: APP_NAME
});
try {
let icon = Gtk.IconTheme.get_default().load_icon("binary", 48, 0);
this._window.set_icon(icon);
} catch (e) {
printerr(e);
}
this._headerbar = new Gtk.HeaderBar({
title: APP_NAME,
show_close_button: true
});
// Add options to set the name and the prefix
let grid = new Gtk.Grid({
column_spacing: 10,
row_spacing: 10,
margin: 10
});
grid.set_column_homogeneous(true);
let namelabel = new Gtk.Label({ label: "File name:" });
namelabel.set_halign(Gtk.Align.END);
let nameentry = new Gtk.Entry();
nameentry.connect("changed", () => res_name = nameentry.get_text());
nameentry.set_placeholder_text(res_name_default);
grid.attach(namelabel, 0, 0, 1, 1);
grid.attach_next_to(nameentry, namelabel, Gtk.PositionType.RIGHT, 2, 1);
let prefixlabel = new Gtk.Label({ label: "Resource prefix:" });
prefixlabel.set_halign(Gtk.Align.END);
let prefixentry = new Gtk.Entry();
prefixentry.set_placeholder_text(res_prefix_default);
prefixentry.connect("changed", () => res_prefix = prefixentry.get_text());
grid.attach(prefixlabel, 0, 1, 1, 1);
grid.attach_next_to(prefixentry, prefixlabel, Gtk.PositionType.RIGHT, 2, 1);
let menubutton = new Gtk.ToggleButton();
menubutton.add(new Gtk.Image({
icon_name: "open-menu-symbolic",
icon_size: Gtk.IconSize.SMALL_TOOLBAR
}));
menubutton.connect("clicked", () => {
if (menubutton.get_active()) {
menu.show_all();
}
});
let menu = new Gtk.Popover();
menu.set_relative_to(menubutton);
menu.connect("show", () => {
nameentry.set_text(res_name);
prefixentry.set_text(res_prefix);
});
menu.connect("closed", () => {
if (menubutton.get_active()) {
menubutton.set_active(false);
}
res_name = res_name || res_name_default;
res_prefix = res_prefix || res_prefix_default;
let write = false;
if (config.res_name !== res_name) {
config.res_name = res_name;
write = true;
}
if (config.res_prefix !== res_prefix) {
config.res_prefix = res_prefix;
write = true;
}
if (write) {
let parent = config_file.get_parent();
if (parent.query_exists(null)) {
if (config_file.query_exists(null)) {
config_file.delete(null);
}
} else {
parent.make_directory_with_parents(null);
}
let outputstream = config_file.create(Gio.FileCreateFlags.REPLACE_DESTINATION, null);
outputstream.write_all(JSON.stringify(config), null);
outputstream.close(null);
}
});
menu.add(grid);
this._headerbar.pack_end(menubutton);
let spinner = new Gtk.Spinner({ active: true });
spinner.set_size_request(64, 64);
let label = new Gtk.Label({ label: "Drop files and folders to generate a gresource file!" });
// Let's set up our window for drag 'n drop
let dnd = new Gtk.Box();
dnd.set_vexpand(true);
dnd.set_hexpand(true);
dnd.drag_dest_set(Gtk.DestDefaults.ALL, null, Gdk.DragAction.COPY);
dnd.drag_dest_add_text_targets();
let generate = (uris) => {
let gresource = new GResource();
for (let uri of uris) {
gresource.add(Gio.File.new_for_uri(uri));
}
gresource.build();
gresource.compile(() => {
let complete = new Gtk.Label({ label: res_name + " generated!" });
dnd.set_center_widget(complete);
dnd.show_all();
GLib.timeout_add(GLib.PRIORITY_DEFAULT, 3000, () => {
dnd.set_center_widget(label);
dnd.show_all();
return false;
}, null);
});
};
dnd.connect("drag_data_received", (s, c, x, y, selection) => {
dnd.set_center_widget(spinner);
dnd.show_all();
let text = selection.get_text();
if (text) {
generate(text.split("\n").map(u => u.trim()).filter(u => !!u));
}
});
dnd.set_center_widget(label);
let addbutton = new Gtk.Button();
addbutton.add(new Gtk.Image({
icon_name: "list-add-symbolic",
icon_size: Gtk.IconSize.SMALL_TOOLBAR
}));
addbutton.connect("clicked", () => {
let chooser = new Gtk.FileChooserDialog({
title: "Select folders",
action: Gtk.FileChooserAction.SELECT_FOLDER,
transient_for: this._window,
modal: true
});
chooser.set_select_multiple(true);
chooser.add_button("Add", Gtk.ResponseType.OK);
chooser.add_button("Cancel", Gtk.ResponseType.CANCEL);
chooser.set_default_response(Gtk.ResponseType.OK);
chooser.connect("response", (dialog, response) => {
let uris = dialog.get_uris();
dialog.destroy();
if (response === Gtk.ResponseType.OK && uris && uris.length) {
generate(uris);
}
});
chooser.run();
});
this._headerbar.pack_start(addbutton);
// Add some styles
let css = new Gtk.CssProvider();
css.load_from_data("* { font-size: large; }");
dnd.get_style_context().add_provider(css, 0);
this._window.add(dnd);
this._window.set_default_size(800, 600);
this._window.set_titlebar(this._headerbar);
this._window.show_all();
},
_onActivate: function() {
this._window.present();
},
_onStartup: function() {
this._buildUI();
}
});
let app = new Application();
app.application.run(ARGV);