Track zoom factor changes through IPC

This commit is contained in:
Fedor Indutny 2021-09-02 16:29:16 -07:00 committed by GitHub
parent 3e18a8a337
commit 0793aa6b43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 27 deletions

22
main.js
View file

@ -310,6 +310,27 @@ function handleCommonWindowEvents(window) {
console.error(`Preload error in ${preloadPath}: `, error.message); console.error(`Preload error in ${preloadPath}: `, error.message);
}); });
// Works only for mainWindow because it has `enablePreferredSizeMode`
let lastZoomFactor = window.webContents.getZoomFactor();
const onZoomChanged = () => {
const zoomFactor = mainWindow.webContents.getZoomFactor();
if (lastZoomFactor === zoomFactor) {
return;
}
if (window.webContents) {
window.webContents.send('callbacks:call:setPassiveZoomFactor', [
zoomFactor,
]);
if (settingsWindow && settingsWindow.webContents) {
settingsWindow.webContents.send('render');
}
}
lastZoomFactor = zoomFactor;
};
window.webContents.on('preferred-size-changed', onZoomChanged);
nativeThemeNotifier.addWindow(window); nativeThemeNotifier.addWindow(window);
} }
@ -386,6 +407,7 @@ async function createWindow() {
nativeWindowOpen: true, nativeWindowOpen: true,
spellcheck: await getSpellCheckSetting(), spellcheck: await getSpellCheckSetting(),
backgroundThrottling: false, backgroundThrottling: false,
enablePreferredSizeMode: true,
}, },
icon: windowIcon, icon: windowIcon,
..._.pick(windowConfig, ['autoHideMenuBar', 'width', 'height', 'x', 'y']), ..._.pick(windowConfig, ['autoHideMenuBar', 'width', 'height', 'x', 'y']),

View file

@ -154,6 +154,29 @@ enum Page {
ChatColor = 'ChatColor', ChatColor = 'ChatColor',
} }
const DEFAULT_ZOOM_FACTORS = [
{
text: '75%',
value: 0.75,
},
{
text: '100%',
value: 1,
},
{
text: '125%',
value: 1.25,
},
{
text: '150%',
value: 1.5,
},
{
text: '200%',
value: 2,
},
];
export const Preferences = ({ export const Preferences = ({
addCustomColor, addCustomColor,
availableCameras, availableCameras,
@ -386,6 +409,18 @@ export const Preferences = ({
</> </>
); );
} else if (page === Page.Appearance) { } else if (page === Page.Appearance) {
let zoomFactors = DEFAULT_ZOOM_FACTORS;
if (!zoomFactors.some(({ value }) => value === zoomFactor)) {
zoomFactors = [
...zoomFactors,
{
text: `${Math.round(zoomFactor * 100)}%`,
value: zoomFactor,
},
].sort((a, b) => a.value - b.value);
}
settings = ( settings = (
<> <>
<div className="Preferences__title"> <div className="Preferences__title">
@ -438,28 +473,7 @@ export const Preferences = ({
right={ right={
<Select <Select
onChange={onZoomSelectChange} onChange={onZoomSelectChange}
options={[ options={zoomFactors}
{
text: '75%',
value: 0.75,
},
{
text: '100%',
value: 1,
},
{
text: '125%',
value: 1.25,
},
{
text: '150%',
value: 1.5,
},
{
text: '200%',
value: 2,
},
]}
value={zoomFactor} value={zoomFactor}
/> />
} }

View file

@ -24,7 +24,7 @@ export type SerializedCertificateType = {
serialized: ArrayBuffer; serialized: ArrayBuffer;
}; };
export type ZoomFactorType = 0.75 | 1 | 1.25 | 1.5 | 2; export type ZoomFactorType = 0.75 | 1 | 1.25 | 1.5 | 2 | number;
export type ThemeSettingType = 'system' | 'light' | 'dark'; export type ThemeSettingType = 'system' | 'light' | 'dark';

View file

@ -107,6 +107,7 @@ export type IPCEventsCallbacksType = {
customColor?: { id: string; value: CustomColorType } customColor?: { id: string; value: CustomColorType }
) => void; ) => void;
getDefaultConversationColor: () => DefaultConversationColorType; getDefaultConversationColor: () => DefaultConversationColorType;
setPassiveZoomFactor: (factor: number) => Promise<void>;
}; };
type ValuesWithGetters = Omit< type ValuesWithGetters = Omit<
@ -166,10 +167,8 @@ export function createIPCEvents(
getDeviceName: () => window.textsecure.storage.user.getDeviceName(), getDeviceName: () => window.textsecure.storage.user.getDeviceName(),
getZoomFactor: () => window.storage.get('zoomFactor', 1), getZoomFactor: () => window.storage.get('zoomFactor', 1),
setZoomFactor: (zoomFactor: ZoomFactorType) => { setZoomFactor: async (zoomFactor: ZoomFactorType) => {
const numZoomFactor = zoomFactor; webFrame.setZoomFactor(zoomFactor);
webFrame.setZoomFactor(numZoomFactor);
return window.storage.put('zoomFactor', numZoomFactor);
}, },
getPreferredAudioInputDevice: () => getPreferredAudioInputDevice: () =>
@ -508,6 +507,9 @@ export function createIPCEvents(
getMediaPermissions: window.getMediaPermissions, getMediaPermissions: window.getMediaPermissions,
getMediaCameraPermissions: window.getMediaCameraPermissions, getMediaCameraPermissions: window.getMediaCameraPermissions,
setPassiveZoomFactor: zoomFactor =>
window.storage.put('zoomFactor', zoomFactor),
...overrideEvents, ...overrideEvents,
}; };
} }

View file

@ -16,6 +16,7 @@ installCallback('resetAllChatColors');
installCallback('resetDefaultChatColor'); installCallback('resetDefaultChatColor');
installCallback('setGlobalDefaultConversationColor'); installCallback('setGlobalDefaultConversationColor');
installCallback('getDefaultConversationColor'); installCallback('getDefaultConversationColor');
installCallback('setPassiveZoomFactor');
// Getters only. These are set by the primary device // Getters only. These are set by the primary device
installSetting('blockedCount', { installSetting('blockedCount', {