Skip to content

Commit b5c5907

Browse files
miyaCopilotnirinchev
authored
feat(compass, preferences): implement zoom level persistence COMPASS-9905 (#7363)
* feat: add optional zoom level to InternalUserPreferences * feat: implement debounced zoom level saving and restoration * fix: simplify zoom level setting by removing validation checks * fix: validate zoom level before restoration to ensure it falls within the acceptable range * fix: remove console log for restoring zoom level * Update packages/compass/src/app/application.tsx Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix: prevent memory leaks in zoom timeout handling * imprv: execute zoom process first * Update packages/compass/src/app/application.tsx Co-authored-by: Nikola Irinchev <irinchev@me.com> * fix: clarify comment for zoom level clamping * Revert "fix: clarify comment for zoom level clamping" This reverts commit bd6d862. * fix: clarify comment for zoom level clamping * fix: simplify zoom level clamping code * fix: zoomLevel saving process runs in the background --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Nikola Irinchev <irinchev@me.com>
1 parent e3c783e commit b5c5907

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

packages/compass-preferences-model/src/preferences-schema.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export type InternalUserPreferences = {
125125
// TODO: Remove this as part of COMPASS-8970.
126126
enableConnectInNewWindow: boolean;
127127
showEndOfLifeConnectionModal: boolean;
128+
zoomLevel?: number;
128129
};
129130

130131
// UserPreferences contains all preferences stored to disk.
@@ -459,6 +460,17 @@ export const storedUserPreferencesProps: Required<{
459460
),
460461
type: 'boolean',
461462
},
463+
/**
464+
* Zoom level for restoring browser zoom state.
465+
*/
466+
zoomLevel: {
467+
ui: false,
468+
cli: false,
469+
global: false,
470+
description: null,
471+
validator: z.number().optional(),
472+
type: 'number',
473+
},
462474
/**
463475
* Enable/disable the AI services. This is currently set
464476
* in the atlas-service initialization where we make a request to the

packages/compass/src/app/application.tsx

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,27 +367,66 @@ class Application {
367367
const ZOOM_INCREMENT = 0.5;
368368
const ZOOM_MAX = 5;
369369
const ZOOM_MIN = -3;
370+
const SAVE_DEBOUNCE_DELAY = 500; // 500ms delay for save operations
371+
372+
// Debounced save zoom level to preferences
373+
let saveTimeoutId: NodeJS.Timeout | null = null;
374+
const debouncedSaveZoomLevel = (zoomLevel: number) => {
375+
if (saveTimeoutId) {
376+
clearTimeout(saveTimeoutId);
377+
}
378+
379+
saveTimeoutId = setTimeout(() => {
380+
void defaultPreferencesInstance.savePreferences({ zoomLevel });
381+
saveTimeoutId = null;
382+
}, SAVE_DEBOUNCE_DELAY);
383+
};
384+
385+
const restoreZoomLevel = () => {
386+
try {
387+
const preferences = defaultPreferencesInstance.getPreferences();
388+
const savedZoomLevel = preferences.zoomLevel ?? ZOOM_DEFAULT;
389+
390+
// Clamp zoom level to allowed range
391+
const zoomLevel = Math.min(
392+
Math.max(savedZoomLevel, ZOOM_MIN),
393+
ZOOM_MAX
394+
);
395+
396+
webFrame.setZoomLevel(zoomLevel);
397+
} catch {
398+
// noop
399+
}
400+
};
370401

371402
const zoomReset = () => {
372-
return webFrame.setZoomLevel(ZOOM_DEFAULT);
403+
webFrame.setZoomLevel(ZOOM_DEFAULT);
404+
debouncedSaveZoomLevel(ZOOM_DEFAULT);
373405
};
406+
374407
const zoomIn = () => {
375408
const currentZoomLevel = webFrame.getZoomLevel();
376409
const newZoomLevel = Math.min(
377410
currentZoomLevel + ZOOM_INCREMENT,
378411
ZOOM_MAX
379412
);
380-
return webFrame.setZoomLevel(newZoomLevel);
413+
webFrame.setZoomLevel(newZoomLevel);
414+
debouncedSaveZoomLevel(newZoomLevel);
381415
};
416+
382417
const zoomOut = () => {
383418
const currentZoomLevel = webFrame.getZoomLevel();
384419
const newZoomLevel = Math.max(
385420
currentZoomLevel - ZOOM_INCREMENT,
386421
ZOOM_MIN
387422
);
388-
return webFrame.setZoomLevel(newZoomLevel);
423+
webFrame.setZoomLevel(newZoomLevel);
424+
debouncedSaveZoomLevel(newZoomLevel);
389425
};
390426

427+
// Restore zoom level on startup
428+
restoreZoomLevel();
429+
391430
ipcRenderer?.on('window:zoom-reset', zoomReset);
392431
ipcRenderer?.on('window:zoom-in', zoomIn);
393432
ipcRenderer?.on('window:zoom-out', zoomOut);

0 commit comments

Comments
 (0)