From e20bcd46d7c4bda50a942f66df552cb99a1b73fb Mon Sep 17 00:00:00 2001 From: Simon Schneegans Date: Thu, 5 May 2022 21:17:03 +0200 Subject: [PATCH] :wrench: Use an array of effect instances --- extension.js | 37 ++++++++++++++++--------------------- src/Fire.js | 33 +++++++++++++++++++++++---------- src/Hexagon.js | 31 ++++++++++++++++++++++--------- 3 files changed, 61 insertions(+), 40 deletions(-) diff --git a/extension.js b/extension.js index db42dc8..3e66690 100644 --- a/extension.js +++ b/extension.js @@ -69,10 +69,6 @@ class Extension { // Store a reference to the settings object. this._settings = ExtensionUtils.getSettings(); - // This will store an item of ALL_EFFECTS array which was used the last time a window - // was opened / closed. - this._currentEffect = 0; - // We will use extensionThis to refer to the extension inside the patched methods. const extensionThis = this; @@ -352,11 +348,11 @@ class Extension { // ------------------------------------------------------------------ choose an effect // Now we chose a random effect from all enabled effects. - this._currentEffect = null; + let effect = null; // First we check if an effect is to be previewed. if (previewNick != '') { - this._currentEffect = ALL_EFFECTS.find(Effect => { + effect = ALL_EFFECTS.find(Effect => { return Effect.getNick() == previewNick; }); @@ -374,12 +370,12 @@ class Extension { // And then choose a random effect. if (enabled.length > 0) { - this._currentEffect = enabled[Math.floor(Math.random() * enabled.length)]; + effect = enabled[Math.floor(Math.random() * enabled.length)]; } } // If nothing was enabled, we have to do nothing :) - if (this._currentEffect == null) { + if (effect == null) { this._fixAnimationTimes(isDialogWindow, forOpening, null); return; } @@ -393,10 +389,9 @@ class Extension { // The following is used to tweak the ongoing transitions of a window actor. Usually // windows are faded in / out scaled up / down slightly by GNOME Shell. Here, we allow // modifications to this behavior by the effects. - const config = this._currentEffect.tweakTransition(actor, this._settings, forOpening); - const duration = testMode ? - 5000 : - this._settings.get_int(this._currentEffect.getNick() + '-animation-time'); + const config = effect.tweakTransition(actor, this._settings, forOpening); + const duration = + testMode ? 5000 : this._settings.get_int(effect.getNick() + '-animation-time'); // All animations are relative to the window's center. actor.set_pivot_point(0.5, 0.5); @@ -447,7 +442,7 @@ class Extension { // -------------------------------------------------------------------- add the shader // Now add a cool shader to our window actor! - const shader = this._currentEffect.getShader(actor, this._settings, forOpening); + const shader = effect.getShader(actor, this._settings, forOpening); if (shader) { // There should always be an opacity transition going on... @@ -459,9 +454,7 @@ class Extension { return; } - // First remove any old effect. - actor.remove_effect_by_name(`burn-my-windows-effect`); - actor.add_effect_with_name(`burn-my-windows-effect`, shader); + actor.add_effect_with_name('burn-my-windows-effect', shader); // Update uniforms at each frame. transition.connect('new-frame', (t) => { @@ -476,11 +469,13 @@ class Extension { }); // Remove the effect if the animation finished or was interrupted. - if (forOpening) { - transition.connect('stopped', () => { - actor.remove_effect_by_name(`burn-my-windows-effect`); - }); - } + transition.connect('stopped', () => { + if (forOpening) { + actor.remove_effect_by_name('burn-my-windows-effect'); + } + + effect.returnShader(shader); + }); } // Finally, ensure that all animation times are set properly so that other extensions diff --git a/src/Fire.js b/src/Fire.js index 9b04c09..bc356ec 100644 --- a/src/Fire.js +++ b/src/Fire.js @@ -30,10 +30,12 @@ const utils = Me.imports.src.utils; ////////////////////////////////////////////////////////////////////////////////////////// // The shader class for this effect is registered further down in this file. When this -// effect is used for the first time, shaderInstance will store one instance of -// ShaderClass which will be used whenever this effect is used. -let ShaderClass = null; -let shaderInstance = null; +// effect is used for the first time, an instance of this shader class is created. Once +// the effect is finished, the shader will be stored in the shaderInstances array and will +// then be reused if a new shader is requested. ShaderClass which will be used whenever +// this effect is used. +let ShaderClass = null; +let availableShaders = []; // The effect class is completely static. It can be used to get some metadata (like the // effect's name or supported GNOME Shell versions), to initialize the respective page of @@ -101,15 +103,26 @@ var Fire = class Fire { // ---------------------------------------------------------------- API for extension.js // This is called from extension.js whenever a window is opened or closed with this - // effect. + // effect. It returns an instance of the shader class, trying to reuse previously + // created shaders. static getShader(actor, settings, forOpening) { - if (shaderInstance == null) { - shaderInstance = new ShaderClass(); + let shader; + + if (availableShaders.length == 0) { + shader = new ShaderClass(); + } else { + shader = availableShaders.pop(); } - shaderInstance.setUniforms(settings, forOpening); + shader.setUniforms(settings, forOpening); - return shaderInstance; + return shader; + } + + // This will be called from extension.js once a shader which was previously acquired via + // getShader() is not used anymore. + static returnShader(shader) { + availableShaders.push(shader); } // The tweakTransition() is called from extension.js to tweak a window's open / close @@ -133,7 +146,7 @@ var Fire = class Fire { // This is called from extension.js if the extension is disabled. This should free all // static resources. static cleanUp() { - shaderInstance = null; + availableShaders = []; } // ----------------------------------------------------------------------- private stuff diff --git a/src/Hexagon.js b/src/Hexagon.js index 08a5903..e9be7cd 100644 --- a/src/Hexagon.js +++ b/src/Hexagon.js @@ -27,10 +27,12 @@ const utils = Me.imports.src.utils; ////////////////////////////////////////////////////////////////////////////////////////// // The shader class for this effect is registered further down in this file. When this -// effect is used for the first time, shaderInstance will store one instance of -// ShaderClass which will be used whenever this effect is used. -let ShaderClass = null; -let shaderInstance = null; +// effect is used for the first time, an instance of this shader class is created. Once +// the effect is finished, the shader will be stored in the shaderInstances array and will +// then be reused if a new shader is requested. ShaderClass which will be used whenever +// this effect is used. +let ShaderClass = null; +let availableShaders = []; // The effect class is completely static. It can be used to get some metadata (like the // effect's name or supported GNOME Shell versions), to initialize the respective page of @@ -83,15 +85,26 @@ var Hexagon = class Hexagon { // ---------------------------------------------------------------- API for extension.js // This is called from extension.js whenever a window is opened or closed with this - // effect. + // effect. It returns an instance of the shader class, trying to reuse previously + // created shaders. static getShader(actor, settings, forOpening) { - if (shaderInstance == null) { - shaderInstance = new ShaderClass(); + let shader; + + if (availableShaders.length == 0) { + shader = new ShaderClass(); + } else { + shader = availableShaders.pop(); } - shaderInstance.setUniforms(settings, forOpening); + shader.setUniforms(settings, forOpening); - return shaderInstance; + return shader; + } + + // This will be called from extension.js once a shader which was previously acquired via + // getShader() is not used anymore. + static returnShader(shader) { + availableShaders.push(shader); } // The tweakTransition() is called from extension.js to tweak a window's open / close