forked from micheleg/dash-to-dock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
416 lines (351 loc) · 12.2 KB
/
utils.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
const Gi = imports._gi;
const Clutter = imports.gi.Clutter;
const GObject = imports.gi.GObject;
const Gtk = imports.gi.Gtk;
const Meta = imports.gi.Meta;
const St = imports.gi.St;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Docking = Me.imports.docking;
var SignalsHandlerFlags = {
NONE: 0,
CONNECT_AFTER: 1
};
/**
* Simplify global signals and function injections handling
* abstract class
*/
const BasicHandler = class DashToDock_BasicHandler {
constructor(parentObject) {
this._storage = new Object();
if (parentObject) {
if (!(parentObject.connect instanceof Function))
throw new TypeError('Not a valid parent object');
if (!(parentObject instanceof GObject.Object) ||
GObject.signal_lookup('destroy', parentObject.constructor.$gtype)) {
this._parentObject = parentObject;
this._destroyId = parentObject.connect('destroy', () => this.destroy());
}
}
}
add(...args) {
// Convert arguments object to array, concatenate with generic
// Call addWithLabel with ags as if they were passed arguments
this.addWithLabel('generic', ...args);
}
destroy() {
this._parentObject?.disconnect(this._destroyId);
this._parentObject = null;
for( let label in this._storage )
this.removeWithLabel(label);
}
addWithLabel(label, ...args) {
let argsArray = [...args];
if (argsArray.every(arg => !Array.isArray(arg)))
argsArray = [argsArray];
if (this._storage[label] == undefined)
this._storage[label] = new Array();
// Skip first element of the arguments
for (const argArray of argsArray) {
if (argArray.length < 3)
throw new Error('Unexpected number of arguments');
let item = this._storage[label];
try {
item.push(this._create(...argArray));
} catch (e) {
logError(e);
}
}
}
removeWithLabel(label) {
if (this._storage[label]) {
for (let i = 0; i < this._storage[label].length; i++)
this._remove(this._storage[label][i]);
delete this._storage[label];
}
}
// Virtual methods to be implemented by subclass
/**
* Create single element to be stored in the storage structure
*/
_create(_object, _element, _callback) {
throw new GObject.NotImplementedError(`_create in ${this.constructor.name}`);
}
/**
* Correctly delete single element
*/
_remove(_item) {
throw new GObject.NotImplementedError(`_remove in ${this.constructor.name}`);
}
};
/**
* Manage global signals
*/
var GlobalSignalsHandler = class DashToDock_GlobalSignalHandler extends BasicHandler {
_create(object, event, callback, flags = SignalsHandlerFlags.NONE) {
if (!object)
throw new Error('Impossible to connect to an invalid object');
let after = flags == SignalsHandlerFlags.CONNECT_AFTER;
let connector = after ? object.connect_after : object.connect;
if (!connector) {
throw new Error(`Requested to connect to signal '${event}', ` +
`but no implementation for 'connect${after ? '_after' : ''}' `+
`found in ${object.constructor.name}`);
}
let id = connector.call(object, event, callback);
return [object, id];
}
_remove(item) {
const [object, id] = item;
object.disconnect(id);
}
};
/**
* Color manipulation utilities
*/
var ColorUtils = class DashToDock_ColorUtils {
// Darken or brigthen color by a fraction dlum
// Each rgb value is modified by the same fraction.
// Return "#rrggbb" string
static ColorLuminance(r, g, b, dlum) {
let rgbString = '#';
rgbString += ColorUtils._decimalToHex(Math.round(Math.min(Math.max(r*(1+dlum), 0), 255)), 2);
rgbString += ColorUtils._decimalToHex(Math.round(Math.min(Math.max(g*(1+dlum), 0), 255)), 2);
rgbString += ColorUtils._decimalToHex(Math.round(Math.min(Math.max(b*(1+dlum), 0), 255)), 2);
return rgbString;
}
// Convert decimal to an hexadecimal string adding the desired padding
static _decimalToHex(d, padding) {
let hex = d.toString(16);
while (hex.length < padding)
hex = '0'+ hex;
return hex;
}
// Convert hsv ([0-1, 0-1, 0-1]) to rgb ([0-255, 0-255, 0-255]).
// Following algorithm in https://en.wikipedia.org/wiki/HSL_and_HSV
// here with h = [0,1] instead of [0, 360]
// Accept either (h,s,v) independently or {h:h, s:s, v:v} object.
// Return {r:r, g:g, b:b} object.
static HSVtoRGB(h, s, v) {
if (arguments.length === 1) {
s = h.s;
v = h.v;
h = h.h;
}
let r,g,b;
let c = v*s;
let h1 = h*6;
let x = c*(1 - Math.abs(h1 % 2 - 1));
let m = v - c;
if (h1 <=1)
r = c + m, g = x + m, b = m;
else if (h1 <=2)
r = x + m, g = c + m, b = m;
else if (h1 <=3)
r = m, g = c + m, b = x + m;
else if (h1 <=4)
r = m, g = x + m, b = c + m;
else if (h1 <=5)
r = x + m, g = m, b = c + m;
else
r = c + m, g = m, b = x + m;
return {
r: Math.round(r * 255),
g: Math.round(g * 255),
b: Math.round(b * 255)
};
}
// Convert rgb ([0-255, 0-255, 0-255]) to hsv ([0-1, 0-1, 0-1]).
// Following algorithm in https://en.wikipedia.org/wiki/HSL_and_HSV
// here with h = [0,1] instead of [0, 360]
// Accept either (r,g,b) independently or {r:r, g:g, b:b} object.
// Return {h:h, s:s, v:v} object.
static RGBtoHSV(r, g, b) {
if (arguments.length === 1) {
r = r.r;
g = r.g;
b = r.b;
}
let h,s,v;
let M = Math.max(r, g, b);
let m = Math.min(r, g, b);
let c = M - m;
if (c == 0)
h = 0;
else if (M == r)
h = ((g-b)/c) % 6;
else if (M == g)
h = (b-r)/c + 2;
else
h = (r-g)/c + 4;
h = h/6;
v = M/255;
if (M !== 0)
s = c/M;
else
s = 0;
return {
h: h,
s: s,
v: v
};
}
};
/**
* Manage function injection: both instances and prototype can be overridden
* and restored
*/
var InjectionsHandler = class DashToDock_InjectionsHandler extends BasicHandler {
_create(object, name, injectedFunction) {
let original = object[name];
if (!(original instanceof Function))
throw new Error(`Virtual function ${name}() is not available for ${object}`);
object[name] = function(...args) { return injectedFunction.call(this, original, ...args) };
return [object, name, original];
}
_remove(item) {
const [object, name, original] = item;
object[name] = original;
}
};
/**
* Manage vfunction injection: both instances and prototype can be overridden
* and restored
*/
var VFuncInjectionsHandler = class DashToDock_VFuncInjectionsHandler extends BasicHandler {
_create(prototype, name, injectedFunction) {
const original = prototype[`vfunc_${name}`];
if (!(original instanceof Function))
throw new Error(`Virtual function ${name} is not available for ${prototype}`);
prototype[Gi.hook_up_vfunc_symbol](name, injectedFunction);
return [prototype, name];
}
_remove(item) {
const [prototype, name] = item;
const originalVFunc = prototype[`vfunc_${name}`];
try {
// This may fail if trying to reset to a never-overridden vfunc
// as gjs doesn't consider it a function, even if it's true that
// originalVFunc instanceof Function.
prototype[Gi.hook_up_vfunc_symbol](name, originalVFunc);
} catch {
try {
prototype[Gi.hook_up_vfunc_symbol](name, function (...args) {
return originalVFunc.call(this, ...args);
});
} catch (e) {
logError(e, `Removing vfunc_${name}`);
}
}
}
};
/**
* Manage properties injection: both instances and prototype can be overridden
* and restored
*/
var PropertyInjectionsHandler = class DashToDock_PropertyInjectionsHandler extends BasicHandler {
_create(instance, name, injectedPropertyDescriptor) {
if (!(name in instance))
throw new Error(`Object ${instance} has no '${name}' property`);
const prototype = instance.constructor.prototype;
const originalPropertyDescriptor = Object.getOwnPropertyDescriptor(prototype, name) ??
Object.getOwnPropertyDescriptor(instance, name);
Object.defineProperty(instance, name, {
...originalPropertyDescriptor,
...injectedPropertyDescriptor,
...{ configurable: true },
});
return [instance, name, originalPropertyDescriptor];
}
_remove(item) {
const [instance, name, originalPropertyDescriptor] = item;
if (originalPropertyDescriptor)
Object.defineProperty(instance, name, originalPropertyDescriptor);
else
delete instance[name];
}
};
/**
* Return the actual position reverseing left and right in rtl
*/
function getPosition() {
let position = Docking.DockManager.settings.get_enum('dock-position');
if (Clutter.get_default_text_direction() == Clutter.TextDirection.RTL) {
if (position == St.Side.LEFT)
position = St.Side.RIGHT;
else if (position == St.Side.RIGHT)
position = St.Side.LEFT;
}
return position;
}
function getPreviewScale() {
return Docking.DockManager.settings.get_double('preview-size-scale');
}
function drawRoundedLine(cr, x, y, width, height, isRoundLeft, isRoundRight, stroke, fill) {
if (height > width) {
y += Math.floor((height - width) / 2.0);
height = width;
}
height = 2.0 * Math.floor(height / 2.0);
var leftRadius = isRoundLeft ? height / 2.0 : 0.0;
var rightRadius = isRoundRight ? height / 2.0 : 0.0;
cr.moveTo(x + width - rightRadius, y);
cr.lineTo(x + leftRadius, y);
if (isRoundLeft)
cr.arcNegative(x + leftRadius, y + leftRadius, leftRadius, -Math.PI/2, Math.PI/2);
else
cr.lineTo(x, y + height);
cr.lineTo(x + width - rightRadius, y + height);
if (isRoundRight)
cr.arcNegative(x + width - rightRadius, y + rightRadius, rightRadius, Math.PI/2, -Math.PI/2);
else
cr.lineTo(x + width, y);
cr.closePath();
if (fill != null) {
cr.setSource(fill);
cr.fillPreserve();
}
if (stroke != null)
cr.setSource(stroke);
cr.stroke();
}
/**
* Convert a signal handler with n value parameters (that is, excluding the
* signal source parameter) to an array of n handlers that are each responsible
* for receiving one of the n values and calling the original handler with the
* most up-to-date arguments.
*/
function splitHandler(handler) {
if (handler.length > 30) {
throw new Error("too many parameters");
}
const count = handler.length - 1;
let missingValueBits = (1 << count) - 1;
const values = Array.from({ length: count });
return values.map((_ignored, i) => {
const mask = ~(1 << i);
return (obj, value) => {
values[i] = value;
missingValueBits &= mask;
if (missingValueBits === 0) {
handler(obj, ...values);
}
};
});
}
var IconTheme = class DashToDockIconTheme {
constructor() {
const settings = St.Settings.get();
this._iconTheme = new Gtk.IconTheme();
this._iconTheme.set_custom_theme(settings.gtkIconTheme);
this._changesId = settings.connect('notify::gtk-icon-theme', () => {
this._iconTheme.set_custom_theme(settings.gtkIconTheme);
});
}
get iconTheme() {
return this._iconTheme;
}
destroy() {
St.Settings.get().disconnect(this._changesId);
this._iconTheme = null;
}
}