repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

quickshell/nugu/Commands.qml

Raw
import QtQuick
import Quickshell.Io

Item {
    id: root
    visible: false

    // Keep all external process launches behind one object so widgets stay
    // declarative and future Hypr bind migration has one command boundary.
    property string sessionSlice: "session-graphical.slice"
    property string workSlice: "work.slice"
    property string lastClipboardPreview: ""
    property bool lastClipboardCopied: false
    property string lastPowerAction: ""
    property string lastOsdAction: ""
    property string nextOsdTone: "info"
    property string pendingOsdAction: ""
    property bool executeConfirmedPowerActions: false

    signal osdRequested(string message, string tone)

    function detached(args: var): void {
        runner.command = args
        runner.startDetached()
    }

    function shellOsd(command: string, fallback: string, tone: string): string {
        // Shell snippets mirror the current Hypr OSD helpers. They stay behind
        // this boundary so Hypr Lua can later shrink to stable Quickshell IPC.
        lastOsdAction = `pending ${fallback}`
        pendingOsdAction = fallback
        nextOsdTone = tone
        osdProcess.command = ["sh", "-c", `(${command}) >/dev/null && printf '%s' "${fallback}"`]
        osdProcess.running = true
        return fallback
    }

    function shellOsdOutput(command: string, tone: string): string {
        // Dynamic controls print their final status message; the collector turns
        // that into a Nugu OSD instead of `hyprctl notify`.
        lastOsdAction = "pending"
        pendingOsdAction = command
        nextOsdTone = tone
        osdProcess.command = ["sh", "-c", command]
        osdProcess.running = true
        return lastOsdAction
    }

    function fixedOsd(message: string, tone: string): string {
        lastOsdAction = message
        osdRequested(message, tone)
        return message
    }

    function uwsm(slice: string, args: var): var {
        return ["uwsm", "app", "-s", slice, "--"].concat(args)
    }

    function tui(slice: string, name: string): void {
        detached(uwsm(slice, ["wezterm", "start", "--class", name, "--", name]))
    }

    function openWifi(): void { tui(sessionSlice, "impala") }
    function openBluetooth(): void { tui(sessionSlice, "bluetui") }
    function openSystemMonitor(): void { tui(workSlice, "btm") }
    function openAudioMixer(): void { tui(sessionSlice, "wiremix") }

    function openBattery(): void {
        detached(uwsm(sessionSlice, [
            "wezterm", "start", "--class", "battery", "--",
            "nu", "-c", "loop { clear; upower -i /org/freedesktop/UPower/devices/battery_BAT0; sleep 2sec }"
        ]))
    }

    function restoreNotification(): void {
        detached(["makoctl", "restore"])
    }

    function dismissNotifications(): void {
        detached(["makoctl", "dismiss", "--all"])
    }

    function copyClipboardEntry(encoded: string, preview: string): void {
        lastClipboardPreview = preview
        clipboardCopy.command = ["sh", "-c", "printf '%s\\n' \"$1\" | cliphist decode | wl-copy", "sh", encoded]
        clipboardCopy.running = true
    }

    function lock(): void {
        lastPowerAction = "lock"
        detached(["hyprlock"])
    }

    function dryRunPower(action: string): void {
        // Phase 2C must not power off, reboot, logout, or suspend during tests.
        // Later phases can replace this with a confirmed execution path.
        lastPowerAction = `dry-run ${action}`
    }

    function confirmedPower(action: string): string {
        // Confirmation UI is live before destructive execution is allowed. The
        // execution switch stays false until full parity and user approval.
        if (!executeConfirmedPowerActions || action === "lock") {
            if (action === "lock") lock()
            else dryRunPower(action)
            return lastPowerAction
        }

        if (action === "sleep") detached(["sh", "-c", "loginctl lock-session && systemctl suspend"])
        else if (action === "logout") detached(["hyprctl", "dispatch", "exit"])
        else if (action === "reboot") detached(["systemctl", "reboot"])
        else if (action === "poweroff") detached(["systemctl", "poweroff"])
        else return "unknown"

        lastPowerAction = action
        return lastPowerAction
    }

    function volumeUp(): string {
        return shellOsdOutput("wpctl set-volume -l 1 @DEFAULT_AUDIO_SINK@ 2%+; level=$(wpctl get-volume @DEFAULT_AUDIO_SINK@ | sed 's/^Volume: //'); printf 'Volume %s' \"$level\"", "info")
    }

    function volumeDown(): string {
        return shellOsdOutput("wpctl set-volume @DEFAULT_AUDIO_SINK@ 2%-; level=$(wpctl get-volume @DEFAULT_AUDIO_SINK@ | sed 's/^Volume: //'); printf 'Volume %s' \"$level\"", "info")
    }

    function muteAudio(): string {
        return shellOsdOutput("wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle; state=$(wpctl get-volume @DEFAULT_AUDIO_SINK@ | grep -q '\\[MUTED\\]' && echo muted || echo live); printf 'Audio %s' \"$state\"", "info")
    }

    function muteMic(): string {
        return shellOsdOutput("wpctl set-mute @DEFAULT_AUDIO_SOURCE@ toggle; state=$(wpctl get-volume @DEFAULT_AUDIO_SOURCE@ | grep -q '\\[MUTED\\]' && echo muted || echo live); printf 'Mic %s' \"$state\"", "info")
    }

    function brightnessUp(): string {
        return shellOsdOutput("brightnessctl -e4 -n2 set 5%+ >/dev/null; level=$(brightnessctl -m | awk -F, '{print $4}'); printf 'Brightness %s' \"$level\"", "warning")
    }

    function brightnessDown(): string {
        return shellOsdOutput("brightnessctl -e4 -n2 set 5%- >/dev/null; level=$(brightnessctl -m | awk -F, '{print $4}'); printf 'Brightness %s' \"$level\"", "warning")
    }

    function mediaNext(): string { return shellOsd("playerctl next", "Next track", "info") }
    function mediaToggle(): string { return shellOsd("playerctl play-pause", "Play/pause", "info") }
    function mediaPrev(): string { return shellOsd("playerctl previous", "Previous track", "info") }
    function reloadHypr(): string { return shellOsd("hyprctl reload", "Hyprland reloaded", "info") }
    function sessionLockOsd(): string { fixedOsd("Locking", "info"); lock(); return "Locking" }
    function sessionSuspendDryRun(): string { dryRunPower("sleep"); return fixedOsd("Suspending", "info") }

    function captureRegion(): string { return shellOsd("uwsm app -s app-graphical.slice -- hyprshot -m region -z", "Region captured", "info") }
    function captureWindow(): string { return shellOsd("uwsm app -s app-graphical.slice -- hyprshot -m window -z", "Window captured", "info") }
    function captureOutput(): string { return shellOsd("uwsm app -s app-graphical.slice -- hyprshot -m output -z", "Output captured", "info") }
    function captureAnnotate(): string { return shellOsd("uwsm app -s app-graphical.slice -- hyprshot -m region -z -- satty --filename", "Screenshot annotated", "info") }
    function pickColor(): string { return shellOsd("hyprpicker -a", "Color copied", "info") }

    Process { id: runner }

    Process {
        id: osdProcess
        onExited: (exitCode, exitStatus) => {
            if (exitCode !== 0) root.lastOsdAction = `failed ${root.pendingOsdAction}`
        }
        stdout: StdioCollector {
            onStreamFinished: {
                const message = this.text.trim()
                if (message.length > 0) {
                    root.lastOsdAction = message
                    root.osdRequested(message, root.nextOsdTone)
                }
            }
        }
    }

    Process {
        id: clipboardCopy
        onExited: (exitCode, exitStatus) => { root.lastClipboardCopied = exitCode === 0 }
    }
}