BlueGit

timfa kwin-marble

Files

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

main.js

main.js 17.9 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", 300));
88 this.pause = animationTime(Number(effect.readConfig("Pause", 600)));
89 this.gravity = Math.max(100, Number(effect.readConfig('Gravity', 4000))); // 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 effect.setUniform(this.shader, 'uCrumpleEnd', this.duration / Math.max(1, this.duration + this.pause + this.fallDuration));
97 }
98
99 static shouldAnimateWindow(window) {
100
101 // We don't want to animate most of plasmashell's windows, yet, some
102 // of them we want to, for example, Task Manager Settings window.
103 // The problem is that all those window share single window class.
104 // So, the only way to decide whether a window should be animated is
105 // to use a heuristic: if a window has decoration, then it's most
106 // likely a dialog or a settings window so we have to animate it.
107 if (window.windowClass == 'plasmashell plasmashell' ||
108 window.windowClass == 'plasmashell org.kde.plasmashell') {
109 return window.hasDecoration;
110 }
111
112 // This is primarily to avoid animating the AltTab popup.
113 // See https://github.com/Schneegans/Burn-My-Windows/issues/384 for details.
114 if (!window.hasDecoration && window.onAllDesktops) {
115 return false
116 }
117
118 if (classBlacklist.indexOf(window.windowClass) != -1) {
119 return false;
120 }
121
122 if (nameBlacklist.indexOf(window.caption) != -1) {
123 return false;
124 }
125
126 if (window.hasDecoration) {
127 return true;
128 }
129
130 // Don't animate combobox popups, tooltips, popup menus, etc.
131 if (window.popupWindow) {
132 return false;
133 }
134
135 // Dont't animate the outline and the screenlocker as it looks bad.
136 if (window.lockScreen || window.outline) {
137 return false;
138 }
139
140 // Override-redirect windows are usually used for user interface
141 // concepts that are not expected to be animated by this effect.
142 if (!window.managed) {
143 return false;
144 }
145
146 return window.normalWindow || window.dialog;
147 }
148
149 setupForcedRoles(window) {
150 // marble: no forced blur/contrast (blurred window-sized rect + cost)
151 // window.setData(Effect.WindowForceBackgroundContrastRole, true);
152 // marble: no forced blur/contrast (blurred window-sized rect + cost)
153 // window.setData(Effect.WindowForceBlurRole, true);
154 }
155
156 cleanupForcedRoles(window) {
157 window.setData(Effect.WindowForceBackgroundContrastRole, null);
158 window.setData(Effect.WindowForceBlurRole, null);
159 }
160
161 slotWindowAdded(window) {
162 if (effects.hasActiveFullScreenEffect) {
163 return;
164 }
165 if (!BurnMyWindowsMarbleMinimizeEffect.shouldAnimateWindow(window)) {
166 return;
167 }
168 if (!window.visible) {
169 return;
170 }
171 if (effect.isGrabbed(window, Effect.WindowAddedGrabRole)) {
172 return;
173 }
174 this.setupForcedRoles(window);
175
176 effect.setUniform(this.shader, 'uForOpening', 1.0);
177 effect.setUniform(this.shader, 'uIsFullscreen', window.fullScreen ? 1.0 : 0.0);
178
179
180
181 window.bmwInAnimation = animate({
182 window: window,
183 curve: QEasingCurve.Linear,
184 duration: this.duration,
185 animations: [{
186 type: Effect.ShaderUniform,
187 fragmentShader: this.shader,
188 uniform: 'uProgress',
189 from: 0.0,
190 to: 1.0
191 }]
192 });
193 }
194
195 // ---- S-shaped cubic Bezier path from the marble (window centre) to the taskbar entry ----
196 // Start and end tangents are perpendicular to the taskbar edge (derived from where the
197 // icon sits on the screen). Returns N segments of equal arc length with gravity timing:
198 // s = 0.5 g t^2 from rest -> t(s) = sqrt(2 s / g).
199 buildPath(window, ir) {
200 const wr = window.geometry;
201 const dx = (ir.x + ir.width / 2) - (wr.x + wr.width / 2);
202 const dy = (ir.y + ir.height / 2) - (wr.y + wr.height / 2);
203 let vs = {x: 0, y: 0, width: 1920, height: 1080};
204 try { const g = effects.virtualScreenGeometry; if (g && g.width) vs = g; } catch (e) {}
205 const cx = ir.x + ir.width / 2, cy = ir.y + ir.height / 2;
206 const dist = [cy - vs.y, vs.y + vs.height - cy, cx - vs.x, vs.x + vs.width - cx]; // top,bottom,left,right
207 const normals = [[0, -1], [0, 1], [-1, 0], [1, 0]];
208 let k = 1; for (let i = 0; i < 4; i++) if (dist[i] < dist[k]) k = i; // bottom is default
209 const n = normals[k];
210 const along = Math.max(60, Math.abs(dx * n[0] + dy * n[1]));
211 const h = 0.55 * along; // tangent handle length
212 const P = [[0, 0], [n[0] * h, n[1] * h], [dx - n[0] * h, dy - n[1] * h], [dx, dy]];
213 const bez = (t) => {
214 const u = 1 - t, a = u * u * u, b = 3 * u * u * t, c = 3 * u * t * t, d = t * t * t;
215 return [a * P[0][0] + b * P[1][0] + c * P[2][0] + d * P[3][0], a * P[0][1] + b * P[1][1] + c * P[2][1] + d * P[3][1]];
216 };
217 // numeric arc length
218 const M = 200, pts = [], cum = [0];
219 for (let i = 0; i <= M; i++) pts.push(bez(i / M));
220 for (let i = 1; i <= M; i++) cum.push(cum[i - 1] + Math.hypot(pts[i][0] - pts[i - 1][0], pts[i][1] - pts[i - 1][1]));
221 const L = Math.max(1, cum[M]);
222 const N = 20, nodes = [];
223 let j = 0;
224 for (let i = 0; i <= N; i++) {
225 const s = L * i / N;
226 while (j < M && cum[j + 1] < s) j++;
227 const f = j < M ? (s - cum[j]) / Math.max(1e-6, cum[j + 1] - cum[j]) : 0;
228 const q = j < M ? [pts[j][0] + (pts[j + 1][0] - pts[j][0]) * f, pts[j][1] + (pts[j + 1][1] - pts[j][1]) * f] : pts[M];
229 nodes.push({x: q[0], y: q[1], t: animationTime(Math.round(1000 * Math.sqrt(2 * s / this.gravity)))});
230 }
231 // continuous position at path time t (ms, already scaled by the animation factor)
232 const fac = Math.max(0.001, animationTime(1000) / 1000), g = this.gravity;
233 const posAt = (tms) => {
234 const ts = Math.max(0, tms) / 1000 / fac;
235 const sa = Math.min(L, 0.5 * g * ts * ts);
236 let lo = 0, hi = M;
237 while (hi - lo > 1) { const mid = (lo + hi) >> 1; if (cum[mid] <= sa) lo = mid; else hi = mid; }
238 const f = (sa - cum[lo]) / Math.max(1e-6, cum[hi] - cum[lo]);
239 return {x: pts[lo][0] + (pts[hi][0] - pts[lo][0]) * f, y: pts[lo][1] + (pts[hi][1] - pts[lo][1]) * f};
240 };
241 return {nodes: nodes, length: L, total: nodes[N].t, posAt: posAt, tScale: nodes[N - 2].t};
242 }
243
244 segAnim(window, a, b, dur, extra) {
245 const anims = [{ type: Effect.Translation, from: {value1: a.x, value2: a.y}, to: {value1: b.x, value2: b.y} },
246 { type: Effect.Opacity, from: 1.0, to: 1.0 }];
247 if (extra) anims.push(extra);
248 window.bmwSegEnd = Date.now() + Math.max(1, dur) - 8;
249 return animate({ window: window, curve: QEasingCurve.Linear, duration: Math.max(1, dur), animations: anims });
250 }
251
252 slotWindowMinimized(window) {
253 if (effects.hasActiveFullScreenEffect) return;
254 const ir = window.iconGeometry;
255 if (!ir || ir.width == 0 || ir.height == 0) return; // no taskbar entry
256 this.stopAll(window);
257 const path = this.buildPath(window, ir);
258 const morph = this.duration + this.pause;
259 const total = morph + path.total;
260 effect.setUniform(this.shader, "uForOpening", 0.0);
261 effect.setUniform(this.shader, "uIsFullscreen", window.fullScreen ? 1.0 : 0.0);
262 effect.setUniform(this.shader, "uDuration", total * 0.001);
263 effect.setUniform(this.shader, "uCrumpleEnd", this.duration / Math.max(1, total));
264 // ONE shader animation over the whole minimize (same setup as restore, which renders fine);
265 // a little longer than the path so the marble is drawn until the window is hidden.
266 window.bmwMinAnimation = animate({
267 // runs far past the path on purpose (uProgress just goes beyond 1 = marble); it is
268 // cancelled explicitly after the invisible tail, so a late chain can never expose
269 // the unshaded window.
270 window: window, curve: QEasingCurve.Linear, duration: total + 3000,
271 animations: [{ type: Effect.ShaderUniform, fragmentShader: this.shader, uniform: "uProgress", from: 0.0, to: (total + 3000) / Math.max(1, total) },
272 { type: Effect.Opacity, from: 1.0, to: 1.0 }]
273 });
274 // marker for the end of morph + pause; when it ends the path traversal starts
275 window.bmwMarbleDone = animate({
276 window: window, curve: QEasingCurve.Linear, duration: morph,
277 animations: [{ type: Effect.Translation, from: {value1: 0.0, value2: 0.0}, to: {value1: 0.0, value2: 0.0} }]
278 });
279 window.bmwPath = path; window.bmwSeg = -1; window.bmwMode = "min";
280 window.bmwSegEnd = Date.now() + morph - 8;
281 }
282
283 slotWindowUnminimized(window) {
284 if (effects.hasActiveFullScreenEffect) return;
285 const ir = window.iconGeometry;
286 this.stopAll(window);
287 if (!ir || ir.width == 0 || ir.height == 0) return;
288 // exact reverse: marble comes up the same S-path (reverse gravity timing), growing from
289 // 10% to 100% over the first 10% of the path, rests, then unfolds into the window.
290 const path = this.buildPath(window, ir);
291 const total = this.duration + this.pause + path.total;
292 effect.setUniform(this.shader, "uForOpening", 0.0);
293 effect.setUniform(this.shader, "uIsFullscreen", window.fullScreen ? 1.0 : 0.0);
294 effect.setUniform(this.shader, "uDuration", total * 0.001);
295 effect.setUniform(this.shader, "uCrumpleEnd", this.duration / Math.max(1, total));
296 window.bmwUnminAnimation = animate({
297 window: window, curve: QEasingCurve.Linear, duration: total,
298 animations: [{ type: Effect.ShaderUniform, fragmentShader: this.shader, uniform: "uProgress", from: 1.0, to: 0.0 }]
299 });
300 const T = path.total, nd = path.nodes, N = nd.length - 1;
301 // reversed timing: going from node N to node N-1 takes (t_N - t_{N-1}) etc.
302 window.bmwScaleAnimation = animate({
303 window: window, curve: QEasingCurve.InOutSine, duration: Math.max(1, T - nd[N - 2].t),
304 animations: [{ type: Effect.Scale, from: {value1: 0.1, value2: 0.1}, to: {value1: 1.0, value2: 1.0} }]
305 });
306 window.bmwPath = path; window.bmwMode = "rise";
307 window.bmwT0 = Date.now(); window.bmwCur = {x: nd[N].x, y: nd[N].y};
308 this.step(window);
309 }
310
311 stopAll(window) {
312 window.bmwMode = null; window.bmwScaling = false;
313 for (const k of ["bmwMinAnimation", "bmwMarbleDone", "bmwSegAnimation", "bmwScaleAnimation",
314 "bmwTailAnimation", "bmwUnminAnimation"]) {
315 if (window[k]) { cancel(window[k]); delete window[k]; }
316 }
317 }
318
319 // Chain the path segments. KWin reports ids as 0 here, so we use our own state.
320 slotAnimationEnded(window, id) {
321 if (!window || !window.bmwMode) return;
322 if (window.bmwSegEnd && Date.now() < window.bmwSegEnd) return; // another animation ended
323 const nd = window.bmwPath.nodes, N = nd.length - 1;
324 if (window.bmwMode == "tail") {
325 // invisible tail done: now stop the shader too; the window is hidden from here on
326 window.bmwMode = null;
327 if (window.bmwMinAnimation) { cancel(window.bmwMinAnimation); delete window.bmwMinAnimation; }
328 try { effects.addRepaintFull(); } catch (e) {}
329 return;
330 }
331 if (window.bmwMode == "min" && window.bmwSeg < 0) { // morph + pause done
332 window.bmwSeg = 0; window.bmwT0 = Date.now(); window.bmwCur = {x: 0, y: 0};
333 }
334 this.step(window);
335 }
336
337 // Clock-driven path: every short segment aims at the path position for (now + STEP), measured
338 // from the real start time, so callback latency never accumulates (no drift, no stretch).
339 step(window) {
340 const STEP = 33; // ~2 frames per segment
341 const path = window.bmwPath, T = path.total, N = path.nodes.length - 1;
342 const now = Date.now() - window.bmwT0;
343 const end = path.nodes[N];
344 if (window.bmwMode == "min") {
345 if (now >= T - 4) {
346 // arrived: invisible hold at the icon (scale 10%) until the shader animation is cancelled
347 window.bmwTailAnimation = animate({
348 window: window, curve: QEasingCurve.Linear, duration: 250,
349 animations: [{ type: Effect.Translation, from: {value1: end.x, value2: end.y}, to: {value1: end.x, value2: end.y} },
350 { type: Effect.Scale, from: {value1: 0.1, value2: 0.1}, to: {value1: 0.1, value2: 0.1} },
351 { type: Effect.Opacity, from: 0.0, to: 0.0 }]
352 });
353 window.bmwSegEnd = Date.now() + 250 - 8;
354 window.bmwMode = "tail";
355 return;
356 }
357 const tgt = Math.min(T, now + STEP);
358 const p = tgt >= T ? {x: end.x, y: end.y} : path.posAt(tgt);
359 if (!window.bmwScaling && tgt >= path.tScale) { // last 10% of arc length: 100% -> 10%
360 window.bmwScaling = true;
361 window.bmwScaleAnimation = animate({
362 window: window, curve: QEasingCurve.InOutSine, duration: Math.max(1, T - now),
363 animations: [{ type: Effect.Scale, from: {value1: 1.0, value2: 1.0}, to: {value1: 0.1, value2: 0.1} }]
364 });
365 }
366 window.bmwSegAnimation = this.segAnim(window, window.bmwCur, p, tgt - now);
367 window.bmwCur = p;
368 } else if (window.bmwMode == "rise") {
369 if (now >= T - 4) {
370 if (window.bmwCur.x != 0 || window.bmwCur.y != 0) {
371 window.bmwSegAnimation = this.segAnim(window, window.bmwCur, {x: 0, y: 0}, 1);
372 window.bmwCur = {x: 0, y: 0};
373 }
374 window.bmwMode = null;
375 return;
376 }
377 const tgt = Math.min(T, now + STEP);
378 const p = tgt >= T ? {x: 0, y: 0} : path.posAt(T - tgt);
379 window.bmwSegAnimation = this.segAnim(window, window.bmwCur, p, tgt - now);
380 window.bmwCur = p;
381 }
382 }
383
384 slotWindowDataChanged(window, role) {
385 if (role == Effect.WindowAddedGrabRole) {
386 if (window.bmwInAnimation && effect.isGrabbed(window, role)) {
387 cancel(window.bmwInAnimation);
388 delete window.bmwInAnimation;
389 this.cleanupForcedRoles(window);
390 }
391 } else if (role == Effect.WindowClosedGrabRole) {
392 if (window.bmwOutAnimation && effect.isGrabbed(window, role)) {
393 cancel(window.bmwOutAnimation);
394 delete window.bmwOutAnimation;
395 this.cleanupForcedRoles(window);
396 }
397 }
398 }
399}
400
401new BurnMyWindowsMarbleMinimizeEffect();