Skip to content

Commit

Permalink
Enable low power mode in battery widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Tinland committed Dec 25, 2023
1 parent afe38e2 commit 12c464b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
33 changes: 19 additions & 14 deletions lib/components/data/battery.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand All @@ -68,11 +72,12 @@ export const Widget = React.memo(() => {
if (loading) return <DataWidgetLoader.Widget className="battery" />;
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,
});

Expand Down Expand Up @@ -111,9 +116,9 @@ export const Widget = React.memo(() => {
{percentage}%
</DataWidget.Widget>
);
});
};

Widget.displayName = "Battery";
// Widget.displayName = "Battery";

function getTransform(value) {
let transform = `0.${value}`;
Expand Down
3 changes: 3 additions & 0 deletions lib/styles/components/data/battery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 12c464b

Please sign in to comment.