Something went wrong. Try again.
Something went wrong. Try again.
5.1 kB · 175 lines
at master
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176import "../components"import "../components/ModuleData.js" as ModuleDataimport QtQuickimport Quickshellimport Quickshell.Io
ModulePill { id: root
required property string moduleName property var ipc: null property string command: "" property int interval: 30000 property bool parseJson: true property bool hideEmptyText: true property bool moduleVisible: true property string onClickCommand: "" property string onRightClickCommand: "" property string onMiddleClickCommand: "" property bool detachedActions: false property string textOverride: "" property string format: "{}" property string icon: "" property var formatIcons: ({}) property string currentText: textOverride property string currentTooltip: "" property string currentClass: ""
function classList(value) { if (Array.isArray(value)) return value.map(item => String(item));
if (value === undefined || value === null || String(value).length === 0) return [];
return [String(value)]; }
function decodeEntities(value) { return value.replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&").replace(/"/g, "\"").replace(/'/g, "'").replace(/'/g, "'"); }
function plainTooltip(value) { return decodeEntities(String(value ?? "").replace(/\r/g, "\n").replace(/<[^>]*>/g, "").trim()); }
function iconFor(parsed, classes) { if (icon.length > 0) return icon;
const candidates = []; if (parsed.alt !== undefined && parsed.alt !== null) candidates.push(String(parsed.alt));
for (const name of classes) candidates.push(name);
for (const name of candidates) { if (formatIcons && formatIcons[name] !== undefined) return String(formatIcons[name]); }
return ""; }
function applyFormat(value, moduleIcon) { return ModuleData.formatText(format, value, moduleIcon); }
function runCommand(commandText, detached = false) { if (commandText.length === 0) return;
const command = ["/usr/bin/sh", "-lc", commandText]; if (detached) Quickshell.execDetached(command); else if (!actionRunner.running) actionRunner.exec(command); }
function refresh() { if (!moduleVisible || refreshRunner.running) return;
if (textOverride.length > 0) { currentText = textOverride; return; } if (command.length > 0) refreshRunner.exec(["/usr/bin/sh", "-lc", command]); }
function applyOutput(output) { const trimmed = output.trim(); if (trimmed.length === 0) { currentText = ""; currentTooltip = ""; currentClass = ""; return; } if (!parseJson) { currentText = applyFormat(trimmed, icon); currentTooltip = ""; currentClass = ""; return; } try { const parsed = ModuleData.parseObject(trimmed); const classes = classList(parsed.class); const rawText = String(parsed.text ?? ""); if (rawText.length === 0 && hideEmptyText) currentText = ""; else currentText = applyFormat(rawText, iconFor(parsed, classes)).trim(); currentTooltip = plainTooltip(parsed.tooltip ?? ""); currentClass = classes[0] ?? ""; } catch (error) { currentText = applyFormat(trimmed, icon); currentTooltip = ""; currentClass = "error"; } }
text: currentText tooltipText: currentTooltip moduleClass: currentClass visible: moduleVisible && (!hideEmptyText || currentText.length > 0) onClicked: button => { if (button === Qt.LeftButton) runCommand(onClickCommand, detachedActions); else if (button === Qt.RightButton) { if (onRightClickCommand.length > 0) runCommand(onRightClickCommand, detachedActions); else refresh(); } else if (button === Qt.MiddleButton) runCommand(onMiddleClickCommand); } Component.onCompleted: refresh() onModuleVisibleChanged: { if (moduleVisible) refresh(); else if (hideEmptyText) currentText = ""; }
Timer { interval: root.interval running: root.moduleVisible && root.command.length > 0 && root.interval > 0 repeat: true onTriggered: root.refresh() }
Process { id: refreshRunner
stdout: StdioCollector { onStreamFinished: root.applyOutput(this.text) } }
Process { id: actionRunner onExited: root.refresh() }
Connections { function onRefreshRequested(name) { if (name === root.moduleName || name === "all") root.refresh(); }
target: root.ipc }}