51 lines
1.7 KiB
QML
51 lines
1.7 KiB
QML
pragma ComponentBehavior: Bound
|
|
import QtQuick
|
|
import Quickshell
|
|
import Quickshell.Io
|
|
|
|
QtObject {
|
|
id: root
|
|
property var devices: []
|
|
property bool busy: false
|
|
property string error: ""
|
|
property var command: ["python3", Quickshell.shellPath("backend/brightness.py")]
|
|
function refresh() {
|
|
if (busy) return false;
|
|
error = "";
|
|
busy = true;
|
|
worker.command = command.concat(["list"]);
|
|
worker.running = true;
|
|
return true;
|
|
}
|
|
function setBrightness(device, percent) {
|
|
if (busy || !/^[A-Za-z0-9_.:-]{1,128}$/.test(device) || !Number.isInteger(percent) || percent < 1 || percent > 100)
|
|
return false;
|
|
if (!devices.some(d => d.name === device)) return false;
|
|
error = "";
|
|
busy = true;
|
|
worker.command = command.concat(["set", device, String(percent)]);
|
|
worker.running = true;
|
|
return true;
|
|
}
|
|
property Process worker: Process {
|
|
stdout: StdioCollector { id: output }
|
|
stderr: StdioCollector { id: errors }
|
|
onExited: (code, status) => {
|
|
root.busy = false;
|
|
try {
|
|
const result = JSON.parse(output.text);
|
|
if (code !== 0 || !result.ok || !Array.isArray(result.devices))
|
|
throw new Error(result.error || errors.text || "Ошибка управления яркостью");
|
|
root.devices = result.devices;
|
|
} catch (e) { root.error = String(e); }
|
|
}
|
|
}
|
|
property Timer refreshTimer: Timer {
|
|
interval: 10000
|
|
repeat: true
|
|
running: true
|
|
triggeredOnStart: true
|
|
onTriggered: root.refresh()
|
|
}
|
|
}
|