Skip to content

Commit

Permalink
app: Remember zoom level after restarts
Browse files Browse the repository at this point in the history
This change refactors the zoom functionality in the Electron app to
remember the zoom level in the Electron app after restarts.

Fixes: #2681

Signed-off-by: Evangelos Skopelitis <eskopelitis@microsoft.com>

change
  • Loading branch information
skoeva committed Jan 15, 2025
1 parent 4b223b8 commit 7dbde4d
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions app/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,18 +767,21 @@ function getDefaultAppMenu(): AppMenu[] {
sep,
{
label: i18n.t('Reset Zoom'),
role: 'resetzoom',
id: 'original-reset-zoom',
accelerator: 'CmdOrCtrl+0',
click: () => saveZoomFactor(1.0),
},
{
label: i18n.t('Zoom In'),
role: 'zoomin',
id: 'original-zoom-in',
accelerator: 'CmdOrCtrl+Plus',
click: () => adjustZoom(0.1),
},
{
label: i18n.t('Zoom Out'),
role: 'zoomout',
id: 'original-zoom-out',
accelerator: 'CmdOrCtrl+-',
click: () => adjustZoom(-0.1),
},
sep,
{
Expand Down Expand Up @@ -1014,6 +1017,37 @@ function killProcess(pid: number) {
}
}

const ZOOM_FILE_PATH = path.join(app.getPath('userData'), 'zoomconfig.json');

function saveZoomFactor(factor: number) {
try {
fs.writeFileSync(ZOOM_FILE_PATH, JSON.stringify({ zoomFactor: factor }), 'utf-8');
} catch (err) {
console.error(`Failed to save zoom factor:`, err);
}
}

function loadZoomFactor(): number {
try {
const { zoomFactor = 1.0 } = JSON.parse(fs.readFileSync(ZOOM_FILE_PATH, 'utf-8'));
return typeof zoomFactor === 'number' ? zoomFactor : 1.0;
} catch {
// If the file doesn’t exist or JSON is invalid, fall back to 1.0
return 1.0;
}
}

function adjustZoom(delta: number) {
if (!mainWindow) {
return;
}

const current = mainWindow.webContents.getZoomFactor();
const next = Math.min(5.0, Math.max(0.25, current + delta));
mainWindow.webContents.setZoomFactor(next);
saveZoomFactor(next);
}

function startElecron() {
console.info('App starting...');

Expand Down Expand Up @@ -1067,6 +1101,11 @@ function startElecron() {
}
});

mainWindow.webContents.on('did-finish-load', () => {
const savedZoomFactor = loadZoomFactor();
mainWindow?.webContents.setZoomFactor(savedZoomFactor);
});

mainWindow.webContents.on('dom-ready', () => {
const defaultMenu = getDefaultAppMenu();
const currentMenu = JSON.parse(JSON.stringify(defaultMenu));
Expand Down

0 comments on commit 7dbde4d

Please sign in to comment.