BlueGit

timfa kwin-marble

Files

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

main.js

main.js 11.8 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 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 slotWindowMinimized(window) {
196 if (effects.hasActiveFullScreenEffect) return;
197 const ir = window.iconGeometry;
198 if (!ir || ir.width == 0 || ir.height == 0) return; // no taskbar entry
199 this.stopAll(window);
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 const dist = Math.max(1, Math.sqrt(dx * dx + dy * dy));
204 // straight-line free fall from rest: d = 0.5 g t^2 -> t = sqrt(2d/g)
205 const fall = animationTime(Math.round(1000 * Math.sqrt(2 * dist / this.gravity)));
206 const total = this.duration + this.pause + fall;
207 effect.setUniform(this.shader, "uForOpening", 0.0);
208 effect.setUniform(this.shader, "uIsFullscreen", window.fullScreen ? 1.0 : 0.0);
209 effect.setUniform(this.shader, "uDuration", total * 0.001);
210 effect.setUniform(this.shader, "uCrumpleEnd", this.duration / Math.max(1, total));
211 window.bmwMinAnimation = animate({
212 window: window, curve: QEasingCurve.Linear, duration: total,
213 animations: [{ type: Effect.ShaderUniform, fragmentShader: this.shader, uniform: "uProgress", from: 0.0, to: 1.0 },
214 { type: Effect.Opacity, from: 1.0, to: 1.0 }]
215 });
216 window.bmwMarbleDone = animate({
217 window: window, curve: QEasingCurve.Linear, duration: this.duration + this.pause,
218 animations: [{ type: Effect.Translation, from: {value1: 0.0, value2: 0.0}, to: {value1: 0.0, value2: 0.0} }]
219 });
220 window.bmwFall = {dx: dx, dy: dy, duration: fall};
221 window.bmwFallPending = true;
222 }
223
224 slotWindowUnminimized(window) {
225 if (effects.hasActiveFullScreenEffect) return;
226 const ir = window.iconGeometry;
227 this.stopAll(window);
228 if (!ir || ir.width == 0 || ir.height == 0) return;
229 // exact reverse: marble rises from the taskbar entry (reverse of the fall timing),
230 // rests, then unfolds back into the window. Window is full only at the very end.
231 const wr = window.geometry;
232 const dx = (ir.x + ir.width / 2) - (wr.x + wr.width / 2);
233 const dy = (ir.y + ir.height / 2) - (wr.y + wr.height / 2);
234 const dist = Math.max(1, Math.sqrt(dx * dx + dy * dy));
235 const fall = animationTime(Math.round(1000 * Math.sqrt(2 * dist / this.gravity)));
236 const total = this.duration + this.pause + fall;
237 effect.setUniform(this.shader, "uForOpening", 0.0);
238 effect.setUniform(this.shader, "uIsFullscreen", window.fullScreen ? 1.0 : 0.0);
239 effect.setUniform(this.shader, "uDuration", total * 0.001);
240 effect.setUniform(this.shader, "uCrumpleEnd", this.duration / Math.max(1, total));
241 window.bmwUnminAnimation = animate({
242 window: window, curve: QEasingCurve.Linear, duration: total,
243 animations: [{ type: Effect.ShaderUniform, fragmentShader: this.shader, uniform: "uProgress", from: 1.0, to: 0.0 }]
244 });
245 // OutQuad = time-reversed InQuad: fast at the taskbar, decelerating to rest
246 window.bmwRiseAnimation = animate({
247 window: window, curve: QEasingCurve.OutQuad, duration: fall,
248 animations: [{ type: Effect.Translation, from: {value1: dx, value2: dy}, to: {value1: 0.0, value2: 0.0} }]
249 });
250 }
251
252 stopAll(window) {
253 window.bmwFallPending = false;
254 for (const k of ["bmwMinAnimation", "bmwMarbleDone", "bmwFallAnimation", "bmwUnminAnimation", "bmwRiseAnimation"]) {
255 if (window[k]) { cancel(window[k]); delete window[k]; }
256 }
257 }
258
259 slotAnimationEnded(window, id) {
260 if (!window || !window.bmwFallPending) return; // first end = marble marker
261 window.bmwFallPending = false;
262 const f = window.bmwFall;
263 window.bmwFallAnimation = animate({
264 window: window, curve: QEasingCurve.InQuad, duration: f.duration, keepAtTarget: true,
265 animations: [{ type: Effect.Translation, from: {value1: 0.0, value2: 0.0}, to: {value1: f.dx, value2: f.dy} },
266 { type: Effect.Opacity, from: 1.0, to: 1.0 }]
267 });
268 }
269
270 slotWindowDataChanged(window, role) {
271 if (role == Effect.WindowAddedGrabRole) {
272 if (window.bmwInAnimation && effect.isGrabbed(window, role)) {
273 cancel(window.bmwInAnimation);
274 delete window.bmwInAnimation;
275 this.cleanupForcedRoles(window);
276 }
277 } else if (role == Effect.WindowClosedGrabRole) {
278 if (window.bmwOutAnimation && effect.isGrabbed(window, role)) {
279 cancel(window.bmwOutAnimation);
280 delete window.bmwOutAnimation;
281 this.cleanupForcedRoles(window);
282 }
283 }
284 }
285}
286
287new BurnMyWindowsMarbleMinimizeEffect();