-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutil.js
75 lines (56 loc) · 2.07 KB
/
util.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
'use strict';
/* exported parse_rgba UtilMixin APP_DATA_DIR */
const { GObject, Gio, Gdk } = imports.gi;
var APP_DATA_DIR = null;
function parse_rgba(s) {
if (!s)
return null;
const v = new Gdk.RGBA();
if (v.parse(s))
return v;
return null;
}
// Signal connections and settings bindings, with lifetime bound to lifetime of 'this'
var UtilMixin = {
run_on_destroy(func, obj = null) {
let this_destroy_id = null, obj_destroy_id = null;
const disconnect_func = () => {
if (this_destroy_id)
GObject.signal_handler_disconnect(this, this_destroy_id);
if (obj_destroy_id)
GObject.signal_handler_disconnect(obj, obj_destroy_id);
func();
obj = null;
};
this_destroy_id = GObject.signal_connect(this, 'destroy', disconnect_func);
if (obj !== null && obj !== this && GObject.signal_lookup('destroy', obj.constructor.$gtype))
obj_destroy_id = GObject.signal_connect(obj, 'destroy', disconnect_func);
},
disconnect_on_destroy(obj, handler_id) {
this.run_on_destroy(
GObject.signal_handler_disconnect.bind(null, obj, handler_id),
obj
);
return handler_id;
},
signal_connect(source, signal, handler) {
return this.disconnect_on_destroy(
source, GObject.signal_connect(source, signal, handler)
);
},
method_handler(source, signal, method) {
return this.signal_connect(source, signal, method.bind(this));
},
settings_bind(key, target, property = null, flags = Gio.SettingsBindFlags.DEFAULT) {
if (property === null)
property = key;
this.settings.bind(key, target, property, flags);
this.run_on_destroy(
Gio.Settings.unbind.bind(null, target, property),
target
);
},
bind_settings_ro(key, target, property = null, flags = Gio.SettingsBindFlags.GET | Gio.SettingsBindFlags.NO_SENSITIVITY) {
this.settings_bind(key, target, property, flags);
},
};