electron#powerSaveBlocker TypeScript Examples

The following examples show how to use electron#powerSaveBlocker. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: main.ts    From tuxedo-control-center with GNU General Public License v3.0 4 votes vote down vote up
app.whenReady().then( async () => {
    try {
        const systemLanguageId = app.getLocale().substring(0, 2);
        if (await userConfig.get('langId') === undefined) {
            if (availableLanguages.includes(systemLanguageId)) {
                userConfig.set('langId', systemLanguageId);
            } else {
                userConfig.set('langId', availableLanguages[0]);
            }
        }
    } catch (err) {
        console.log('Error determining user language => ' + err);
        quitCurrentTccSession();
    }

    if (startTCCAccelerator !== 'none') {
        const success = globalShortcut.register(startTCCAccelerator, () => {
            activateTccGui();
        });
        if (!success) { console.log('Failed to register global shortcut'); }
    }

    tray.state.tccGUIVersion = 'v' + app.getVersion();
    tray.state.isAutostartTrayInstalled = isAutostartTrayInstalled();
    tray.state.primeQuery = primeSelectQuery();
    tray.state.isPrimeSupported = primeSupported();
    await updateTrayProfiles();
    tray.events.startTCCClick = () => activateTccGui();
    tray.events.exitClick = () => quitCurrentTccSession();
    tray.events.autostartTrayToggle = () => {
        if (tray.state.isAutostartTrayInstalled) {
            removeAutostartTray();
        } else {
            installAutostartTray();
        }
        tray.state.isAutostartTrayInstalled = isAutostartTrayInstalled();
        tray.create();
    };
    const messageBoxprimeSelectAccept = {
        type: 'question',
        buttons: [ 'yes', 'cancel' ],
        message: 'Change graphics configuration and shutdown?'
    };
    tray.events.selectNvidiaClick = () => {
        if (dialog.showMessageBoxSync(messageBoxprimeSelectAccept) === 0) { primeSelectSet('on'); }
    };
    tray.events.selectBuiltInClick = () => {
        if (dialog.showMessageBoxSync(messageBoxprimeSelectAccept) === 0) { primeSelectSet('off'); }
    };
    tray.events.profileClick = (profileName: string) => { setTempProfile(profileName); };
    tray.create();

    tray.state.powersaveBlockerActive = powersaveBlockerId !== undefined && powerSaveBlocker.isStarted(powersaveBlockerId);
    tray.events.powersaveBlockerClick = () => {
        if (powersaveBlockerId !== undefined && powerSaveBlocker.isStarted(powersaveBlockerId)) {
            powerSaveBlocker.stop(powersaveBlockerId);
        } else {
            powersaveBlockerId = powerSaveBlocker.start('prevent-display-sleep');
        }
        tray.state.powersaveBlockerActive = powerSaveBlocker.isStarted(powersaveBlockerId);
        tray.create();
    }

    if (!trayOnlyOption) {
        activateTccGui();
    }

    tccDBus = new TccDBusController();
    tccDBus.init().then(() => {
        if (!noTccdVersionCheck) {
            // Regularly check if running tccd version is different to running gui version
            const tccdVersionCheckInterval = 5000;
            setInterval(async () => {
                if (await tccDBus.tuxedoWmiAvailable()) {
                    const tccdVersion = await tccDBus.tccdVersion();
                    if (tccdVersion.length > 0 && tccdVersion !== app.getVersion()) {
                        console.log('Other tccd version detected, restarting..');
                        process.on('exit', function () {
                            child_process.spawn(
                                process.argv[0],
                                process.argv.slice(1).concat(['--tray']),
                                {
                                    cwd: process.cwd(),
                                    detached : true,
                                    stdio: "inherit"
                                }
                            );
                        });
                        process.exit();
                    }
                }
            }, tccdVersionCheckInterval);
        }

        tccDBus.consumeModeReapplyPending().then((result) => {
            if (result) {
                child_process.exec("xset dpms force off && xset dpms force on");
            }
        });
        tccDBus.onModeReapplyPendingChanged(() => {
            tccDBus.consumeModeReapplyPending().then((result) => {
                if (result) {
                    child_process.exec("xset dpms force off && xset dpms force on");
                }
            });
        });
    });

    const profilesCheckInterval = 4000;
    setInterval(async () => { updateTrayProfiles(); }, profilesCheckInterval);
});