signal-desktop/ts/windows/settings/preload.ts

398 lines
14 KiB
TypeScript
Raw Normal View History

2022-06-23 19:15:27 +00:00
// Copyright 2021-2022 Signal Messenger, LLC
2021-08-18 20:08:14 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import React from 'react';
import ReactDOM from 'react-dom';
import { contextBridge, ipcRenderer } from 'electron';
2021-08-18 20:08:14 +00:00
import { SignalContext } from '../context';
2021-08-18 20:08:14 +00:00
import * as Settings from '../../types/Settings';
import { Preferences } from '../../components/Preferences';
2021-08-18 20:08:14 +00:00
import {
SystemTraySetting,
parseSystemTraySetting,
shouldMinimizeToSystemTray,
} from '../../types/SystemTraySetting';
import { awaitObject } from '../../util/awaitObject';
2022-11-16 20:18:02 +00:00
import { DurationInSeconds } from '../../util/durations';
2021-08-18 20:08:14 +00:00
import { createSetting, createCallback } from '../../util/preload';
import { startInteractionMode } from '../startInteractionMode';
2021-08-18 20:08:14 +00:00
function doneRendering() {
ipcRenderer.send('settings-done-rendering');
}
2021-08-18 20:08:14 +00:00
const settingAudioNotification = createSetting('audioNotification');
const settingAutoDownloadUpdate = createSetting('autoDownloadUpdate');
2021-08-18 20:08:14 +00:00
const settingAutoLaunch = createSetting('autoLaunch');
const settingCallRingtoneNotification = createSetting(
'callRingtoneNotification'
);
const settingCallSystemNotification = createSetting('callSystemNotification');
const settingCountMutedConversations = createSetting('countMutedConversations');
const settingDeviceName = createSetting('deviceName', { setter: false });
const settingHideMenuBar = createSetting('hideMenuBar');
const settingIncomingCallNotification = createSetting(
'incomingCallNotification'
);
const settingMediaCameraPermissions = createSetting('mediaCameraPermissions');
const settingMediaPermissions = createSetting('mediaPermissions');
const settingNotificationDrawAttention = createSetting(
'notificationDrawAttention'
);
const settingNotificationSetting = createSetting('notificationSetting');
const settingRelayCalls = createSetting('alwaysRelayCalls');
const settingSpellCheck = createSetting('spellCheck');
const settingSentMediaQuality = createSetting('sentMediaQualitySetting');
2021-08-18 20:08:14 +00:00
const settingTheme = createSetting('themeSetting');
const settingSystemTraySetting = createSetting('systemTraySetting');
const settingLastSyncTime = createSetting('lastSyncTime');
const settingHasStoriesDisabled = createSetting('hasStoriesDisabled');
2021-08-18 20:08:14 +00:00
const settingZoomFactor = createSetting('zoomFactor');
// Getters only.
const settingBlockedCount = createSetting('blockedCount');
const settingLinkPreview = createSetting('linkPreviewSetting', {
setter: false,
});
const settingPhoneNumberDiscoverability = createSetting(
'phoneNumberDiscoverabilitySetting',
{ setter: false }
);
const settingPhoneNumberSharing = createSetting('phoneNumberSharingSetting', {
setter: false,
});
const settingReadReceipts = createSetting('readReceiptSetting', {
setter: false,
});
const settingTypingIndicators = createSetting('typingIndicatorSetting', {
setter: false,
});
// Media settings
const settingAudioInput = createSetting('preferredAudioInputDevice');
const settingAudioOutput = createSetting('preferredAudioOutputDevice');
const settingVideoInput = createSetting('preferredVideoInputDevice');
const settingUniversalExpireTimer = createSetting('universalExpireTimer');
// Callbacks
const ipcGetAvailableIODevices = createCallback('getAvailableIODevices');
const ipcGetCustomColors = createCallback('getCustomColors');
const ipcIsSyncNotSupported = createCallback('isPrimary');
const ipcMakeSyncRequest = createCallback('syncRequest');
2021-08-19 01:04:38 +00:00
const ipcPNP = createCallback('isPhoneNumberSharingEnabled');
2022-07-20 00:47:05 +00:00
const ipcShouldShowStoriesSettings = createCallback(
'shouldShowStoriesSettings'
);
const ipcDeleteAllMyStories = createCallback('deleteAllMyStories');
2021-08-18 20:08:14 +00:00
// ChatColorPicker redux hookups
// The redux actions update over IPC through a preferences re-render
const ipcGetDefaultConversationColor = createCallback(
'getDefaultConversationColor'
);
const ipcGetConversationsWithCustomColor = createCallback(
'getConversationsWithCustomColor'
);
const ipcAddCustomColor = createCallback('addCustomColor');
const ipcEditCustomColor = createCallback('editCustomColor');
const ipcRemoveCustomColor = createCallback('removeCustomColor');
const ipcRemoveCustomColorOnConversations = createCallback(
'removeCustomColorOnConversations'
);
const ipcResetAllChatColors = createCallback('resetAllChatColors');
const ipcResetDefaultChatColor = createCallback('resetDefaultChatColor');
const ipcSetGlobalDefaultConversationColor = createCallback(
'setGlobalDefaultConversationColor'
);
const DEFAULT_NOTIFICATION_SETTING = 'message';
2021-11-11 22:43:05 +00:00
function getSystemTraySettingValues(systemTraySetting: SystemTraySetting): {
2021-08-18 20:08:14 +00:00
hasMinimizeToAndStartInSystemTray: boolean;
hasMinimizeToSystemTray: boolean;
} {
const parsedSystemTraySetting = parseSystemTraySetting(systemTraySetting);
const hasMinimizeToAndStartInSystemTray =
parsedSystemTraySetting ===
SystemTraySetting.MinimizeToAndStartInSystemTray;
const hasMinimizeToSystemTray = shouldMinimizeToSystemTray(
parsedSystemTraySetting
);
return {
hasMinimizeToAndStartInSystemTray,
hasMinimizeToSystemTray,
};
}
const renderPreferences = async () => {
startInteractionMode();
2021-08-18 20:08:14 +00:00
const {
blockedCount,
deviceName,
hasAudioNotifications,
hasAutoDownloadUpdate,
2021-08-18 20:08:14 +00:00
hasAutoLaunch,
hasCallNotifications,
hasCallRingtoneNotification,
hasCountMutedConversations,
hasHideMenuBar,
hasIncomingCallNotifications,
hasLinkPreviews,
hasMediaCameraPermissions,
hasMediaPermissions,
hasNotificationAttention,
hasReadReceipts,
hasRelayCalls,
hasSpellCheck,
hasStoriesDisabled,
2021-08-18 20:08:14 +00:00
hasTypingIndicators,
2021-08-19 01:04:38 +00:00
isPhoneNumberSharingSupported,
2021-08-18 20:08:14 +00:00
lastSyncTime,
notificationContent,
selectedCamera,
selectedMicrophone,
selectedSpeaker,
sentMediaQualitySetting,
2021-08-18 20:08:14 +00:00
systemTraySetting,
themeSetting,
universalExpireTimer,
whoCanFindMe,
whoCanSeeMe,
zoomFactor,
availableIODevices,
customColors,
defaultConversationColor,
2022-07-20 00:47:05 +00:00
isSyncNotSupported,
shouldShowStoriesSettings,
2021-08-18 20:08:14 +00:00
} = await awaitObject({
blockedCount: settingBlockedCount.getValue(),
deviceName: settingDeviceName.getValue(),
hasAudioNotifications: settingAudioNotification.getValue(),
hasAutoDownloadUpdate: settingAutoDownloadUpdate.getValue(),
2021-08-18 20:08:14 +00:00
hasAutoLaunch: settingAutoLaunch.getValue(),
hasCallNotifications: settingCallSystemNotification.getValue(),
hasCallRingtoneNotification: settingCallRingtoneNotification.getValue(),
hasCountMutedConversations: settingCountMutedConversations.getValue(),
hasHideMenuBar: settingHideMenuBar.getValue(),
hasIncomingCallNotifications: settingIncomingCallNotification.getValue(),
hasLinkPreviews: settingLinkPreview.getValue(),
hasMediaCameraPermissions: settingMediaCameraPermissions.getValue(),
hasMediaPermissions: settingMediaPermissions.getValue(),
hasNotificationAttention: settingNotificationDrawAttention.getValue(),
hasReadReceipts: settingReadReceipts.getValue(),
hasRelayCalls: settingRelayCalls.getValue(),
hasSpellCheck: settingSpellCheck.getValue(),
hasStoriesDisabled: settingHasStoriesDisabled.getValue(),
2021-08-18 20:08:14 +00:00
hasTypingIndicators: settingTypingIndicators.getValue(),
2021-08-19 01:04:38 +00:00
isPhoneNumberSharingSupported: ipcPNP(),
2021-08-18 20:08:14 +00:00
lastSyncTime: settingLastSyncTime.getValue(),
notificationContent: settingNotificationSetting.getValue(),
selectedCamera: settingVideoInput.getValue(),
selectedMicrophone: settingAudioInput.getValue(),
selectedSpeaker: settingAudioOutput.getValue(),
sentMediaQualitySetting: settingSentMediaQuality.getValue(),
2021-08-18 20:08:14 +00:00
systemTraySetting: settingSystemTraySetting.getValue(),
themeSetting: settingTheme.getValue(),
universalExpireTimer: settingUniversalExpireTimer.getValue(),
whoCanFindMe: settingPhoneNumberDiscoverability.getValue(),
whoCanSeeMe: settingPhoneNumberSharing.getValue(),
zoomFactor: settingZoomFactor.getValue(),
// Callbacks
availableIODevices: ipcGetAvailableIODevices(),
customColors: ipcGetCustomColors(),
defaultConversationColor: ipcGetDefaultConversationColor(),
2022-07-20 00:47:05 +00:00
isSyncNotSupported: ipcIsSyncNotSupported(),
shouldShowStoriesSettings: ipcShouldShowStoriesSettings(),
2021-08-18 20:08:14 +00:00
});
2021-11-11 22:43:05 +00:00
const { availableCameras, availableMicrophones, availableSpeakers } =
availableIODevices;
2021-08-18 20:08:14 +00:00
2021-11-11 22:43:05 +00:00
const { hasMinimizeToAndStartInSystemTray, hasMinimizeToSystemTray } =
getSystemTraySettingValues(systemTraySetting);
2021-08-18 20:08:14 +00:00
2022-11-16 20:18:02 +00:00
const onUniversalExpireTimerChange = reRender(
settingUniversalExpireTimer.setValue
);
2021-08-18 20:08:14 +00:00
const props = {
// Settings
availableCameras,
availableMicrophones,
availableSpeakers,
blockedCount,
customColors,
defaultConversationColor,
deviceName,
hasAudioNotifications,
hasAutoDownloadUpdate,
2021-08-18 20:08:14 +00:00
hasAutoLaunch,
hasCallNotifications,
hasCallRingtoneNotification,
hasCountMutedConversations,
hasHideMenuBar,
hasIncomingCallNotifications,
hasLinkPreviews,
hasMediaCameraPermissions,
hasMediaPermissions,
hasMinimizeToAndStartInSystemTray,
hasMinimizeToSystemTray,
hasNotificationAttention,
hasNotifications: notificationContent !== 'off',
hasReadReceipts,
hasRelayCalls,
hasSpellCheck,
hasStoriesDisabled,
2021-08-18 20:08:14 +00:00
hasTypingIndicators,
lastSyncTime,
notificationContent,
selectedCamera,
selectedMicrophone,
selectedSpeaker,
sentMediaQualitySetting,
2021-08-18 20:08:14 +00:00
themeSetting,
2022-11-16 20:18:02 +00:00
universalExpireTimer: DurationInSeconds.fromSeconds(universalExpireTimer),
2021-08-18 20:08:14 +00:00
whoCanFindMe,
whoCanSeeMe,
zoomFactor,
// Actions and other props
addCustomColor: ipcAddCustomColor,
closeSettings: () => SignalContext.executeMenuRole('close'),
2021-08-18 20:08:14 +00:00
doDeleteAllData: () => ipcRenderer.send('delete-all-data'),
doneRendering,
2021-08-18 20:08:14 +00:00
editCustomColor: ipcEditCustomColor,
getConversationsWithCustomColor: ipcGetConversationsWithCustomColor,
initialSpellCheckSetting:
SignalContext.config.appStartInitialSpellcheckSetting,
2021-08-18 20:08:14 +00:00
makeSyncRequest: ipcMakeSyncRequest,
removeCustomColor: ipcRemoveCustomColor,
removeCustomColorOnConversations: ipcRemoveCustomColorOnConversations,
resetAllChatColors: ipcResetAllChatColors,
resetDefaultChatColor: ipcResetDefaultChatColor,
setGlobalDefaultConversationColor: ipcSetGlobalDefaultConversationColor,
2022-07-20 00:47:05 +00:00
shouldShowStoriesSettings,
2021-08-18 20:08:14 +00:00
// Limited support features
isAudioNotificationsSupported: Settings.isAudioNotificationSupported(),
isAutoDownloadUpdatesSupported: Settings.isAutoDownloadUpdatesSupported(),
2021-08-18 20:08:14 +00:00
isAutoLaunchSupported: Settings.isAutoLaunchSupported(),
isHideMenuBarSupported: Settings.isHideMenuBarSupported(),
isNotificationAttentionSupported: Settings.isDrawAttentionSupported(),
2021-08-19 01:04:38 +00:00
isPhoneNumberSharingSupported,
2021-08-18 20:08:14 +00:00
isSyncSupported: !isSyncNotSupported,
isSystemTraySupported: Settings.isSystemTraySupported(
SignalContext.getVersion()
),
2022-09-06 22:09:52 +00:00
isMinimizeToAndStartInSystemTraySupported:
Settings.isMinimizeToAndStartInSystemTraySupported(
SignalContext.getVersion()
),
2021-08-18 20:08:14 +00:00
// Change handlers
onAudioNotificationsChange: reRender(settingAudioNotification.setValue),
onAutoDownloadUpdateChange: reRender(settingAutoDownloadUpdate.setValue),
2021-08-18 20:08:14 +00:00
onAutoLaunchChange: reRender(settingAutoLaunch.setValue),
onCallNotificationsChange: reRender(settingCallSystemNotification.setValue),
onCallRingtoneNotificationChange: reRender(
settingCallRingtoneNotification.setValue
),
onCountMutedConversationsChange: reRender(
settingCountMutedConversations.setValue
),
onHasStoriesDisabledChanged: reRender(async (value: boolean) => {
await settingHasStoriesDisabled.setValue(value);
if (!value) {
void ipcDeleteAllMyStories();
}
return value;
}),
2021-08-18 20:08:14 +00:00
onHideMenuBarChange: reRender(settingHideMenuBar.setValue),
onIncomingCallNotificationsChange: reRender(
settingIncomingCallNotification.setValue
),
onLastSyncTimeChange: reRender(settingLastSyncTime.setValue),
onMediaCameraPermissionsChange: reRender(
settingMediaCameraPermissions.setValue
),
onMinimizeToAndStartInSystemTrayChange: reRender(async (value: boolean) => {
await settingSystemTraySetting.setValue(
value
? SystemTraySetting.MinimizeToAndStartInSystemTray
: SystemTraySetting.MinimizeToSystemTray
);
return value;
}),
onMinimizeToSystemTrayChange: reRender(async (value: boolean) => {
await settingSystemTraySetting.setValue(
value
? SystemTraySetting.MinimizeToSystemTray
: SystemTraySetting.DoNotUseSystemTray
);
return value;
}),
onMediaPermissionsChange: reRender(settingMediaPermissions.setValue),
onNotificationAttentionChange: reRender(
settingNotificationDrawAttention.setValue
),
onNotificationContentChange: reRender(settingNotificationSetting.setValue),
onNotificationsChange: reRender(async (value: boolean) => {
await settingNotificationSetting.setValue(
value ? DEFAULT_NOTIFICATION_SETTING : 'off'
);
return value;
}),
onRelayCallsChange: reRender(settingRelayCalls.setValue),
onSelectedCameraChange: reRender(settingVideoInput.setValue),
onSelectedMicrophoneChange: reRender(settingAudioInput.setValue),
onSelectedSpeakerChange: reRender(settingAudioOutput.setValue),
onSentMediaQualityChange: reRender(settingSentMediaQuality.setValue),
2021-08-18 20:08:14 +00:00
onSpellCheckChange: reRender(settingSpellCheck.setValue),
onThemeChange: reRender(settingTheme.setValue),
2022-11-16 20:18:02 +00:00
onUniversalExpireTimerChange: (newValue: number): Promise<void> => {
return onUniversalExpireTimerChange(
DurationInSeconds.fromSeconds(newValue)
);
},
// Zoom factor change doesn't require immediate rerender since it will:
// 1. Update the zoom factor in the main window
// 2. Trigger `preferred-size-changed` in the main process
// 3. Finally result in `window.storage` update which will cause the
// rerender.
onZoomFactorChange: settingZoomFactor.setValue,
2021-08-18 20:08:14 +00:00
i18n: SignalContext.i18n,
2022-07-05 16:44:53 +00:00
hasCustomTitleBar: SignalContext.OS.hasCustomTitleBar(),
executeMenuRole: SignalContext.executeMenuRole,
2021-08-18 20:08:14 +00:00
};
function reRender<Value>(f: (value: Value) => Promise<Value>) {
return async (value: Value) => {
await f(value);
void renderPreferences();
2021-08-18 20:08:14 +00:00
};
}
ReactDOM.render(
React.createElement(Preferences, props),
document.getElementById('app')
);
};
2021-09-29 18:37:30 +00:00
ipcRenderer.on('preferences-changed', () => renderPreferences());
contextBridge.exposeInMainWorld('SignalContext', {
...SignalContext,
renderWindow: renderPreferences,
});