| 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 | // Spectacle (KDE screenshot tool) should not be animated. |
| 26 | 'spectacle org.kde.spectacle' |
| 27 | ]; |
| 28 | |
| 29 | const 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 | |
| 35 | class 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 | return {nodes: nodes, length: L, total: nodes[N].t}; |
| 232 | } |
| 233 | |
| 234 | segAnim(window, a, b, dur, extra) { |
| 235 | const anims = [{ type: Effect.Translation, from: {value1: a.x, value2: a.y}, to: {value1: b.x, value2: b.y} }, |
| 236 | { type: Effect.Opacity, from: 1.0, to: 1.0 }]; |
| 237 | if (extra) anims.push(extra); |
| 238 | window.bmwSegEnd = Date.now() + Math.max(1, dur) - 8; |
| 239 | return animate({ window: window, curve: QEasingCurve.Linear, duration: Math.max(1, dur), animations: anims }); |
| 240 | } |
| 241 | |
| 242 | slotWindowMinimized(window) { |
| 243 | if (effects.hasActiveFullScreenEffect) return; |
| 244 | const ir = window.iconGeometry; |
| 245 | if (!ir || ir.width == 0 || ir.height == 0) return; // no taskbar entry |
| 246 | this.stopAll(window); |
| 247 | const path = this.buildPath(window, ir); |
| 248 | const morph = this.duration + this.pause; |
| 249 | const total = morph + path.total; |
| 250 | effect.setUniform(this.shader, "uForOpening", 0.0); |
| 251 | effect.setUniform(this.shader, "uIsFullscreen", window.fullScreen ? 1.0 : 0.0); |
| 252 | effect.setUniform(this.shader, "uDuration", total * 0.001); |
| 253 | effect.setUniform(this.shader, "uCrumpleEnd", this.duration / Math.max(1, total)); |
| 254 | // ONE shader animation over the whole minimize (same setup as restore, which renders fine); |
| 255 | // a little longer than the path so the marble is drawn until the window is hidden. |
| 256 | window.bmwMinAnimation = animate({ |
| 257 | window: window, curve: QEasingCurve.Linear, duration: total + 150, |
| 258 | animations: [{ type: Effect.ShaderUniform, fragmentShader: this.shader, uniform: "uProgress", from: 0.0, to: 1.0 }, |
| 259 | { type: Effect.Opacity, from: 1.0, to: 1.0 }] |
| 260 | }); |
| 261 | // marker for the end of morph + pause; when it ends the path traversal starts |
| 262 | window.bmwMarbleDone = animate({ |
| 263 | window: window, curve: QEasingCurve.Linear, duration: morph, |
| 264 | animations: [{ type: Effect.Translation, from: {value1: 0.0, value2: 0.0}, to: {value1: 0.0, value2: 0.0} }] |
| 265 | }); |
| 266 | window.bmwPath = path; window.bmwSeg = -1; window.bmwMode = "min"; |
| 267 | window.bmwSegEnd = Date.now() + morph - 8; |
| 268 | } |
| 269 | |
| 270 | slotWindowUnminimized(window) { |
| 271 | if (effects.hasActiveFullScreenEffect) return; |
| 272 | const ir = window.iconGeometry; |
| 273 | this.stopAll(window); |
| 274 | if (!ir || ir.width == 0 || ir.height == 0) return; |
| 275 | // exact reverse: marble comes up the same S-path (reverse gravity timing), growing from |
| 276 | // 10% to 100% over the first 10% of the path, rests, then unfolds into the window. |
| 277 | const path = this.buildPath(window, ir); |
| 278 | const total = this.duration + this.pause + path.total; |
| 279 | effect.setUniform(this.shader, "uForOpening", 0.0); |
| 280 | effect.setUniform(this.shader, "uIsFullscreen", window.fullScreen ? 1.0 : 0.0); |
| 281 | effect.setUniform(this.shader, "uDuration", total * 0.001); |
| 282 | effect.setUniform(this.shader, "uCrumpleEnd", this.duration / Math.max(1, total)); |
| 283 | window.bmwUnminAnimation = animate({ |
| 284 | window: window, curve: QEasingCurve.Linear, duration: total, |
| 285 | animations: [{ type: Effect.ShaderUniform, fragmentShader: this.shader, uniform: "uProgress", from: 1.0, to: 0.0 }] |
| 286 | }); |
| 287 | const T = path.total, nd = path.nodes, N = nd.length - 1; |
| 288 | // reversed timing: going from node N to node N-1 takes (t_N - t_{N-1}) etc. |
| 289 | window.bmwScaleAnimation = animate({ |
| 290 | window: window, curve: QEasingCurve.InOutSine, duration: Math.max(1, T - nd[N - 2].t), |
| 291 | animations: [{ type: Effect.Scale, from: {value1: 0.1, value2: 0.1}, to: {value1: 1.0, value2: 1.0} }] |
| 292 | }); |
| 293 | window.bmwPath = path; window.bmwSeg = N; window.bmwMode = "rise"; |
| 294 | window.bmwSegAnimation = this.segAnim(window, nd[N], nd[N - 1], nd[N].t - nd[N - 1].t); |
| 295 | } |
| 296 | |
| 297 | stopAll(window) { |
| 298 | window.bmwMode = null; |
| 299 | for (const k of ["bmwMinAnimation", "bmwMarbleDone", "bmwSegAnimation", "bmwScaleAnimation", |
| 300 | "bmwTailAnimation", "bmwUnminAnimation"]) { |
| 301 | if (window[k]) { cancel(window[k]); delete window[k]; } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | // Chain the path segments. KWin reports ids as 0 here, so we use our own state. |
| 306 | slotAnimationEnded(window, id) { |
| 307 | if (!window || !window.bmwMode) return; |
| 308 | if (window.bmwSegEnd && Date.now() < window.bmwSegEnd) return; // another animation ended |
| 309 | const nd = window.bmwPath.nodes, N = nd.length - 1; |
| 310 | if (window.bmwMode == "min") { |
| 311 | if (window.bmwSeg >= N) return; // already in the tail |
| 312 | window.bmwSeg++; |
| 313 | const i = window.bmwSeg; |
| 314 | if (i == N) { |
| 315 | // arrived: invisible hold at the icon (scale 10%) until the shader animation ends |
| 316 | window.bmwTailAnimation = animate({ |
| 317 | window: window, curve: QEasingCurve.Linear, duration: 200, |
| 318 | animations: [{ type: Effect.Translation, from: {value1: nd[N].x, value2: nd[N].y}, to: {value1: nd[N].x, value2: nd[N].y} }, |
| 319 | { type: Effect.Scale, from: {value1: 0.1, value2: 0.1}, to: {value1: 0.1, value2: 0.1} }, |
| 320 | { type: Effect.Opacity, from: 0.0, to: 0.0 }] |
| 321 | }); |
| 322 | window.bmwMode = null; |
| 323 | return; |
| 324 | } |
| 325 | let extra = null; |
| 326 | if (i == N - 2) { // last 10% of arc length: 100% -> 10% |
| 327 | window.bmwScaleAnimation = animate({ |
| 328 | window: window, curve: QEasingCurve.InOutSine, duration: Math.max(1, nd[N].t - nd[N - 2].t), |
| 329 | animations: [{ type: Effect.Scale, from: {value1: 1.0, value2: 1.0}, to: {value1: 0.1, value2: 0.1} }] |
| 330 | }); |
| 331 | } |
| 332 | window.bmwSegAnimation = this.segAnim(window, nd[i], nd[i + 1], nd[i + 1].t - nd[i].t, extra); |
| 333 | } else if (window.bmwMode == "rise") { |
| 334 | window.bmwSeg--; |
| 335 | const i = window.bmwSeg; |
| 336 | if (i <= 0) { window.bmwMode = null; return; } |
| 337 | window.bmwSegAnimation = this.segAnim(window, nd[i], nd[i - 1], nd[i].t - nd[i - 1].t); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | slotWindowDataChanged(window, role) { |
| 342 | if (role == Effect.WindowAddedGrabRole) { |
| 343 | if (window.bmwInAnimation && effect.isGrabbed(window, role)) { |
| 344 | cancel(window.bmwInAnimation); |
| 345 | delete window.bmwInAnimation; |
| 346 | this.cleanupForcedRoles(window); |
| 347 | } |
| 348 | } else if (role == Effect.WindowClosedGrabRole) { |
| 349 | if (window.bmwOutAnimation && effect.isGrabbed(window, role)) { |
| 350 | cancel(window.bmwOutAnimation); |
| 351 | delete window.bmwOutAnimation; |
| 352 | this.cleanupForcedRoles(window); |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | new BurnMyWindowsMarbleMinimizeEffect(); |