// SPDX-FileCopyrightText: Vlad Zahorodnii , Martin Flöser // , Simon Schneegans // SPDX-License-Identifier: GPL-3.0-or-later 'use strict'; const classBlacklist = [ // The logout screen has to be animated only by the logout effect. 'ksmserver ksmserver', 'ksmserver-logout-greeter ksmserver-logout-greeter', // KDE Plasma splash screen has to be animated only by the login effect. 'ksplashqml ksplashqml', // All the tooltips of IntelliJ IDEs use the same window class as their main window. // This results in many awkward animations, so we have to blacklist them all. // See https://github.com/Schneegans/Burn-My-Windows/issues/284 for details. 'jetbrains-studio jetbrains-studio', 'jetbrains-aqua jetbrains-aqua', 'jetbrains-clion jetbrains-clion', 'jetbrains-datagrip jetbrains-datagrip', 'jetbrains-dataspell jetbrains-dataspell', 'jetbrains-goland jetbrains-goland', 'jetbrains-idea jetbrains-idea', 'jetbrains-idea-ce jetbrains-idea-ce', 'jetbrains-phpstorm jetbrains-phpstorm', 'jetbrains-pycharm jetbrains-pycharm', 'jetbrains-pycharm-ce jetbrains-pycharm-ce', 'jetbrains-rider jetbrains-rider', 'jetbrains-rubymine jetbrains-rubymine', 'jetbrains-webstorm jetbrains-webstorm', // Spectacle (KDE screenshot tool) should not be animated. 'spectacle org.kde.spectacle' ]; const nameBlacklist = [ // Kando uses a normal window, but it should look like a popup menu. We do not // blacklist it by class as this would also affect the settings window. 'Kando Menu' ]; class BurnMyWindowsMarbleMinimizeEffect { constructor() { effect.configChanged.connect(this.loadConfig.bind(this)); effect.animationEnded.connect(this.cleanupForcedRoles.bind(this)); effect.animationEnded.connect(this.slotAnimationEnded.bind(this)); //effects.windowAdded.connect(this.slotWindowAdded.bind(this)); const hook = (w) => { w.minimizedChanged.connect(() => { if (w.minimized) this.slotWindowMinimized(w); else this.slotWindowUnminimized(w); }); }; effects.windowAdded.connect(hook); for (const w of effects.stackingOrder) hook(w); effects.windowDataChanged.connect(this.slotWindowDataChanged.bind(this)); this.shader = effect.addFragmentShader(Effect.MapTexture, 'marble.frag'); this.loadConfig(); } // A small helper which can be used to read a hex color string (e.g. #ff3244) from the // settings. This method will return an array of four floating point values // [r, g, b, a]. If the settings key only refers to an rgb value, the alpha component // will be set to 1.0. readRGBAConfig(key) { const raw = String(effect.readConfig(key, '#ffffff')); const parts = raw.split(',').map(x => parseFloat(x.trim())); if (parts.length >= 3 && parts.every(x => !isNaN(x))) { const c = parts.map(x => x / 255); return [c[0], c[1], c[2], c.length > 3 ? c[3] : 1.0]; } const color = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?([a-f\d]{2})?$/i .exec(effect.readConfig(key, '#ffffff')) .slice(1) .filter(x => x !== undefined) .map(x => parseInt(x, 16) / 255); return color.length == 3 ? [color[0], color[1], color[2], 1.0] : [color[1], color[2], color[3], color[0]]; } // A small helper which can be used to read a hex color string (e.g. #ff3244) from the // settings. This method will return an array of three floating point values [r, g, b] // regardless of whether the settings key actually refers to an argb hex string. readRGBConfig(key) { const color = this.readRGBAConfig(key); color.pop(); return color; } // This is called when the effect is loaded and whenever the settings of the effect // are changed by the user. loadConfig() { this.duration = animationTime(effect.readConfig("Duration", 300)); this.pause = animationTime(Number(effect.readConfig("Pause", 600))); this.gravity = Math.max(100, Number(effect.readConfig('Gravity', 4000))); // px/s^2 let sh = 1080; try { sh = effects.virtualScreenGeometry.height || sh; } catch (e) {} this.fallDistance = 1.5 * sh; // free fall from rest: d = 0.5 g t^2 -> t = sqrt(2d/g); scaled by the global speed factor this.fallDuration = animationTime(Math.round(1000 * Math.sqrt(2 * this.fallDistance / this.gravity))); effect.setUniform(this.shader, 'uDuration', (this.duration + this.pause + this.fallDuration) * 0.001); effect.setUniform(this.shader, 'uCrumpleEnd', this.duration / Math.max(1, this.duration + this.pause + this.fallDuration)); } static shouldAnimateWindow(window) { // We don't want to animate most of plasmashell's windows, yet, some // of them we want to, for example, Task Manager Settings window. // The problem is that all those window share single window class. // So, the only way to decide whether a window should be animated is // to use a heuristic: if a window has decoration, then it's most // likely a dialog or a settings window so we have to animate it. if (window.windowClass == 'plasmashell plasmashell' || window.windowClass == 'plasmashell org.kde.plasmashell') { return window.hasDecoration; } // This is primarily to avoid animating the AltTab popup. // See https://github.com/Schneegans/Burn-My-Windows/issues/384 for details. if (!window.hasDecoration && window.onAllDesktops) { return false } if (classBlacklist.indexOf(window.windowClass) != -1) { return false; } if (nameBlacklist.indexOf(window.caption) != -1) { return false; } if (window.hasDecoration) { return true; } // Don't animate combobox popups, tooltips, popup menus, etc. if (window.popupWindow) { return false; } // Dont't animate the outline and the screenlocker as it looks bad. if (window.lockScreen || window.outline) { return false; } // Override-redirect windows are usually used for user interface // concepts that are not expected to be animated by this effect. if (!window.managed) { return false; } return window.normalWindow || window.dialog; } setupForcedRoles(window) { // marble: no forced blur/contrast (blurred window-sized rect + cost) // window.setData(Effect.WindowForceBackgroundContrastRole, true); // marble: no forced blur/contrast (blurred window-sized rect + cost) // window.setData(Effect.WindowForceBlurRole, true); } cleanupForcedRoles(window) { window.setData(Effect.WindowForceBackgroundContrastRole, null); window.setData(Effect.WindowForceBlurRole, null); } slotWindowAdded(window) { if (effects.hasActiveFullScreenEffect) { return; } if (!BurnMyWindowsMarbleMinimizeEffect.shouldAnimateWindow(window)) { return; } if (!window.visible) { return; } if (effect.isGrabbed(window, Effect.WindowAddedGrabRole)) { return; } this.setupForcedRoles(window); effect.setUniform(this.shader, 'uForOpening', 1.0); effect.setUniform(this.shader, 'uIsFullscreen', window.fullScreen ? 1.0 : 0.0); window.bmwInAnimation = animate({ window: window, curve: QEasingCurve.Linear, duration: this.duration, animations: [{ type: Effect.ShaderUniform, fragmentShader: this.shader, uniform: 'uProgress', from: 0.0, to: 1.0 }] }); } // ---- S-shaped cubic Bezier path from the marble (window centre) to the taskbar entry ---- // Start and end tangents are perpendicular to the taskbar edge (derived from where the // icon sits on the screen). Returns N segments of equal arc length with gravity timing: // s = 0.5 g t^2 from rest -> t(s) = sqrt(2 s / g). buildPath(window, ir) { const wr = window.geometry; const dx = (ir.x + ir.width / 2) - (wr.x + wr.width / 2); const dy = (ir.y + ir.height / 2) - (wr.y + wr.height / 2); let vs = {x: 0, y: 0, width: 1920, height: 1080}; try { const g = effects.virtualScreenGeometry; if (g && g.width) vs = g; } catch (e) {} const cx = ir.x + ir.width / 2, cy = ir.y + ir.height / 2; const dist = [cy - vs.y, vs.y + vs.height - cy, cx - vs.x, vs.x + vs.width - cx]; // top,bottom,left,right const normals = [[0, -1], [0, 1], [-1, 0], [1, 0]]; let k = 1; for (let i = 0; i < 4; i++) if (dist[i] < dist[k]) k = i; // bottom is default const n = normals[k]; const along = Math.max(60, Math.abs(dx * n[0] + dy * n[1])); const h = 0.55 * along; // tangent handle length const P = [[0, 0], [n[0] * h, n[1] * h], [dx - n[0] * h, dy - n[1] * h], [dx, dy]]; const bez = (t) => { const u = 1 - t, a = u * u * u, b = 3 * u * u * t, c = 3 * u * t * t, d = t * t * t; 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]]; }; // numeric arc length const M = 200, pts = [], cum = [0]; for (let i = 0; i <= M; i++) pts.push(bez(i / M)); 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])); const L = Math.max(1, cum[M]); const N = 20, nodes = []; let j = 0; for (let i = 0; i <= N; i++) { const s = L * i / N; while (j < M && cum[j + 1] < s) j++; const f = j < M ? (s - cum[j]) / Math.max(1e-6, cum[j + 1] - cum[j]) : 0; 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]; nodes.push({x: q[0], y: q[1], t: animationTime(Math.round(1000 * Math.sqrt(2 * s / this.gravity)))}); } return {nodes: nodes, length: L, total: nodes[N].t}; } segAnim(window, a, b, dur, extra) { const anims = [{ type: Effect.Translation, from: {value1: a.x, value2: a.y}, to: {value1: b.x, value2: b.y} }, { type: Effect.Opacity, from: 1.0, to: 1.0 }]; if (extra) anims.push(extra); window.bmwSegEnd = Date.now() + Math.max(1, dur) - 8; return animate({ window: window, curve: QEasingCurve.Linear, duration: Math.max(1, dur), animations: anims }); } slotWindowMinimized(window) { if (effects.hasActiveFullScreenEffect) return; const ir = window.iconGeometry; if (!ir || ir.width == 0 || ir.height == 0) return; // no taskbar entry this.stopAll(window); const path = this.buildPath(window, ir); const morph = this.duration + this.pause; const total = morph + path.total; effect.setUniform(this.shader, "uForOpening", 0.0); effect.setUniform(this.shader, "uIsFullscreen", window.fullScreen ? 1.0 : 0.0); effect.setUniform(this.shader, "uDuration", total * 0.001); effect.setUniform(this.shader, "uCrumpleEnd", this.duration / Math.max(1, total)); // ONE shader animation over the whole minimize (same setup as restore, which renders fine); // a little longer than the path so the marble is drawn until the window is hidden. window.bmwMinAnimation = animate({ window: window, curve: QEasingCurve.Linear, duration: total + 150, animations: [{ type: Effect.ShaderUniform, fragmentShader: this.shader, uniform: "uProgress", from: 0.0, to: 1.0 }, { type: Effect.Opacity, from: 1.0, to: 1.0 }] }); // marker for the end of morph + pause; when it ends the path traversal starts window.bmwMarbleDone = animate({ window: window, curve: QEasingCurve.Linear, duration: morph, animations: [{ type: Effect.Translation, from: {value1: 0.0, value2: 0.0}, to: {value1: 0.0, value2: 0.0} }] }); window.bmwPath = path; window.bmwSeg = -1; window.bmwMode = "min"; window.bmwSegEnd = Date.now() + morph - 8; } slotWindowUnminimized(window) { if (effects.hasActiveFullScreenEffect) return; const ir = window.iconGeometry; this.stopAll(window); if (!ir || ir.width == 0 || ir.height == 0) return; // exact reverse: marble comes up the same S-path (reverse gravity timing), growing from // 10% to 100% over the first 10% of the path, rests, then unfolds into the window. const path = this.buildPath(window, ir); const total = this.duration + this.pause + path.total; effect.setUniform(this.shader, "uForOpening", 0.0); effect.setUniform(this.shader, "uIsFullscreen", window.fullScreen ? 1.0 : 0.0); effect.setUniform(this.shader, "uDuration", total * 0.001); effect.setUniform(this.shader, "uCrumpleEnd", this.duration / Math.max(1, total)); window.bmwUnminAnimation = animate({ window: window, curve: QEasingCurve.Linear, duration: total, animations: [{ type: Effect.ShaderUniform, fragmentShader: this.shader, uniform: "uProgress", from: 1.0, to: 0.0 }] }); const T = path.total, nd = path.nodes, N = nd.length - 1; // reversed timing: going from node N to node N-1 takes (t_N - t_{N-1}) etc. window.bmwScaleAnimation = animate({ window: window, curve: QEasingCurve.InOutSine, duration: Math.max(1, T - nd[N - 2].t), animations: [{ type: Effect.Scale, from: {value1: 0.1, value2: 0.1}, to: {value1: 1.0, value2: 1.0} }] }); window.bmwPath = path; window.bmwSeg = N; window.bmwMode = "rise"; window.bmwSegAnimation = this.segAnim(window, nd[N], nd[N - 1], nd[N].t - nd[N - 1].t); } stopAll(window) { window.bmwMode = null; for (const k of ["bmwMinAnimation", "bmwMarbleDone", "bmwSegAnimation", "bmwScaleAnimation", "bmwTailAnimation", "bmwUnminAnimation"]) { if (window[k]) { cancel(window[k]); delete window[k]; } } } // Chain the path segments. KWin reports ids as 0 here, so we use our own state. slotAnimationEnded(window, id) { if (!window || !window.bmwMode) return; if (window.bmwSegEnd && Date.now() < window.bmwSegEnd) return; // another animation ended const nd = window.bmwPath.nodes, N = nd.length - 1; if (window.bmwMode == "min") { if (window.bmwSeg >= N) return; // already in the tail window.bmwSeg++; const i = window.bmwSeg; if (i == N) { // arrived: invisible hold at the icon (scale 10%) until the shader animation ends window.bmwTailAnimation = animate({ window: window, curve: QEasingCurve.Linear, duration: 200, animations: [{ type: Effect.Translation, from: {value1: nd[N].x, value2: nd[N].y}, to: {value1: nd[N].x, value2: nd[N].y} }, { type: Effect.Scale, from: {value1: 0.1, value2: 0.1}, to: {value1: 0.1, value2: 0.1} }, { type: Effect.Opacity, from: 0.0, to: 0.0 }] }); window.bmwMode = null; return; } let extra = null; if (i == N - 2) { // last 10% of arc length: 100% -> 10% window.bmwScaleAnimation = animate({ window: window, curve: QEasingCurve.InOutSine, duration: Math.max(1, nd[N].t - nd[N - 2].t), animations: [{ type: Effect.Scale, from: {value1: 1.0, value2: 1.0}, to: {value1: 0.1, value2: 0.1} }] }); } window.bmwSegAnimation = this.segAnim(window, nd[i], nd[i + 1], nd[i + 1].t - nd[i].t, extra); } else if (window.bmwMode == "rise") { window.bmwSeg--; const i = window.bmwSeg; if (i <= 0) { window.bmwMode = null; return; } window.bmwSegAnimation = this.segAnim(window, nd[i], nd[i - 1], nd[i].t - nd[i - 1].t); } } slotWindowDataChanged(window, role) { if (role == Effect.WindowAddedGrabRole) { if (window.bmwInAnimation && effect.isGrabbed(window, role)) { cancel(window.bmwInAnimation); delete window.bmwInAnimation; this.cleanupForcedRoles(window); } } else if (role == Effect.WindowClosedGrabRole) { if (window.bmwOutAnimation && effect.isGrabbed(window, role)) { cancel(window.bmwOutAnimation); delete window.bmwOutAnimation; this.cleanupForcedRoles(window); } } } } new BurnMyWindowsMarbleMinimizeEffect();