From 767035e3131c1230a401407a41feffffefd21918 Mon Sep 17 00:00:00 2001 From: Simon Schneegans Date: Sat, 9 Sep 2023 06:05:15 +0200 Subject: [PATCH] :recycle: Use utility method to import module in Shell process only --- docs/how-to-create-new-effects.md | 6 +++--- src/effects/Apparition.js | 5 +++-- src/effects/BrokenGlass.js | 5 +++-- src/effects/Doom.js | 5 +++-- src/effects/EnergizeA.js | 5 +++-- src/effects/EnergizeB.js | 5 +++-- src/effects/Fire.js | 5 +++-- src/effects/Glide.js | 5 +++-- src/effects/Glitch.js | 5 +++-- src/effects/Hexagon.js | 5 +++-- src/effects/Incinerate.js | 5 +++-- src/effects/Matrix.js | 5 +++-- src/effects/PaintBrush.js | 5 +++-- src/effects/PixelWheel.js | 5 +++-- src/effects/PixelWipe.js | 5 +++-- src/effects/Pixelate.js | 5 +++-- src/effects/Portal.js | 5 +++-- src/effects/SnapOfDisintegration.js | 5 +++-- src/effects/TRexAttack.js | 5 +++-- src/effects/TVEffect.js | 5 +++-- src/effects/TVGlitch.js | 5 +++-- src/effects/Wisps.js | 5 +++-- src/utils.js | 10 ++++++++++ 23 files changed, 76 insertions(+), 45 deletions(-) diff --git a/docs/how-to-create-new-effects.md b/docs/how-to-create-new-effects.md index db50bdb..035e4cb 100644 --- a/docs/how-to-create-new-effects.md +++ b/docs/how-to-create-new-effects.md @@ -165,11 +165,11 @@ void main() { "use strict"; -const GObject = imports.gi.GObject; - const _ = imports.gettext.domain("burn-my-windows").gettext; -import { ShaderFactory } from "../ShaderFactory.js"; +import * as utils from "../utils.js"; + +const ShaderFactory = await utils.importInShellOnly("./ShaderFactory.js"); ////////////////////////////////////////////////////////////////////////////////////////// // This effect ... // diff --git a/src/effects/Apparition.js b/src/effects/Apparition.js index 07f830a..9d5ea21 100644 --- a/src/effects/Apparition.js +++ b/src/effects/Apparition.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // 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 5cc0f95..7b2928c 100644 --- a/src/effects/BrokenGlass.js +++ b/src/effects/BrokenGlass.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // This effect shatters the window into pieces. For an explanation how this works, look // diff --git a/src/effects/Doom.js b/src/effects/Doom.js index be02ff6..2847cc7 100644 --- a/src/effects/Doom.js +++ b/src/effects/Doom.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // This effect melts your windows. Inspired by the legendary screen transitions of the // diff --git a/src/effects/EnergizeA.js b/src/effects/EnergizeA.js index 2ee4535..3d91a01 100644 --- a/src/effects/EnergizeA.js +++ b/src/effects/EnergizeA.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // This effect looks a bit like the transporter effect from TOS. // diff --git a/src/effects/EnergizeB.js b/src/effects/EnergizeB.js index 1d2be49..6a2354d 100644 --- a/src/effects/EnergizeB.js +++ b/src/effects/EnergizeB.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // This effect looks a bit like the transporter effect from TNG. // diff --git a/src/effects/Fire.js b/src/effects/Fire.js index 10a2b54..b255ff0 100644 --- a/src/effects/Fire.js +++ b/src/effects/Fire.js @@ -18,8 +18,9 @@ import Gio from 'gi://Gio'; const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // This effect is a homage to the good old Compiz days. However, it is implemented // diff --git a/src/effects/Glide.js b/src/effects/Glide.js index 2edf1d8..2c0c606 100644 --- a/src/effects/Glide.js +++ b/src/effects/Glide.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // 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 767cb23..3483b51 100644 --- a/src/effects/Glitch.js +++ b/src/effects/Glitch.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // This effect applies some intentional graphics issues to your windows. // diff --git a/src/effects/Hexagon.js b/src/effects/Hexagon.js index 9747897..9cc3c2b 100644 --- a/src/effects/Hexagon.js +++ b/src/effects/Hexagon.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // This effect overlays a glowing hexagonal grid over the window. The grid cells then // diff --git a/src/effects/Incinerate.js b/src/effects/Incinerate.js index 9838db7..8fbffeb 100644 --- a/src/effects/Incinerate.js +++ b/src/effects/Incinerate.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // This effect burns windows to ashes. The fire starts at a random position at the // diff --git a/src/effects/Matrix.js b/src/effects/Matrix.js index fbfd153..9a683e8 100644 --- a/src/effects/Matrix.js +++ b/src/effects/Matrix.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // The Matrix shader multiplies a grid of random letters with some gradients which are // diff --git a/src/effects/PaintBrush.js b/src/effects/PaintBrush.js index 1f6ab7a..59a6e49 100644 --- a/src/effects/PaintBrush.js +++ b/src/effects/PaintBrush.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // This effect paints your windows with a thick paint brush. // diff --git a/src/effects/PixelWheel.js b/src/effects/PixelWheel.js index 4983e4c..06d0b05 100644 --- a/src/effects/PixelWheel.js +++ b/src/effects/PixelWheel.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // This simple effect pixelates the window texture and hides the pixels in a wheel-like // diff --git a/src/effects/PixelWipe.js b/src/effects/PixelWipe.js index 35cdf9c..50077e2 100644 --- a/src/effects/PixelWipe.js +++ b/src/effects/PixelWipe.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // This effect pixelates the window texture and hides the pixels radially, starting // diff --git a/src/effects/Pixelate.js b/src/effects/Pixelate.js index 0cc0c31..dee05c2 100644 --- a/src/effects/Pixelate.js +++ b/src/effects/Pixelate.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // This very simple effect pixelates the window texture and randomly hides pixels until // diff --git a/src/effects/Portal.js b/src/effects/Portal.js index e22cbce..5e9d3ce 100644 --- a/src/effects/Portal.js +++ b/src/effects/Portal.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // This ridiculous effect teleports your windows from and to alternative dimensions. // diff --git a/src/effects/SnapOfDisintegration.js b/src/effects/SnapOfDisintegration.js index 5af84c1..6e7002f 100644 --- a/src/effects/SnapOfDisintegration.js +++ b/src/effects/SnapOfDisintegration.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // This effects dissolves your windows into a cloud of dust. For this, it uses an // diff --git a/src/effects/TRexAttack.js b/src/effects/TRexAttack.js index 44ae5a1..1e8cfd0 100644 --- a/src/effects/TRexAttack.js +++ b/src/effects/TRexAttack.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // This effect tears your windows apart with a series of violent scratches! // diff --git a/src/effects/TVEffect.js b/src/effects/TVEffect.js index 846b900..713ea15 100644 --- a/src/effects/TVEffect.js +++ b/src/effects/TVEffect.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // This effect hides the actor by making it first transparent from top and bottom // diff --git a/src/effects/TVGlitch.js b/src/effects/TVGlitch.js index 9c7345b..083be22 100644 --- a/src/effects/TVGlitch.js +++ b/src/effects/TVGlitch.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // This effect combines the Glitch and the TV Effect. Credits go to Kurt Wilson // diff --git a/src/effects/Wisps.js b/src/effects/Wisps.js index e59c083..478d487 100644 --- a/src/effects/Wisps.js +++ b/src/effects/Wisps.js @@ -16,8 +16,9 @@ const _ = imports.gettext.domain('burn-my-windows').gettext; -const ShaderFactory = - typeof global !== 'undefined' ? (await import('../ShaderFactory.js'))?.default : null; +import * as utils from '../utils.js'; + +const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js'); ////////////////////////////////////////////////////////////////////////////////////////// // This effect lets your windows be carried to the realm of dreams by some little // diff --git a/src/utils.js b/src/utils.js index 4018c26..b46df40 100644 --- a/src/utils.js +++ b/src/utils.js @@ -35,6 +35,16 @@ export function debug(message) { console.log('[' + stack[0].slice(extensionRoot) + '] ' + message); } +// This method can be used to import a module in the GNOME Shell process only. This +// is useful if you want to use a module in extension.js, but not in the preferences +// process. This method returns null if it is called in the preferences process. +export async function importInShellOnly(module) { + if (typeof global !== 'undefined') { + return (await import(module)).default; + } + return null; +} + // Reads the contents of a file contained in the global resources archive. The data // is returned as a string. export function getStringResource(path) {