feat: add powerMonitor.onBatteryPower (#26494)

This commit is contained in:
Jeremy Rose 2020-11-16 16:31:46 -08:00 committed by GitHub
parent 8594b55dee
commit a9924e1c32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 2 deletions

View file

@ -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).

View file

@ -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();

View file

@ -136,6 +136,10 @@ int GetSystemIdleTime() {
return ui::CalculateIdleTime();
}
bool IsOnBatteryPower() {
return base::PowerMonitor::IsOnBatteryPower();
}
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
@ -147,6 +151,7 @@ void Initialize(v8::Local<v8::Object> exports,
dict.SetMethod("getSystemIdleState",
base::BindRepeating(&GetSystemIdleState));
dict.SetMethod("getSystemIdleTime", base::BindRepeating(&GetSystemIdleTime));
dict.SetMethod("isOnBatteryPower", base::BindRepeating(&IsOnBatteryPower));
}
} // namespace

View file

@ -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');
});
});
});
});