Add critical battery state

This commit is contained in:
Supertiger
2025-08-01 22:06:16 +01:00
committed by GitHub
parent b98e280e92
commit 40ac8c5eb4
2 changed files with 62 additions and 16 deletions

View File

@@ -1,11 +1,14 @@
import type { Module } from "./createModule";
import { clamp, replaceTextWithUnicode } from "./utils"; import { clamp, replaceTextWithUnicode } from "./utils";
interface LabelOpts { interface LabelOpts {
config?: { config?: {
[key: string]: any; [key: string]: any;
format?: string; format?: string;
states: string[];
"format-icons"?: string[]; "format-icons"?: string[];
}; };
module: Module
interval?: number; interval?: number;
update: () => void; update: () => void;
} }
@@ -53,9 +56,37 @@ export const createLabel = (opts: LabelOpts) => {
return resIcon; return resIcon;
}; };
const getState = (value: number, lesser: boolean) => {
const states = opts.config?.states;
if (typeof states !== "object") {
return "";
}
const entries = Object.entries(states) as unknown as [string, number][];
entries.sort((a, b) => {
if (lesser) {
return a[1] - b[1];
}
return b[1] - a[1];
});
let valid_state = "";
entries.forEach((state) => {
if ((lesser ? value <= state[1] : value >= state[1]) && !valid_state) {
opts.module.element.classList.add(state[0]);
valid_state = state[0];
} else {
opts.module.element.classList.remove(state[0]);
}
})
return valid_state;
}
return { return {
element, element,
set, set,
getIcon, getIcon,
getState
}; };
}; };

View File

@@ -10,10 +10,31 @@ export const createBatteryModule = (
interval: 1000, interval: 1000,
update: () => update(), update: () => update(),
config: config!, config: config!,
module,
}); });
module.element.appendChild(label.element); module.element.appendChild(label.element);
const getState = (battery: {level: number, charging: boolean}) => {
let status = "Unknown";
if (battery.charging && battery.level < 1) {
status = "Charging";
}
if (!battery.charging && battery.level === 1) {
status = "Full";
}
if (battery.charging && battery.level === 1) {
status = "Plugged"
}
if (!battery.charging && battery.level < 1) {
status = "Discharging"
}
return status;
}
let lastStatus = "Unknown"
const update = async () => { const update = async () => {
// const battery = await navigator.getBattery(); // const battery = await navigator.getBattery();
@@ -21,24 +42,18 @@ export const createBatteryModule = (
level: Math.random(), level: Math.random(),
charging: Math.random() > 0.5, charging: Math.random() > 0.5,
}; };
let state = "Unknown";
if (battery.charging && battery.level < 1) {
state = "Charging";
}
if (battery.charging && battery.level === 1) {
state = "Full";
}
const charging = battery.charging;
const batteryPercent = Math.round(battery.level * 100); const batteryPercent = Math.round(battery.level * 100);
if (charging) {
module.element.classList.add("charging"); const status = getState(battery);
} else {
module.element.classList.remove("charging");
} module.element.classList.remove(lastStatus.toLowerCase());
module.element.classList.add(status.toLowerCase())
lastStatus = status;
module.element.title = status;
const state = label.getState(batteryPercent, true);
console.log(state)
label.set({ label.set({
capacity: batteryPercent, capacity: batteryPercent,