mutter/patches/ubuntu/desktop_detect.patch
Ward Nakchbandi (Cosmic Fusion) deaf92469b
2023-10-03 21:58:03 +03:00

66 lines
2.2 KiB
Diff

From: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
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 @@
<file>misc/animationUtils.js</file>
<file>misc/config.js</file>
<file>misc/extensionUtils.js</file>
+ <file>misc/desktop.js</file>
<file>misc/fileUtils.js</file>
<file>misc/dateUtils.js</file>
<file>misc/dbusUtils.js</file>
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);
+}