From: Ubuntu Developers Date: Wed, 20 Jun 2018 19:22:06 +0200 Subject: Add an helper to detect current desktop We will differentiate some behavior depending on current desktop. Add an helper to centralize the current desktop detection. Forwarded: not-needed Origin: ubuntu --- js/js-resources.gresource.xml | 1 + js/misc/desktop.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 js/misc/desktop.js diff --git a/js/js-resources.gresource.xml b/js/js-resources.gresource.xml index ee4b763..a69c004 100644 --- a/js/js-resources.gresource.xml +++ b/js/js-resources.gresource.xml @@ -17,6 +17,7 @@ misc/animationUtils.js misc/config.js misc/extensionUtils.js + misc/desktop.js misc/fileUtils.js misc/dateUtils.js misc/dbusUtils.js diff --git a/js/misc/desktop.js b/js/misc/desktop.js new file mode 100644 index 0000000..05044c1 --- /dev/null +++ b/js/misc/desktop.js @@ -0,0 +1,33 @@ +// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*- + +import GLib from 'gi://GLib'; + +// current desktop doesn't change unless we restart the shell or control +// the env variable. It's safe to cache matching result +const _currentDesktopsMatches = new Map(); + +// is: +// @name: desktop string you want to assert if it matches the current desktop env +// +// The function examples XDG_CURRENT_DESKTOP and return if the current desktop +// is part of that desktop string. +// +// Return value: if the environment isn't set or doesn't match, return False +// otherwise, return True. +export function is(name) { + if (!_currentDesktopsMatches.size) { + const desktopsEnv = GLib.getenv('XDG_CURRENT_DESKTOP'); + if (!desktopsEnv) { + _currentDesktopsMatches.set(name, false); + return false; + } + + const desktops = desktopsEnv.split(':'); + desktops.forEach(d => _currentDesktopsMatches.set(d, true)); + + if (!_currentDesktopsMatches.size) + _currentDesktopsMatches.set(name, _currentDesktopsMatches.has(name)); + } + + return !!_currentDesktopsMatches.get(name); +}