BlueGit

timfa kwin-marble

Files

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

main.js

main.js 10.3 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 effect.setUniform(this.shader, 'uMixOuter', Math.min(100, Math.max(0, Number(effect.readConfig('BlackMixOuter', 80)))) / 100);
91 effect.setUniform(this.shader, 'uMixCenter', Math.min(100, Math.max(0, Number(effect.readConfig('BlackMixCenter', 5)))) / 100);
92 effect.setUniform(this.shader, 'uCrumpleEnd', this.duration / Math.max(1, this.duration + this.pause + this.fallDuration));
93 }
94
95 static shouldAnimateWindow(window) {
96
97 // We don't want to animate most of plasmashell's windows, yet, some
98 // of them we want to, for example, Task Manager Settings window.
99 // The problem is that all those window share single window class.
100 // So, the only way to decide whether a window should be animated is
101 // to use a heuristic: if a window has decoration, then it's most
102 // likely a dialog or a settings window so we have to animate it.
103 if (window.windowClass == 'plasmashell plasmashell' ||
104 window.windowClass == 'plasmashell org.kde.plasmashell') {
105 return window.hasDecoration;
106 }
107
108 // This is primarily to avoid animating the AltTab popup.
109 // See https://github.com/Schneegans/Burn-My-Windows/issues/384 for details.
110 if (!window.hasDecoration && window.onAllDesktops) {
111 return false
112 }
113
114 if (classBlacklist.indexOf(window.windowClass) != -1) {
115 return false;
116 }
117
118 if (nameBlacklist.indexOf(window.caption) != -1) {
119 return false;
120 }
121
122 if (window.hasDecoration) {
123 return true;
124 }
125
126 // Don't animate combobox popups, tooltips, popup menus, etc.
127 if (window.popupWindow) {
128 return false;
129 }
130
131 // Dont't animate the outline and the screenlocker as it looks bad.
132 if (window.lockScreen || window.outline) {
133 return false;
134 }
135
136 // Override-redirect windows are usually used for user interface
137 // concepts that are not expected to be animated by this effect.
138 if (!window.managed) {
139 return false;
140 }
141
142 return window.normalWindow || window.dialog;
143 }
144
145 setupForcedRoles(window) {
146 // marble: no forced blur/contrast (blurred window-sized rect + cost)
147 // window.setData(Effect.WindowForceBackgroundContrastRole, true);
148 // marble: no forced blur/contrast (blurred window-sized rect + cost)
149 // window.setData(Effect.WindowForceBlurRole, true);
150 }
151
152 cleanupForcedRoles(window) {
153 window.setData(Effect.WindowForceBackgroundContrastRole, null);
154 window.setData(Effect.WindowForceBlurRole, null);
155 }
156
157 slotWindowAdded(window) {
158 if (effects.hasActiveFullScreenEffect) {
159 return;
160 }
161 if (!BurnMyWindowsMarbleEffect.shouldAnimateWindow(window)) {
162 return;
163 }
164 if (!window.visible) {
165 return;
166 }
167 if (effect.isGrabbed(window, Effect.WindowAddedGrabRole)) {
168 return;
169 }
170 this.setupForcedRoles(window);
171
172 effect.setUniform(this.shader, 'uForOpening', 1.0);
173 effect.setUniform(this.shader, 'uIsFullscreen', window.fullScreen ? 1.0 : 0.0);
174
175
176
177 window.bmwInAnimation = animate({
178 window: window,
179 curve: QEasingCurve.Linear,
180 duration: this.duration,
181 animations: [{
182 type: Effect.ShaderUniform,
183 fragmentShader: this.shader,
184 uniform: 'uProgress',
185 from: 0.0,
186 to: 1.0
187 }]
188 });
189 }
190
191 slotWindowClosed(window) {
192 if (effects.hasActiveFullScreenEffect) {
193 return;
194 }
195 if (!BurnMyWindowsMarbleEffect.shouldAnimateWindow(window)) {
196 return;
197 }
198 if (!window.visible || window.skipsCloseAnimation) {
199 return;
200 }
201 if (effect.isGrabbed(window, Effect.WindowClosedGrabRole)) {
202 return;
203 }
204 if (window.bmwInAnimation) {
205 cancel(window.bmwInAnimation);
206 delete window.bmwInAnimation;
207 }
208 this.setupForcedRoles(window);
209
210 effect.setUniform(this.shader, 'uForOpening', 0.0);
211 effect.setUniform(this.shader, 'uIsFullscreen', window.fullScreen ? 1.0 : 0.0);
212
213
214
215 // 1) marble morph (shader); runs the whole time so the marble stays drawn while falling
216 window.bmwOutAnimation = animate({
217 window: window,
218 curve: QEasingCurve.Linear,
219 duration: this.duration + this.pause + this.fallDuration,
220 animations: [{
221 type: Effect.ShaderUniform,
222 fragmentShader: this.shader,
223 uniform: 'uProgress',
224 from: 0.0,
225 to: 1.0
226 }]
227 });
228 // 2) marker animation that ends exactly when the marble is done; the fall is
229 // started from its animationEnded (KWin ignored the per-call delay).
230 window.bmwMarbleDone = animate({
231 window: window,
232 curve: QEasingCurve.Linear,
233 duration: this.duration + this.pause,
234 animations: [{ type: Effect.Translation, from: {value1: 0.0, value2: 0.0}, to: {value1: 0.0, value2: 0.0} }]
235 });
236 window.bmwFallPending = true;
237 }
238
239 slotAnimationEnded(window, id) {
240 // marble is drawn outside the window's own damage region: repaint all on every end
241 this.repaintAll();
242 if (!window || !window.bmwFallPending) return; // first end = marble marker (ids arrive as 0)
243 window.bmwFallPending = false;
244 // free fall from rest: InQuad = t^2 = constant acceleration, d = 0.5 g t^2
245 window.bmwFallAnimation = animate({
246 window: window,
247 curve: QEasingCurve.InQuad,
248 duration: this.fallDuration,
249 animations: [{ type: Effect.Translation, from: {value1: 0.0, value2: 0.0},
250 to: {value1: 0.0, value2: this.fallDistance} }]
251 });
252 }
253
254 repaintAll() {
255 try { effects.addRepaintFull(); } catch (e) {
256 if (!this.bmwWarned) { this.bmwWarned = true; console.warn("marble: addRepaintFull unavailable: " + e); }
257 }
258 }
259
260 slotWindowDataChanged(window, role) {
261 if (role == Effect.WindowAddedGrabRole) {
262 if (window.bmwInAnimation && effect.isGrabbed(window, role)) {
263 cancel(window.bmwInAnimation);
264 delete window.bmwInAnimation;
265 this.cleanupForcedRoles(window);
266 }
267 } else if (role == Effect.WindowClosedGrabRole) {
268 if (window.bmwOutAnimation && effect.isGrabbed(window, role)) {
269 cancel(window.bmwOutAnimation);
270 delete window.bmwOutAnimation;
271 this.cleanupForcedRoles(window);
272 }
273 }
274 }
275}
276
277new BurnMyWindowsMarbleEffect();