[gnome-shell/T29763: 1/249] Add an helper to detect current desktop



commit 47f3d97d59e6beb4e8b9a6435866e3056436a78e
Author: Ubuntu Developers <ubuntu-devel-discuss lists ubuntu com>
Date:   Wed Jun 20 19:22:06 2018 +0200

    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            | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
---
diff --git a/js/js-resources.gresource.xml b/js/js-resources.gresource.xml
index aec3427e0b..302fdfb42e 100644
--- a/js/js-resources.gresource.xml
+++ b/js/js-resources.gresource.xml
@@ -11,6 +11,7 @@
 
     <file>misc/config.js</file>
     <file>misc/extensionUtils.js</file>
+    <file>misc/desktop.js</file>
     <file>misc/fileUtils.js</file>
     <file>misc/gnomeSession.js</file>
     <file>misc/history.js</file>
diff --git a/js/misc/desktop.js b/js/misc/desktop.js
new file mode 100644
index 0000000000..3b0d12857c
--- /dev/null
+++ b/js/misc/desktop.js
@@ -0,0 +1,39 @@
+// -*- 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
+let _currentDesktopsMatches = {};
+
+// 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[name] !== undefined) {
+        return _currentDesktopsMatches[name];
+    }
+
+    let desktopsEnv = GLib.getenv('XDG_CURRENT_DESKTOP');
+    if (!desktopsEnv) {
+        _currentDesktopsMatches[name] = false;
+        return false;
+    }
+
+    let desktops = desktopsEnv.split(":");
+    for (let i = 0; i < desktops.length; i++) {
+        if (desktops[i] === name) {
+            _currentDesktopsMatches[name] = true;
+            return true;
+        }
+    }
+
+    _currentDesktopsMatches[name] = false;
+    return false;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]