Skip to content

Commit 01a4b16

Browse files
committed
fix: update UI state after zoom level changes
Add React state management for zoom percentage in AppearanceSettings to ensure the UI updates after zoom changes. The zoom functions now work correctly with Tauri's webview zoom API.
1 parent db59f39 commit 01a4b16

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
lines changed

src/components/settings/AppearanceSettings.tsx

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { FC } from 'react';
1+
import { type FC, useCallback, useState } from 'react';
22

33
import {
44
PaintbrushIcon,
@@ -16,12 +16,14 @@ import {
1616
} from '@primer/react';
1717

1818
import { useAppContext } from '../../context/App';
19+
import type { Percentage } from '../../types';
1920
import { Theme } from '../../types';
2021
import { hasMultipleAccounts } from '../../utils/auth/utils';
2122
import {
2223
canDecreaseZoom,
2324
canIncreaseZoom,
2425
decreaseZoom,
26+
getCurrentZoomLevel,
2527
increaseZoom,
2628
resetZoomLevel,
2729
zoomLevelToPercentage,
@@ -32,7 +34,24 @@ import { Title } from '../primitives/Title';
3234

3335
export const AppearanceSettings: FC = () => {
3436
const { auth, settings, updateSetting } = useAppContext();
35-
const zoomPercentage = zoomLevelToPercentage(window.gitify.zoom.getLevel());
37+
const [zoomPercentage, setZoomPercentage] = useState<Percentage>(
38+
zoomLevelToPercentage(getCurrentZoomLevel()),
39+
);
40+
41+
const handleZoomOut = useCallback(() => {
42+
decreaseZoom(zoomPercentage);
43+
setZoomPercentage(zoomLevelToPercentage(getCurrentZoomLevel()));
44+
}, [zoomPercentage]);
45+
46+
const handleZoomIn = useCallback(() => {
47+
increaseZoom(zoomPercentage);
48+
setZoomPercentage(zoomLevelToPercentage(getCurrentZoomLevel()));
49+
}, [zoomPercentage]);
50+
51+
const handleZoomReset = useCallback(() => {
52+
resetZoomLevel();
53+
setZoomPercentage(zoomLevelToPercentage(getCurrentZoomLevel()));
54+
}, []);
3655

3756
return (
3857
<fieldset>
@@ -108,7 +127,7 @@ export const AppearanceSettings: FC = () => {
108127
data-testid="settings-zoom-out"
109128
disabled={!canDecreaseZoom(zoomPercentage)}
110129
icon={ZoomOutIcon}
111-
onClick={() => decreaseZoom(zoomPercentage)}
130+
onClick={handleZoomOut}
112131
size="small"
113132
unsafeDisableTooltip={true}
114133
/>
@@ -122,7 +141,7 @@ export const AppearanceSettings: FC = () => {
122141
data-testid="settings-zoom-in"
123142
disabled={!canIncreaseZoom(zoomPercentage)}
124143
icon={ZoomInIcon}
125-
onClick={() => increaseZoom(zoomPercentage)}
144+
onClick={handleZoomIn}
126145
size="small"
127146
unsafeDisableTooltip={true}
128147
/>
@@ -131,7 +150,7 @@ export const AppearanceSettings: FC = () => {
131150
aria-label="Reset zoom"
132151
data-testid="settings-zoom-reset"
133152
icon={SyncIcon}
134-
onClick={() => resetZoomLevel()}
153+
onClick={handleZoomReset}
135154
size="small"
136155
unsafeDisableTooltip={true}
137156
variant="danger"

src/utils/zoom.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,44 @@
11
import { defaultSettings } from '../context/defaults';
22
import type { Percentage } from '../types';
3+
import { isTauriEnvironment } from './environment';
34

45
const MINIMUM_ZOOM_PERCENTAGE = 0 as Percentage;
56
const MAXIMUM_ZOOM_PERCENTAGE = 120 as Percentage;
67
const RECOMMENDED_ZOOM_PERCENTAGE = defaultSettings.zoomPercentage;
78
const MULTIPLIER = 2;
89
const ZOOM_STEP = 10 as Percentage;
910

11+
/**
12+
* Browser fallback for zoom using CSS zoom and localStorage
13+
*/
14+
const browserZoom = {
15+
getLevel: (): number => {
16+
if (typeof window === 'undefined') {
17+
return 0;
18+
}
19+
const zoomLevel = localStorage.getItem('zoomLevel');
20+
return zoomLevel ? Number.parseFloat(zoomLevel) : 0;
21+
},
22+
setLevel: (zoomLevel: number) => {
23+
if (typeof window === 'undefined') {
24+
return;
25+
}
26+
localStorage.setItem('zoomLevel', zoomLevel.toString());
27+
const zoomFactor = 1.2 ** zoomLevel;
28+
const rootElement = document.getElementById('root');
29+
if (rootElement) {
30+
rootElement.style.zoom = zoomFactor.toString();
31+
}
32+
},
33+
};
34+
35+
/**
36+
* Get zoom API - uses Tauri if available, otherwise browser fallback
37+
*/
38+
function getZoomApi() {
39+
return isTauriEnvironment() ? window.gitify.zoom : browserZoom;
40+
}
41+
1042
/**
1143
* Zoom percentage to level. 100% is the recommended zoom level (0).
1244
* If somehow the percentage is not set, it will return 0, the default zoom level.
@@ -55,7 +87,7 @@ export function canIncreaseZoom(zoomPercentage: Percentage) {
5587
*/
5688
export function decreaseZoom(zoomPercentage: Percentage) {
5789
if (canDecreaseZoom(zoomPercentage)) {
58-
window.gitify.zoom.setLevel(
90+
getZoomApi().setLevel(
5991
zoomPercentageToLevel((zoomPercentage - ZOOM_STEP) as Percentage),
6092
);
6193
}
@@ -66,7 +98,7 @@ export function decreaseZoom(zoomPercentage: Percentage) {
6698
*/
6799
export function increaseZoom(zoomPercentage: Percentage) {
68100
if (canIncreaseZoom(zoomPercentage)) {
69-
window.gitify.zoom.setLevel(
101+
getZoomApi().setLevel(
70102
zoomPercentageToLevel((zoomPercentage + ZOOM_STEP) as Percentage),
71103
);
72104
}
@@ -76,7 +108,12 @@ export function increaseZoom(zoomPercentage: Percentage) {
76108
* Reset zoom level
77109
*/
78110
export function resetZoomLevel() {
79-
window.gitify.zoom.setLevel(
80-
zoomPercentageToLevel(RECOMMENDED_ZOOM_PERCENTAGE),
81-
);
111+
getZoomApi().setLevel(zoomPercentageToLevel(RECOMMENDED_ZOOM_PERCENTAGE));
112+
}
113+
114+
/**
115+
* Get current zoom level (browser-compatible)
116+
*/
117+
export function getCurrentZoomLevel(): number {
118+
return getZoomApi().getLevel();
82119
}

0 commit comments

Comments
 (0)