@@ -4,12 +4,18 @@ import { getCurrentWebview } from "@tauri-apps/api/webview";
44import { hasPrimaryShortcutModifier } from "@/shared/lib/platform" ;
55
66const DEFAULT_ZOOM_FACTOR = 1 ;
7- const MIN_ZOOM_FACTOR = 0.2 ;
8- const MAX_ZOOM_FACTOR = 10 ;
9- const ZOOM_STEP = 0.2 ;
7+ const MIN_ZOOM_FACTOR = 0.75 ;
8+ const MAX_ZOOM_FACTOR = 1.5 ;
9+ const ZOOM_STEP = 0.1 ;
10+ const BASE_FONT_SIZE_PX = 16 ;
11+ const TEXT_SCALE_STORAGE_KEY = "sprout:text-scale" ;
1012
1113type ZoomAction = "increase" | "decrease" | "reset" ;
1214
15+ function roundZoomFactor ( zoomFactor : number ) {
16+ return Math . round ( zoomFactor * 10 ) / 10 ;
17+ }
18+
1319function getZoomAction ( event : KeyboardEvent ) : ZoomAction | null {
1420 if ( ! hasPrimaryShortcutModifier ( event ) || event . altKey ) {
1521 return null ;
@@ -49,17 +55,51 @@ function getNextZoomFactor(action: ZoomAction, zoomFactor: number) {
4955 }
5056
5157 if ( action === "increase" ) {
52- return Math . min ( zoomFactor + ZOOM_STEP , MAX_ZOOM_FACTOR ) ;
58+ return Math . min ( roundZoomFactor ( zoomFactor + ZOOM_STEP ) , MAX_ZOOM_FACTOR ) ;
5359 }
5460
55- return Math . max ( zoomFactor - ZOOM_STEP , MIN_ZOOM_FACTOR ) ;
61+ return Math . max ( roundZoomFactor ( zoomFactor - ZOOM_STEP ) , MIN_ZOOM_FACTOR ) ;
62+ }
63+
64+ function readStoredZoomFactor ( ) {
65+ const raw = window . localStorage . getItem ( TEXT_SCALE_STORAGE_KEY ) ;
66+ if ( ! raw ) {
67+ return DEFAULT_ZOOM_FACTOR ;
68+ }
69+
70+ const parsed = Number . parseFloat ( raw ) ;
71+ if ( ! Number . isFinite ( parsed ) ) {
72+ return DEFAULT_ZOOM_FACTOR ;
73+ }
74+
75+ return Math . min ( Math . max ( parsed , MIN_ZOOM_FACTOR ) , MAX_ZOOM_FACTOR ) ;
76+ }
77+
78+ function applyTextScale ( zoomFactor : number ) {
79+ if ( zoomFactor === DEFAULT_ZOOM_FACTOR ) {
80+ document . documentElement . style . fontSize = "" ;
81+ window . localStorage . removeItem ( TEXT_SCALE_STORAGE_KEY ) ;
82+ return ;
83+ }
84+
85+ document . documentElement . style . fontSize = `${ BASE_FONT_SIZE_PX * zoomFactor } px` ;
86+ window . localStorage . setItem ( TEXT_SCALE_STORAGE_KEY , String ( zoomFactor ) ) ;
5687}
5788
5889export function useWebviewZoomShortcuts ( ) {
5990 const zoomFactorRef = React . useRef ( DEFAULT_ZOOM_FACTOR ) ;
6091
6192 React . useLayoutEffect ( ( ) => {
6293 const webview = getCurrentWebview ( ) ;
94+ const storedZoomFactor = readStoredZoomFactor ( ) ;
95+
96+ zoomFactorRef . current = storedZoomFactor ;
97+ applyTextScale ( storedZoomFactor ) ;
98+
99+ // Keep the webview coordinate system stable; only text should scale.
100+ void webview . setZoom ( DEFAULT_ZOOM_FACTOR ) . catch ( ( error ) => {
101+ console . error ( "Failed to reset webview zoom" , error ) ;
102+ } ) ;
63103
64104 function handleKeyDown ( event : KeyboardEvent ) {
65105 const action = getZoomAction ( event ) ;
@@ -77,11 +117,7 @@ export function useWebviewZoomShortcuts() {
77117 }
78118
79119 zoomFactorRef . current = nextZoomFactor ;
80-
81- void webview . setZoom ( nextZoomFactor ) . catch ( ( error ) => {
82- zoomFactorRef . current = previousZoomFactor ;
83- console . error ( "Failed to update webview zoom" , error ) ;
84- } ) ;
120+ applyTextScale ( nextZoomFactor ) ;
85121 }
86122
87123 window . addEventListener ( "keydown" , handleKeyDown ) ;
0 commit comments