// osd.js — pure OSD logic: event normalization, role, meter value, icon name, // labels. NO timers, NO Quickshell imports. Dual-mode: node `require` (tests) // + QML import. Osd.qml owns the visible/expiry state and delegates here. var supportedEvents = ["volume", "microphone", "brightness", "media", "workspace", "screenshot", "color", "error"]; // unknown events render as errors instead of silently inventing a new kind function normalizeEvent(event) { var name = String(event); return supportedEvents.indexOf(name) >= 0 ? name : "error"; } function roleFor(event, value) { var name = String(event); var text = String(value); if (name === "error") return "error"; if (name === "microphone" && (text === "muted" || text === "active")) return "warning"; if (name === "volume" || name === "brightness") return "ui.accent"; return "info"; } function isNumeric(value) { var text = String(value).replace("%", "").trim(); return text.length > 0 && isFinite(Number(text)); } // 0..1 for the meter-style events (volume/brightness/microphone) when the // value is a real number; -1 otherwise (mute toggles, media, color, error) // so the bar is hidden rather than drawn empty. function normalizedValue(event, value) { var name = String(event); if (name !== "volume" && name !== "brightness" && name !== "microphone") return -1; if (!isNumeric(value)) return -1; return Math.max(0, Math.min(1, Number(String(value).replace("%", "")) / 100)); } // Nerd Font (Material Design) glyph for the event, the same family as the bar // chips, so every event draws an icon without depending on the icon theme. // Code points verified with the nerdfont-icons helper. var glyphs = { "volume-off": "\u{F0581}", // nf-md-volume_off "volume-low": "\u{F057F}", // nf-md-volume_low "volume-medium": "\u{F0580}", // nf-md-volume_medium "volume-high": "\u{F057E}", // nf-md-volume_high "microphone": "\u{F036C}", // nf-md-microphone "microphone-off": "\u{F036D}", // nf-md-microphone_off "brightness": "\u{F00DF}", // nf-md-brightness_6 "play": "\u{F040A}", // nf-md-play "pause": "\u{F03E4}", // nf-md-pause "screenshot": "\u{F0E51}", // nf-md-monitor_screenshot "color": "\u{F020A}", // nf-md-eyedropper "error": "\u{F0028}", // nf-md-alert_circle "info": "\u{F02FC}" // nf-md-information }; function glyph(event, value) { var name = String(event); var text = String(value); var level = Number(text.replace("%", "")); var muted = text === "muted" || (isNumeric(text) && level <= 0); if (name === "volume") return glyphs[muted ? "volume-off" : (level < 34 ? "volume-low" : (level < 67 ? "volume-medium" : "volume-high"))]; if (name === "microphone") return glyphs[muted ? "microphone-off" : "microphone"]; if (name === "brightness" || name === "screenshot" || name === "color" || name === "error") return glyphs[name]; if (name === "media") return glyphs[text.indexOf("pause") >= 0 ? "pause" : "play"]; return glyphs.info; } // An error is titled by its subject and keeps the failure as its body: the // message splits at its first " · " or ": ", e.g. "screenshot not taken · no // save path given". Only a message without a subject keeps the generic heading. function errorParts(value) { var text = String(value); var match = /^(.+?)(?: · |: )(.+)$/.exec(text); return match ? { "title": match[1], "body": match[2] } : { "title": "error", "body": text }; } // An unsupported event renders as an error whose subject is the event's name. function normalizeValue(event, value) { var name = String(event); return normalizeEvent(name) === "error" && name !== "error" && name.length > 0 ? name + " · " + String(value) : String(value); } // the label is the lowercase event name itself, an error's its subject function displayLabel(event, value) { var name = String(event); return name === "error" ? errorParts(value).title : name; } // right-aligned glance value: "72%" for meters, a word for toggles, the // raw payload (media event / colour hex) otherwise. Errors render their // message in the body line instead, so the corner stays empty. function displayValue(event, value) { var name = String(event); var text = String(value); var normalized = normalizedValue(name, text); if (normalized >= 0) return Math.round(normalized * 100) + "%"; if (text === "muted") return "muted"; if (text === "unmuted") return "on"; if (name === "error") return ""; return text; } function iconRole(event, value) { var name = String(event); var text = String(value); if (name === "volume") return Number(text) <= 0 ? "audio-muted" : "audio-volume"; if (name === "microphone") return text === "muted" ? "microphone-muted" : "microphone-active"; if (name === "brightness") return "brightness"; if (name === "screenshot") return "screenshot"; if (name === "color") return "color-picker"; if (name === "error") return "danger"; return name.length > 0 ? name : "info"; } function numericText(event, value) { var normalized = normalizedValue(event, value); return normalized >= 0 ? String(Math.round(normalized * 100)) : ""; } function valueIndicator(event, value, role) { var name = String(event); var text = String(value); var normalized = normalizedValue(name, text); var hasProgress = normalized >= 0; return { "source": "Nuguland OsdValueIndicator.qml plus Nuguland OSD sliders", "layout": "icon-left-value-right", "iconRole": iconRole(name, text), "glyph": glyph(name, text), "displayLabel": displayLabel(name, text), "displayValue": displayValue(name, text), "value": text, "normalizedValue": hasProgress ? normalized : null, "progressBar": { "visible": hasProgress, "from": 0, "to": 1, "value": hasProgress ? normalized : 0, "role": role }, "numericText": { "text": numericText(name, text), "odometerMotion": hasProgress, "hardSeparator": hasProgress ? "%" : "" }, "slideFromEdge": { "edge": "right", "hiddenOffset": "width+sidebar-gap", "visibleOffset": 0 }, "wheelActions": name === "volume" ? ["volume-up", "volume-down"] : name === "brightness" ? ["brightness-up", "brightness-down"] : [], "protectionMessage": { "visible": name === "error", "role": "error", "text": name === "error" ? errorParts(text).body : "" }, "hardSeparator": hasProgress ? "%" : "" }; } if (typeof module !== 'undefined' && module.exports) { module.exports = { supportedEvents: supportedEvents, normalizeEvent: normalizeEvent, normalizeValue: normalizeValue, errorParts: errorParts, roleFor: roleFor, isNumeric: isNumeric, normalizedValue: normalizedValue, glyph: glyph, displayLabel: displayLabel, displayValue: displayValue, iconRole: iconRole, numericText: numericText, valueIndicator: valueIndicator }; }