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 76b5f95..5babd0f 100644 --- a/js/js-resources.gresource.xml +++ b/js/js-resources.gresource.xml @@ -13,6 +13,7 @@ misc/config.js misc/extensionUtils.js + misc/desktop.js misc/fileUtils.js misc/dbusUtils.js misc/gnomeSession.js diff --git a/js/misc/desktop.js b/js/misc/desktop.js new file mode 100644 index 0000000..8bdca57 --- /dev/null +++ b/js/misc/desktop.js @@ -0,0 +1,33 @@ +// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*- + +const GLib = imports.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. +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); +}