BlueGit

timfa kwin-marble

Files

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

main.js

main.js 17.1 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 // Spectacle (KDE screenshot tool) should not be animated.
26 'spectacle org.kde.spectacle'
27];
28
29const nameBlacklist = [
30 // Kando uses a normal window, but it should look like a popup menu. We do not
31 // blacklist it by class as this would also affect the settings window.
32 'Kando Menu'
33];
34
35class BurnMyWindowsMarbleMinimizeEffect {
36 constructor() {
37 effect.configChanged.connect(this.loadConfig.bind(this));
38 effect.animationEnded.connect(this.cleanupForcedRoles.bind(this));
39 effect.animationEnded.connect(this.slotAnimationEnded.bind(this));
40 //effects.windowAdded.connect(this.slotWindowAdded.bind(this));
41 const hook = (w) => {
42 w.minimizedChanged.connect(() => {
43 if (w.minimized) this.slotWindowMinimized(w); else this.slotWindowUnminimized(w);
44 });
45 };
46 effects.windowAdded.connect(hook);
47 for (const w of effects.stackingOrder) hook(w);
48 effects.windowDataChanged.connect(this.slotWindowDataChanged.bind(this));
49
50 this.shader = effect.addFragmentShader(Effect.MapTexture, 'marble.frag');
51
52 this.loadConfig();
53 }
54
55 // A small helper which can be used to read a hex color string (e.g. #ff3244) from the
56 // settings. This method will return an array of four floating point values
57 // [r, g, b, a]. If the settings key only refers to an rgb value, the alpha component
58 // will be set to 1.0.
59 readRGBAConfig(key) {
60 const raw = String(effect.readConfig(key, '#ffffff'));
61 const parts = raw.split(',').map(x => parseFloat(x.trim()));
62 if (parts.length >= 3 && parts.every(x => !isNaN(x))) {
63 const c = parts.map(x => x / 255);
64 return [c[0], c[1], c[2], c.length > 3 ? c[3] : 1.0];
65 }
66 const color = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?([a-f\d]{2})?$/i
67 .exec(effect.readConfig(key, '#ffffff'))
68 .slice(1)
69 .filter(x => x !== undefined)
70 .map(x => parseInt(x, 16) / 255);
71 return color.length == 3 ? [color[0], color[1], color[2], 1.0] :
72 [color[1], color[2], color[3], color[0]];
73 }
74
75 // A small helper which can be used to read a hex color string (e.g. #ff3244) from the
76 // settings. This method will return an array of three floating point values [r, g, b]
77 // regardless of whether the settings key actually refers to an argb hex string.
78 readRGBConfig(key) {
79 const color = this.readRGBAConfig(key);
80 color.pop();
81 return color;
82 }
83
84 // This is called when the effect is loaded and whenever the settings of the effect
85 // are changed by the user.
86 loadConfig() {
87 this.duration = animationTime(effect.readConfig("Duration", 200));
88 this.pause = animationTime(Number(effect.readConfig("Pause", 300)));
89 this.gravity = Math.max(100, Number(effect.readConfig('Gravity', 4500))); // px/s^2
90 let sh = 1080;
91 try { sh = effects.virtualScreenGeometry.height || sh; } catch (e) {}
92 this.fallDistance = 1.5 * sh;
93 // free fall from rest: d = 0.5 g t^2 -> t = sqrt(2d/g); scaled by the global speed factor
94 this.fallDuration = animationTime(Math.round(1000 * Math.sqrt(2 * this.fallDistance / this.gravity)));
95 effect.setUniform(this.shader, 'uDuration', (this.duration + this.pause + this.fallDuration) * 0.001);
96 // 0..10 colours, 11 = random. Values saved by v0.3.0 (10 = white, 12 = random) fall back safely.
97 let mc = Math.floor(Number(effect.readConfig('MixColor', 0)));
98 if (!(mc >= 0)) mc = 0; else if (mc > 11) mc = 11;
99 this.tintMode = mc;
100 this.applyTint(null, false);
101 effect.setUniform(this.shader, 'uMarbleSize', Math.max(4, Number(effect.readConfig('MarbleSize', 42))));
102 effect.setUniform(this.shader, 'uMixOuter', Math.min(100, Math.max(0, Number(effect.readConfig('BlackMixOuter', 80)))) / 100);
103 effect.setUniform(this.shader, 'uMixCenter', Math.min(100, Math.max(0, Number(effect.readConfig('BlackMixCenter', 5)))) / 100);
104 effect.setUniform(this.shader, 'uCrumpleEnd', this.duration / Math.max(1, this.duration + this.pause + this.fallDuration));
105 }
106
107 // Mix target colour. tintMode 11 = random; with reuse, a random colour picked for this
108 // window (e.g. at minimize) is used again (e.g. at unminimize) and then forgotten.
109 applyTint(window, reuse) {
110 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]];
111 let c;
112 if (this.tintMode < C.length) c = C[this.tintMode];
113 else if (window && reuse && window.bmwTint) { c = window.bmwTint; window.bmwTint = null; }
114 else { c = C[Math.floor(Math.random() * C.length)]; if (window) window.bmwTint = c; }
115 effect.setUniform(this.shader, 'uTintR', c[0]);
116 effect.setUniform(this.shader, 'uTintG', c[1]);
117 effect.setUniform(this.shader, 'uTintB', c[2]);
118 }
119
120 static shouldAnimateWindow(window) {
121
122 // We don't want to animate most of plasmashell's windows, yet, some
123 // of them we want to, for example, Task Manager Settings window.
124 // The problem is that all those window share single window class.
125 // So, the only way to decide whether a window should be animated is
126 // to use a heuristic: if a window has decoration, then it's most
127 // likely a dialog or a settings window so we have to animate it.
128 if (window.windowClass == 'plasmashell plasmashell' ||
129 window.windowClass == 'plasmashell org.kde.plasmashell') {
130 return window.hasDecoration;
131 }
132
133 // This is primarily to avoid animating the AltTab popup.
134 // See https://github.com/Schneegans/Burn-My-Windows/issues/384 for details.
135 if (!window.hasDecoration && window.onAllDesktops) {
136 return false
137 }
138
139 if (classBlacklist.indexOf(window.windowClass) != -1) {
140 return false;
141 }
142
143 if (nameBlacklist.indexOf(window.caption) != -1) {
144 return false;
145 }
146
147 if (window.hasDecoration) {
148 return true;
149 }
150
151 // Don't animate combobox popups, tooltips, popup menus, etc.
152 if (window.popupWindow) {
153 return false;
154 }
155
156 // Dont't animate the outline and the screenlocker as it looks bad.
157 if (window.lockScreen || window.outline) {
158 return false;
159 }
160
161 // Override-redirect windows are usually used for user interface
162 // concepts that are not expected to be animated by this effect.
163 if (!window.managed) {
164 return false;
165 }
166
167 return window.normalWindow || window.dialog;
168 }
169
170 setupForcedRoles(window) {
171 // marble: no forced blur/contrast (blurred window-sized rect + cost)
172 // window.setData(Effect.WindowForceBackgroundContrastRole, true);
173 // marble: no forced blur/contrast (blurred window-sized rect + cost)
174 // window.setData(Effect.WindowForceBlurRole, true);
175 }
176
177 cleanupForcedRoles(window) {
178 window.setData(Effect.WindowForceBackgroundContrastRole, null);
179 window.setData(Effect.WindowForceBlurRole, null);
180 }
181
182 slotWindowAdded(window) {
183 if (effects.hasActiveFullScreenEffect) {
184 return;
185 }
186 if (!BurnMyWindowsMarbleMinimizeEffect.shouldAnimateWindow(window)) {
187 return;
188 }
189 if (!window.visible) {
190 return;
191 }
192 if (effect.isGrabbed(window, Effect.WindowAddedGrabRole)) {
193 return;
194 }
195 this.setupForcedRoles(window);
196
197 effect.setUniform(this.shader, 'uForOpening', 1.0);
198 effect.setUniform(this.shader, 'uIsFullscreen', window.fullScreen ? 1.0 : 0.0);
199
200
201
202 window.bmwInAnimation = animate({
203 window: window,
204 curve: QEasingCurve.Linear,
205 duration: this.duration,
206 animations: [{
207 type: Effect.ShaderUniform,
208 fragmentShader: this.shader,
209 uniform: 'uProgress',
210 from: 0.0,
211 to: 1.0
212 }]
213 });
214 }
215
216 // ---- Smooth S-path: two parallel translations with different easing curves ----
217 // Along the taskbar normal the marble falls with gravity (InQuad = 0.5 g t^2 from rest);
218 // sideways it moves with InOutSine (slow, fast in the middle, slow). Together this gives an
219 // S-shaped path that leaves straight down and enters the taskbar perpendicular. Duration
220 // from the distance along the normal: t = sqrt(2 d / g). No chains, no per-frame calls.
221 fallPlan(window, ir) {
222 const wr = window.geometry;
223 const dx = (ir.x + ir.width / 2) - (wr.x + wr.width / 2);
224 const dy = (ir.y + ir.height / 2) - (wr.y + wr.height / 2);
225 let vs = {x: 0, y: 0, width: 1920, height: 1080};
226 try { const g = effects.virtualScreenGeometry; if (g && g.width) vs = g; } catch (e) {}
227 const cx = ir.x + ir.width / 2, cy = ir.y + ir.height / 2;
228 const d = [cy - vs.y, vs.y + vs.height - cy, cx - vs.x, vs.x + vs.width - cx]; // top,bottom,left,right
229 let k = 1; for (let i = 0; i < 4; i++) if (d[i] < d[k]) k = i; // bottom is default
230 const vertical = k < 2; // taskbar at top/bottom
231 const along = Math.max(30, Math.abs(vertical ? dy : dx));
232 const T = animationTime(Math.round(1000 * Math.sqrt(2 * along / this.gravity)));
233 // KWin scales around the centre of the expanded geometry (incl. shadow), not the window
234 // centre; when the marble shrinks to ~0 it would drift there. Offset to compensate.
235 let ox = 0, oy = 0;
236 try { const eg = window.expandedGeometry;
237 ox = (wr.x + wr.width / 2) - (eg.x + eg.width / 2); oy = (wr.y + wr.height / 2) - (eg.y + eg.height / 2); } catch (e) {}
238 return {dx: dx, dy: dy, T: Math.max(1, T), vertical: vertical, ox: ox, oy: oy};
239 }
240
241 // dir: 1 = minimize (towards the icon), -1 = restore (time-reversed)
242 fallAnims(window, f, dir) {
243 const fwd = dir > 0;
244 const gravityCurve = fwd ? QEasingCurve.InQuad : QEasingCurve.OutQuad; // OutQuad = reversed InQuad
245 const sideCurve = QEasingCurve.InOutSine; // symmetric
246 const xCurve = f.vertical ? sideCurve : gravityCurve;
247 const yCurve = f.vertical ? gravityCurve : sideCurve;
248 const ax = fwd ? [0, f.dx] : [f.dx, 0], ay = fwd ? [0, f.dy] : [f.dy, 0];
249 window.bmwXAnimation = animate({ window: window, curve: xCurve, duration: f.T,
250 animations: [{ type: Effect.Translation, from: {value1: ax[0], value2: 0.0}, to: {value1: ax[1], value2: 0.0} },
251 { type: Effect.Opacity, from: 1.0, to: 1.0 }] });
252 window.bmwYAnimation = animate({ window: window, curve: yCurve, duration: f.T,
253 animations: [{ type: Effect.Translation, from: {value1: 0.0, value2: ay[0]}, to: {value1: 0.0, value2: ay[1]} }] });
254 // single scale 100% -> ~0 (0.001, never 0) essentially in the last part (InExpo stays ~1 until late); reverse = OutExpo
255 window.bmwScaleAnimation = animate({ window: window, curve: fwd ? QEasingCurve.InExpo : QEasingCurve.OutExpo, duration: f.T,
256 animations: [{ type: Effect.Scale, from: {value1: fwd ? 1.0 : 0.001, value2: fwd ? 1.0 : 0.001},
257 to: {value1: fwd ? 0.001 : 1.0, value2: fwd ? 0.001 : 1.0} }] });
258
259 }
260
261 slotWindowMinimized(window) {
262 if (effects.hasActiveFullScreenEffect) return;
263 const ir = window.iconGeometry;
264 if (!ir || ir.width == 0 || ir.height == 0) return; // no taskbar entry
265 this.stopAll(window);
266 const f = this.fallPlan(window, ir);
267 this.applyTint(window, false);
268 const morph = this.duration + this.pause;
269 const total = morph + f.T;
270 effect.setUniform(this.shader, "uForOpening", 0.0);
271 effect.setUniform(this.shader, "uIsFullscreen", window.fullScreen ? 1.0 : 0.0);
272 effect.setUniform(this.shader, "uDuration", total * 0.001);
273 effect.setUniform(this.shader, "uCrumpleEnd", this.duration / Math.max(1, total));
274 // one shader animation, deliberately longer; cancelled after the invisible tail (flash fix)
275 window.bmwMinAnimation = animate({
276 window: window, curve: QEasingCurve.Linear, duration: total + 3000,
277 animations: [{ type: Effect.ShaderUniform, fragmentShader: this.shader, uniform: "uProgress", from: 0.0, to: (total + 3000) / Math.max(1, total) },
278 { type: Effect.Opacity, from: 1.0, to: 1.0 }]
279 });
280 window.bmwMarbleDone = animate({
281 window: window, curve: QEasingCurve.Linear, duration: morph,
282 animations: [{ type: Effect.Translation, from: {value1: 0.0, value2: 0.0}, to: {value1: 0.0, value2: 0.0} }]
283 });
284 window.bmwFall = f; window.bmwMode = "morph"; window.bmwSegEnd = Date.now() + morph - 8;
285 }
286
287 slotWindowUnminimized(window) {
288 if (effects.hasActiveFullScreenEffect) return;
289 const ir = window.iconGeometry;
290 this.stopAll(window);
291 if (!ir || ir.width == 0 || ir.height == 0) return;
292 // reverse: rise from the icon (same curves reversed), rest, unfold
293 const f = this.fallPlan(window, ir);
294 this.applyTint(window, true);
295 const total = this.duration + this.pause + f.T;
296 effect.setUniform(this.shader, "uForOpening", 0.0);
297 effect.setUniform(this.shader, "uIsFullscreen", window.fullScreen ? 1.0 : 0.0);
298 effect.setUniform(this.shader, "uDuration", total * 0.001);
299 effect.setUniform(this.shader, "uCrumpleEnd", this.duration / Math.max(1, total));
300 window.bmwUnminAnimation = animate({
301 window: window, curve: QEasingCurve.Linear, duration: total,
302 animations: [{ type: Effect.ShaderUniform, fragmentShader: this.shader, uniform: "uProgress", from: 1.0, to: 0.0 }]
303 });
304 this.fallAnims(window, f, -1);
305 window.bmwFadeInAnimation = animate({ window: window, curve: QEasingCurve.InExpo, duration: 80,
306 animations: [{ type: Effect.Opacity, from: 0.0, to: 1.0 }] });
307 }
308
309 stopAll(window) {
310 window.bmwMode = null;
311 for (const k of ["bmwMinAnimation", "bmwMarbleDone", "bmwXAnimation", "bmwYAnimation", "bmwScaleAnimation", "bmwScale2Animation", "bmwCompAnimation",
312 "bmwTailAnimation", "bmwUnminAnimation", "bmwFadeInAnimation"]) {
313 if (window[k]) { cancel(window[k]); delete window[k]; }
314 }
315 this.repaintAll();
316 }
317
318 // KWin reports ids as 0 here, so we use our own state + end times.
319 slotAnimationEnded(window, id) {
320 // marble is drawn outside the window's own damage region: repaint all on every end
321 this.repaintAll();
322 if (!window || !window.bmwMode) return;
323 if (window.bmwSegEnd && Date.now() < window.bmwSegEnd) return; // another animation ended
324 const f = window.bmwFall;
325 if (window.bmwMode == "morph") { // morph + pause done -> fall (one shot)
326 this.fallAnims(window, f, 1);
327 window.bmwMode = "fall"; window.bmwSegEnd = Date.now() + f.T - 8;
328 } else if (window.bmwMode == "fall") { // arrived: invisible hold at the icon
329 window.bmwTailAnimation = animate({
330 window: window, curve: QEasingCurve.Linear, duration: 250,
331 animations: [{ type: Effect.Translation, from: {value1: f.dx, value2: f.dy}, to: {value1: f.dx, value2: f.dy} },
332 { type: Effect.Scale, from: {value1: 0.001, value2: 0.001}, to: {value1: 0.001, value2: 0.001} },
333 { type: Effect.Opacity, from: 0.0, to: 0.0 }]
334 });
335 window.bmwMode = "tail"; window.bmwSegEnd = Date.now() + 250 - 8;
336 this.repaintAll();
337 } else if (window.bmwMode == "tail") { // now stop the shader; window is hidden
338 window.bmwMode = null;
339 if (window.bmwMinAnimation) { cancel(window.bmwMinAnimation); delete window.bmwMinAnimation; }
340 this.repaintAll();
341 }
342 }
343
344 repaintAll() {
345 try { effects.addRepaintFull(); } catch (e) {
346 if (!this.bmwWarned) { this.bmwWarned = true; console.warn("marble: addRepaintFull unavailable: " + e); }
347 }
348 }
349
350 slotWindowDataChanged(window, role) {
351 if (role == Effect.WindowAddedGrabRole) {
352 if (window.bmwInAnimation && effect.isGrabbed(window, role)) {
353 cancel(window.bmwInAnimation);
354 delete window.bmwInAnimation;
355 this.cleanupForcedRoles(window);
356 }
357 } else if (role == Effect.WindowClosedGrabRole) {
358 if (window.bmwOutAnimation && effect.isGrabbed(window, role)) {
359 cancel(window.bmwOutAnimation);
360 delete window.bmwOutAnimation;
361 this.cleanupForcedRoles(window);
362 }
363 }
364 }
365}
366
367new BurnMyWindowsMarbleMinimizeEffect();