aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/island
diff options
context:
space:
mode:
Diffstat (limited to 'desktop/island')
-rw-r--r--desktop/island/Logic.js62
-rw-r--r--desktop/island/test_logic.js52
2 files changed, 114 insertions, 0 deletions
diff --git a/desktop/island/Logic.js b/desktop/island/Logic.js
new file mode 100644
index 0000000..8df35de
--- /dev/null
+++ b/desktop/island/Logic.js
@@ -0,0 +1,62 @@
+.pragma library
+
+// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as
+// published by the Free Software Foundation.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// The default sink's icon name. Thresholds in thirds, as waybar's wb-icon did.
+function volumeIcon(volume, muted) {
+ if (muted || volume <= 0) return "audio-volume-muted";
+ if (volume < 0.33) return "audio-volume-low";
+ if (volume < 0.66) return "audio-volume-medium";
+ return "audio-volume-high";
+}
+
+// A VM's dot colour as a Theme property name, or "" for no dot. States are
+// the names Virsh._stateName gives libvirt's enum: "blocked" is what virsh
+// list prints as idle, "suspended" is pmsuspended. A shut-off VM only gets a
+// dot when it carries a managed save, because starting it is then a restore.
+function vmRole(vm) {
+ if (!vm || !vm.state) return "";
+ if (vm.state === "shut off") return vm.saved ? "blue" : "";
+ return ({
+ "running": "green",
+ "paused": "yellow",
+ "blocked": "yellow",
+ "shutting down": "yellow",
+ "suspended": "accent",
+ "crashed": "red",
+ })[vm.state] || "";
+}
+
+// Flag image for a Hyprland active_keymap string, wb-lang's mapping.
+function flagFor(keymap) {
+ if (!keymap || keymap.startsWith("Italian")) return "flag-it.svg";
+ return "burger.svg";
+}
+
+// Active capture kinds as Theme property names, in a fixed order so the
+// sliver's segments do not shuffle.
+function privacyRoles(mic, cam, loc, scr) {
+ const out = [];
+ if (mic) out.push("green");
+ if (cam) out.push("yellow");
+ if (loc) out.push("blue");
+ if (scr) out.push("red");
+ return out;
+}
+
+// Hyprland's openwindow event data: "address,workspace,class,title". The
+// title is last and may contain commas, so only the first three fields are
+// read.
+function parseOpenWindow(data) {
+ const p = String(data || "").split(",");
+ return { address: p[0] || "", workspace: p[1] || "", appClass: p[2] || "" };
+}
diff --git a/desktop/island/test_logic.js b/desktop/island/test_logic.js
new file mode 100644
index 0000000..44e7a73
--- /dev/null
+++ b/desktop/island/test_logic.js
@@ -0,0 +1,52 @@
+// Run: node desktop/island/test_logic.js
+// Logic.js is a QML library, so its .pragma line is stripped before node
+// evaluates it.
+
+const fs = require("fs");
+const assert = require("assert");
+
+const src = fs.readFileSync(__dirname + "/Logic.js", "utf8")
+ .replace(/^\.pragma library$/m, "");
+const L = new Function(src +
+ "\nreturn { volumeIcon, vmRole, flagFor, privacyRoles, parseOpenWindow };")();
+
+// Volume: muted or zero is muted, then thirds.
+assert.strictEqual(L.volumeIcon(0.5, true), "audio-volume-muted");
+assert.strictEqual(L.volumeIcon(0, false), "audio-volume-muted");
+assert.strictEqual(L.volumeIcon(0.2, false), "audio-volume-low");
+assert.strictEqual(L.volumeIcon(0.5, false), "audio-volume-medium");
+assert.strictEqual(L.volumeIcon(0.66, false), "audio-volume-high");
+assert.strictEqual(L.volumeIcon(1.2, false), "audio-volume-high");
+
+// VM dots: Theme property names, "" for no dot.
+assert.strictEqual(L.vmRole({ state: "running" }), "green");
+assert.strictEqual(L.vmRole({ state: "paused" }), "yellow");
+assert.strictEqual(L.vmRole({ state: "blocked" }), "yellow");
+assert.strictEqual(L.vmRole({ state: "shutting down" }), "yellow");
+assert.strictEqual(L.vmRole({ state: "suspended" }), "accent");
+assert.strictEqual(L.vmRole({ state: "crashed" }), "red");
+assert.strictEqual(L.vmRole({ state: "shut off", saved: true }), "blue");
+assert.strictEqual(L.vmRole({ state: "shut off", saved: false }), "");
+assert.strictEqual(L.vmRole({ state: "unknown" }), "");
+assert.strictEqual(L.vmRole({}), "");
+
+// Flags: wb-lang's mapping, an unknown layout is the English one, an empty
+// read is Italian (the default keymap here).
+assert.strictEqual(L.flagFor("Italian"), "flag-it.svg");
+assert.strictEqual(L.flagFor("English (US)"), "burger.svg");
+assert.strictEqual(L.flagFor("German"), "burger.svg");
+assert.strictEqual(L.flagFor(""), "flag-it.svg");
+
+// Privacy: fixed order mic, cam, loc, scr.
+assert.deepStrictEqual(L.privacyRoles(false, false, false, false), []);
+assert.deepStrictEqual(L.privacyRoles(true, false, false, true), ["green", "red"]);
+assert.deepStrictEqual(L.privacyRoles(true, true, true, true),
+ ["green", "yellow", "blue", "red"]);
+
+// openwindow: "address,workspace,class,title"; the title may hold commas.
+assert.deepStrictEqual(L.parseOpenWindow("55d1a0,3,kitty,a, b, c"),
+ { address: "55d1a0", workspace: "3", appClass: "kitty" });
+assert.deepStrictEqual(L.parseOpenWindow(""),
+ { address: "", workspace: "", appClass: "" });
+
+console.log("ok");