BlueGit

timfa kwin-marble

Files

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

main.js

main.js 12.5 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 BurnMyWindowsMarbleEffect {
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 effects.windowClosed.connect(this.slotWindowClosed.bind(this));
42 effects.windowDataChanged.connect(this.slotWindowDataChanged.bind(this));
43
44 this.shader = effect.addFragmentShader(Effect.MapTexture, 'marble.frag');
45
46 this.loadConfig();
47 }
48
49 // A small helper which can be used to read a hex color string (e.g. #ff3244) from the
50 // settings. This method will return an array of four floating point values
51 // [r, g, b, a]. If the settings key only refers to an rgb value, the alpha component
52 // will be set to 1.0.
53 readRGBAConfig(key) {
54 const raw = String(effect.readConfig(key, '#ffffff'));
55 const parts = raw.split(',').map(x => parseFloat(x.trim()));
56 if (parts.length >= 3 && parts.every(x => !isNaN(x))) {
57 const c = parts.map(x => x / 255);
58 return [c[0], c[1], c[2], c.length > 3 ? c[3] : 1.0];
59 }
60 const color = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?([a-f\d]{2})?$/i
61 .exec(effect.readConfig(key, '#ffffff'))
62 .slice(1)
63 .filter(x => x !== undefined)
64 .map(x => parseInt(x, 16) / 255);
65 return color.length == 3 ? [color[0], color[1], color[2], 1.0] :
66 [color[1], color[2], color[3], color[0]];
67 }
68
69 // A small helper which can be used to read a hex color string (e.g. #ff3244) from the
70 // settings. This method will return an array of three floating point values [r, g, b]
71 // regardless of whether the settings key actually refers to an argb hex string.
72 readRGBConfig(key) {
73 const color = this.readRGBAConfig(key);
74 color.pop();
75 return color;
76 }
77
78 // This is called when the effect is loaded and whenever the settings of the effect
79 // are changed by the user.
80 loadConfig() {
81 this.duration = animationTime(effect.readConfig("Duration", 200));
82 this.pause = animationTime(Number(effect.readConfig("Pause", 400)));
83 this.gravity = Math.max(100, Number(effect.readConfig('Gravity', 4500))); // px/s^2
84 let sh = 1080;
85 try { sh = effects.virtualScreenGeometry.height || sh; } catch (e) {}
86 this.fallDistance = 1.5 * sh;
87 // free fall from rest: d = 0.5 g t^2 -> t = sqrt(2d/g); scaled by the global speed factor
88 this.fallDuration = animationTime(Math.round(1000 * Math.sqrt(2 * this.fallDistance / this.gravity)));
89 effect.setUniform(this.shader, 'uDuration', (this.duration + this.pause + this.fallDuration) * 0.001);
90 // 0..10 colours, 11 = random. Values saved by v0.3.0 (10 = white, 12 = random) fall back safely.
91 let mc = Math.floor(Number(effect.readConfig('MixColor', 0)));
92 if (!(mc >= 0)) mc = 0; else if (mc > 11) mc = 11;
93 this.tintMode = mc;
94 this.applyTint(null, false);
95 effect.setUniform(this.shader, 'uMarbleSize', Math.max(4, Number(effect.readConfig('MarbleSize', 42))));
96 effect.setUniform(this.shader, 'uMixOuter', Math.min(100, Math.max(0, Number(effect.readConfig('BlackMixOuter', 80)))) / 100);
97 effect.setUniform(this.shader, 'uMixCenter', Math.min(100, Math.max(0, Number(effect.readConfig('BlackMixCenter', 5)))) / 100);
98 effect.setUniform(this.shader, 'uCrumpleEnd', this.duration / Math.max(1, this.duration + this.pause + this.fallDuration));
99 }
100
101 // Mix target colour. tintMode 11 = random; with reuse, a random colour picked for this
102 // window (e.g. at minimize) is used again (e.g. at unminimize) and then forgotten.
103 // uProgress value for this window: tint base + morph-normalised progress m (capped, see shader)
104 prog(window, m) { return (window.bmwBase || 0) + Math.min(4000, Math.max(0, m)); }
105
106 crumpleEnd() { return Math.max(1e-4, this.duration / Math.max(1, this.duration + this.pause + this.fallDuration)); }
107
108 applyTint(window, reuse) {
109 const C = [[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]];
110 let c;
111 if (this.tintMode < C.length) { c = C[this.tintMode]; if (window) window.bmwTint = null; }
112 else if (window && reuse && window.bmwTint) { c = window.bmwTint; window.bmwTint = null; }
113 else if (window) { // random: derived from the window id + a per-window counter (new colour per close/minimize)
114 let h = 0; const id = String(window.internalId) + '#' + (window.bmwN = (window.bmwN || 0) + 1);
115 for (let i = 0; i < id.length; i++) h = (h * 31 + id.charCodeAt(i)) | 0;
116 c = C[Math.abs(h) % C.length]; window.bmwTint = c;
117 }
118 else c = C[0];
119 if (window) window.bmwBase = 4096 * Math.max(0, C.findIndex(x => x[0] == c[0] && x[1] == c[1] && x[2] == c[2])); // per-window tint, carried in uProgress
120 effect.setUniform(this.shader, 'uTintR', c[0]);
121 effect.setUniform(this.shader, 'uTintG', c[1]);
122 effect.setUniform(this.shader, 'uTintB', c[2]);
123 }
124
125 static shouldAnimateWindow(window) {
126
127 // We don't want to animate most of plasmashell's windows, yet, some
128 // of them we want to, for example, Task Manager Settings window.
129 // The problem is that all those window share single window class.
130 // So, the only way to decide whether a window should be animated is
131 // to use a heuristic: if a window has decoration, then it's most
132 // likely a dialog or a settings window so we have to animate it.
133 if (window.windowClass == 'plasmashell plasmashell' ||
134 window.windowClass == 'plasmashell org.kde.plasmashell') {
135 return window.hasDecoration;
136 }
137
138 // This is primarily to avoid animating the AltTab popup.
139 // See https://github.com/Schneegans/Burn-My-Windows/issues/384 for details.
140 if (!window.hasDecoration && window.onAllDesktops) {
141 return false
142 }
143
144 if (classBlacklist.indexOf(window.windowClass) != -1) {
145 return false;
146 }
147
148 if (nameBlacklist.indexOf(window.caption) != -1) {
149 return false;
150 }
151
152 if (window.hasDecoration) {
153 return true;
154 }
155
156 // Don't animate combobox popups, tooltips, popup menus, etc.
157 if (window.popupWindow) {
158 return false;
159 }
160
161 // Dont't animate the outline and the screenlocker as it looks bad.
162 if (window.lockScreen || window.outline) {
163 return false;
164 }
165
166 // Override-redirect windows are usually used for user interface
167 // concepts that are not expected to be animated by this effect.
168 if (!window.managed) {
169 return false;
170 }
171
172 return window.normalWindow || window.dialog;
173 }
174
175 setupForcedRoles(window) {
176 // marble: no forced blur/contrast (blurred window-sized rect + cost)
177 // window.setData(Effect.WindowForceBackgroundContrastRole, true);
178 // marble: no forced blur/contrast (blurred window-sized rect + cost)
179 // window.setData(Effect.WindowForceBlurRole, true);
180 }
181
182 cleanupForcedRoles(window) {
183 window.setData(Effect.WindowForceBackgroundContrastRole, null);
184 window.setData(Effect.WindowForceBlurRole, null);
185 }
186
187 slotWindowAdded(window) {
188 if (effects.hasActiveFullScreenEffect) {
189 return;
190 }
191 if (!BurnMyWindowsMarbleEffect.shouldAnimateWindow(window)) {
192 return;
193 }
194 if (!window.visible) {
195 return;
196 }
197 if (effect.isGrabbed(window, Effect.WindowAddedGrabRole)) {
198 return;
199 }
200 this.setupForcedRoles(window);
201
202 this.applyTint(window, false);
203 effect.setUniform(this.shader, 'uForOpening', 1.0);
204 effect.setUniform(this.shader, 'uIsFullscreen', window.fullScreen ? 1.0 : 0.0);
205
206
207
208 window.bmwInAnimation = animate({
209 window: window,
210 curve: QEasingCurve.Linear,
211 duration: this.duration,
212 animations: [{
213 type: Effect.ShaderUniform,
214 fragmentShader: this.shader,
215 uniform: 'uProgress',
216 from: this.prog(window, 0.0),
217 to: this.prog(window, 1.0 / this.crumpleEnd())
218 }]
219 });
220 }
221
222 slotWindowClosed(window) {
223 if (effects.hasActiveFullScreenEffect) {
224 return;
225 }
226 if (!BurnMyWindowsMarbleEffect.shouldAnimateWindow(window)) {
227 return;
228 }
229 if (!window.visible || window.skipsCloseAnimation) {
230 return;
231 }
232 if (effect.isGrabbed(window, Effect.WindowClosedGrabRole)) {
233 return;
234 }
235 if (window.bmwInAnimation) {
236 cancel(window.bmwInAnimation);
237 delete window.bmwInAnimation;
238 }
239 this.setupForcedRoles(window);
240
241 this.applyTint(window, false);
242 effect.setUniform(this.shader, 'uForOpening', 0.0);
243 effect.setUniform(this.shader, 'uIsFullscreen', window.fullScreen ? 1.0 : 0.0);
244
245
246
247 // 1) marble morph (shader); runs the whole time so the marble stays drawn while falling
248 window.bmwOutAnimation = animate({
249 window: window,
250 curve: QEasingCurve.Linear,
251 duration: this.duration + this.pause + this.fallDuration,
252 animations: [{
253 type: Effect.ShaderUniform,
254 fragmentShader: this.shader,
255 uniform: 'uProgress',
256 from: this.prog(window, 0.0),
257 to: this.prog(window, 1.0 / this.crumpleEnd())
258 }]
259 });
260 // 2) marker animation that ends exactly when the marble is done; the fall is
261 // started from its animationEnded (KWin ignored the per-call delay).
262 window.bmwMarbleDone = animate({
263 window: window,
264 curve: QEasingCurve.Linear,
265 duration: this.duration + this.pause,
266 animations: [{ type: Effect.Translation, from: {value1: 0.0, value2: 0.0}, to: {value1: 0.0, value2: 0.0} }]
267 });
268 window.bmwFallPending = true;
269 }
270
271 slotAnimationEnded(window, id) {
272 // marble is drawn outside the window's own damage region: repaint all on every end
273 this.repaintAll();
274 if (!window || !window.bmwFallPending) return; // first end = marble marker (ids arrive as 0)
275 window.bmwFallPending = false;
276 // free fall from rest: InQuad = t^2 = constant acceleration, d = 0.5 g t^2
277 window.bmwFallAnimation = animate({
278 window: window,
279 curve: QEasingCurve.InQuad,
280 duration: this.fallDuration,
281 animations: [{ type: Effect.Translation, from: {value1: 0.0, value2: 0.0},
282 to: {value1: 0.0, value2: this.fallDistance} }]
283 });
284 }
285
286 repaintAll() {
287 try { effects.addRepaintFull(); } catch (e) {
288 if (!this.bmwWarned) { this.bmwWarned = true; console.warn("marble: addRepaintFull unavailable: " + e); }
289 }
290 }
291
292 slotWindowDataChanged(window, role) {
293 if (role == Effect.WindowAddedGrabRole) {
294 if (window.bmwInAnimation && effect.isGrabbed(window, role)) {
295 cancel(window.bmwInAnimation);
296 delete window.bmwInAnimation;
297 this.cleanupForcedRoles(window);
298 }
299 } else if (role == Effect.WindowClosedGrabRole) {
300 if (window.bmwOutAnimation && effect.isGrabbed(window, role)) {
301 cancel(window.bmwOutAnimation);
302 delete window.bmwOutAnimation;
303 this.cleanupForcedRoles(window);
304 }
305 }
306 }
307}
308
309new BurnMyWindowsMarbleEffect();