pragma ComponentBehavior: Bound import QtQuick import Quickshell import Quickshell.Io // Everything the Luna surfaces know, read off luna-voiced's unix socket // (services/desktop/luna-voice.nix). // // Read-only on purpose: the key that toggles voice mode runs `luna-voice // toggle` from Hyprland rather than writing here, so the toggle keeps working // while the shell is reloading — and a QML error can never leave the mic on // with nothing showing it. // // The daemon is a USER service that may start after the shell, restart under // it, or not be installed at all, so a missing socket is a normal state and // not an error: `reconnect` retries quietly and `connected` is what the // widget shows. QtObject { id: root // "idle" | "listening" | "thinking" | "speaking" property string state: "idle" // Voice mode itself — the widget is only on screen while this is true. property bool active: false property bool connected: socket.connected // Mic level while listening, her output level while speaking. Drives the // only genuinely live part of the visuals. property real rms: 0 property string userText: "" property string lunaText: "" property string error: "" readonly property var socket: Socket { id: socket path: `${Quickshell.env("XDG_RUNTIME_DIR") ?? "/run/user/1000"}/luna-voiced.sock` connected: true onConnectionStateChanged: { if (!socket.connected) { // A daemon that went away is not a daemon that is idle: reset // rather than leave the last frame of a conversation frozen // on screen. root.active = false; root.state = "idle"; root.rms = 0; } } parser: SplitParser { splitMarker: "\n" onRead: line => root.ingest(line) } } // Retry forever, cheaply. Setting `connected` true on an already-open // socket is a no-op, so this doubles as the initial connect. readonly property var reconnect: Timer { interval: 2000 running: !socket.connected repeat: true onTriggered: socket.connected = true } function ingest(line: string): void { let msg; try { msg = JSON.parse(line); } catch (e) { return; // a half-written line is not worth a console error } switch (msg.type) { case "mode": root.active = msg.active; if (!msg.active) { root.state = "idle"; root.rms = 0; } break; case "state": root.state = msg.value; // Level is only meaningful during the phase that produces it. if (msg.value !== "listening" && msg.value !== "speaking") root.rms = 0; break; case "amplitude": root.rms = msg.rms; break; case "caption": if (msg.speaker === "user") { // A new user caption starts a turn, so it is what clears her // last reply — NOT speech_start, which arrives after her // first caption, not before it. root.userText = msg.text; root.lunaText = ""; } else { root.lunaText = msg.text; } break; case "error": root.error = msg.message; break; } } }