From a9924e1c32e8445887e3a6b5cdff445d93c2b18f Mon Sep 17 00:00:00 2001 From: Jeremy Rose Date: Mon, 16 Nov 2020 16:31:46 -0800 Subject: [PATCH] feat: add powerMonitor.onBatteryPower (#26494) --- docs/api/power-monitor.md | 15 +++++++++++++++ lib/browser/api/power-monitor.ts | 11 ++++++++++- shell/browser/api/electron_api_power_monitor.cc | 5 +++++ spec-main/api-power-monitor-spec.ts | 9 ++++++++- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/docs/api/power-monitor.md b/docs/api/power-monitor.md index 1bfceaf3984..0f5e5b53ac7 100644 --- a/docs/api/power-monitor.md +++ b/docs/api/power-monitor.md @@ -65,3 +65,18 @@ before considered idle. `locked` is available on supported systems only. Returns `Integer` - Idle time in seconds Calculate system idle time in seconds. + +### `powerMonitor.isOnBatteryPower()` + +Returns `Boolean` - Whether the system is on battery power. + +To monitor for changes in this property, use the `on-battery` and `on-ac` +events. + +## Properties + +### `powerMonitor.onBatteryPower` + +A `Boolean` property. True if the system is on battery power. + +See [`powerMonitor.isOnBatteryPower()`](#powermonitorisonbatterypower). diff --git a/lib/browser/api/power-monitor.ts b/lib/browser/api/power-monitor.ts index d07e773f32f..02fdde2ed56 100644 --- a/lib/browser/api/power-monitor.ts +++ b/lib/browser/api/power-monitor.ts @@ -4,7 +4,8 @@ import { app } from 'electron/main'; const { createPowerMonitor, getSystemIdleState, - getSystemIdleTime + getSystemIdleTime, + isOnBatteryPower } = process._linkedBinding('electron_browser_power_monitor'); class PowerMonitor extends EventEmitter { @@ -45,6 +46,14 @@ class PowerMonitor extends EventEmitter { getSystemIdleTime () { return getSystemIdleTime(); } + + isOnBatteryPower () { + return isOnBatteryPower(); + } + + get onBatteryPower () { + return this.isOnBatteryPower(); + } } module.exports = new PowerMonitor(); diff --git a/shell/browser/api/electron_api_power_monitor.cc b/shell/browser/api/electron_api_power_monitor.cc index 706228a953f..2a87038d386 100644 --- a/shell/browser/api/electron_api_power_monitor.cc +++ b/shell/browser/api/electron_api_power_monitor.cc @@ -136,6 +136,10 @@ int GetSystemIdleTime() { return ui::CalculateIdleTime(); } +bool IsOnBatteryPower() { + return base::PowerMonitor::IsOnBatteryPower(); +} + void Initialize(v8::Local exports, v8::Local unused, v8::Local context, @@ -147,6 +151,7 @@ void Initialize(v8::Local exports, dict.SetMethod("getSystemIdleState", base::BindRepeating(&GetSystemIdleState)); dict.SetMethod("getSystemIdleTime", base::BindRepeating(&GetSystemIdleTime)); + dict.SetMethod("isOnBatteryPower", base::BindRepeating(&IsOnBatteryPower)); } } // namespace diff --git a/spec-main/api-power-monitor-spec.ts b/spec-main/api-power-monitor-spec.ts index 9ac3644f1e0..c1f55fc5606 100644 --- a/spec-main/api-power-monitor-spec.ts +++ b/spec-main/api-power-monitor-spec.ts @@ -167,10 +167,17 @@ describe('powerMonitor', () => { }); describe('powerMonitor.getSystemIdleTime', () => { - it('notify current system idle time', () => { + it('returns current system idle time', () => { const idleTime = powerMonitor.getSystemIdleTime(); expect(idleTime).to.be.at.least(0); }); }); + + describe('powerMonitor.onBatteryPower', () => { + it('returns a boolean', () => { + expect(powerMonitor.onBatteryPower).to.be.a('boolean'); + expect(powerMonitor.isOnBatteryPower()).to.be.a('boolean'); + }); + }); }); });