feat: disable high power consumption without charger (#14281)

Co-authored-by: DarkSky <darksky2048@gmail.com>
This commit is contained in:
Akshaj Rawat
2026-01-27 02:16:16 +05:30
committed by GitHub
parent 3b4b0bad22
commit b8f626513f
9 changed files with 143 additions and 5 deletions
@@ -1,4 +1,4 @@
import { ipcMain, webContents } from 'electron';
import { ipcMain, powerMonitor, webContents } from 'electron';
import {
AFFINE_EVENT_CHANNEL_NAME,
@@ -7,6 +7,7 @@ import {
import { applicationMenuEvents } from './application-menu';
import { beforeAppQuit } from './cleanup';
import { logger } from './logger';
import { powerEvents } from './power';
import { recordingEvents } from './recording';
import { sharedStorageEvents } from './shared-storage';
import { uiEvents } from './ui/events';
@@ -20,6 +21,7 @@ export const allEvents = {
sharedStorage: sharedStorageEvents,
recording: recordingEvents,
popup: popupEvents,
power: powerEvents,
};
const subscriptions = new Map<number, Set<string>>();
@@ -71,6 +73,13 @@ export function registerEvents() {
if (typeof channel !== 'string') return;
if (action === 'subscribe') {
addSubscription(event.sender, channel);
if (channel === 'power:power-source') {
event.sender.send(
AFFINE_EVENT_CHANNEL_NAME,
channel,
powerMonitor.isOnBatteryPower()
);
}
} else {
removeSubscription(event.sender, channel);
}
@@ -0,0 +1,29 @@
import { powerMonitor } from 'electron';
/**
* Power-related event handlers for the Electron main process.
*/
export const powerEvents = {
/**
* Subscribes to system power source changes.
* Emits the initial state immediately upon subscription.
* @param emit - Callback function to send power state to the renderer.
* @returns A cleanup function to remove listeners from powerMonitor.
*/
'power-source': (emit: (isOnBattery: boolean) => void) => {
// emit initial state
emit(powerMonitor.isOnBatteryPower());
const onBattery = () => emit(true);
const onAC = () => emit(false);
powerMonitor.on('on-battery', onBattery);
powerMonitor.on('on-ac', onAC);
// cleanup
return () => {
powerMonitor.off('on-battery', onBattery);
powerMonitor.off('on-ac', onAC);
};
},
};