pragma ComponentBehavior: Bound import "../components" import ".." import "../components/ModuleData.js" as ModuleData import QtQuick import Quickshell.Widgets ClippingRectangle { id: root required property var barWindow property var ipc: null property bool moduleVisible: true property int interval: 300000 readonly property int compactHorizontalPadding: 20 readonly property int compactHeight: 34 readonly property int cornerRadius: 8 property var primary: ({ "label": "5h", "available": false, "usedPercent": 0, "resetIn": "", "resetAt": "" }) property var secondary: ({ "label": "7d", "available": false, "usedPercent": 0, "resetIn": "", "resetAt": "" }) property var credits: ({ "hasCredits": false, "unlimited": false, "balance": null }) property string moduleState: "normal" property string errorText: "" readonly property bool hasData: errorText.length === 0 readonly property real primaryPercent: ModuleData.clampPercent(primary.usedPercent) readonly property real secondaryPercent: ModuleData.clampPercent(secondary.usedPercent) readonly property bool hasCredits: Boolean(credits.hasCredits || credits.unlimited || (credits.balance && String(credits.balance) !== "0")) readonly property string accent: moduleState === "critical" || errorText.length > 0 ? theme.red : moduleState === "warning" ? theme.yellow : theme.sky readonly property string accentSoft: theme.alpha(String(accent), moduleState === "normal" ? 0.3 : 0.42) readonly property bool tooltipVisible: mouse.containsMouse readonly property string creditsText: credits.unlimited ? "credits: unlimited" : `credits balance: ${credits.balance ?? "n/a"}` function plainTooltip(value) { return String(value ?? "").replace(/\r/g, "\n").replace(/<[^>]*>/g, "").trim(); } function resetText(windowData) { if (!windowData.available) return "not reported by Codex"; return `resets in ${windowData.resetIn || "?"} at ${windowData.resetAt || "?"}`; } function refresh() { if (!moduleVisible || refreshRunner.running) return; refreshRunner.run(); } function applyOutput(output) { const trimmed = output.trim(); if (trimmed.length === 0) { errorText = "No Codex usage output"; return; } try { const parsed = ModuleData.parseObject(trimmed); if (parsed.error) { errorText = plainTooltip(parsed.error); return; } primary = parsed.primary || primary; secondary = parsed.secondary || secondary; credits = parsed.credits || credits; moduleState = String(parsed.state || "normal"); errorText = ""; } catch (error) { errorText = plainTooltip(trimmed); } } implicitWidth: errorText.length > 0 ? errorLabel.implicitWidth + 24 : compactContent.implicitWidth + compactHorizontalPadding implicitHeight: compactHeight radius: cornerRadius color: mouse.containsMouse ? theme.surface2 : theme.alpha(theme.surface0, 0.28) border.color: theme.alpha(theme.text, 0.1) border.width: 1 contentUnderBorder: true visible: moduleVisible Theme { id: theme } Rectangle { anchors.left: parent.left anchors.top: parent.top anchors.bottom: parent.bottom visible: root.hasData width: parent.width * root.secondaryPercent / 100 radius: 0 color: root.accentSoft } Row { id: compactContent anchors.centerIn: parent visible: root.hasData spacing: 6 Text { anchors.verticalCenter: parent.verticalCenter color: root.accent font.family: theme.fontFamily font.pixelSize: theme.barFontPixelSize text: "󱙺" } Text { id: usageLabel anchors.verticalCenter: parent.verticalCenter visible: Boolean(root.primary.available) color: theme.text font.family: theme.fontFamily font.pixelSize: theme.barFontPixelSize font.bold: false text: `${Math.round(root.primaryPercent)}%` } } Text { id: errorLabel anchors.centerIn: parent visible: !root.hasData color: theme.red font.family: theme.fontFamily font.pixelSize: theme.barFontPixelSize text: "Codex: err" } BarPopup { id: tooltipWindow barWindow: root.barWindow anchorItem: root shown: root.tooltipVisible implicitWidth: 300 implicitHeight: root.errorText.length > 0 ? errorPopupLabel.implicitHeight + 24 : detailsColumn.implicitHeight + 28 PopupSurface { anchors.fill: parent Text { id: errorPopupLabel anchors.fill: parent anchors.margins: 12 visible: root.errorText.length > 0 text: root.errorText color: theme.red font.family: theme.fontFamily font.pixelSize: theme.tooltipFontPixelSize textFormat: Text.PlainText wrapMode: Text.Wrap } Column { id: detailsColumn anchors.fill: parent anchors.margins: 14 visible: root.errorText.length === 0 spacing: 10 Row { width: parent.width spacing: 8 Text { color: root.accent font.family: theme.fontFamily font.pixelSize: theme.barFontPixelSize + 2 text: "󱙺" } Text { width: parent.width - 28 color: theme.text font.family: theme.fontFamily font.pixelSize: theme.tooltipFontPixelSize + 1 font.bold: true text: "Codex usage" } } UsageDetail { width: parent.width usage: root.primary percent: root.primaryPercent accent: root.accent fillColor: root.accentSoft } UsageDetail { width: parent.width usage: root.secondary percent: root.secondaryPercent accent: root.accent fillColor: root.accentSoft } Rectangle { visible: root.hasCredits width: parent.width height: 1 color: theme.alpha(theme.text, 0.1) } Text { visible: root.hasCredits width: parent.width color: theme.subtext0 font.family: theme.fontFamily font.pixelSize: theme.tooltipFontPixelSize text: root.creditsText } } } } component UsageDetail: Column { id: detailRoot required property var usage required property real percent required property string accent required property string fillColor spacing: 3 UsageBar { width: parent.width label: String(detailRoot.usage.label || "") percent: detailRoot.percent available: Boolean(detailRoot.usage.available) accent: detailRoot.accent fillColor: detailRoot.fillColor } Text { width: parent.width color: theme.subtext0 font.family: theme.fontFamily font.pixelSize: theme.tooltipFontPixelSize text: root.resetText(detailRoot.usage) } } component UsageBar: Item { id: rowRoot required property string label required property real percent required property bool available required property string accent required property string fillColor property int barHeight: 22 property int fontPixelSize: 11 property int labelWidth: 34 height: barHeight ClippingRectangle { anchors.fill: parent radius: 4 color: theme.alpha(theme.crust, 0.28) border.color: theme.alpha(String(rowRoot.accent), 0.22) border.width: 1 contentUnderBorder: true Rectangle { anchors.left: parent.left anchors.top: parent.top anchors.bottom: parent.bottom width: rowRoot.available ? Math.max(3, parent.width * rowRoot.percent / 100) : 0 radius: 0 color: rowRoot.fillColor } } Row { anchors.fill: parent anchors.leftMargin: 5 anchors.rightMargin: 5 Text { anchors.verticalCenter: parent.verticalCenter width: rowRoot.labelWidth color: theme.text font.family: theme.fontFamily font.pixelSize: rowRoot.fontPixelSize text: rowRoot.label } Text { anchors.verticalCenter: parent.verticalCenter width: parent.width - rowRoot.labelWidth color: theme.text font.family: theme.fontFamily font.pixelSize: rowRoot.fontPixelSize horizontalAlignment: Text.AlignRight text: rowRoot.available ? `${Math.round(rowRoot.percent)}%` : "--" } } } MouseArea { id: mouse anchors.fill: parent acceptedButtons: Qt.LeftButton | Qt.RightButton hoverEnabled: true z: 10 onClicked: root.refresh() } Timer { interval: root.interval running: root.moduleVisible && root.interval > 0 repeat: true onTriggered: root.refresh() } HelperProcess { id: refreshRunner helper: "codex-usage" onOutput: text => root.applyOutput(text) } Connections { target: root.ipc function onRefreshRequested(name) { if (name === "codex-usage" || name === "all") root.refresh(); } } Component.onCompleted: refresh() onModuleVisibleChanged: { if (moduleVisible) refresh(); } }