From ac794dccb0371ad0c44b7dbe7ee36b93c22817c5 Mon Sep 17 00:00:00 2001 From: Rubin Bhandari Date: Mon, 18 Nov 2024 01:55:16 +0545 Subject: [PATCH] fix: add battery notif (#441) * fix: waybar style battery msgs * Revert "fix: waybar style battery msgs" This reverts commit 924d998ddda6cb4becc87fabfd7958a0f7ecc773. * fix: revert changes --------- Co-authored-by: Jas Singh --- lib/utils.ts | 21 ++++++++++++++++++++ main.ts | 4 ++-- modules/bar/battery/index.ts | 6 +++--- options.ts | 6 ++++++ widget/settings/pages/config/menus/power.ts | 22 +++++++++++++++++++++ 5 files changed, 54 insertions(+), 5 deletions(-) diff --git a/lib/utils.ts b/lib/utils.ts index 198ab4b..cee751d 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -13,6 +13,8 @@ import { Window } from 'types/@girs/gtk-3.0/gtk-3.0.cjs'; import { namedColors } from './constants/colors'; import { distroIcons } from './constants/distro'; import { distro } from './variables'; +const battery = await Service.import('battery'); +import options from 'options'; export type Binding = import('types/service').Binding; @@ -200,3 +202,22 @@ export function getDistroIcon(): string { const icon = distroIcons.find(([id]) => id === distro.id); return icon ? icon[1] : ''; // default icon if not found } + +export const warnOnLowBattery = (): void => { + battery.connect('notify::percent', () => { + const { lowBatteryThreshold, lowBatteryNotification, lowBatteryNotificationText, lowBatteryNotificationTitle } = + options.menus.power; + if (!lowBatteryNotification.value || battery.charging) return; + const lowThreshold = lowBatteryThreshold.value; + + if (battery.percent === lowThreshold || battery.percent === lowThreshold / 2) { + Notify({ + summary: lowBatteryNotificationTitle.value.replace('/$POWER_LEVEL/g', battery.percent.toString()), + body: lowBatteryNotificationText.value.replace('/$POWER_LEVEL/g', battery.percent.toString()), + iconName: icons.ui.warning, + urgency: 'critical', + timeout: 7000, + }); + } + }); +}; diff --git a/main.ts b/main.ts index 8a2758a..efd5185 100644 --- a/main.ts +++ b/main.ts @@ -11,12 +11,12 @@ import { Bar } from 'modules/bar/Bar'; import MenuWindows from './modules/menus/main.js'; import SettingsDialog from 'widget/settings/SettingsDialog'; import Notifications from './modules/notifications/index.js'; -import { bash, forMonitors } from 'lib/utils'; +import { bash, forMonitors, warnOnLowBattery } from 'lib/utils'; import options from 'options.js'; import OSD from 'modules/osd/index'; App.config({ - onConfigParsed: () => Utils.execAsync(`python3 ${App.configDir}/services/bluetooth.py`), + onConfigParsed: () => [Utils.execAsync(`python3 ${App.configDir}/services/bluetooth.py`), warnOnLowBattery()], windows: [...MenuWindows, Notifications(), SettingsDialog(), ...forMonitors(Bar), OSD()], closeWindowDelay: { sideright: 350, diff --git a/modules/bar/battery/index.ts b/modules/bar/battery/index.ts index 8d22ef7..9a9b4f9 100644 --- a/modules/bar/battery/index.ts +++ b/modules/bar/battery/index.ts @@ -32,14 +32,14 @@ const BatteryLabel = (): BarBoxChild => { const generateTooltip = (timeSeconds: number, isCharging: boolean, isCharged: boolean): string => { if (isCharged) { - return 'Fully Charged!!!'; + return 'Full'; } const { hours, minutes } = formatTime(timeSeconds); if (isCharging) { - return `${hours} hours ${minutes} minutes until full`; + return `Time to full: ${hours} h ${minutes} min`; } else { - return `${hours} hours ${minutes} minutes left`; + return `Time to empty: ${hours} h ${minutes} min`; } }; diff --git a/options.ts b/options.ts index a5f2720..2caaab3 100644 --- a/options.ts +++ b/options.ts @@ -1160,6 +1160,12 @@ const options = mkOptions(OPTIONS, { raiseMaximumVolume: opt(false), }, power: { + lowBatteryNotification: opt(false), + lowBatteryThreshold: opt(20), + lowBatteryNotificationTitle: opt('Warning: Low battery'), + lowBatteryNotificationText: opt( + 'Your battery is running low ($POWER_LEVEL %).\n\nPlease plug in your charger.', + ), showLabel: opt(true), confirmation: opt(true), sleep: opt('systemctl suspend'), diff --git a/widget/settings/pages/config/menus/power.ts b/widget/settings/pages/config/menus/power.ts index e1ce949..d3a8501 100644 --- a/widget/settings/pages/config/menus/power.ts +++ b/widget/settings/pages/config/menus/power.ts @@ -17,6 +17,28 @@ export const PowerMenuSettings = (): Scrollable => { children: [ Header('Power Menu'), Option({ opt: options.menus.power.showLabel, title: 'Show Label', type: 'boolean' }), + Option({ + opt: options.menus.power.lowBatteryNotification, + title: 'Show Notification For Low Battery', + type: 'boolean', + }), + Option({ + opt: options.menus.power.lowBatteryThreshold, + title: 'Battery Level For Notification', + type: 'number', + }), + Option({ + opt: options.menus.power.lowBatteryNotificationTitle, + title: 'Low Battery Notification Title', + subtitle: 'Use $POWER_LEVEL To Show Battery Percent', + type: 'string', + }), + Option({ + opt: options.menus.power.lowBatteryNotificationText, + title: 'Low Battery Notification Body', + subtitle: 'Use $POWER_LEVEL To Show Battery Percent', + type: 'string', + }), Option({ opt: options.menus.power.confirmation, title: 'Confirmation Dialog', type: 'boolean' }), Option({ opt: options.menus.power.shutdown, title: 'Shutdown Command', type: 'string' }), Option({ opt: options.menus.power.reboot, title: 'Reboot Command', type: 'string' }),