Prevent display sleep while on a call

This commit is contained in:
Evan Hahn 2021-12-10 16:53:10 -06:00 committed by GitHub
parent 34fd945f83
commit 0e3d12c457
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 152 additions and 0 deletions

View file

@ -0,0 +1,40 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { PowerSaveBlocker } from 'electron';
import * as log from '../ts/logging/log';
export class PreventDisplaySleepService {
private blockerId: undefined | number;
constructor(private powerSaveBlocker: PowerSaveBlocker) {}
setEnabled(isEnabled: boolean): void {
log.info(
`Prevent display sleep service: ${
isEnabled ? 'preventing' : 'allowing'
} display sleep`
);
if (isEnabled) {
this.enable();
} else {
this.disable();
}
}
private enable(): void {
if (this.blockerId !== undefined) {
return;
}
this.blockerId = this.powerSaveBlocker.start('prevent-display-sleep');
}
private disable(): void {
if (this.blockerId === undefined) {
return;
}
this.powerSaveBlocker.stop(this.blockerId);
delete this.blockerId;
}
}