From 12c464b00811913bf26b4a723dc62858d26a9bdb Mon Sep 17 00:00:00 2001 From: Jean Tinland Date: Mon, 25 Dec 2023 21:33:23 +0100 Subject: [PATCH] Enable low power mode in battery widget --- lib/components/data/battery.jsx | 33 +++++++++++++++------------ lib/styles/components/data/battery.js | 3 +++ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/components/data/battery.jsx b/lib/components/data/battery.jsx index ac499052..7e5b81a5 100644 --- a/lib/components/data/battery.jsx +++ b/lib/components/data/battery.jsx @@ -13,7 +13,7 @@ const { React } = Uebersicht; const DEFAULT_REFRESH_FREQUENCY = 10000; -export const Widget = React.memo(() => { +export const Widget = () => { const { displayIndex, settings } = useSimpleBarContext(); const { widgets, batteryWidgetOptions } = settings; const { batteryWidget } = widgets; @@ -43,21 +43,25 @@ export const Widget = React.memo(() => { const getBattery = React.useCallback(async () => { if (!visible) return; - const [system, percentage, status, caffeinate] = await Promise.all([ - Utils.getSystem(), - Uebersicht.run( - `pmset -g batt | egrep '([0-9]+%).*' -o --colour=auto | cut -f1 -d'%'` - ), - Uebersicht.run( - `pmset -g batt | grep "'.*'" | sed "s/'//g" | cut -c 18-19` - ), - Uebersicht.run(`pgrep caffeinate`), - ]); + // TODO: merge these into one call and parse result + const [system, percentage, status, caffeinate, lowPowerMode] = + await Promise.all([ + Utils.getSystem(), + Uebersicht.run( + `pmset -g batt | egrep '([0-9]+%).*' -o --colour=auto | cut -f1 -d'%'` + ), + Uebersicht.run( + `pmset -g batt | grep "'.*'" | sed "s/'//g" | cut -c 18-19` + ), + Uebersicht.run(`pgrep caffeinate`), + Uebersicht.run(`pmset -g | grep lowpowermode | awk '{print $2}'`), + ]); setState({ system, percentage: parseInt(percentage, 10), charging: Utils.cleanupOutput(status) === "AC", caffeinate: Utils.cleanupOutput(caffeinate), + lowPowerMode: Utils.cleanupOutput(lowPowerMode) === "1", }); setLoading(false); }, [visible]); @@ -68,11 +72,12 @@ export const Widget = React.memo(() => { if (loading) return ; if (!state) return null; - const { system, percentage, charging, caffeinate } = state; + const { system, percentage, charging, caffeinate, lowPowerMode } = state; const isLowBattery = !charging && percentage < 20; const classes = Utils.classnames("battery", { "battery--low": isLowBattery, + "battery--low-power-mode": lowPowerMode, "battery--caffeinate": caffeinate.length, }); @@ -111,9 +116,9 @@ export const Widget = React.memo(() => { {percentage}% ); -}); +}; -Widget.displayName = "Battery"; +// Widget.displayName = "Battery"; function getTransform(value) { let transform = `0.${value}`; diff --git a/lib/styles/components/data/battery.js b/lib/styles/components/data/battery.js index d7b645f0..7eef8c72 100644 --- a/lib/styles/components/data/battery.js +++ b/lib/styles/components/data/battery.js @@ -91,6 +91,9 @@ export const batteryStyles = /* css */ ` .battery--low .battery__icon-filler { background-color: var(--red); } +.battery--low-power-mode .battery__icon-filler { + background-color: var(--yellow); +} .battery__caffeinate-icon { position: absolute; top: calc(50% - 12px);