BlueGit

timfa kwin-marble

Files

kwin-marble / kwin6_effect_marble / contents / code / main.js

main.js

main.js 13.2 KiB Download Raw
1// SPDX-FileCopyrightText: Vlad Zahorodnii <vlad.zahorodnii@kde.org>, Martin Flöser
2// <mgraesslin@kde.org>, Simon Schneegans <code@simonschneegans.de>
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5'use strict';
6
7const classBlacklist = [
8 // The logout screen has to be animated only by the logout effect.
9 'ksmserver ksmserver', 'ksmserver-logout-greeter ksmserver-logout-greeter',
10
11 // KDE Plasma splash screen has to be animated only by the login effect.
12 'ksplashqml ksplashqml',
13
14 // All the tooltips of IntelliJ IDEs use the same window class as their main window.
15 // This results in many awkward animations, so we have to blacklist them all.
16 // See https://github.com/Schneegans/Burn-My-Windows/issues/284 for details.
17 'jetbrains-studio jetbrains-studio', 'jetbrains-aqua jetbrains-aqua',
18 'jetbrains-clion jetbrains-clion', 'jetbrains-datagrip jetbrains-datagrip',
19 'jetbrains-dataspell jetbrains-dataspell', 'jetbrains-goland jetbrains-goland',
20 'jetbrains-idea jetbrains-idea', 'jetbrains-idea-ce jetbrains-idea-ce',
21 'jetbrains-phpstorm jetbrains-phpstorm', 'jetbrains-pycharm jetbrains-pycharm',
22 'jetbrains-pycharm-ce jetbrains-pycharm-ce', 'jetbrains-rider jetbrains-rider',
23 'jetbrains-rubymine jetbrains-rubymine', 'jetbrains-webstorm jetbrains-webstorm'
24];
25
26const nameBlacklist = [
27 // Kando uses a normal window, but it should look like a popup menu. We do not
28 // blacklist it by class as this would also affect the settings window.
29 'Kando Menu'
30];
31
32class BurnMyWindowsMarbleEffect {
33 constructor() {
34 effect.configChanged.connect(this.loadConfig.bind(this));
35 effect.animationEnded.connect(this.cleanupForcedRoles.bind(this));
36 effect.animationEnded.connect(this.slotAnimationEnded.bind(this));
37 //effects.windowAdded.connect(this.slotWindowAdded.bind(this));
38 effects.windowClosed.connect(this.slotWindowClosed.bind(this));
39 effects.windowDataChanged.connect(this.slotWindowDataChanged.bind(this));
40
41 this.shader = effect.addFragmentShader(Effect.MapTexture, 'marble.frag');
42
43 this.loadConfig();
44 }
45
46 // A small helper which can be used to read a hex color string (e.g. #ff3244) from the
47 // settings. This method will return an array of four floating point values
48 // [r, g, b, a]. If the settings key only refers to an rgb value, the alpha component
49 // will be set to 1.0.
50 readRGBAConfig(key) {
51 const raw = String(effect.readConfig(key, '#ffffff'));
52 const parts = raw.split(',').map(x => parseFloat(x.trim()));
53 if (parts.length >= 3 && parts.every(x => !isNaN(x))) {
54 const c = parts.map(x => x / 255);
55 return [c[0], c[1], c[2], c.length > 3 ? c[3] : 1.0];
56 }
57 const color = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?([a-f\d]{2})?$/i
58 .exec(effect.readConfig(key, '#ffffff'))
59 .slice(1)
60 .filter(x => x !== undefined)
61 .map(x => parseInt(x, 16) / 255);
62 return color.length == 3 ? [color[0], color[1], color[2], 1.0] :
63 [color[1], color[2], color[3], color[0]];
64 }
65
66 // A small helper which can be used to read a hex color string (e.g. #ff3244) from the
67 // settings. This method will return an array of three floating point values [r, g, b]
68 // regardless of whether the settings key actually refers to an argb hex string.
69 readRGBConfig(key) {
70 const color = this.readRGBAConfig(key);
71 color.pop();
72 return color;
73 }
74
75 // This is called when the effect is loaded and whenever the settings of the effect
76 // are changed by the user.
77 loadConfig() {
78 this.duration = animationTime(effect.readConfig("Duration", 200));
79 this.pause = animationTime(Number(effect.readConfig("Pause", 400)));
80 this.gravity = Math.max(100, Number(effect.readConfig('Gravity', 4500))); // px/s^2
81 let sh = 1080;
82 try { sh = effects.virtualScreenGeometry.height || sh; } catch (e) {}
83 this.fallDistance = 1.5 * sh;
84 // free fall from rest: d = 0.5 g t^2 -> t = sqrt(2d/g); scaled by the global speed factor
85 this.fallDuration = animationTime(Math.round(1000 * Math.sqrt(2 * this.fallDistance / this.gravity)));
86 effect.setUniform(this.shader, 'uDuration', (this.duration + this.pause + this.fallDuration) * 0.001);
87 // 0..10 colours, 11 = random. Values saved by v0.3.0 (10 = white, 12 = random) fall back safely.
88 let mc = Math.floor(Number(effect.readConfig('MixColor', 0)));
89 if (!(mc >= 0)) mc = 0; else if (mc > 11) mc = 11;
90 this.tintMode = mc;
91 this.applyTint(null, false);
92 effect.setUniform(this.shader, 'uMarbleSize', Math.max(4, Number(effect.readConfig('MarbleSize', 42))));
93 this.debug = Math.max(0, Number(effect.readConfig('DebugMarble', 0)) | 0);
94 effect.setUniform(this.shader, 'uDebug', this.debug);
95 effect.setUniform(this.shader, 'uMixOuter', Math.min(100, Math.max(0, Number(effect.readConfig('BlackMixOuter', 80)))) / 100);
96 effect.setUniform(this.shader, 'uMixCenter', Math.min(100, Math.max(0, Number(effect.readConfig('BlackMixCenter', 5)))) / 100);
97 effect.setUniform(this.shader, 'uCrumpleEnd', this.duration / Math.max(1, this.duration + this.pause + this.fallDuration));
98 }
99
100 // Mix target colour. tintMode 11 = random; with reuse, a random colour picked for this
101 // window (e.g. at minimize) is used again (e.g. at unminimize) and then forgotten.
102 // uProgress value for this window: tint base + morph-normalised progress m (capped, see shader)
103 prog(window, m) {
104 const v = (window.bmwBase || 0) + Math.min(4000, Math.max(0, m));
105 if (this.debug) console.log('MARBLEDBG prog caption=' + window.caption + ' m=' + m + ' v=' + v);
106 return v;
107 }
108
109 crumpleEnd() { return Math.max(1e-4, this.duration / Math.max(1, this.duration + this.pause + this.fallDuration)); }
110
111 applyTint(window, reuse) {
112 const C = [[0.0000,0.0000,0.0000],[0.0000,0.0000,0.6667],[0.6667,0.0000,0.0000],[0.0000,0.6667,0.0000],[0.4000,0.0000,0.6667],[0.6667,0.6667,0.0000],[0.6667,0.3333,0.0000],[0.0000,0.6667,0.6667],[0.6667,0.0000,0.4000],[0.3333,0.3333,0.3333],[0.3333,0.2000,0.0000]];
113 let c;
114 if (this.tintMode < C.length) { c = C[this.tintMode]; if (window) window.bmwTint = null; }
115 else if (window && reuse && window.bmwTint) { c = window.bmwTint; window.bmwTint = null; }
116 else if (window) { // random: per-window number (window.internalId is undefined in scripted effects)
117 if (!(window.bmwSeq >= 1)) window.bmwSeq = (this.seq = (this.seq || 0) + 1);
118 c = C[window.bmwSeq % C.length]; window.bmwTint = c;
119 }
120 else c = C[0];
121 if (window) window.bmwBase = 4096 * Math.max(0, C.findIndex(x => x[0] == c[0] && x[1] == c[1] && x[2] == c[2])); // per-window tint, carried in uProgress
122 if (this.debug && window) console.log('MARBLEDBG tint caption=' + window.caption + ' seq=' + window.bmwSeq + ' reuse=' + reuse + ' mode=' + this.tintMode + ' base=' + window.bmwBase + ' internalId=' + typeof window.internalId + ' windowId=' + window.windowId + ' minimized=' + window.minimized);
123 effect.setUniform(this.shader, 'uTintR', c[0]);
124 effect.setUniform(this.shader, 'uTintG', c[1]);
125 effect.setUniform(this.shader, 'uTintB', c[2]);
126 }
127
128 static shouldAnimateWindow(window) {
129
130 // We don't want to animate most of plasmashell's windows, yet, some
131 // of them we want to, for example, Task Manager Settings window.
132 // The problem is that all those window share single window class.
133 // So, the only way to decide whether a window should be animated is
134 // to use a heuristic: if a window has decoration, then it's most
135 // likely a dialog or a settings window so we have to animate it.
136 if (window.windowClass == 'plasmashell plasmashell' ||
137 window.windowClass == 'plasmashell org.kde.plasmashell') {
138 return window.hasDecoration;
139 }
140
141 // This is primarily to avoid animating the AltTab popup.
142 // See https://github.com/Schneegans/Burn-My-Windows/issues/384 for details.
143 if (!window.hasDecoration && window.onAllDesktops) {
144 return false
145 }
146
147 if (classBlacklist.indexOf(window.windowClass) != -1) {
148 return false;
149 }
150
151 if (nameBlacklist.indexOf(window.caption) != -1) {
152 return false;
153 }
154
155 // Spectacle: animate only its decorated windows (result/preview), never the
156 // undecorated region-selection overlay.
157 if (window.windowClass == 'spectacle org.kde.spectacle' && !window.hasDecoration) {
158 return false;
159 }
160
161 if (window.hasDecoration) {
162 return true;
163 }
164
165 // Don't animate combobox popups, tooltips, popup menus, etc.
166 if (window.popupWindow) {
167 return false;
168 }
169
170 // Dont't animate the outline and the screenlocker as it looks bad.
171 if (window.lockScreen || window.outline) {
172 return false;
173 }
174
175 // Override-redirect windows are usually used for user interface
176 // concepts that are not expected to be animated by this effect.
177 if (!window.managed) {
178 return false;
179 }
180
181 return window.normalWindow || window.dialog;
182 }
183
184 setupForcedRoles(window) {
185 // marble: no forced blur/contrast (blurred window-sized rect + cost)
186 // window.setData(Effect.WindowForceBackgroundContrastRole, true);
187 // marble: no forced blur/contrast (blurred window-sized rect + cost)
188 // window.setData(Effect.WindowForceBlurRole, true);
189 }
190
191 cleanupForcedRoles(window) {
192 window.setData(Effect.WindowForceBackgroundContrastRole, null);
193 window.setData(Effect.WindowForceBlurRole, null);
194 }
195
196 slotWindowAdded(window) {
197 if (effects.hasActiveFullScreenEffect) {
198 return;
199 }
200 if (!BurnMyWindowsMarbleEffect.shouldAnimateWindow(window)) {
201 return;
202 }
203 if (!window.visible) {
204 return;
205 }
206 if (effect.isGrabbed(window, Effect.WindowAddedGrabRole)) {
207 return;
208 }
209 this.setupForcedRoles(window);
210
211 this.applyTint(window, false);
212 effect.setUniform(this.shader, 'uForOpening', 1.0);
213 effect.setUniform(this.shader, 'uIsFullscreen', window.fullScreen ? 1.0 : 0.0);
214
215
216
217 window.bmwInAnimation = animate({
218 window: window,
219 curve: QEasingCurve.Linear,
220 duration: this.duration,
221 animations: [{
222 type: Effect.ShaderUniform,
223 fragmentShader: this.shader,
224 uniform: 'uProgress',
225 from: this.prog(window, 0.0),
226 to: this.prog(window, 1.0 / this.crumpleEnd())
227 }]
228 });
229 }
230
231 slotWindowClosed(window) {
232 if (effects.hasActiveFullScreenEffect) {
233 return;
234 }
235 if (!BurnMyWindowsMarbleEffect.shouldAnimateWindow(window)) {
236 return;
237 }
238 if (!window.visible || window.skipsCloseAnimation) {
239 return;
240 }
241 if (effect.isGrabbed(window, Effect.WindowClosedGrabRole)) {
242 return;
243 }
244 const oldIn = window.bmwInAnimation; // cancelled after the close animation exists (keeps the snapshot)
245 delete window.bmwInAnimation;
246 this.setupForcedRoles(window);
247
248 this.applyTint(window, false);
249 effect.setUniform(this.shader, 'uForOpening', 0.0);
250 effect.setUniform(this.shader, 'uIsFullscreen', window.fullScreen ? 1.0 : 0.0);
251
252
253
254 // 1) marble morph (shader); runs the whole time so the marble stays drawn while falling
255 window.bmwOutAnimation = animate({
256 window: window,
257 curve: QEasingCurve.Linear,
258 duration: this.duration + this.pause + this.fallDuration,
259 animations: [{
260 type: Effect.ShaderUniform,
261 fragmentShader: this.shader,
262 uniform: 'uProgress',
263 from: this.prog(window, 0.0),
264 to: this.prog(window, 1.0 / this.crumpleEnd())
265 }]
266 });
267 if (oldIn) cancel(oldIn);
268 // 2) marker animation that ends exactly when the marble is done; the fall is
269 // started from its animationEnded (KWin ignored the per-call delay).
270 window.bmwMarbleDone = animate({
271 window: window,
272 curve: QEasingCurve.Linear,
273 duration: this.duration + this.pause,
274 animations: [{ type: Effect.Translation, from: {value1: 0.0, value2: 0.0}, to: {value1: 0.0, value2: 0.0} }]
275 });
276 window.bmwFallPending = true;
277 }
278
279 slotAnimationEnded(window, id) {
280 // marble is drawn outside the window's own damage region: repaint all on every end
281 this.repaintAll();
282 if (!window || !window.bmwFallPending) return; // first end = marble marker (ids arrive as 0)
283 window.bmwFallPending = false;
284 // free fall from rest: InQuad = t^2 = constant acceleration, d = 0.5 g t^2
285 window.bmwFallAnimation = animate({
286 window: window,
287 curve: QEasingCurve.InQuad,
288 duration: this.fallDuration,
289 animations: [{ type: Effect.Translation, from: {value1: 0.0, value2: 0.0},
290 to: {value1: 0.0, value2: this.fallDistance} }]
291 });
292 }
293
294 repaintAll() {
295 try { effects.addRepaintFull(); } catch (e) {
296 if (!this.bmwWarned) { this.bmwWarned = true; console.warn("marble: addRepaintFull unavailable: " + e); }
297 }
298 }
299
300 slotWindowDataChanged(window, role) {
301 if (role == Effect.WindowAddedGrabRole) {
302 if (window.bmwInAnimation && effect.isGrabbed(window, role)) {
303 cancel(window.bmwInAnimation);
304 delete window.bmwInAnimation;
305 this.cleanupForcedRoles(window);
306 }
307 } else if (role == Effect.WindowClosedGrabRole) {
308 if (window.bmwOutAnimation && effect.isGrabbed(window, role)) {
309 cancel(window.bmwOutAnimation);
310 delete window.bmwOutAnimation;
311 this.cleanupForcedRoles(window);
312 }
313 }
314 }
315}
316
317new BurnMyWindowsMarbleEffect();