-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
208 lines (179 loc) · 4.46 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
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
"use strict";
const { Gdk, Gio, GLib, GObject } = imports.gi;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Gettext = imports.gettext;
const Domain = Gettext.domain(Me.metadata.uuid);
const _ = Domain.gettext;
const ngettext = Domain.ngettext;
/**
* Convert a Gdk.RGBA color object to a string.
*
* @param {Gdk.RGBA} color
* @returns {string} the string representation of the color
*/
var colorToString = (color) =>
{
return color.to_string();
};
/**
* Convert a string to a new Gdk.RGBA color.
*
* @param {string} str
* @returns {Gdk.RGBA} the newly created RGBA color
*/
var stringToColor = (str) =>
{
const newColor = new Gdk.RGBA();
newColor.parse(str);
return newColor;
};
/**
* Gets the CSS RGBA string from a given Gdk.RGBA and opacity value.
*
* @param {Gdk.RGBA} rgba
* @param {double} opacity
* @returns {string}
*/
var getCSSColor = (rgba, opacity) =>
{
return `rgba(${rgba.red * 255}, ${rgba.green * 255}, ${rgba.blue * 255}, ${opacity});`;
};
/**
* Append a style to a widget.
*
* @param {St.Widget} widget
* @returns {St.Widget}
*/
var appendStyle = (widget, style) =>
{
widget.set_style(`${widget.get_style()} ${style}`);
return widget;
};
/**
* Convert bytes to a human-readable format.
*
* @param {number} bytes number of bytes
* @param {boolean} si use SI units (1000), or 1024
* @param {number} d decimal places
* @returns {string}
*/
var bytesToHuman = (bytes, si = false, d = 2) =>
{
const units = si ?
["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"] :
["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
const div = si ? 1000 : 1024;
let i = 0;
while (bytes >= div)
{
bytes /= div;
i++;
}
return `${bytes.toFixed(d)} ${units[i]}`;
};
/**
* Convert a number from microseconds (monotonic time) to seconds.
*
* @param {number} microseconds a number in microseconds
* @returns {number} a number in seconds
*/
var monoToSeconds = (microseconds) =>
{
return microseconds / (1000 * 1000);
}
/**
* Convert a number from seconds to microseconds (monotonic time).
*
* @param {number} seconds a number in seconds
* @returns {number} a number in microseconds
*/
var secondsToMono = (seconds) =>
{
return seconds * 1000 * 1000;
}
// var monoToHuman = (microseconds, d = 2) =>
// {
// const units = ["s", "m", "h", "d"];
// const divisor = [1000 * 1000, 60, 60, 24];
// let string = `${microseconds / (1000 * 1000)}`;
// return string;
// }
/**
* Convert a GTK-style font to a CSS-style font
*
* @param {string} font GTK-style font, e.g. "Monospace Bold 16"
* @returns {string} CSS-style font, e.g. "16px Bold Monospace"
*/
var fontToCSS = (font) =>
{
let css = "";
let f = null;
// font-size
const regSize = /\d+/g;
if (f = font.match(regSize))
{
css += `${f[0]}px `;
font = font.replace(regSize, "");
}
return css.concat(font).trim();
}
/**
* Gio.Cancellable which can be cancelled independently of its parent, and cancelling
* the parent will cancel its children.
*
* Reference:
* https://github.com/micheleg/dash-to-dock/blob/ubuntu-dock/utils.js#L543-L596
*
* @param {Gio.Cancellable} parent
*/
var CancellableChild = class CancellableChild extends Gio.Cancellable
{
static { GObject.registerClass(this); }
constructor(parent)
{
if (parent && !(parent instanceof Gio.Cancellable))
{
throw TypeError("Parent not of type Gio.Cancellable");
}
super();
if (parent.is_cancelled())
{
this.cancel();
return;
}
this.parent = parent;
this._connectToParent();
}
_connectToParent()
{
this.connection = this.parent.connect(() =>
{
this._cancel();
if (this._disconnection)
{
return;
}
this._disconnection = GLib.idle_add(GLib.PRIORITY_DEFAULT, () =>
{
this._disconnectFromParent();
this._disconnection = null;
return GLib.SOURCE_REMOVE;
});
});
}
_disconnectFromParent()
{
if (this.connection) this.parent.disconnect(this.connection);
this.connection = null;
}
_cancel()
{
super.cancel();
}
cancel()
{
this._disconnectFromParent();
this._cancel();
}
}