forked from micheleg/dash-to-dock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
theming.js
216 lines (181 loc) · 7.66 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
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
const Clutter = imports.gi.Clutter;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Gtk = imports.gi.Gtk;
const Signals = imports.signals;
const Lang = imports.lang;
const Meta = imports.gi.Meta;
const Shell = imports.gi.Shell;
const St = imports.gi.St;
const Mainloop = imports.mainloop;
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 Tweener = imports.ui.tweener;
const Util = imports.misc.util;
const Workspace = imports.ui.workspace;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Convenience = Me.imports.convenience;
/**
* Manage theme customization and custom theme support
*/
const ThemeManager = new Lang.Class({
Name: 'DashToDock.ThemeManager',
_init: function(settings, actor, dash) {
this._settings = settings;
this._bindSettingsChanges();
this._actor = actor;
this._dash = dash;
// initialize colors with generic values
this._defaultBackground = {red: 0, green: 0, blue: 0, alpha: 0};
this._defaultBackgroundColor = {red: 0, green: 0, blue: 0, alpha: 0};
this._customizedBackground = {red: 0, green: 0, blue: 0, alpha: 0};
this._signalsHandler = new Convenience.GlobalSignalsHandler();
this._signalsHandler.add([
// When theme changes re-obtain default background color
St.ThemeContext.get_for_stage (global.stage),
'changed',
Lang.bind(this, this.updateCustomTheme)
], [
// update :overview pseudoclass
Main.overview,
'showing',
Lang.bind(this, this._onOverviewShowing)
], [
Main.overview,
'hiding',
Lang.bind(this, this._onOverviewHiding)
]);
this._updateCustomStyleClasses();
},
destroy: function() {
this._signalsHandler.destroy();
},
_onOverviewShowing: function() {
this._actor.add_style_pseudo_class('overview');
},
_onOverviewHiding: function() {
this._actor.remove_style_pseudo_class('overview');
},
_updateBackgroundOpacity: function() {
let newAlpha = this._settings.get_double('background-opacity');
this._defaultBackground = 'rgba(' +
this._defaultBackgroundColor.red + ',' +
this._defaultBackgroundColor.green + ',' +
this._defaultBackgroundColor.blue + ',' +
Math.round(this._defaultBackgroundColor.alpha/2.55)/100 + ')';
this._customizedBackground = 'rgba(' +
this._defaultBackgroundColor.red + ',' +
this._defaultBackgroundColor.green + ',' +
this._defaultBackgroundColor.blue + ',' +
newAlpha + ')';
},
_getBackgroundColor: function() {
// 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;
// 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);
this._defaultBackgroundColor = themeNode.get_background_color();
},
_updateCustomStyleClasses: function() {
if (this._settings.get_boolean('apply-custom-theme'))
this._actor.add_style_class_name('dashtodock');
else
this._actor.remove_style_class_name('dashtodock');
if (this._settings.get_boolean('custom-theme-shrink'))
this._actor.add_style_class_name('shrink');
else
this._actor.remove_style_class_name('shrink');
},
updateCustomTheme: function() {
this._updateCustomStyleClasses();
this._getBackgroundColor();
this._updateBackgroundOpacity();
this._adjustTheme();
this._dash._redisplay();
},
/**
* Reimported back and adapted from atomdock
*/
_adjustTheme: function() {
// 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;
// Remove prior style edits
this._dash._container.set_style(null);
// If built-in theme is enabled do nothing else
if (this._settings.get_boolean('apply-custom-theme'))
return;
let newStyle = '';
let position = Convenience.getPosition(this._settings);
if (!this._settings.get_boolean('custom-theme-shrink')) {
// obtain theme border settings
let themeNode = this._dash._container.get_theme_node();
let borderColor = themeNode.get_border_color(St.Side.TOP);
let borderWidth = themeNode.get_border_width(St.Side.TOP);
let borderRadius = themeNode.get_border_radius(St.Corner.TOPRIGHT);
// 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 borderRadiusValue = '';
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() + ';';
switch (position) {
case St.Side.LEFT:
borderInner = 'border-left';
borderRadiusValue = '0 ' + borderRadius + 'px ' + borderRadius + 'px 0;';
break;
case St.Side.RIGHT:
borderInner = 'border-right';
borderRadiusValue = borderRadius + 'px 0 0 ' + borderRadius + 'px;';
break;
case St.Side.TOP:
borderInner = 'border-top';
borderRadiusValue = '0 0 ' + borderRadius + 'px ' + borderRadius + 'px;';
break;
case St.Side.BOTTOM:
borderInner = 'border-bottom';
borderRadiusValue = borderRadius + 'px ' + borderRadius + 'px 0 0;';
break;
}
newStyle = borderInner + ': none;' +
'border-radius: ' + borderRadiusValue +
borderMissingStyle;
// 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._container.set_style(newStyle);
}
// Customize background
if (this._settings.get_boolean('opaque-background')) {
newStyle = newStyle + 'background-color:'+ this._customizedBackground + '; ' +
'transition-delay: 0s; transition-duration: 0.250s;';
this._dash._container.set_style(newStyle);
}
},
_bindSettingsChanges: function() {
let keys = ['opaque-background',
'background-opacity',
'apply-custom-theme',
'custom-theme-shrink',
'extend-height'];
keys.forEach(function(key) {
this._settings.connect('changed::' + key, Lang.bind(this, this.updateCustomTheme));
}, this);
}
});