forked from ewlsh/dash-to-dock
-
Notifications
You must be signed in to change notification settings - Fork 2
/
theming.js
557 lines (467 loc) · 20.1 KB
/
theming.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Signals = imports.signals;
const Meta = imports.gi.Meta;
const Shell = imports.gi.Shell;
const St = imports.gi.St;
const Clutter = imports.gi.Clutter;
const AppDisplay = imports.ui.appDisplay;
const AppFavorites = imports.ui.appFavorites;
const Dash = imports.ui.dash;
const DND = imports.ui.dnd;
const IconGrid = imports.ui.iconGrid;
const Main = imports.ui.main;
const PopupMenu = imports.ui.popupMenu;
const Util = imports.misc.util;
const Workspace = imports.ui.workspace;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Docking = Me.imports.docking;
const Utils = Me.imports.utils;
/*
* DEFAULT: transparency given by theme
* FIXED: constant transparency chosen by user
* DYNAMIC: apply 'transparent' style when no windows are close to the dock
* */
const TransparencyMode = {
DEFAULT: 0,
FIXED: 1,
DYNAMIC: 3
};
/**
* Manage theme customization and custom theme support
*/
var ThemeManager = class DashToDock_ThemeManager {
constructor(dock) {
this._signalsHandler = new Utils.GlobalSignalsHandler(this);
this._bindSettingsChanges();
this._actor = dock;
this._dash = dock.dash;
// initialize colors with generic values
this._customizedBackground = {red: 0, green: 0, blue: 0, alpha: 0};
this._customizedBorder = {red: 0, green: 0, blue: 0, alpha: 0};
this._transparency = new Transparency(dock);
this._signalsHandler.add([
// When theme changes re-obtain default background color
St.ThemeContext.get_for_stage (global.stage),
'changed',
this.updateCustomTheme.bind(this)
], [
// update :overview pseudoclass
Main.overview,
'showing',
this._onOverviewShowing.bind(this)
], [
Main.overview,
'hiding',
this._onOverviewHiding.bind(this)
]);
this._updateCustomStyleClasses();
// destroy themeManager when the managed actor is destroyed (e.g. extension unload)
// in order to disconnect signals
this._signalsHandler.add(this._actor, 'destroy', () => this.destroy());
}
destroy() {
this.emit('destroy');
this._transparency.destroy();
this._destroyed = true;
}
_onOverviewShowing() {
this._actor.add_style_pseudo_class('overview');
}
_onOverviewHiding() {
this._actor.remove_style_pseudo_class('overview');
}
_updateDashOpacity() {
let newAlpha = Docking.DockManager.settings.get_double('background-opacity');
let [backgroundColor, borderColor] = this._getDefaultColors();
if (backgroundColor==null)
return;
// Get the background and border alphas. We check the background alpha
// for a minimum of .001 to prevent division by 0 errors
let backgroundAlpha = Math.max(Math.round(backgroundColor.alpha/2.55)/100, .001);
let borderAlpha = Math.round(borderColor.alpha/2.55)/100;
// The border and background alphas should remain in sync
// We also limit the borderAlpha to a maximum of 1 (full opacity)
borderAlpha = Math.min((borderAlpha/backgroundAlpha)*newAlpha, 1);
this._customizedBackground = 'rgba(' +
backgroundColor.red + ',' +
backgroundColor.green + ',' +
backgroundColor.blue + ',' +
newAlpha + ')';
this._customizedBorder = 'rgba(' +
borderColor.red + ',' +
borderColor.green + ',' +
borderColor.blue + ',' +
borderAlpha + ')';
}
_getDefaultColors() {
// Prevent shell crash if the actor is not on the stage.
// It happens enabling/disabling repeatedly the extension
if (!this._dash._container.get_stage())
return [null, null];
// Remove custom style
let oldStyle = this._dash._container.get_style();
this._dash._container.set_style(null);
let themeNode = this._dash._container.get_theme_node();
this._dash._container.set_style(oldStyle);
let backgroundColor = themeNode.get_background_color();
// Just in case the theme has different border colors ..
// We want to find the inside border-color of the dock because it is
// the side most visible to the user. We do this by finding the side
// opposite the position
let position = Utils.getPosition();
let side = position + 2;
if (side > 3)
side = Math.abs(side - 4);
let borderColor = themeNode.get_border_color(side);
return [backgroundColor, borderColor];
}
_updateDashColor() {
// Retrieve the color. If needed we will adjust it before passing it to
// this._transparency.
let [backgroundColor, borderColor] = this._getDefaultColors();
if (backgroundColor==null)
return;
let settings = Docking.DockManager.settings;
if (settings.get_boolean('custom-background-color')) {
// When applying a custom color, we need to check the alpha value,
// if not the opacity will always be overridden by the color below.
// Note that if using 'dynamic' transparency modes,
// the opacity will be set by the opaque/transparent styles anyway.
let newAlpha = Math.round(backgroundColor.alpha/2.55)/100;
if (settings.get_enum('transparency-mode') == TransparencyMode.FIXED)
newAlpha = settings.get_double('background-opacity');
backgroundColor = settings.get_string('background-color');
this._customizedBackground = backgroundColor;
this._customizedBorder = this._customizedBackground;
// backgroundColor is a string like rgb(0,0,0)
const [ret, color] = Clutter.Color.from_string(backgroundColor);
if (!ret) {
logError(new Error(`${backgroundColor} is not a valid color string`));
return;
}
color.alpha = newAlpha * 255;
this._transparency.setColor(color);
} else {
// backgroundColor is a Clutter.Color object
this._transparency.setColor(backgroundColor);
}
}
_updateCustomStyleClasses() {
let settings = Docking.DockManager.settings;
if (settings.get_boolean('apply-custom-theme'))
this._actor.add_style_class_name('dashtodock');
else
this._actor.remove_style_class_name('dashtodock');
if (settings.get_boolean('custom-theme-shrink'))
this._actor.add_style_class_name('shrink');
else
this._actor.remove_style_class_name('shrink');
if (settings.get_enum('running-indicator-style') !== 0)
this._actor.add_style_class_name('running-dots');
else
this._actor.remove_style_class_name('running-dots');
// If not the built-in theme option is not selected
if (!settings.get_boolean('apply-custom-theme')) {
if (settings.get_boolean('force-straight-corner'))
this._actor.add_style_class_name('straight-corner');
else
this._actor.remove_style_class_name('straight-corner');
} else {
this._actor.remove_style_class_name('straight-corner');
}
}
updateCustomTheme() {
if (this._destroyed)
throw new Error(`Impossible to update a destroyed ${this.constructor.name}`);
this._updateCustomStyleClasses();
this._updateDashOpacity();
this._updateDashColor();
this._adjustTheme();
this.emit('updated');
}
/**
* Reimported back and adapted from atomdock
*/
_adjustTheme() {
// Prevent shell crash if the actor is not on the stage.
// It happens enabling/disabling repeatedly the extension
if (!this._dash._background.get_stage())
return;
let settings = Docking.DockManager.settings;
// Remove prior style edits
this._dash._background.set_style(null);
this._transparency.disable();
// If built-in theme is enabled do nothing else
if (settings.get_boolean('apply-custom-theme'))
return;
let newStyle = '';
let position = Utils.getPosition(settings);
// obtain theme border settings
let themeNode = this._dash._background.get_theme_node();
let borderColor = themeNode.get_border_color(St.Side.TOP);
let borderWidth = themeNode.get_border_width(St.Side.TOP);
// We're copying border and corner styles to left border and top-left
// corner, also removing bottom border and bottom-right corner styles
let borderInner = '';
let borderMissingStyle = '';
if (this._rtl && (position != St.Side.RIGHT))
borderMissingStyle = 'border-right: ' + borderWidth + 'px solid ' +
borderColor.to_string() + ';';
else if (!this._rtl && (position != St.Side.LEFT))
borderMissingStyle = 'border-left: ' + borderWidth + 'px solid ' +
borderColor.to_string() + ';';
newStyle = borderMissingStyle;
if (newStyle) {
// I do call set_style possibly twice so that only the background gets the transition.
// The transition-property css rules seems to be unsupported
this._dash._background.set_style(newStyle);
}
// Customize background
let fixedTransparency = settings.get_enum('transparency-mode') == TransparencyMode.FIXED;
let defaultTransparency = settings.get_enum('transparency-mode') == TransparencyMode.DEFAULT;
if (!defaultTransparency && !fixedTransparency) {
this._transparency.enable();
}
else if (!defaultTransparency || settings.get_boolean('custom-background-color')) {
newStyle = newStyle + 'background-color:'+ this._customizedBackground + '; ' +
'border-color:'+ this._customizedBorder + '; ' +
'transition-delay: 0s; transition-duration: 0.250s;';
this._dash._background.set_style(newStyle);
}
}
_bindSettingsChanges() {
let keys = ['transparency-mode',
'customize-alphas',
'min-alpha',
'max-alpha',
'background-opacity',
'custom-background-color',
'background-color',
'apply-custom-theme',
'custom-theme-shrink',
'custom-theme-running-dots',
'extend-height',
'force-straight-corner'];
this._signalsHandler.add(...keys.map(key => [
Docking.DockManager.settings,
`changed::${key}`,
() => this.updateCustomTheme(),
]));
}
};
Signals.addSignalMethods(ThemeManager.prototype);
/**
* The following class is based on the following upstream commit:
* https://git.gnome.org/browse/gnome-shell/commit/?id=447bf55e45b00426ed908b1b1035f472c2466956
* Transparency when free-floating
*/
var Transparency = class DashToDock_Transparency {
constructor(dock) {
this._dash = dock.dash;
this._actor = this._dash._container;
this._backgroundActor = this._dash._background;
this._dockActor = dock;
this._dock = dock;
this._panel = Main.panel;
this._position = Utils.getPosition();
// All these properties are replaced with the ones in the .dummy-opaque and .dummy-transparent css classes
this._backgroundColor = '0,0,0';
this._transparentAlpha = '0.2';
this._opaqueAlpha = '1';
this._transparentAlphaBorder = '0.1';
this._opaqueAlphaBorder = '0.5';
this._transparentTransition = '0ms';
this._opaqueTransition = '0ms';
this._base_actor_style = "";
this._signalsHandler = new Utils.GlobalSignalsHandler();
this._trackedWindows = new Map();
}
enable() {
// ensure I never double-register/inject
// although it should never happen
this.disable();
this._base_actor_style = this._actor.get_style();
if (this._base_actor_style == null) {
this._base_actor_style = "";
}
this._signalsHandler.addWithLabel('transparency', [
global.window_group,
'actor-added',
this._onWindowActorAdded.bind(this)
], [
global.window_group,
'actor-removed',
this._onWindowActorRemoved.bind(this)
], [
global.window_manager,
'switch-workspace',
this._updateSolidStyle.bind(this)
], [
Main.overview,
'hiding',
this._updateSolidStyle.bind(this)
], [
Main.overview,
'showing',
this._updateSolidStyle.bind(this)
]);
// Window signals
global.window_group.get_children().filter(function(child) {
// An irrelevant window actor ('Gnome-shell') produces an error when the signals are
// disconnected, therefore do not add signals to it.
return child instanceof Meta.WindowActor &&
child.get_meta_window().get_wm_class() !== 'Gnome-shell';
}).forEach(function(win) {
this._onWindowActorAdded(null, win);
}, this);
if (this._actor.get_stage())
this._updateSolidStyle();
this._updateStyles();
this._updateSolidStyle();
this.emit('transparency-enabled');
}
disable() {
// ensure I never double-register/inject
// although it should never happen
this._signalsHandler.removeWithLabel('transparency');
for (let key of this._trackedWindows.keys())
this._trackedWindows.get(key).forEach(id => {
key.disconnect(id);
});
this._trackedWindows.clear();
this.emit('transparency-disabled');
}
destroy() {
this.disable();
this._signalsHandler.destroy();
}
_onWindowActorAdded(container, metaWindowActor) {
let signalIds = [];
['notify::allocation', 'notify::visible'].forEach(s => {
signalIds.push(metaWindowActor.connect(s, this._updateSolidStyle.bind(this)));
});
this._trackedWindows.set(metaWindowActor, signalIds);
}
_onWindowActorRemoved(container, metaWindowActor) {
if (!this._trackedWindows.get(metaWindowActor))
return;
this._trackedWindows.get(metaWindowActor).forEach(id => {
metaWindowActor.disconnect(id);
});
this._trackedWindows.delete(metaWindowActor);
this._updateSolidStyle();
}
_updateSolidStyle() {
let isNear = this._dockIsNear();
if (isNear) {
this._backgroundActor.set_style(this._opaque_style);
this._dockActor.remove_style_class_name('transparent');
this._dockActor.add_style_class_name('opaque');
}
else {
this._backgroundActor.set_style(this._transparent_style);
this._dockActor.remove_style_class_name('opaque');
this._dockActor.add_style_class_name('transparent');
}
this.emit('solid-style-updated', isNear);
}
_dockIsNear() {
if (this._dockActor.has_style_pseudo_class('overview'))
return false;
/* Get all the windows in the active workspace that are in the primary monitor and visible */
let activeWorkspace = global.workspace_manager.get_active_workspace();
let dash = this._dash;
let windows = activeWorkspace.list_windows().filter(function(metaWindow) {
return metaWindow.get_monitor() === dash._monitorIndex &&
metaWindow.showing_on_its_workspace() &&
metaWindow.get_window_type() != Meta.WindowType.DESKTOP;
});
/* Check if at least one window is near enough to the panel.
* If the dock is hidden, we need to account for the space it would take
* up when it slides out. This is avoid an ugly transition.
* */
let factor = 0;
if (!Docking.DockManager.settings.dockFixed &&
this._dock.getDockState() == Docking.State.HIDDEN)
factor = 1;
let [leftCoord, topCoord] = this._actor.get_transformed_position();
let threshold;
if (this._position === St.Side.LEFT)
threshold = leftCoord + this._actor.get_width() * (factor + 1);
else if (this._position === St.Side.RIGHT)
threshold = leftCoord - this._actor.get_width() * factor;
else if (this._position === St.Side.TOP)
threshold = topCoord + this._actor.get_height() * (factor + 1);
else
threshold = topCoord - this._actor.get_height() * factor;
let scale = St.ThemeContext.get_for_stage(global.stage).scale_factor;
let isNearEnough = windows.some((metaWindow) => {
let coord;
if (this._position === St.Side.LEFT) {
coord = metaWindow.get_frame_rect().x;
return coord < threshold + 5 * scale;
}
else if (this._position === St.Side.RIGHT) {
coord = metaWindow.get_frame_rect().x + metaWindow.get_frame_rect().width;
return coord > threshold - 5 * scale;
}
else if (this._position === St.Side.TOP) {
coord = metaWindow.get_frame_rect().y;
return coord < threshold + 5 * scale;
}
else {
coord = metaWindow.get_frame_rect().y + metaWindow.get_frame_rect().height;
return coord > threshold - 5 * scale;
}
});
return isNearEnough;
}
_updateStyles() {
this._getAlphas();
this._transparent_style = this._base_actor_style +
'background-color: rgba(' +
this._backgroundColor + ', ' + this._transparentAlpha + ');' +
'border-color: rgba(' +
this._backgroundColor + ', ' + this._transparentAlphaBorder + ');' +
'transition-duration: ' + this._transparentTransition + 'ms;';
this._opaque_style = this._base_actor_style +
'background-color: rgba(' +
this._backgroundColor + ', ' + this._opaqueAlpha + ');' +
'border-color: rgba(' +
this._backgroundColor + ',' + this._opaqueAlphaBorder + ');' +
'transition-duration: ' + this._opaqueTransition + 'ms;';
this.emit('styles-updated');
}
setColor(color) {
this._backgroundColor = color.red + ',' + color.green + ',' + color.blue;
this._updateStyles();
}
_getAlphas() {
// Create dummy object and add to the uiGroup to get it to the stage
let dummyObject = new St.Bin({
name: 'dashtodockContainer',
});
Main.uiGroup.add_child(dummyObject);
dummyObject.add_style_class_name('dummy-opaque');
let themeNode = dummyObject.get_theme_node();
this._opaqueAlpha = themeNode.get_background_color().alpha / 255;
this._opaqueAlphaBorder = themeNode.get_border_color(0).alpha / 255;
this._opaqueTransition = themeNode.get_transition_duration();
dummyObject.add_style_class_name('dummy-transparent');
themeNode = dummyObject.get_theme_node();
this._transparentAlpha = themeNode.get_background_color().alpha / 255;
this._transparentAlphaBorder = themeNode.get_border_color(0).alpha / 255;
this._transparentTransition = themeNode.get_transition_duration();
Main.uiGroup.remove_child(dummyObject);
let settings = Docking.DockManager.settings;
if (settings.get_boolean('customize-alphas')) {
this._opaqueAlpha = settings.get_double('max-alpha');
this._opaqueAlphaBorder = this._opaqueAlpha / 2;
this._transparentAlpha = settings.get_double('min-alpha');
this._transparentAlphaBorder = this._transparentAlpha / 2;
}
}
};
Signals.addSignalMethods(Transparency.prototype);