From 734530fdab98ba8369f6b698d0ff46f3a83c172e Mon Sep 17 00:00:00 2001 From: Simon Schneegans Date: Sat, 9 Sep 2023 06:46:09 +0200 Subject: [PATCH] :wrench: Use dynamic imports for gettext --- docs/how-to-create-new-effects.md | 4 ++-- src/effects/Apparition.js | 4 ++-- src/effects/BrokenGlass.js | 4 ++-- src/effects/Doom.js | 5 ++--- src/effects/EnergizeA.js | 4 ++-- src/effects/EnergizeB.js | 4 ++-- src/effects/Fire.js | 4 ++-- src/effects/Glide.js | 4 ++-- src/effects/Glitch.js | 4 ++-- src/effects/Hexagon.js | 4 ++-- src/effects/Incinerate.js | 4 ++-- src/effects/Matrix.js | 4 ++-- src/effects/PaintBrush.js | 4 ++-- src/effects/PixelWheel.js | 4 ++-- src/effects/PixelWipe.js | 4 ++-- src/effects/Pixelate.js | 4 ++-- src/effects/Portal.js | 4 ++-- src/effects/SnapOfDisintegration.js | 4 ++-- src/effects/TRexAttack.js | 4 ++-- src/effects/TVEffect.js | 4 ++-- src/effects/TVGlitch.js | 4 ++-- src/effects/Wisps.js | 4 ++-- src/utils.js | 20 ++++++++++++++++++++ 23 files changed, 64 insertions(+), 45 deletions(-) diff --git a/docs/how-to-create-new-effects.md b/docs/how-to-create-new-effects.md index 606cb2d..36a8aac 100644 --- a/docs/how-to-create-new-effects.md +++ b/docs/how-to-create-new-effects.md @@ -165,8 +165,6 @@ void main() { "use strict"; -const _ = imports.gettext.domain("burn-my-windows").gettext; - import * as utils from "../utils.js"; // We import the ShaderFactory only in the Shell process as it is not required in the @@ -174,6 +172,8 @@ import * as utils from "../utils.js"; // only uses the static metadata of the effect. const ShaderFactory = await utils.importInShellOnly("./ShaderFactory.js"); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This effect ... // // <- Please add a description of your effect here -> // diff --git a/src/effects/Apparition.js b/src/effects/Apparition.js index 46665f6..f770e71 100644 --- a/src/effects/Apparition.js +++ b/src/effects/Apparition.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import the ShaderFactory only in the Shell process as it is not required in the @@ -23,6 +21,8 @@ import * as utils from '../utils.js'; // only uses the static metadata of the effect. const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This effect hides the actor by violently sucking it into the void of magic. // ////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/effects/BrokenGlass.js b/src/effects/BrokenGlass.js index 7239294..f31efb7 100644 --- a/src/effects/BrokenGlass.js +++ b/src/effects/BrokenGlass.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import some modules only in the Shell process as they are not available in the @@ -26,6 +24,8 @@ const Clutter = await utils.importInShellOnly('gi://Clutter'); const GdkPixbuf = await utils.importInShellOnly('gi://GdkPixbuf'); const Cogl = await utils.importInShellOnly('gi://Cogl'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This effect shatters the window into pieces. For an explanation how this works, look // // further down in this file into the documentation of the Shader class. This effect is // diff --git a/src/effects/Doom.js b/src/effects/Doom.js index 7a782d5..7afa97c 100644 --- a/src/effects/Doom.js +++ b/src/effects/Doom.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import the ShaderFactory only in the Shell process as it is not required in the @@ -23,6 +21,8 @@ import * as utils from '../utils.js'; // only uses the static metadata of the effect. const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This effect melts your windows. Inspired by the legendary screen transitions of the // // original Doom. // @@ -32,7 +32,6 @@ const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); // GNOME Shell versions), to initialize the respective page of the settings dialog, as // well as to create the actual shader for the effect. export default class Effect { - // The constructor creates a ShaderFactory which will be used by extension.js to create // shader instances for this effect. The shaders will be automagically created using the // GLSL file in resources/shaders/.glsl. The callback will be called for each diff --git a/src/effects/EnergizeA.js b/src/effects/EnergizeA.js index 6476b3e..508cb95 100644 --- a/src/effects/EnergizeA.js +++ b/src/effects/EnergizeA.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import some modules only in the Shell process as they are not available in the @@ -24,6 +22,8 @@ import * as utils from '../utils.js'; const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); const Clutter = await utils.importInShellOnly('gi://Clutter'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This effect looks a bit like the transporter effect from TOS. // ////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/effects/EnergizeB.js b/src/effects/EnergizeB.js index d2649e9..32748eb 100644 --- a/src/effects/EnergizeB.js +++ b/src/effects/EnergizeB.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import some modules only in the Shell process as they are not available in the @@ -24,6 +22,8 @@ import * as utils from '../utils.js'; const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); const Clutter = await utils.importInShellOnly('gi://Clutter'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This effect looks a bit like the transporter effect from TNG. // ////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/effects/Fire.js b/src/effects/Fire.js index 34d1771..cd9cadf 100644 --- a/src/effects/Fire.js +++ b/src/effects/Fire.js @@ -16,8 +16,6 @@ import Gio from 'gi://Gio'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import some modules only in the Shell process as they are not available in the @@ -26,6 +24,8 @@ import * as utils from '../utils.js'; const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); const Clutter = await utils.importInShellOnly('gi://Clutter'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This effect is a homage to the good old Compiz days. However, it is implemented // // quite differently. While Compiz used a particle system, this effect uses a noise // diff --git a/src/effects/Glide.js b/src/effects/Glide.js index a567808..7aee968 100644 --- a/src/effects/Glide.js +++ b/src/effects/Glide.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import the ShaderFactory only in the Shell process as it is not required in the @@ -23,6 +21,8 @@ import * as utils from '../utils.js'; // only uses the static metadata of the effect. const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This very simple effect fades the window to transparency with subtle 3D effects. // ////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/effects/Glitch.js b/src/effects/Glitch.js index 8e675ea..659c350 100644 --- a/src/effects/Glitch.js +++ b/src/effects/Glitch.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import some modules only in the Shell process as they are not available in the @@ -24,6 +22,8 @@ import * as utils from '../utils.js'; const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); const Clutter = await utils.importInShellOnly('gi://Clutter'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This effect applies some intentional graphics issues to your windows. // ////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/effects/Hexagon.js b/src/effects/Hexagon.js index ac39669..20b6500 100644 --- a/src/effects/Hexagon.js +++ b/src/effects/Hexagon.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import some modules only in the Shell process as they are not available in the @@ -24,6 +22,8 @@ import * as utils from '../utils.js'; const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); const Clutter = await utils.importInShellOnly('gi://Clutter'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This effect overlays a glowing hexagonal grid over the window. The grid cells then // // gradually shrink until the window is fully dissolved. // diff --git a/src/effects/Incinerate.js b/src/effects/Incinerate.js index cb52e8c..0f5cd09 100644 --- a/src/effects/Incinerate.js +++ b/src/effects/Incinerate.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import some modules only in the Shell process as they are not available in the @@ -24,6 +22,8 @@ import * as utils from '../utils.js'; const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); const Clutter = await utils.importInShellOnly('gi://Clutter'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This effect burns windows to ashes. The fire starts at a random position at the // // window's boundary and then burns through the window in a circular shape. // diff --git a/src/effects/Matrix.js b/src/effects/Matrix.js index f8f2559..b87a6a9 100644 --- a/src/effects/Matrix.js +++ b/src/effects/Matrix.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import some modules only in the Shell process as they are not available in the @@ -26,6 +24,8 @@ const Clutter = await utils.importInShellOnly('gi://Clutter'); const GdkPixbuf = await utils.importInShellOnly('gi://GdkPixbuf'); const Cogl = await utils.importInShellOnly('gi://Cogl'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // The Matrix shader multiplies a grid of random letters with some gradients which are // // moving from top to bottom. The speed of the moving gradients is chosen randomly. // diff --git a/src/effects/PaintBrush.js b/src/effects/PaintBrush.js index 5d0d1ec..4a90729 100644 --- a/src/effects/PaintBrush.js +++ b/src/effects/PaintBrush.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import some modules only in the Shell process as they are not available in the @@ -26,6 +24,8 @@ const Clutter = await utils.importInShellOnly('gi://Clutter'); const GdkPixbuf = await utils.importInShellOnly('gi://GdkPixbuf'); const Cogl = await utils.importInShellOnly('gi://Cogl'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This effect paints your windows with a thick paint brush. // // This effect is not available on GNOME 3.3x, due to the limitation described in the // diff --git a/src/effects/PixelWheel.js b/src/effects/PixelWheel.js index 72352fa..af74c72 100644 --- a/src/effects/PixelWheel.js +++ b/src/effects/PixelWheel.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import the ShaderFactory only in the Shell process as it is not required in the @@ -23,6 +21,8 @@ import * as utils from '../utils.js'; // only uses the static metadata of the effect. const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This simple effect pixelates the window texture and hides the pixels in a wheel-like // // fashion. // diff --git a/src/effects/PixelWipe.js b/src/effects/PixelWipe.js index 057af94..9d33df8 100644 --- a/src/effects/PixelWipe.js +++ b/src/effects/PixelWipe.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import the ShaderFactory only in the Shell process as it is not required in the @@ -23,6 +21,8 @@ import * as utils from '../utils.js'; // only uses the static metadata of the effect. const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This effect pixelates the window texture and hides the pixels radially, starting // // from the pointer position. // diff --git a/src/effects/Pixelate.js b/src/effects/Pixelate.js index 406ad23..1730b59 100644 --- a/src/effects/Pixelate.js +++ b/src/effects/Pixelate.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import the ShaderFactory only in the Shell process as it is not required in the @@ -23,6 +21,8 @@ import * as utils from '../utils.js'; // only uses the static metadata of the effect. const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This very simple effect pixelates the window texture and randomly hides pixels until // // the entire window is gone. // diff --git a/src/effects/Portal.js b/src/effects/Portal.js index 48a8e58..0bd643c 100644 --- a/src/effects/Portal.js +++ b/src/effects/Portal.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import some modules only in the Shell process as they are not available in the @@ -24,6 +22,8 @@ import * as utils from '../utils.js'; const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); const Clutter = await utils.importInShellOnly('gi://Clutter'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This ridiculous effect teleports your windows from and to alternative dimensions. // // It may resemble the portal from a well-known cartoon series... // diff --git a/src/effects/SnapOfDisintegration.js b/src/effects/SnapOfDisintegration.js index f8f595d..e9b718b 100644 --- a/src/effects/SnapOfDisintegration.js +++ b/src/effects/SnapOfDisintegration.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import some modules only in the Shell process as they are not available in the @@ -26,6 +24,8 @@ const Clutter = await utils.importInShellOnly('gi://Clutter'); const GdkPixbuf = await utils.importInShellOnly('gi://GdkPixbuf'); const Cogl = await utils.importInShellOnly('gi://Cogl'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This effects dissolves your windows into a cloud of dust. For this, it uses an // // approach similar to the Broken Glass effect. A dust texture is used to segment the // diff --git a/src/effects/TRexAttack.js b/src/effects/TRexAttack.js index aa94a9f..c519fc0 100644 --- a/src/effects/TRexAttack.js +++ b/src/effects/TRexAttack.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import some modules only in the Shell process as they are not available in the @@ -26,6 +24,8 @@ const Clutter = await utils.importInShellOnly('gi://Clutter'); const GdkPixbuf = await utils.importInShellOnly('gi://GdkPixbuf'); const Cogl = await utils.importInShellOnly('gi://Cogl'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This effect tears your windows apart with a series of violent scratches! // // This effect is not available on GNOME 3.3x, due to the limitation described in the // diff --git a/src/effects/TVEffect.js b/src/effects/TVEffect.js index 5dd2eae..b1f6cb8 100644 --- a/src/effects/TVEffect.js +++ b/src/effects/TVEffect.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import some modules only in the Shell process as they are not available in the @@ -24,6 +22,8 @@ import * as utils from '../utils.js'; const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); const Clutter = await utils.importInShellOnly('gi://Clutter'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This effect hides the actor by making it first transparent from top and bottom // // towards the middle and then hiding the resulting line from left and right towards // diff --git a/src/effects/TVGlitch.js b/src/effects/TVGlitch.js index 7082d31..e44ad4f 100644 --- a/src/effects/TVGlitch.js +++ b/src/effects/TVGlitch.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import some modules only in the Shell process as they are not available in the @@ -24,6 +22,8 @@ import * as utils from '../utils.js'; const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); const Clutter = await utils.importInShellOnly('gi://Clutter'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This effect combines the Glitch and the TV Effect. Credits go to Kurt Wilson // // (https://github.com/Kurtoid) for this idea! // diff --git a/src/effects/Wisps.js b/src/effects/Wisps.js index 5eb466c..1bc0dd3 100644 --- a/src/effects/Wisps.js +++ b/src/effects/Wisps.js @@ -14,8 +14,6 @@ 'use strict'; -const _ = imports.gettext.domain('burn-my-windows').gettext; - import * as utils from '../utils.js'; // We import some modules only in the Shell process as they are not available in the @@ -24,6 +22,8 @@ import * as utils from '../utils.js'; const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); const Clutter = await utils.importInShellOnly('gi://Clutter'); +const _ = await utils.importGettext(); + ////////////////////////////////////////////////////////////////////////////////////////// // This effect lets your windows be carried to the realm of dreams by some little // // fairies. It's implemented with several overlaid grids of randomly moving points. // diff --git a/src/utils.js b/src/utils.js index b46df40..8a4731e 100644 --- a/src/utils.js +++ b/src/utils.js @@ -45,6 +45,26 @@ export async function importInShellOnly(module) { return null; } +// This method can be used to import a module in the preferences process only. This +// is useful if you want to use a module in the preferences process, but not in +// extension.js. This method returns null if it is called in the GNOME Shell process. +export async function importInPrefsOnly(module) { + if (typeof global === 'undefined') { + return (await import(module)); + } + return null; +} + +// This method can be used to import gettext. This is done differently in the +// GNOME Shell process and in the preferences process. +export async function importGettext() { + if (typeof global === 'undefined') { + return (await import('resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js')) + .gettext; + } + return (await import('resource:///org/gnome/shell/extensions/extension.js')).gettext; +} + // Reads the contents of a file contained in the global resources archive. The data // is returned as a string. export function getStringResource(path) {