signal-desktop/ts/windows/minimalContext.ts

88 lines
3.3 KiB
TypeScript
Raw Normal View History

// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { MenuItemConstructorOptions } from 'electron';
import { ipcRenderer } from 'electron';
import type { MenuOptionsType } from '../types/menu';
2024-03-21 16:35:54 +00:00
import type { LocaleEmojiListType } from '../types/emoji';
import { LocaleEmojiListSchema } from '../types/emoji';
import type { MainWindowStatsType, MinimalSignalContextType } from './context';
import { activeWindowService } from '../context/activeWindowService';
import { config } from '../context/config';
import { createNativeThemeListener } from '../context/createNativeThemeListener';
import { createSetting } from '../util/preload';
import { environment } from '../context/environment';
import {
localeDisplayNames,
countryDisplayNames,
localeMessages,
} from '../context/localeMessages';
import { waitForSettingsChange } from '../context/waitForSettingsChange';
import { isTestOrMockEnvironment } from '../environment';
2024-03-21 16:35:54 +00:00
const emojiListCache = new Map<string, LocaleEmojiListType>();
export const MinimalSignalContext: MinimalSignalContextType = {
activeWindowService,
config,
async executeMenuRole(
role: MenuItemConstructorOptions['role']
): Promise<void> {
await ipcRenderer.invoke('executeMenuRole', role);
},
getAppInstance: (): string | undefined =>
config.appInstance ? String(config.appInstance) : undefined,
getEnvironment: () => environment,
getNodeVersion: (): string => String(config.nodeVersion),
2023-08-01 16:06:29 +00:00
getPath: (name: 'userData' | 'home' | 'install'): string => {
return String(config[`${name}Path`]);
},
getVersion: (): string => String(config.version),
async getMainWindowStats(): Promise<MainWindowStatsType> {
return ipcRenderer.invoke('getMainWindowStats');
},
async getMenuOptions(): Promise<MenuOptionsType> {
return ipcRenderer.invoke('getMenuOptions');
},
2024-03-21 16:35:54 +00:00
async getLocalizedEmojiList(locale: string) {
const cached = emojiListCache.get(locale);
if (cached) {
return cached;
}
const buf = await ipcRenderer.invoke(
'OptionalResourceService:getData',
`emoji-index-${locale}.json`
);
const json = JSON.parse(Buffer.from(buf).toString());
const result = LocaleEmojiListSchema.parse(json);
emojiListCache.set(locale, result);
return result;
},
2023-11-06 21:19:23 +00:00
getI18nAvailableLocales: () => config.availableLocales,
getI18nLocale: () => config.resolvedTranslationsLocale,
getI18nLocaleMessages: () => localeMessages,
getLocaleDisplayNames: () => localeDisplayNames,
getCountryDisplayNames: () => countryDisplayNames,
getResolvedMessagesLocale: () => config.resolvedTranslationsLocale,
getResolvedMessagesLocaleDirection: () =>
config.resolvedTranslationsLocaleDirection,
getHourCyclePreference: () => config.hourCyclePreference,
getPreferredSystemLocales: () => config.preferredSystemLocales,
getLocaleOverride: () => config.localeOverride,
isTestOrMockEnvironment,
nativeThemeListener: createNativeThemeListener(ipcRenderer, window),
2023-11-06 21:19:23 +00:00
restartApp: () => ipcRenderer.send('restart'),
OS: {
getClassName: () => ipcRenderer.sendSync('OS.getClassName'),
platform: process.platform,
release: config.osRelease,
},
Settings: {
themeSetting: createSetting('themeSetting', { setter: false }),
waitForChange: waitForSettingsChange,
},
};