aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/Prefs.qml
blob: 610e004de3fe9ad7906c5b5a68f32a4f11b2fe79 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// 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.

pragma Singleton

import Quickshell
import Quickshell.Io
import QtQuick

// Preferences that must survive a reboot, in one JSON file under the state
// home. Only the shell writes it, which is why it is not in ~/.config.
//
// Nothing is written until the user changes something. A file that fails to
// parse reads as the defaults and stays on disk as it is, so a hand edit
// with a typo is not replaced by a panel that was merely opened.
Singleton {
    id: root

    readonly property string path:
        (Quickshell.env("XDG_STATE_HOME") || Quickshell.env("HOME") + "/.local/state")
        + "/keystone/desktop.json"

    // The public IP the user pinned from the performance panel; "" for none.
    property string pinnedIp: ""

    // The assistant's Ask presets as saved, or null for the defaults
    // (assistant/Presets.js, which also validates what is read here).
    property var presets: null

    // The router model preferred for the assistant, "" for none. The daemon
    // still uses whatever is loaded; the settings page loads this on demand.
    property string assistantModel: ""
    function setAssistantModel(id) {
        if (id === root.assistantModel) return;
        root.assistantModel = id;
        const a = Object.assign({}, root.doc.assistant, { model: id });
        root.write(Object.assign({}, root.doc, { assistant: a }));
    }

    // Whether the desktop companion is shown: SUPER+\ and the Companion
    // settings page both set it, so it survives a restart. ready is false
    // until the file has been read, so she does not show and then vanish.
    property bool companion: true
    property bool ready: false
    function setCompanion(on) {
        if (on === root.companion) return;
        root.companion = on;
        root.write(Object.assign({}, root.doc, { companion: on }));
    }

    // Seconds the companion stays in her leotard after the self-destruct,
    // 0 for until she is hidden and shown again.
    property int companionSkirtless: 60
    function setCompanionSkirtless(s) {
        if (s === root.companionSkirtless) return;
        root.companionSkirtless = s;
        root.write(Object.assign({}, root.doc, { companionSkirtless: s }));
    }

    // The companion's wallpaper list, "" for the default in
    // companion/Admired.qml.
    property string companionBlushFile: ""
    function setCompanionBlushFile(p) {
        p = p.trim();
        if (p === root.companionBlushFile) return;
        root.companionBlushFile = p;
        root.write(Object.assign({}, root.doc, { companionBlushFile: p }));
    }

    // The companion's app lines as saved: a map of lowercased window class
    // to phrases, or null for the defaults in companion/Lines.js. The
    // Companion phrases page edits it.
    property var companionApps: null
    function setCompanionApps(map) {
        root.companionApps = map;
        root.write(Object.assign({}, root.doc, { companionApps: map }));
    }
    function resetCompanionApps() {
        root.companionApps = null;
        const d = Object.assign({}, root.doc);
        delete d.companionApps;
        root.write(d);
    }

    // The companion's artist lines as saved, keyed by lowercased artist, or
    // null for the defaults in companion/Lines.js. The Companion artists
    // page edits it.
    property var companionArtists: null
    function setCompanionArtists(map) {
        root.companionArtists = map;
        root.write(Object.assign({}, root.doc, { companionArtists: map }));
    }
    function resetCompanionArtists() {
        root.companionArtists = null;
        const d = Object.assign({}, root.doc);
        delete d.companionArtists;
        root.write(d);
    }

    // The settings window's sidebar width, dragged by its edge.
    readonly property int sidebarMin: 160
    readonly property int sidebarMax: 420
    property int settingsSidebar: 200

    // The whole parsed object, so a write keeps keys this version does not
    // know about.
    property var doc: ({})

    function setPinnedIp(ip) {
        if (ip === root.pinnedIp) return;
        root.pinnedIp = ip;
        const perf = Object.assign({}, root.doc.perf, { pinnedIp: ip });
        root.write(Object.assign({}, root.doc, { perf: perf }));
    }

    // For the settings window's Ask presets page.
    function setPresets(list) {
        root.presets = list;
        const a = Object.assign({}, root.doc.assistant, { presets: list });
        root.write(Object.assign({}, root.doc, { assistant: a }));
    }
    function resetPresets() {
        root.presets = null;
        const a = Object.assign({}, root.doc.assistant);
        delete a.presets;
        root.write(Object.assign({}, root.doc, { assistant: a }));
    }

    function setSettingsSidebar(w) {
        w = Math.round(Math.max(root.sidebarMin, Math.min(root.sidebarMax, w)));
        if (w === root.settingsSidebar) return;
        root.settingsSidebar = w;
        const s = Object.assign({}, root.doc.settings, { sidebar: w });
        root.write(Object.assign({}, root.doc, { settings: s }));
    }

    function write(out) {
        root.doc = out;
        file.setText(JSON.stringify(out, null, 2) + "\n");
    }

    function reparse() {
        let parsed = {};
        try {
            const v = JSON.parse(file.text());
            if (v && typeof v === "object" && !Array.isArray(v)) parsed = v;
        } catch (e) {}
        root.doc = parsed;
        const pin = parsed.perf && typeof parsed.perf.pinnedIp === "string" ? parsed.perf.pinnedIp : "";
        if (pin !== root.pinnedIp) root.pinnedIp = pin;
        root.presets = parsed.assistant && parsed.assistant.presets !== undefined
            ? parsed.assistant.presets : null;
        root.assistantModel = parsed.assistant && typeof parsed.assistant.model === "string"
            ? parsed.assistant.model : "";
        const w = parsed.settings ? parsed.settings.sidebar : undefined;
        root.settingsSidebar = typeof w === "number" && w >= root.sidebarMin && w <= root.sidebarMax
            ? Math.round(w) : 200;
        root.companion = parsed.companion !== false;
        const sk = parsed.companionSkirtless;
        root.companionSkirtless = Number.isInteger(sk) && sk >= 0 ? sk : 60;
        root.companionBlushFile = typeof parsed.companionBlushFile === "string" ? parsed.companionBlushFile : "";
        const ca = parsed.companionApps;
        root.companionApps = ca && typeof ca === "object" && !Array.isArray(ca) ? ca : null;
        const cr = parsed.companionArtists;
        root.companionArtists = cr && typeof cr === "object" && !Array.isArray(cr) ? cr : null;
        root.ready = true;
    }

    FileView {
        id: file
        path: root.path
        atomicWrites: true
        watchChanges: true
        printErrors: false
        // printErrors is off for the missing-file read; a failed write still
        // has to be heard, or the pin silently vanishes at restart.
        onSaveFailed: error => console.warn("Prefs: writing", root.path, "failed:", error)
        onFileChanged: reload()
        onLoaded: root.reparse()
        // Missing file: the defaults, and nothing written.
        onLoadFailed: { root.doc = {}; root.pinnedIp = ""; root.presets = null; root.settingsSidebar = 200; root.assistantModel = ""; root.companion = true; root.companionBlushFile = ""; root.companionSkirtless = 60; root.companionApps = null; root.companionArtists = null; root.ready = true; }
    }
}