BlueGit

timfa kwin-marble

Files

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

main.js

main.js 14.4 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 named colours, 11 = Willekeurig, 12 = Semiwillekeurig. Older >12 → 12.
88 let mc = Math.floor(Number(effect.readConfig('MixColor', 0)));
89 if (!(mc >= 0)) mc = 0; else if (mc > 12) mc = 12;
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 crumpleEnd() { return Math.max(1e-4, this.duration / Math.max(1, this.duration + this.pause + this.fallDuration)); }
101
102
103 // Mix target. tintMode 0..10 = named colours; 11 = Willekeurig (seq → hue);
104 // 12 = Semiwillekeurig (hash of window.windowClass → hue). Random modes use HSV
105 // with fixed S=1 and V=0.6667 (matches the named #aa… glass colours), not a palette.
106 prog(window, m) {
107 const v = (window.bmwBase || 0) + Math.min(4000, Math.max(0, m));
108 if (this.debug) console.log('MARBLEDBG prog caption=' + window.caption + ' m=' + m + ' v=' + v);
109 return v;
110 }
111
112 hsvToRgb(h, s, v) {
113 const c = v * s, x = c * (1 - Math.abs((h / 60) % 2 - 1)), m = v - c;
114 let r = 0, g = 0, b = 0;
115 if (h < 60) { r = c; g = x; }
116 else if (h < 120) { r = x; g = c; }
117 else if (h < 180) { g = c; b = x; }
118 else if (h < 240) { g = x; b = c; }
119 else if (h < 300) { r = x; b = c; }
120 else { r = c; b = x; }
121 return [r + m, g + m, b + m];
122 }
123
124 hashHue(str) {
125 let h = 2166136261;
126 for (let i = 0; i < str.length; i++) { h ^= str.charCodeAt(i); h = Math.imul(h, 16777619); }
127 return (h >>> 0) % 360;
128 }
129
130 applyTint(window, reuse) {
131 const FIXED = [[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]];
132 const HS = 1.0, HV = 0.6667; // same peak channel as #aa0000 / #0000aa / …
133 let c, hue = null;
134 if (this.tintMode < FIXED.length) {
135 c = FIXED[this.tintMode];
136 if (window) { window.bmwTint = null; window.bmwHue = null; }
137 } else if (this.tintMode === 12 && window) {
138 // Semiwillekeurig: stable per application (windowClass); empty → black
139 const cls = String(window.windowClass || '').trim();
140 if (!cls) { c = FIXED[0]; hue = null; }
141 else { hue = this.hashHue(cls); c = this.hsvToRgb(hue, HS, HV); }
142 window.bmwTint = c; window.bmwHue = hue;
143 } else if (window && reuse && window.bmwTint) {
144 c = window.bmwTint; hue = (window.bmwHue == null ? null : window.bmwHue);
145 window.bmwTint = null;
146 } else if (window) {
147 // Willekeurig: next sequence number → hue spaced by ~golden angle
148 if (!(window.bmwSeq >= 1)) window.bmwSeq = (this.seq = (this.seq || 0) + 1);
149 hue = Math.floor((window.bmwSeq * 137.508) % 360);
150 c = this.hsvToRgb(hue, HS, HV);
151 window.bmwTint = c; window.bmwHue = hue;
152 } else {
153 c = FIXED[0];
154 }
155 if (window) {
156 // Shader: tk < 11 → named table; tk >= 1000 → HSV hue = tk - 1000
157 window.bmwBase = (hue == null)
158 ? 4096 * Math.max(0, FIXED.findIndex(x => x[0] == c[0] && x[1] == c[1] && x[2] == c[2]))
159 : 4096 * (1000 + (hue | 0));
160 }
161 if (this.debug && window) console.log('MARBLEDBG tint caption=' + window.caption + ' class=' + window.windowClass + ' seq=' + window.bmwSeq + ' hue=' + hue + ' reuse=' + reuse + ' mode=' + this.tintMode + ' base=' + window.bmwBase);
162 effect.setUniform(this.shader, 'uTintR', c[0]);
163 effect.setUniform(this.shader, 'uTintG', c[1]);
164 effect.setUniform(this.shader, 'uTintB', c[2]);
165 }
166
167 static shouldAnimateWindow(window) {
168
169 // We don't want to animate most of plasmashell's windows, yet, some
170 // of them we want to, for example, Task Manager Settings window.
171 // The problem is that all those window share single window class.
172 // So, the only way to decide whether a window should be animated is
173 // to use a heuristic: if a window has decoration, then it's most
174 // likely a dialog or a settings window so we have to animate it.
175 if (window.windowClass == 'plasmashell plasmashell' ||
176 window.windowClass == 'plasmashell org.kde.plasmashell') {
177 return window.hasDecoration;
178 }
179
180 // This is primarily to avoid animating the AltTab popup.
181 // See https://github.com/Schneegans/Burn-My-Windows/issues/384 for details.
182 if (!window.hasDecoration && window.onAllDesktops) {
183 return false
184 }
185
186 if (classBlacklist.indexOf(window.windowClass) != -1) {
187 return false;
188 }
189
190 if (nameBlacklist.indexOf(window.caption) != -1) {
191 return false;
192 }
193
194 // Spectacle: animate only its decorated windows (result/preview), never the
195 // undecorated region-selection overlay.
196 if (window.windowClass == 'spectacle org.kde.spectacle' && !window.hasDecoration) {
197 return false;
198 }
199
200 if (window.hasDecoration) {
201 return true;
202 }
203
204 // Don't animate combobox popups, tooltips, popup menus, etc.
205 if (window.popupWindow) {
206 return false;
207 }
208
209 // Dont't animate the outline and the screenlocker as it looks bad.
210 if (window.lockScreen || window.outline) {
211 return false;
212 }
213
214 // Override-redirect windows are usually used for user interface
215 // concepts that are not expected to be animated by this effect.
216 if (!window.managed) {
217 return false;
218 }
219
220 return window.normalWindow || window.dialog;
221 }
222
223 setupForcedRoles(window) {
224 // marble: no forced blur/contrast (blurred window-sized rect + cost)
225 // window.setData(Effect.WindowForceBackgroundContrastRole, true);
226 // marble: no forced blur/contrast (blurred window-sized rect + cost)
227 // window.setData(Effect.WindowForceBlurRole, true);
228 }
229
230 cleanupForcedRoles(window) {
231 window.setData(Effect.WindowForceBackgroundContrastRole, null);
232 window.setData(Effect.WindowForceBlurRole, null);
233 }
234
235 slotWindowAdded(window) {
236 if (effects.hasActiveFullScreenEffect) {
237 return;
238 }
239 if (!BurnMyWindowsMarbleEffect.shouldAnimateWindow(window)) {
240 return;
241 }
242 if (!window.visible) {
243 return;
244 }
245 if (effect.isGrabbed(window, Effect.WindowAddedGrabRole)) {
246 return;
247 }
248 this.setupForcedRoles(window);
249
250 this.applyTint(window, false);
251 effect.setUniform(this.shader, 'uForOpening', 1.0);
252 effect.setUniform(this.shader, 'uIsFullscreen', window.fullScreen ? 1.0 : 0.0);
253
254
255
256 window.bmwInAnimation = animate({
257 window: window,
258 curve: QEasingCurve.Linear,
259 duration: this.duration,
260 animations: [{
261 type: Effect.ShaderUniform,
262 fragmentShader: this.shader,
263 uniform: 'uProgress',
264 from: this.prog(window, 0.0),
265 to: this.prog(window, 1.0 / this.crumpleEnd())
266 }]
267 });
268 }
269
270 slotWindowClosed(window) {
271 if (effects.hasActiveFullScreenEffect) {
272 return;
273 }
274 if (!BurnMyWindowsMarbleEffect.shouldAnimateWindow(window)) {
275 return;
276 }
277 if (!window.visible || window.skipsCloseAnimation) {
278 return;
279 }
280 if (effect.isGrabbed(window, Effect.WindowClosedGrabRole)) {
281 return;
282 }
283 const oldIn = window.bmwInAnimation; // cancelled after the close animation exists (keeps the snapshot)
284 delete window.bmwInAnimation;
285 this.setupForcedRoles(window);
286
287 this.applyTint(window, false);
288 effect.setUniform(this.shader, 'uForOpening', 0.0);
289 effect.setUniform(this.shader, 'uIsFullscreen', window.fullScreen ? 1.0 : 0.0);
290
291
292
293 // 1) marble morph (shader); runs the whole time so the marble stays drawn while falling
294 window.bmwOutAnimation = animate({
295 window: window,
296 curve: QEasingCurve.Linear,
297 duration: this.duration + this.pause + this.fallDuration,
298 animations: [{
299 type: Effect.ShaderUniform,
300 fragmentShader: this.shader,
301 uniform: 'uProgress',
302 from: this.prog(window, 0.0),
303 to: this.prog(window, 1.0 / this.crumpleEnd())
304 }]
305 });
306 if (oldIn) cancel(oldIn);
307 // 2) marker animation that ends exactly when the marble is done; the fall is
308 // started from its animationEnded (KWin ignored the per-call delay).
309 window.bmwMarbleDone = animate({
310 window: window,
311 curve: QEasingCurve.Linear,
312 duration: this.duration + this.pause,
313 animations: [{ type: Effect.Translation, from: {value1: 0.0, value2: 0.0}, to: {value1: 0.0, value2: 0.0} }]
314 });
315 window.bmwFallPending = true;
316 }
317
318 slotAnimationEnded(window, id) {
319 // marble is drawn outside the window's own damage region: repaint all on every end
320 this.repaintAll();
321 if (!window || !window.bmwFallPending) return; // first end = marble marker (ids arrive as 0)
322 window.bmwFallPending = false;
323 // free fall from rest: InQuad = t^2 = constant acceleration, d = 0.5 g t^2
324 window.bmwFallAnimation = animate({
325 window: window,
326 curve: QEasingCurve.InQuad,
327 duration: this.fallDuration,
328 animations: [{ type: Effect.Translation, from: {value1: 0.0, value2: 0.0},
329 to: {value1: 0.0, value2: this.fallDistance} }]
330 });
331 }
332
333 repaintAll() {
334 try { effects.addRepaintFull(); } catch (e) {
335 if (!this.bmwWarned) { this.bmwWarned = true; console.warn("marble: addRepaintFull unavailable: " + e); }
336 }
337 }
338
339 slotWindowDataChanged(window, role) {
340 if (role == Effect.WindowAddedGrabRole) {
341 if (window.bmwInAnimation && effect.isGrabbed(window, role)) {
342 cancel(window.bmwInAnimation);
343 delete window.bmwInAnimation;
344 this.cleanupForcedRoles(window);
345 }
346 } else if (role == Effect.WindowClosedGrabRole) {
347 if (window.bmwOutAnimation && effect.isGrabbed(window, role)) {
348 cancel(window.bmwOutAnimation);
349 delete window.bmwOutAnimation;
350 this.cleanupForcedRoles(window);
351 }
352 }
353 }
354}
355
356new BurnMyWindowsMarbleEffect();