import QtQuick import Quickshell import Quickshell.Io // Shared state for every Keymap widget (one per bar): the parsed sheet and // the repo watch. helpers/keymap.py does the git work; a check is a // `git ls-remote`, and only a new commit fetches and parses again. Item { id: root property var shell: null property string pluginId: "mx-keymap" property var sheet: null property bool busy: false property string error: "" // Changed since the sheet was last on screen: the bar icon lights up. property bool unseen: false property int openPanels: 0 readonly property bool panelOpen: openPanels > 0 property real lastCheck: 0 // A layer asked for over IPC; the panel picks it up when it opens. property string wantedView: "" signal showView(string layer) // Where to look, from the widget settings. property string repository: "" property string branch: "" property string path: "" property int checkMinutes: 2 readonly property string helper: decodeURIComponent(Qt.resolvedUrl("helpers/keymap.py").toString().replace(/^file:\/\//, "")) function configure(settings) { const s = settings || {} const repo = String(s.repository === undefined ? "xnmrph/corne" : s.repository).trim() const br = String(s.branch || "").trim() const p = String(s.path || "").trim() checkMinutes = Math.max(1, Number(s.checkMinutes) || 2) if (repo === repository && br === branch && p === path) return repository = repo branch = br path = p sheet = null error = "" call("load", false) call("check", false) } function refresh(force) { call("check", !!force) } function checkIfStale() { if (Date.now() - lastCheck > 30000) call("check", false) } // One helper run at a time; a request made meanwhile runs after it. property var queued: null function call(op, force) { const request = { op: op, repo: repository, branch: branch, path: path, force: force } if (job.running) { // A check supersedes a load; a forced check supersedes a plain one. if (!queued || op === "check") queued = { op: op, force: force || (queued && queued.force) } return } if (op === "check") { busy = true; lastCheck = Date.now() } job.request = request job.command = ["python3", helper, JSON.stringify(request)] job.running = true } Process { id: job property var request: ({}) stdout: StdioCollector { id: out } stderr: StdioCollector { id: err } onExited: function(code) { let result try { result = JSON.parse(out.text) } catch (e) { result = { ok: false, error: (err.text || "The keymap helper failed").trim().split("\n").pop() } } const req = job.request // Settings changed while it ran: drop the answer. const current = req.repo === root.repository && req.branch === root.branch && req.path === root.path if (current) { if (!result.ok) { if (req.op === "check") root.error = result.error || "Could not read the keymap" } else { if (req.op === "check") root.error = "" if (result.sheet) { const had = !!root.sheet root.sheet = result.sheet if (result.changed && had && !root.panelOpen) root.unseen = true } } } if (req.op === "check") root.busy = false const next = root.queued root.queued = null if (next) Qt.callLater(() => root.call(next.op, next.force)) } } onPanelOpenChanged: { if (!panelOpen) return unseen = false checkIfStale() } // Poll faster while the repo's CI redraws a keymap-drawer file for a new keymap. readonly property bool pending: !!sheet && !!sheet.pending Timer { interval: root.pending ? 20000 : root.checkMinutes * 60000 repeat: true running: root.repository !== "" onTriggered: root.call("check", false) } IpcHandler { target: "keymap" function toggle(): void { if (root.shell) root.shell.toggle(root.pluginId, "") } // Open the sheet on a layer by name ("overview" for every layer at once). function layer(name: string): void { root.wantedView = name if (root.shell && !root.panelOpen) root.shell.summon(root.pluginId, "") root.showView(name) } // Fetch and parse again even when the repo has not changed. function refresh(): void { root.refresh(true) } function status(): string { return JSON.stringify({ repository: root.repository, busy: root.busy, error: root.error, unseen: root.unseen, open: root.panelOpen, sha: root.sheet ? root.sheet.sha : "", source: root.sheet ? root.sheet.source : "", layers: root.sheet ? root.sheet.layers.map(l => l.name) : [], pending: root.pending }) } } }