// 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", 200)); this.pause = animationTime(Number(effect.readConfig("Pause", 300))); this.gravity = Math.max(100, Number(effect.readConfig('Gravity', 4500))); // 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); // 0..10 named colours, 11 = Willekeurig, 12 = Semiwillekeurig. Older >12 → 12. let mc = Math.floor(Number(effect.readConfig('MixColor', 12))); if (!(mc >= 0)) mc = 0; else if (mc > 12) mc = 12; this.tintMode = mc; // Drop per-class cache when settings reload (MixColor / speed etc. may change). this.classHueCache = Object.create(null); this.applyTint(null, false); effect.setUniform(this.shader, 'uMarbleSize', Math.max(4, Number(effect.readConfig('MarbleSize', 42)))); this.debug = Math.max(0, Number(effect.readConfig('DebugMarble', 0)) | 0); effect.setUniform(this.shader, 'uDebug', this.debug); effect.setUniform(this.shader, 'uMixOuter', Math.min(100, Math.max(0, Number(effect.readConfig('BlackMixOuter', 80)))) / 100); effect.setUniform(this.shader, 'uMixCenter', Math.min(100, Math.max(0, Number(effect.readConfig('BlackMixCenter', 5)))) / 100); effect.setUniform(this.shader, 'uCrumpleEnd', this.duration / Math.max(1, this.duration + this.pause + this.fallDuration)); } // Mix target. tintMode 0..10 = named colours; 11 = Willekeurig (seq → hue); // 12 = Semiwillekeurig (hash of window.windowClass → hue). Random modes use HSV // with fixed S=1 and V=0.6667 (matches the named #aa… glass colours), not a palette. // Final RGB is computed once in JS (and cached per windowClass for mode 12); the shader // only indexes a precomputed hue→RGB table — never runs HSV per fragment. // Single-channel pack: uProgress = tintId + morph01*0.999 (tintId 0..10 or 1000..1359). // Callers may still pass overshoot m (m_to = 1/crumpleEnd); values m>=1 encode as morph=1. // Shader restores timing with uCrumpleEnd: c = raw/(uCrumpleEnd*0.999). No second ShaderUniform. prog(window, m) { const tid = (window && window.bmwTintId >= 0) ? window.bmwTintId : (window ? (window.bmwBase || 0) : 0); const morph = Math.min(1, Math.max(0, m)); const v = tid + morph * 0.999; if (this.debug) console.log('MARBLEDBG prog caption=' + window.caption + ' m=' + m + ' morph=' + morph + ' v=' + v + ' tintId=' + tid); return v; } hsvToRgb(h, s, v) { const c = v * s, x = c * (1 - Math.abs((h / 60) % 2 - 1)), m = v - c; let r = 0, g = 0, b = 0; if (h < 60) { r = c; g = x; } else if (h < 120) { r = x; g = c; } else if (h < 180) { g = c; b = x; } else if (h < 240) { g = x; b = c; } else if (h < 300) { r = x; b = c; } else { r = c; b = x; } return [r + m, g + m, b + m]; } hashHue(str) { let h = 2166136261; for (let i = 0; i < str.length; i++) { h ^= str.charCodeAt(i); h = Math.imul(h, 16777619); } return (h >>> 0) % 360; } applyTint(window, reuse) { 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]]; const HS = 1.0, HV = 0.6667; // same peak channel as #aa0000 / #0000aa / … let c, hue = null; if (this.tintMode < FIXED.length) { c = FIXED[this.tintMode]; if (window) { window.bmwTint = null; window.bmwHue = null; } } else if (this.tintMode === 12 && window) { // Semiwillekeurig: stable per application (windowClass); empty → black. // RGB/hue computed once per class (cached); shader gets ready RGB via HUE_RGB[hue] (no per-fragment HSV). const cls = String(window.windowClass || '').trim(); if (!cls) { c = FIXED[0]; hue = null; } else { if (!this.classHueCache) this.classHueCache = Object.create(null); let cached = this.classHueCache[cls]; if (!cached) { hue = this.hashHue(cls); c = this.hsvToRgb(hue, HS, HV); this.classHueCache[cls] = { hue: hue, rgb: c }; } else { hue = cached.hue; c = cached.rgb; } } window.bmwTint = c; window.bmwHue = hue; } else if (window && reuse && window.bmwTint) { c = window.bmwTint; hue = (window.bmwHue == null ? null : window.bmwHue); window.bmwTint = null; } else if (window) { // Willekeurig: next sequence number → hue spaced by ~golden angle if (!(window.bmwSeq >= 1)) window.bmwSeq = (this.seq = (this.seq || 0) + 1); hue = Math.floor((window.bmwSeq * 137.508) % 360); c = this.hsvToRgb(hue, HS, HV); window.bmwTint = c; window.bmwHue = hue; } else { c = FIXED[0]; } if (window) { // Shader: tk < 11 → named table; tk >= 1000 → HUE_RGB[tk-1000]. // Compact id in uProgress integer part (NOT *4096 — that destroyed float32 morph precision). window.bmwTintId = (hue == null) ? Math.max(0, FIXED.findIndex(x => x[0] == c[0] && x[1] == c[1] && x[2] == c[2])) : (1000 + (hue | 0)); window.bmwBase = window.bmwTintId; } if (this.debug && window) console.log('MARBLEDBG tint caption=' + window.caption + ' class=' + window.windowClass + ' seq=' + window.bmwSeq + ' hue=' + hue + ' reuse=' + reuse + ' mode=' + this.tintMode + ' tintId=' + window.bmwTintId); effect.setUniform(this.shader, 'uTintR', c[0]); effect.setUniform(this.shader, 'uTintG', c[1]); effect.setUniform(this.shader, 'uTintB', c[2]); } 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: this.prog(window, 0.0), to: this.prog(window, 1.0 / Math.max(1e-4, this.duration / Math.max(1, this.duration + this.pause + this.fallDuration))) }] }); } // ---- Smooth S-path: two parallel translations with different easing curves ---- // Along the taskbar normal the marble falls with gravity (InQuad = 0.5 g t^2 from rest); // sideways it moves with InOutSine (slow, fast in the middle, slow). Together this gives an // S-shaped path that leaves straight down and enters the taskbar perpendicular. Duration // from the distance along the normal: t = sqrt(2 d / g). No chains, no per-frame calls. fallPlan(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 d = [cy - vs.y, vs.y + vs.height - cy, cx - vs.x, vs.x + vs.width - cx]; // top,bottom,left,right let k = 1; for (let i = 0; i < 4; i++) if (d[i] < d[k]) k = i; // bottom is default const vertical = k < 2; // taskbar at top/bottom const along = Math.max(30, Math.abs(vertical ? dy : dx)); const T = animationTime(Math.round(1000 * Math.sqrt(2 * along / this.gravity))); // KWin scales around the centre of the expanded geometry (incl. shadow), not the window // centre; when the marble shrinks to ~0 it would drift there. Offset to compensate. let ox = 0, oy = 0; try { const eg = window.expandedGeometry; ox = (wr.x + wr.width / 2) - (eg.x + eg.width / 2); oy = (wr.y + wr.height / 2) - (eg.y + eg.height / 2); } catch (e) {} return {dx: dx, dy: dy, T: Math.max(1, T), vertical: vertical, ox: ox, oy: oy}; } // dir: 1 = minimize (towards the icon), -1 = restore (time-reversed) fallAnims(window, f, dir) { const fwd = dir > 0; const gravityCurve = fwd ? QEasingCurve.InQuad : QEasingCurve.OutQuad; // OutQuad = reversed InQuad const sideCurve = QEasingCurve.InOutSine; // symmetric const xCurve = f.vertical ? sideCurve : gravityCurve; const yCurve = f.vertical ? gravityCurve : sideCurve; const ax = fwd ? [0, f.dx] : [f.dx, 0], ay = fwd ? [0, f.dy] : [f.dy, 0]; window.bmwXAnimation = animate({ window: window, curve: xCurve, duration: f.T, animations: [{ type: Effect.Translation, from: {value1: ax[0], value2: 0.0}, to: {value1: ax[1], value2: 0.0} }, { type: Effect.Opacity, from: 1.0, to: 1.0 }] }); window.bmwYAnimation = animate({ window: window, curve: yCurve, duration: f.T, animations: [{ type: Effect.Translation, from: {value1: 0.0, value2: ay[0]}, to: {value1: 0.0, value2: ay[1]} }] }); // single scale 100% -> ~0 (0.001, never 0) essentially in the last part (InExpo stays ~1 until late); reverse = OutExpo window.bmwScaleAnimation = animate({ window: window, curve: fwd ? QEasingCurve.InExpo : QEasingCurve.OutExpo, duration: f.T, animations: [{ type: Effect.Scale, from: {value1: fwd ? 1.0 : 0.001, value2: fwd ? 1.0 : 0.001}, to: {value1: fwd ? 0.001 : 1.0, value2: fwd ? 0.001 : 1.0} }] }); } slotWindowMinimized(window) { if (effects.hasActiveFullScreenEffect) return; const ir = window.iconGeometry; if (!ir || ir.width == 0 || ir.height == 0) return; // no taskbar entry const old = this.takeAll(window); const f = this.fallPlan(window, ir); this.applyTint(window, false); const morph = this.duration + this.pause; const total = morph + f.T; 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 + 3000)); // match ShaderUniform duration (total+3000) // one shader animation, deliberately longer; cancelled after the invisible tail (flash fix) window.bmwMinAnimation = animate({ window: window, curve: QEasingCurve.Linear, duration: total + 3000, animations: [{ type: Effect.ShaderUniform, fragmentShader: this.shader, uniform: "uProgress", from: this.prog(window, 0.0), to: this.prog(window, (total + 3000) / Math.max(1, this.duration)) }, { type: Effect.Opacity, from: 1.0, to: 1.0 }] }); 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.bmwFall = f; window.bmwMode = "morph"; window.bmwSegEnd = Date.now() + morph - 8; this.cancelIds(old); // after the new shader animation exists (keeps the snapshot) } slotWindowUnminimized(window) { if (effects.hasActiveFullScreenEffect) return; const ir = window.iconGeometry; const old = this.takeAll(window); if (!ir || ir.width == 0 || ir.height == 0) { this.cancelIds(old); return; } // reverse: rise from the icon (same curves reversed), rest, unfold const f = this.fallPlan(window, ir); this.applyTint(window, true); const total = this.duration + this.pause + f.T; 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: this.prog(window, total / Math.max(1, this.duration)), to: this.prog(window, 0.0) }] }); this.fallAnims(window, f, -1); window.bmwFadeInAnimation = animate({ window: window, curve: QEasingCurve.InExpo, duration: 80, animations: [{ type: Effect.Opacity, from: 0.0, to: 1.0 }] }); this.cancelIds(old); // after the new shader animation exists (keeps the snapshot) } // Detach the running animations of this window without cancelling them yet. They are cancelled // (cancelIds) only after the new animations exist: KWin's scripted shader animations draw a // one-time snapshot of the window, and a snapshot taken while this effect is already active // comes out empty (black/invisible marble). Keeping one shader animation alive makes KWin // reuse the existing, valid snapshot for the new animation. takeAll(window) { window.bmwMode = null; const ids = []; for (const k of ["bmwMinAnimation", "bmwMarbleDone", "bmwXAnimation", "bmwYAnimation", "bmwScaleAnimation", "bmwScale2Animation", "bmwCompAnimation", "bmwTailAnimation", "bmwUnminAnimation", "bmwFadeInAnimation"]) { if (window[k]) { ids.push(window[k]); delete window[k]; } } return ids; } cancelIds(ids) { for (const id of ids) cancel(id); this.repaintAll(); } stopAll(window) { this.cancelIds(this.takeAll(window)); } // KWin reports ids as 0 here, so we use our own state + end times. slotAnimationEnded(window, id) { // marble is drawn outside the window's own damage region: repaint all on every end this.repaintAll(); if (!window || !window.bmwMode) return; if (window.bmwSegEnd && Date.now() < window.bmwSegEnd) return; // another animation ended const f = window.bmwFall; if (window.bmwMode == "morph") { // morph + pause done -> fall (one shot) this.fallAnims(window, f, 1); window.bmwMode = "fall"; window.bmwSegEnd = Date.now() + f.T - 8; } else if (window.bmwMode == "fall") { // arrived: invisible hold at the icon window.bmwTailAnimation = animate({ window: window, curve: QEasingCurve.Linear, duration: 250, animations: [{ type: Effect.Translation, from: {value1: f.dx, value2: f.dy}, to: {value1: f.dx, value2: f.dy} }, { type: Effect.Scale, from: {value1: 0.001, value2: 0.001}, to: {value1: 0.001, value2: 0.001} }, { type: Effect.Opacity, from: 0.0, to: 0.0 }] }); window.bmwMode = "tail"; window.bmwSegEnd = Date.now() + 250 - 8; this.repaintAll(); } else if (window.bmwMode == "tail") { // now stop the shader; window is hidden window.bmwMode = null; if (window.bmwMinAnimation) { cancel(window.bmwMinAnimation); delete window.bmwMinAnimation; } this.repaintAll(); } } repaintAll() { try { effects.addRepaintFull(); } catch (e) { if (!this.bmwWarned) { this.bmwWarned = true; console.warn("marble: addRepaintFull unavailable: " + e); } } } 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();