Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/features/config/Config.qml

Raw
import QtQuick
import Quickshell
import Quickshell.Io
import "../.."

Item {
    id: root
    required property Theme theme
    property list<FeatureEntry> entries: []
    readonly property list<ConfigSection> sections: entries.filter(entry => entry.configuration !== null).map(entry => entry.configuration)
    property string selectedSection: "appearance"
    readonly property ConfigSection section: sections.find(candidate => candidate.name === selectedSection) || null
    property bool popupOpen: false
    readonly property string mode: theme.mode
    property bool busy: false
    property string error: ""
    // Explicit nonsecret preference, never a generic declaration/draft store.
    property string configPath: Quickshell.statePath("config.json")
    readonly property string orientation: orientationState.value
    property string orientationError: ""
    signal openRequested

    function setOrientation(value: string): string {
        let next = value;
        if (next === "toggle")
            next = orientation === "vertical" ? "horizontal" : "vertical";
        if (next !== "horizontal" && next !== "vertical")
            return orientation;
        orientationState.value = next;
        orientationError = "";
        orientationFile.setText(JSON.stringify({
            "orientation": next
        }) + "\n");
        return orientation;
    }

    function loadOrientation(): void {
        try {
            const data = JSON.parse(orientationFile.text());
            if (!data || (data.orientation !== "horizontal" && data.orientation !== "vertical"))
                throw new Error("invalid orientation");
            orientationState.value = data.orientation;
            orientationError = "";
        } catch (failure) {
            orientationError = "orientation could not be read; select again";
        }
    }

    QtObject {
        id: orientationState
        property string value: "horizontal"
    }
    FileView {
        id: orientationFile
        path: root.configPath
        blockLoading: true
        blockWrites: true
        atomicWrites: true
        watchChanges: true
        printErrors: false
        onLoaded: root.loadOrientation()
        onFileChanged: reload()
        onSaveFailed: root.orientationError = "orientation not saved; select again"
        onLoadFailed: error => {
            if (error !== FileViewError.FileNotFound)
                root.orientationError = "orientation could not be read; select again";
        }
    }

    function selectSection(name: string): void {
        if (name === selectedSection || !sections.some(candidate => candidate.name === name))
            return;
        if (popupOpen && section)
            section.left();
        selectedSection = name;
        if (popupOpen && section)
            section.entered();
    }

    function requestSection(name: string): void {
        selectSection(name);
        openRequested();
    }

    function setOpen(value: bool): void {
        if (popupOpen === value)
            return;
        popupOpen = value;
        if (section) {
            if (value)
                section.entered();
            else
                section.left();
        }
    }

    function selectMode(value: string): void {
        if (busy || (value !== "dark" && value !== "light"))
            return;
        error = "";
        busy = true;
        selection.command = ["nu", "--no-config-file", Qt.resolvedUrl("../../runtime/theme.nu").toString().replace("file://", ""), "set", value];
        selection.running = true;
    }

    Process {
        id: selection
        stderr: StdioCollector {
            id: outcome
        }
        onExited: (code, exitStatus) => {
            root.busy = false;
            if (code !== 0 || exitStatus !== 0) {
                // Diagnostics stay at this process boundary, never show raw paths.
                if (outcome.text.includes("incomplete reconciliation")) {
                    const steps = [];
                    if (outcome.text.includes("desktop preference:"))
                        steps.push("desktop preference");
                    if (outcome.text.includes("aerc reload:"))
                        steps.push("mail reload");
                    if (outcome.text.includes("/niri.kdl:"))
                        steps.push("window theme");
                    if (outcome.text.includes("/aerc/nugu:"))
                        steps.push("mail theme");
                    root.error = (steps.length ? steps.join(" and ") : "appearance") + " incomplete; retry selected mode";
                } else {
                    // A crash may follow authority commit; never claim rollback.
                    root.error = "appearance interrupted; retry selected mode";
                }
            }
        }
        onRunningChanged: if (!running)
            Qt.callLater(() => {
                // FailedToStart never emits exited; keep retry actionable then too.
                if (root.busy && !selection.running) {
                    root.busy = false;
                    root.error = "appearance interrupted; retry selected mode";
                }
            })
    }
}