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 <jaskiratpal.singh@outlook.com>
This commit is contained in:
Rubin Bhandari
2024-11-18 01:55:16 +05:45
committed by GitHub
parent 0028b9484e
commit ac794dccb0
5 changed files with 54 additions and 5 deletions

View File

@@ -13,6 +13,8 @@ import { Window } from 'types/@girs/gtk-3.0/gtk-3.0.cjs';
import { namedColors } from './constants/colors'; import { namedColors } from './constants/colors';
import { distroIcons } from './constants/distro'; import { distroIcons } from './constants/distro';
import { distro } from './variables'; import { distro } from './variables';
const battery = await Service.import('battery');
import options from 'options';
export type Binding<T> = import('types/service').Binding<any, any, T>; export type Binding<T> = import('types/service').Binding<any, any, T>;
@@ -200,3 +202,22 @@ export function getDistroIcon(): string {
const icon = distroIcons.find(([id]) => id === distro.id); const icon = distroIcons.find(([id]) => id === distro.id);
return icon ? icon[1] : ''; // default icon if not found 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,
});
}
});
};

View File

@@ -11,12 +11,12 @@ import { Bar } from 'modules/bar/Bar';
import MenuWindows from './modules/menus/main.js'; import MenuWindows from './modules/menus/main.js';
import SettingsDialog from 'widget/settings/SettingsDialog'; import SettingsDialog from 'widget/settings/SettingsDialog';
import Notifications from './modules/notifications/index.js'; 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 options from 'options.js';
import OSD from 'modules/osd/index'; import OSD from 'modules/osd/index';
App.config({ 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()], windows: [...MenuWindows, Notifications(), SettingsDialog(), ...forMonitors(Bar), OSD()],
closeWindowDelay: { closeWindowDelay: {
sideright: 350, sideright: 350,

View File

@@ -32,14 +32,14 @@ const BatteryLabel = (): BarBoxChild => {
const generateTooltip = (timeSeconds: number, isCharging: boolean, isCharged: boolean): string => { const generateTooltip = (timeSeconds: number, isCharging: boolean, isCharged: boolean): string => {
if (isCharged) { if (isCharged) {
return 'Fully Charged!!!'; return 'Full';
} }
const { hours, minutes } = formatTime(timeSeconds); const { hours, minutes } = formatTime(timeSeconds);
if (isCharging) { if (isCharging) {
return `${hours} hours ${minutes} minutes until full`; return `Time to full: ${hours} h ${minutes} min`;
} else { } else {
return `${hours} hours ${minutes} minutes left`; return `Time to empty: ${hours} h ${minutes} min`;
} }
}; };

View File

@@ -1160,6 +1160,12 @@ const options = mkOptions(OPTIONS, {
raiseMaximumVolume: opt(false), raiseMaximumVolume: opt(false),
}, },
power: { 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), showLabel: opt(true),
confirmation: opt(true), confirmation: opt(true),
sleep: opt('systemctl suspend'), sleep: opt('systemctl suspend'),

View File

@@ -17,6 +17,28 @@ export const PowerMenuSettings = (): Scrollable<Child, Attribute> => {
children: [ children: [
Header('Power Menu'), Header('Power Menu'),
Option({ opt: options.menus.power.showLabel, title: 'Show Label', type: 'boolean' }), 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.confirmation, title: 'Confirmation Dialog', type: 'boolean' }),
Option({ opt: options.menus.power.shutdown, title: 'Shutdown Command', type: 'string' }), Option({ opt: options.menus.power.shutdown, title: 'Shutdown Command', type: 'string' }),
Option({ opt: options.menus.power.reboot, title: 'Reboot Command', type: 'string' }), Option({ opt: options.menus.power.reboot, title: 'Reboot Command', type: 'string' }),