aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/island
diff options
context:
space:
mode:
Diffstat (limited to 'desktop/island')
-rw-r--r--desktop/island/Controls.js102
-rw-r--r--desktop/island/test_controls.js95
2 files changed, 197 insertions, 0 deletions
diff --git a/desktop/island/Controls.js b/desktop/island/Controls.js
new file mode 100644
index 0000000..39865b2
--- /dev/null
+++ b/desktop/island/Controls.js
@@ -0,0 +1,102 @@
+.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 controls panel's tiles and the pure operations on their order. The
+// order itself lives in Prefs.qml; everything here takes a list and returns
+// a new one, so it runs under node (test_controls.js).
+
+// Table order is the default order and the order of the hidden strip.
+const TILES = {
+ wifi: { label: "Wi-Fi", page: "network" },
+ bluetooth: { label: "Bluetooth", page: "bluetooth" },
+ dnd: { label: "DND", page: "status" },
+ presentation: { label: "Presentation", page: "status" },
+ nolock: { label: "Auto-lock", page: "status" },
+ output: { label: "Output", page: "sound" },
+ mic: { label: "Mic", page: "sound" },
+};
+
+function defaults() {
+ return Object.keys(TILES);
+}
+
+function tile(id) {
+ return TILES[id] || null;
+}
+
+// Icon name for a tile's state. `on` is the tile's lit state: for the mic
+// that is "not muted". Output is "" because Logic.volumeIcon owns it.
+function icon(id, on) {
+ switch (id) {
+ case "wifi": return on ? "network-wireless" : "network-wireless-disconnected";
+ case "bluetooth": return "preferences-system-bluetooth";
+ case "dnd": return on ? "notifications-disabled" : "notifications";
+ case "presentation": return "video-display";
+ case "nolock": return "system-lock-screen";
+ case "mic": return on ? "audio-input-microphone" : "microphone-sensitivity-muted";
+ default: return "";
+ }
+}
+
+// A stored list made safe: unknown ids (a tile removed from the code) and
+// duplicates dropped. An empty list is a valid choice; a non-list is not.
+function normalize(list) {
+ if (!Array.isArray(list)) return defaults();
+ const out = [];
+ for (let i = 0; i < list.length; i++) {
+ const id = list[i];
+ if (TILES[id] && out.indexOf(id) < 0) out.push(id);
+ }
+ return out;
+}
+
+function hidden(list) {
+ return defaults().filter(id => list.indexOf(id) < 0);
+}
+
+function move(list, from, to) {
+ const out = list.slice();
+ const id = out.splice(from, 1)[0];
+ out.splice(to, 0, id);
+ return out;
+}
+
+function hide(list, id) {
+ return list.filter(x => x !== id);
+}
+
+function show(list, id) {
+ if (!TILES[id] || list.indexOf(id) >= 0) return list.slice();
+ return list.concat([id]);
+}
+
+// Audio tile bar width as a fraction. Output goes to 1.5, the bar stops at
+// 1. !(> 0) also catches a volume not populated yet.
+function fill(volume) {
+ if (!(volume > 0)) return 0;
+ return Math.min(1, volume);
+}
+
+// One scroll step, rounded to the percent so repeated steps do not drift.
+function step(volume, delta, max) {
+ const v = Math.round(((volume > 0 ? volume : 0) + delta) * 100) / 100;
+ return Math.max(0, Math.min(max, v));
+}
+
+// The slot a dragged tile lands in, from its centre in grid coordinates.
+// Off the grid clamps to the nearest edge; past the last tile is the last.
+function slotAt(x, y, pitch, cols, count) {
+ const col = Math.max(0, Math.min(cols - 1, Math.floor(x / pitch)));
+ const row = Math.max(0, Math.floor(y / pitch));
+ return Math.max(0, Math.min(count - 1, row * cols + col));
+}
diff --git a/desktop/island/test_controls.js b/desktop/island/test_controls.js
new file mode 100644
index 0000000..f84e472
--- /dev/null
+++ b/desktop/island/test_controls.js
@@ -0,0 +1,95 @@
+// 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.
+
+// Run: node desktop/island/test_controls.js
+// Controls.js is a QML library, so its .pragma line is stripped before node
+// evaluates it, as test_logic.js does for Logic.js.
+
+const fs = require("fs");
+const assert = require("assert");
+
+const src = fs.readFileSync(__dirname + "/Controls.js", "utf8")
+ .replace(/^\.pragma library$/m, "");
+const C = new Function(src +
+ "\nreturn { defaults, tile, icon, normalize, hidden, move, hide, show, fill, step, slotAt };")();
+
+const ALL = ["wifi", "bluetooth", "dnd", "presentation", "nolock", "output", "mic"];
+
+// Defaults: all seven, a fresh copy each time.
+assert.deepStrictEqual(C.defaults(), ALL);
+const d = C.defaults(); d.pop();
+assert.deepStrictEqual(C.defaults(), ALL);
+
+// Table: label and drawer page; unknown is null.
+assert.strictEqual(C.tile("wifi").page, "network");
+assert.strictEqual(C.tile("nolock").label, "Auto-lock");
+assert.strictEqual(C.tile("mic").page, "sound");
+assert.strictEqual(C.tile("nope"), null);
+
+// Icons by state. Output is Logic.volumeIcon's job, so "" here.
+assert.strictEqual(C.icon("wifi", true), "network-wireless");
+assert.strictEqual(C.icon("wifi", false), "network-wireless-disconnected");
+assert.strictEqual(C.icon("dnd", true), "notifications-disabled");
+assert.strictEqual(C.icon("dnd", false), "notifications");
+assert.strictEqual(C.icon("mic", true), "audio-input-microphone");
+assert.strictEqual(C.icon("mic", false), "microphone-sensitivity-muted");
+assert.strictEqual(C.icon("bluetooth", false), "preferences-system-bluetooth");
+assert.strictEqual(C.icon("output", true), "");
+
+// normalize: drops unknown ids and duplicates, keeps order; an empty list
+// is valid (everything hidden); a non-array is the default.
+assert.deepStrictEqual(C.normalize(["mic", "gone", "wifi", "mic"]), ["mic", "wifi"]);
+assert.deepStrictEqual(C.normalize([]), []);
+assert.deepStrictEqual(C.normalize("wifi"), ALL);
+assert.deepStrictEqual(C.normalize(undefined), ALL);
+
+// hidden: known ids not in the list, in table order.
+assert.deepStrictEqual(C.hidden(["wifi", "mic"]), ["bluetooth", "dnd", "presentation", "nolock", "output"]);
+assert.deepStrictEqual(C.hidden(ALL), []);
+
+// move: from index to index, input untouched.
+const src3 = ["a", "b", "c"];
+assert.deepStrictEqual(C.move(src3, 0, 2), ["b", "c", "a"]);
+assert.deepStrictEqual(C.move(src3, 2, 0), ["c", "a", "b"]);
+assert.deepStrictEqual(C.move(src3, 1, 1), ["a", "b", "c"]);
+assert.deepStrictEqual(src3, ["a", "b", "c"]);
+
+// hide / show.
+assert.deepStrictEqual(C.hide(["wifi", "mic"], "wifi"), ["mic"]);
+assert.deepStrictEqual(C.show(["mic"], "wifi"), ["mic", "wifi"]);
+assert.deepStrictEqual(C.show(["mic"], "mic"), ["mic"]);
+assert.deepStrictEqual(C.show(["mic"], "nope"), ["mic"]);
+
+// fill: volume clamped to 0..1; unpopulated reads as empty, not full.
+assert.strictEqual(C.fill(0.4), 0.4);
+assert.strictEqual(C.fill(1.5), 1);
+assert.strictEqual(C.fill(-1), 0);
+assert.strictEqual(C.fill(undefined), 0);
+assert.strictEqual(C.fill(NaN), 0);
+
+// step: 5% steps, rounded to the percent, clamped to 0..max.
+assert.strictEqual(C.step(0.5, 0.05, 1.5), 0.55);
+assert.strictEqual(C.step(0.02, -0.05, 1.5), 0);
+assert.strictEqual(C.step(1.48, 0.05, 1.5), 1.5);
+assert.strictEqual(C.step(0.98, 0.05, 1.0), 1);
+assert.strictEqual(C.step(0.1 + 0.2, 0.05, 1), 0.35);
+assert.strictEqual(C.step(undefined, 0.05, 1), 0.05);
+
+// slotAt: pitch 70, 4 columns, 7 tiles.
+assert.strictEqual(C.slotAt(35, 35, 70, 4, 7), 0);
+assert.strictEqual(C.slotAt(175, 35, 70, 4, 7), 2);
+assert.strictEqual(C.slotAt(105, 105, 70, 4, 7), 5);
+assert.strictEqual(C.slotAt(-50, -50, 70, 4, 7), 0); // off the top left
+assert.strictEqual(C.slotAt(500, 35, 70, 4, 7), 3); // past the right edge
+assert.strictEqual(C.slotAt(275, 105, 70, 4, 7), 6); // empty 8th slot -> last
+assert.strictEqual(C.slotAt(35, 900, 70, 4, 7), 6); // below the grid -> last
+
+console.log("test_controls: ok");