pragma Singleton

import Quickshell
import Quickshell.Io
import QtQuick

// Single GameMode watcher shared by every bar instance, so only one long-lived
// `gdbus monitor` runs regardless of how many screens exist. Event-driven:
// re-checks status only when GameMode emits a register/unregister signal.
Singleton {
    id: root

    property bool active: false

    Process {
        id: gameProc
        command: ["gamemoded", "--status"]
        stdout: StdioCollector {
            onStreamFinished: root.active = this.text.indexOf("is active") >= 0
        }
    }

    function poll() { gameProc.running = false; gameProc.running = true; }

    Process {
        id: monitor
        running: true
        command: ["gdbus", "monitor", "-e",
                  "-d", "com.feralinteractive.GameMode",
                  "-o", "/com/feralinteractive/GameMode"]
        stdout: SplitParser {
            onRead: line => { if (line.indexOf("Registered") >= 0) root.poll() }
        }
    }

    Component.onCompleted: poll()
}
