aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/island/Controls.js
blob: 0ca2f6cc0acd5612aa7f51e5c55a21d08b14f13b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
.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.
// 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));
}