2021-08-18 20:08:14 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-07 23:28:47 +00:00
|
|
|
import { ipcRenderer } from 'electron';
|
2022-06-08 22:00:32 +00:00
|
|
|
import type { MenuItemConstructorOptions } from 'electron';
|
2022-06-13 21:39:35 +00:00
|
|
|
|
2022-06-08 22:00:32 +00:00
|
|
|
import type { MenuOptionsType, MenuActionType } from '../types/menu';
|
2021-10-07 23:28:47 +00:00
|
|
|
import type { IPCEventsValuesType } from '../util/createIPCEvents';
|
|
|
|
import type { LocalizerType } from '../types/Util';
|
|
|
|
import type { LoggerType } from '../types/Logging';
|
2022-06-08 22:00:32 +00:00
|
|
|
import type { LocaleMessagesType } from '../types/I18N';
|
2021-10-07 23:28:47 +00:00
|
|
|
import type { NativeThemeType } from '../context/createNativeThemeListener';
|
|
|
|
import type { SettingType } from '../util/preload';
|
2022-06-13 21:39:35 +00:00
|
|
|
import type { RendererConfigType } from '../types/RendererConfig';
|
2022-07-05 16:44:53 +00:00
|
|
|
import { ActiveWindowService } from '../services/ActiveWindowService';
|
2022-06-13 21:39:35 +00:00
|
|
|
|
2021-10-07 23:28:47 +00:00
|
|
|
import { Bytes } from '../context/Bytes';
|
|
|
|
import { Crypto } from '../context/Crypto';
|
|
|
|
import { Timers } from '../context/Timers';
|
2021-08-18 20:08:14 +00:00
|
|
|
|
2021-10-07 23:28:47 +00:00
|
|
|
import { setupI18n } from '../util/setupI18n';
|
|
|
|
import {
|
|
|
|
getEnvironment,
|
|
|
|
parseEnvironment,
|
|
|
|
setEnvironment,
|
|
|
|
} from '../environment';
|
|
|
|
import { strictAssert } from '../util/assert';
|
|
|
|
import { createSetting } from '../util/preload';
|
|
|
|
import { initialize as initializeLogging } from '../logging/set_up_renderer_logging';
|
|
|
|
import { waitForSettingsChange } from './waitForSettingsChange';
|
|
|
|
import { createNativeThemeListener } from '../context/createNativeThemeListener';
|
2022-07-05 16:44:53 +00:00
|
|
|
import { isWindows, isLinux, isMacOS, hasCustomTitleBar } from '../OS';
|
|
|
|
|
|
|
|
const activeWindowService = new ActiveWindowService();
|
|
|
|
activeWindowService.initialize(window.document, ipcRenderer);
|
2021-08-18 20:08:14 +00:00
|
|
|
|
2022-06-13 21:39:35 +00:00
|
|
|
const params = new URLSearchParams(document.location.search);
|
|
|
|
const configParam = params.get('config');
|
|
|
|
strictAssert(typeof configParam === 'string', 'config is not a string');
|
|
|
|
const config = JSON.parse(configParam);
|
|
|
|
|
2021-10-07 23:28:47 +00:00
|
|
|
const { locale } = config;
|
|
|
|
strictAssert(locale, 'locale could not be parsed from config');
|
|
|
|
strictAssert(typeof locale === 'string', 'locale is not a string');
|
|
|
|
|
|
|
|
const localeMessages = ipcRenderer.sendSync('locale-data');
|
|
|
|
setEnvironment(parseEnvironment(config.environment));
|
|
|
|
|
|
|
|
strictAssert(Boolean(window.SignalContext), 'context must be defined');
|
|
|
|
|
|
|
|
initializeLogging();
|
|
|
|
|
2022-06-08 22:00:32 +00:00
|
|
|
export type MainWindowStatsType = Readonly<{
|
|
|
|
isMaximized: boolean;
|
|
|
|
isFullScreen: boolean;
|
|
|
|
}>;
|
|
|
|
|
2021-10-07 23:28:47 +00:00
|
|
|
export type SignalContextType = {
|
|
|
|
bytes: Bytes;
|
|
|
|
crypto: Crypto;
|
|
|
|
timers: Timers;
|
|
|
|
nativeThemeListener: NativeThemeType;
|
|
|
|
setIsCallActive: (isCallActive: boolean) => unknown;
|
|
|
|
|
2022-07-05 16:44:53 +00:00
|
|
|
activeWindowService: typeof activeWindowService;
|
2021-10-07 23:28:47 +00:00
|
|
|
Settings: {
|
|
|
|
themeSetting: SettingType<IPCEventsValuesType['themeSetting']>;
|
|
|
|
waitForChange: () => Promise<void>;
|
|
|
|
};
|
2022-06-09 16:21:17 +00:00
|
|
|
OS: {
|
2022-06-15 18:21:03 +00:00
|
|
|
platform: string;
|
2022-06-09 16:21:17 +00:00
|
|
|
isWindows: typeof isWindows;
|
|
|
|
isLinux: typeof isLinux;
|
|
|
|
isMacOS: typeof isMacOS;
|
2022-07-05 16:44:53 +00:00
|
|
|
hasCustomTitleBar: typeof hasCustomTitleBar;
|
2022-06-09 16:21:17 +00:00
|
|
|
};
|
2022-06-13 21:39:35 +00:00
|
|
|
config: RendererConfigType;
|
2021-10-07 23:28:47 +00:00
|
|
|
getAppInstance: () => string | undefined;
|
|
|
|
getEnvironment: () => string;
|
|
|
|
getNodeVersion: () => string;
|
|
|
|
getVersion: () => string;
|
2022-03-11 22:49:16 +00:00
|
|
|
getPath: (name: 'userData' | 'home') => string;
|
2021-10-07 23:28:47 +00:00
|
|
|
i18n: LocalizerType;
|
2022-06-08 22:00:32 +00:00
|
|
|
localeMessages: LocaleMessagesType;
|
2021-10-07 23:28:47 +00:00
|
|
|
log: LoggerType;
|
|
|
|
renderWindow?: () => void;
|
2022-06-08 22:00:32 +00:00
|
|
|
executeMenuRole: (role: MenuItemConstructorOptions['role']) => Promise<void>;
|
|
|
|
getMainWindowStats: () => Promise<MainWindowStatsType>;
|
|
|
|
getMenuOptions: () => Promise<MenuOptionsType>;
|
|
|
|
executeMenuAction: (action: MenuActionType) => Promise<void>;
|
2021-10-07 23:28:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const SignalContext: SignalContextType = {
|
2022-07-05 16:44:53 +00:00
|
|
|
activeWindowService,
|
2021-10-07 23:28:47 +00:00
|
|
|
Settings: {
|
|
|
|
themeSetting: createSetting('themeSetting', { setter: false }),
|
|
|
|
waitForChange: waitForSettingsChange,
|
|
|
|
},
|
2022-06-09 16:21:17 +00:00
|
|
|
OS: {
|
2022-06-15 18:21:03 +00:00
|
|
|
platform: process.platform,
|
2022-06-09 16:21:17 +00:00
|
|
|
isWindows,
|
|
|
|
isLinux,
|
|
|
|
isMacOS,
|
2022-07-05 16:44:53 +00:00
|
|
|
hasCustomTitleBar,
|
2022-06-09 16:21:17 +00:00
|
|
|
},
|
2021-10-07 23:28:47 +00:00
|
|
|
bytes: new Bytes(),
|
|
|
|
config,
|
|
|
|
crypto: new Crypto(),
|
|
|
|
getAppInstance: (): string | undefined =>
|
|
|
|
config.appInstance ? String(config.appInstance) : undefined,
|
|
|
|
getEnvironment,
|
2022-06-13 21:39:35 +00:00
|
|
|
getNodeVersion: (): string => String(config.nodeVersion),
|
2021-10-07 23:28:47 +00:00
|
|
|
getVersion: (): string => String(config.version),
|
2022-03-11 22:49:16 +00:00
|
|
|
getPath: (name: 'userData' | 'home'): string => {
|
2021-10-27 17:54:16 +00:00
|
|
|
return String(config[`${name}Path`]);
|
|
|
|
},
|
2021-10-07 23:28:47 +00:00
|
|
|
i18n: setupI18n(locale, localeMessages),
|
2022-06-08 22:00:32 +00:00
|
|
|
localeMessages,
|
2021-10-07 23:28:47 +00:00
|
|
|
log: window.SignalContext.log,
|
|
|
|
nativeThemeListener: createNativeThemeListener(ipcRenderer, window),
|
|
|
|
setIsCallActive(isCallActive: boolean): void {
|
|
|
|
ipcRenderer.send('set-is-call-active', isCallActive);
|
|
|
|
},
|
|
|
|
timers: new Timers(),
|
2022-06-08 22:00:32 +00:00
|
|
|
async executeMenuRole(
|
|
|
|
role: MenuItemConstructorOptions['role']
|
|
|
|
): Promise<void> {
|
|
|
|
await ipcRenderer.invoke('executeMenuRole', role);
|
|
|
|
},
|
|
|
|
async getMainWindowStats(): Promise<MainWindowStatsType> {
|
|
|
|
return ipcRenderer.invoke('getMainWindowStats');
|
|
|
|
},
|
|
|
|
async getMenuOptions(): Promise<MenuOptionsType> {
|
|
|
|
return ipcRenderer.invoke('getMenuOptions');
|
|
|
|
},
|
|
|
|
async executeMenuAction(action: MenuActionType): Promise<void> {
|
|
|
|
return ipcRenderer.invoke('executeMenuAction', action);
|
|
|
|
},
|
2021-10-07 23:28:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
window.SignalContext = SignalContext;
|
2022-06-14 01:48:07 +00:00
|
|
|
window.i18n = SignalContext.i18n;
|