repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by admin
quickshell/nuguland/features/power/power.js
Raw// power.js — pure power/session shaping + action routing. NO I/O, no UPower.
// Dual-mode: node `require` (tests) + QML import ("power.js" as Power).
// Same pattern as audio.js / brightness.js / network.js. No `.pragma library`.
//
// The native side (Power.qml) owns UPower devices and the actual exec; this
// module owns the plain-data transforms and — crucially — actionPlan(), the
// single routing table that says HOW each session action is carried out. That
// table is pure, so the dangerous part (which argv runs for "poweroff") is
// unit-tested instead of buried in QML.
// --- Battery / device shaping ----------------------------------------------
// minutesToLabel(seconds) — "unknown" / "45m" / "2h" / "1h 20m".
function minutesToLabel(seconds) {
var value = Math.max(0, Math.round(Number(seconds || 0) / 60));
if (value <= 0) return "unknown";
var hours = Math.floor(value / 60);
var minutes = value % 60;
if (hours <= 0) return minutes + "m";
if (minutes <= 0) return hours + "h";
return hours + "h " + minutes + "m";
}
// batteryRole(percent) — theme token by charge level.
function batteryRole(percent) {
var p = Number(percent || 0);
if (p <= 10) return "error";
if (p <= 25) return "warning";
return "ui.accent";
}
// timeEstimateLabel(state, secsToFull, secsToEmpty) — charging counts up to
// full, otherwise down to empty.
function timeEstimateLabel(state, secsToFull, secsToEmpty) {
return state === "charging"
? minutesToLabel(secsToFull)
: minutesToLabel(secsToEmpty);
}
// batteryMeta(battery) — the line under the percentage: time to full while
// charging, time left otherwise, then health; "" when neither is known.
function batteryMeta(battery) {
var b = battery || {};
var parts = [];
var estimate = String(b.timeEstimate || "unknown");
if (estimate !== "unknown")
parts.push(estimate + (b.state === "charging" ? " to full" : " left"));
if (Number(b.health) >= 0)
parts.push("health " + b.health + "%");
return parts.join(" · ");
}
function hasMainBattery(battery) {
var b = battery || {};
var state = b.state || "unknown";
return Number(b.percent || 0) > 0 || state !== "unknown" || String(b.timeEstimate || "unknown") !== "unknown" || Number(b.health || -1) >= 0;
}
// isLowPower(state) — running on battery (drives global animation throttling).
function isLowPower(state) {
return state === "discharging" || state === "pendingdischarge";
}
// batteryAttention(battery) — what the bar chip flags: "" for a quiet chip,
// "warning" for a low and "error" for a critical main battery that is not
// charging. Desktops and charging laptops never ask for attention.
function batteryAttention(battery) {
var b = battery || {};
if (!hasMainBattery(b) || !isLowPower(b.state)) return "";
var role = batteryRole(b.percent);
return role === "ui.accent" ? "" : role;
}
// --- Session actions -------------------------------------------------------
var SESSION_ACTIONS = ["lock", "suspend", "monitor-off", "monitor-on", "logout", "reboot", "poweroff"];
var DESTRUCTIVE_ACTIONS = ["logout", "reboot", "poweroff"];
// requiresConfirmation(action) — irreversible session actions are two-step.
// suspend is recoverable, so it fires immediately.
function requiresConfirmation(action) {
return DESTRUCTIVE_ACTIONS.indexOf(String(action)) >= 0;
}
// actionPlan(action) — the routing table. Returns how Power.qml executes it:
// { via: "lock" } -> lockService.request()
// { via: "niri", args: [...] } -> niriService.action(args)
// { via: "exec", argv: [...] } -> Quickshell.execDetached(argv)
// { via: "none" } -> unknown action, do nothing
// argv/args are discrete tokens (never a shell string) — no injection surface.
function actionPlan(action) {
switch (String(action)) {
case "lock": return { via: "lock" };
case "monitor-off": return { via: "niri", args: ["power-off-monitors"] };
case "monitor-on": return { via: "niri", args: ["power-on-monitors"] };
// niri owns the session; quit ends it. --skip-confirmation because the
// popup already two-step confirms (avoids niri's own Enter prompt).
case "logout": return { via: "niri", args: ["quit", "--skip-confirmation"] };
case "suspend": return { via: "exec", argv: ["systemctl", "suspend"] };
case "reboot": return { via: "exec", argv: ["systemctl", "reboot"] };
case "poweroff": return { via: "exec", argv: ["systemctl", "poweroff"] };
default: return { via: "none" };
}
}
// gridStep(index, count, columns, direction) — the cell an arrow key moves
// focus to in a row-major grid of `count` cells; edges hold, nothing wraps.
function gridStep(index, count, columns, direction) {
var column = index % columns;
if (direction === "left" && column > 0) return index - 1;
if (direction === "right" && column < columns - 1 && index + 1 < count) return index + 1;
if (direction === "up" && index - columns >= 0) return index - columns;
if (direction === "down" && index + columns < count) return index + columns;
return index;
}
// actionLabel(action) — short grid label.
function actionLabel(action) {
if (action === "monitor-off") return "mon off";
if (action === "monitor-on") return "mon on";
if (action === "poweroff") return "power";
return String(action);
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
minutesToLabel: minutesToLabel,
batteryRole: batteryRole,
timeEstimateLabel: timeEstimateLabel,
batteryMeta: batteryMeta,
hasMainBattery: hasMainBattery,
isLowPower: isLowPower,
batteryAttention: batteryAttention,
gridStep: gridStep,
SESSION_ACTIONS: SESSION_ACTIONS,
DESTRUCTIVE_ACTIONS: DESTRUCTIVE_ACTIONS,
requiresConfirmation: requiresConfirmation,
actionPlan: actionPlan,
actionLabel: actionLabel
};
}