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