diff options
Diffstat (limited to 'desktop/island/Controls.js')
| -rw-r--r-- | desktop/island/Controls.js | 102 |
1 files changed, 102 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)); +} |
