2021-06-29 17:18:03 +00:00
|
|
|
// Copyright 2017-2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import * as log from '../ts/logging/log';
|
|
|
|
import {
|
|
|
|
parseSystemTraySetting,
|
|
|
|
SystemTraySetting,
|
|
|
|
} from '../ts/types/SystemTraySetting';
|
|
|
|
import { isSystemTraySupported } from '../ts/types/Settings';
|
|
|
|
import type { MainSQL } from '../ts/sql/main';
|
2021-10-20 19:56:49 +00:00
|
|
|
import type { ConfigType } from './base_config';
|
2021-06-29 17:18:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A small helper class to get and cache the `system-tray-setting` preference in the main
|
|
|
|
* process.
|
|
|
|
*/
|
|
|
|
export class SystemTraySettingCache {
|
|
|
|
private cachedValue: undefined | SystemTraySetting;
|
|
|
|
|
|
|
|
private getPromise: undefined | Promise<SystemTraySetting>;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private readonly sql: Pick<MainSQL, 'sqlCall'>,
|
2021-10-20 19:56:49 +00:00
|
|
|
private readonly ephemeralConfig: Pick<ConfigType, 'get' | 'set'>,
|
2021-06-29 19:29:40 +00:00
|
|
|
private readonly argv: Array<string>,
|
|
|
|
private readonly appVersion: string
|
2021-06-29 17:18:03 +00:00
|
|
|
) {}
|
|
|
|
|
|
|
|
async get(): Promise<SystemTraySetting> {
|
|
|
|
if (this.cachedValue !== undefined) {
|
|
|
|
return this.cachedValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getPromise = this.getPromise || this.doFirstGet();
|
|
|
|
return this.getPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
set(value: SystemTraySetting): void {
|
|
|
|
this.cachedValue = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async doFirstGet(): Promise<SystemTraySetting> {
|
|
|
|
let result: SystemTraySetting;
|
|
|
|
|
|
|
|
// These command line flags are not officially supported, but many users rely on them.
|
|
|
|
// Be careful when removing them or making changes.
|
|
|
|
if (this.argv.some(arg => arg === '--start-in-tray')) {
|
|
|
|
result = SystemTraySetting.MinimizeToAndStartInSystemTray;
|
|
|
|
log.info(
|
|
|
|
`getSystemTraySetting saw --start-in-tray flag. Returning ${result}`
|
|
|
|
);
|
|
|
|
} else if (this.argv.some(arg => arg === '--use-tray-icon')) {
|
|
|
|
result = SystemTraySetting.MinimizeToSystemTray;
|
|
|
|
log.info(
|
|
|
|
`getSystemTraySetting saw --use-tray-icon flag. Returning ${result}`
|
|
|
|
);
|
2021-06-29 19:29:40 +00:00
|
|
|
} else if (isSystemTraySupported(this.appVersion)) {
|
2021-10-20 19:56:49 +00:00
|
|
|
const fastValue = this.ephemeralConfig.get('system-tray-setting');
|
|
|
|
if (fastValue !== undefined) {
|
|
|
|
log.info('getSystemTraySetting got fast value', fastValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
const value =
|
|
|
|
fastValue ??
|
2022-11-17 00:29:15 +00:00
|
|
|
(await this.sql.sqlCall('getItemById', 'system-tray-setting'))?.value;
|
2021-06-29 17:18:03 +00:00
|
|
|
|
|
|
|
if (value !== undefined) {
|
|
|
|
result = parseSystemTraySetting(value);
|
2021-10-20 19:56:49 +00:00
|
|
|
log.info(`getSystemTraySetting returning ${result}`);
|
2021-06-29 17:18:03 +00:00
|
|
|
} else {
|
2022-09-06 22:09:52 +00:00
|
|
|
result = SystemTraySetting.Uninitialized;
|
2021-10-20 19:56:49 +00:00
|
|
|
log.info(`getSystemTraySetting got no value, returning ${result}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result !== fastValue) {
|
|
|
|
this.ephemeralConfig.set('system-tray-setting', result);
|
2021-06-29 17:18:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = SystemTraySetting.DoNotUseSystemTray;
|
|
|
|
log.info(
|
|
|
|
`getSystemTraySetting had no flags and did no DB lookups. Returning ${result}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-20 19:56:49 +00:00
|
|
|
return this.updateCachedValue(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
private updateCachedValue(value: SystemTraySetting): SystemTraySetting {
|
2021-06-29 17:18:03 +00:00
|
|
|
// If there's a value in the cache, someone has updated the value "out from under us",
|
|
|
|
// so we should return that because it's newer.
|
|
|
|
this.cachedValue =
|
2021-10-20 19:56:49 +00:00
|
|
|
this.cachedValue === undefined ? value : this.cachedValue;
|
2021-06-29 17:18:03 +00:00
|
|
|
|
|
|
|
return this.cachedValue;
|
|
|
|
}
|
|
|
|
}
|