mirror of
https://github.com/SupertigerDev/waybar-online.git
synced 2026-03-24 16:06:37 +00:00
Add critical battery state
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
import type { Module } from "./createModule";
|
||||
import { clamp, replaceTextWithUnicode } from "./utils";
|
||||
|
||||
interface LabelOpts {
|
||||
config?: {
|
||||
[key: string]: any;
|
||||
format?: string;
|
||||
states: string[];
|
||||
"format-icons"?: string[];
|
||||
};
|
||||
module: Module
|
||||
interval?: number;
|
||||
update: () => void;
|
||||
}
|
||||
@@ -53,9 +56,37 @@ export const createLabel = (opts: LabelOpts) => {
|
||||
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 {
|
||||
element,
|
||||
set,
|
||||
getIcon,
|
||||
getState
|
||||
};
|
||||
};
|
||||
|
||||
@@ -10,10 +10,31 @@ export const createBatteryModule = (
|
||||
interval: 1000,
|
||||
update: () => update(),
|
||||
config: config!,
|
||||
module,
|
||||
});
|
||||
|
||||
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 battery = await navigator.getBattery();
|
||||
|
||||
@@ -21,24 +42,18 @@ export const createBatteryModule = (
|
||||
level: Math.random(),
|
||||
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);
|
||||
|
||||
if (charging) {
|
||||
module.element.classList.add("charging");
|
||||
} else {
|
||||
module.element.classList.remove("charging");
|
||||
}
|
||||
|
||||
const status = getState(battery);
|
||||
|
||||
|
||||
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({
|
||||
capacity: batteryPercent,
|
||||
|
||||
Reference in New Issue
Block a user