repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

quickshell/nugu/Theme.qml

Raw
import QtQuick
import Quickshell
import Quickshell.Io

Item {
    id: theme

    // Theme is a shared data object, not a visible surface. Keeping it as one Item
    // gives every widget the same Nugu tokens and keeps dark/light switching atomic.
    visible: false

    // Read the live Nugu mode so Quickshell follows the rest of the desktop.
    // IPC can override it for preview/testing without writing global state.
    readonly property string modePath: `${Quickshell.env("HOME")}/.cache/nugu/mode`

    // Keep Quickshell self-contained: bootstrapping only has to symlink this folder.
    // The file is copied from hypr/generated/nugu.json when the config is updated.
    readonly property string palettePath: Qt.resolvedUrl("nugu.json")

    property string mode: "dark"
    property bool followsModeFile: true
    readonly property string modeSource: followsModeFile ? "auto" : "preview"
    property var palettes: ({ "dark": fallbackDark(), "light": fallbackDark() })
    property var palette: fallbackDark()

    // Expose semantic namespaces used by the desktop. These are assigned explicitly
    // on every mode change because QML does not reliably notify nested JS object reads.
    property var content: palette.content
    property var ui: palette.ui
    property string error: palette.error
    property string warning: palette.warning
    property string info: palette.info
    property string debug: palette.debug

    FileView {
        id: paletteFile
        path: theme.palettePath
        blockLoading: true
        printErrors: false
    }

    FileView {
        id: modeFile
        path: theme.modePath
        blockLoading: true
        printErrors: false
        watchChanges: true

        // FileView updates text when loading finishes. Reading text immediately
        // from fileChanged can see the old mode, so file changes only trigger a
        // reload. Loaded applies the freshly-read mode only while source is auto;
        // preview overrides must stay stable until IPC selects auto again.
        onFileChanged: modeFile.reload()
        onLoaded: {
            if (theme.followsModeFile) theme.applyMode(theme.readMode())
        }
    }

    Component.onCompleted: reload()

    function readJson() {
        const text = paletteFile.text()
        if (text.length === 0) {
            return { "dark": fallbackDark(), "light": fallbackDark() }
        }
        return JSON.parse(text)
    }

    function readMode() {
        const text = modeFile.text().trim()
        return text === "light" ? "light" : "dark"
    }

    function applyMode(next: string): void {
        mode = next === "light" ? "light" : "dark"
        const selected = palettes[mode] || palettes.dark || fallbackDark()

        // Clone the selected object so bindings see a fresh value each time.
        palette = JSON.parse(JSON.stringify(selected))
        content = palette.content
        ui = palette.ui
        error = palette.error
        warning = palette.warning
        info = palette.info
        debug = palette.debug
    }

    function reload(): void {
        paletteFile.reload()
        modeFile.reload()
        palettes = readJson()

        // Reload means reload palette data. Mode follows the file only in auto;
        // preview mode reloads colors but keeps the selected preview mode.
        applyMode(followsModeFile ? readMode() : mode)
    }

    function setMode(next: string): void {
        // `auto` means follow Nugu's shared mode cache again. Dark/light are
        // stable preview overrides only; they never write global desktop state.
        if (next === "auto") {
            followsModeFile = true
            reload()
        } else {
            followsModeFile = false
            applyMode(next)
        }
    }

    function fallbackDark() {
        return {
            "debug": "#ed5eed",
            "error": "#ec6376",
            "warning": "#f0ee69",
            "info": "#3ec5e3",
            "content": {
                "normal": "#ededed",
                "backdrop": "#121212",
                "accent": "#d151d1",
                "minor": "#8b8b8b",
                "focus": "#7ba1aa",
                "unfocus": "#5d5d5d",
                "important_local": "#b59ae4",
                "important_global": "#dfd3f3"
            },
            "ui": {
                "normal": "#dbdbdb",
                "backdrop": "#242424",
                "accent": "#d86dd8",
                "minor": "#9d9d9d",
                "focus": "#9aabae",
                "unfocus": "#6f6f6f",
                "important_local": "#c9b6eb",
                "important_global": "#f3effb"
            }
        }
    }
}