.pragma library // Copyright (C) 2026 Danilo M. // // 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. // The cap only stops a step up: a level already above it (set elsewhere, // e.g. a mic at 120% from pavucontrol) is left alone by up and stepped from // by down, never snapped to the cap. function step(volume, delta, max) { const cur = volume > 0 ? volume : 0; const v = Math.round((cur + delta) * 100) / 100; if (delta < 0) return Math.max(0, v); return Math.max(cur, 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)); }