61 lines
2.3 KiB
QML
61 lines
2.3 KiB
QML
pragma Singleton
|
|
import QtQuick
|
|
import Quickshell
|
|
import Quickshell.Io
|
|
import "Policy.js" as Policy
|
|
|
|
Singleton {
|
|
id: root
|
|
property var data: ({version: 1, mode: "auto", theme: "dark", reducedMotion: false, touch: false, outputs: {}})
|
|
property string error: ""
|
|
property bool writable: false
|
|
property bool initialized: false
|
|
readonly property string path: Quickshell.statePath("preferences.json")
|
|
function modeFor(screen) {
|
|
return Policy.modeFor(data.outputs[screen.name] || data.mode, screen.width, data.touch);
|
|
}
|
|
function setValue(key, value) {
|
|
if (!writable) return;
|
|
try {
|
|
const next = Object.assign({}, data);
|
|
next[key] = value;
|
|
Policy.validate(next);
|
|
data = next;
|
|
file.setText(JSON.stringify(next, null, 2));
|
|
} catch (e) { error = String(e); }
|
|
}
|
|
function recordLaunch(id) { setValue("appHistory", Policy.recordLaunch(data.appHistory, id)); }
|
|
function toggleFavorite(id) { setValue("favorites", Policy.toggleFavorite(data.favorites, id)); }
|
|
function moveFavorite(id, direction) { setValue("favorites", Policy.moveFavorite(data.favorites, id, direction)); }
|
|
function setOutput(name, mode) {
|
|
const outputs = Object.assign({}, data.outputs);
|
|
outputs[name] = mode;
|
|
setValue("outputs", outputs);
|
|
}
|
|
FileView {
|
|
id: file
|
|
path: root.path
|
|
watchChanges: true
|
|
atomicWrites: true
|
|
printErrors: false
|
|
onFileChanged: reload()
|
|
onLoaded: {
|
|
try {
|
|
root.data = Policy.validate(JSON.parse(text()));
|
|
root.writable = true;
|
|
root.error = "";
|
|
} catch (e) {
|
|
root.writable = false;
|
|
root.error = "Настройки не загружены: " + String(e) + ". Исходный файл сохранён: " + root.path;
|
|
}
|
|
root.initialized = true;
|
|
}
|
|
onLoadFailed: error => {
|
|
if (error === FileViewError.FileNotFound) root.writable = true;
|
|
else root.error = "Не удалось прочитать " + root.path;
|
|
root.initialized = true;
|
|
}
|
|
onSaveFailed: root.error = "Не удалось сохранить " + root.path
|
|
}
|
|
}
|