Skip to content

Commit 4973d73

Browse files
feat: Add zoom hotkey polyfill for non windows platforms (#9386)
1 parent 58a7a55 commit 4973d73

File tree

10 files changed

+97
-6
lines changed

10 files changed

+97
-6
lines changed

.changes/zoom-polyfill.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": minor:feat
3+
"tauri-runtime": minor:feat
4+
---
5+
6+
Provide a basic zoom hotkey polyfill for non-Windows platforms

core/tauri-config-schema/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@
452452
"format": "uri"
453453
},
454454
"zoomHotkeysEnabled": {
455-
"description": "Whether page zooming by hotkeys is enabled **Windows Only**",
455+
"description": "Whether page zooming by hotkeys is enabled\n\n## Platform-specific:\n\n- **Windows**: Controls WebView2's [`IsZoomControlEnabled`](https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2settings?view=webview2-winrt-1.0.2420.47#iszoomcontrolenabled) setting. - **MacOS / Linux**: Injects a polyfill that zooms in and out with `ctrl/command` + `-/=`, 20% in each step, ranging from 20% to 1000%. Requires `webview:allow-set-webview-zoom` permission\n\n- **Android / iOS**: Unsupported.",
456456
"default": false,
457457
"type": "boolean"
458458
}

core/tauri-runtime/src/webview.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,14 @@ impl WebviewAttributes {
350350
}
351351

352352
/// Whether page zooming by hotkeys is enabled
353+
///
354+
/// ## Platform-specific:
355+
///
356+
/// - **Windows**: Controls WebView2's [`IsZoomControlEnabled`](https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2settings?view=webview2-winrt-1.0.2420.47#iszoomcontrolenabled) setting.
357+
/// - **MacOS / Linux**: Injects a polyfill that zooms in and out with `ctrl/command` + `-/=`,
358+
/// 20% in each step, ranging from 20% to 1000%. Requires `webview:allow-set-webview-zoom` permission
359+
///
360+
/// - **Android / iOS**: Unsupported.
353361
#[must_use]
354362
pub fn zoom_hotkeys_enabled(mut self, enabled: bool) -> Self {
355363
self.zoom_hotkeys_enabled = enabled;

core/tauri-utils/src/config.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,15 @@ pub struct WindowConfig {
12931293
///
12941294
/// - **macOS**: Requires the `macos-proxy` feature flag and only compiles for macOS 14+.
12951295
pub proxy_url: Option<Url>,
1296-
/// Whether page zooming by hotkeys is enabled **Windows Only**
1296+
/// Whether page zooming by hotkeys is enabled
1297+
///
1298+
/// ## Platform-specific:
1299+
///
1300+
/// - **Windows**: Controls WebView2's [`IsZoomControlEnabled`](https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2settings?view=webview2-winrt-1.0.2420.47#iszoomcontrolenabled) setting.
1301+
/// - **MacOS / Linux**: Injects a polyfill that zooms in and out with `ctrl/command` + `-/=`,
1302+
/// 20% in each step, ranging from 20% to 1000%. Requires `webview:allow-set-webview-zoom` permission
1303+
///
1304+
/// - **Android / iOS**: Unsupported.
12971305
#[serde(default)]
12981306
pub zoom_hotkeys_enabled: bool,
12991307
}

core/tauri/src/manager/webview.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,23 @@ impl<R: Runtime> WebviewManager<R> {
534534
}
535535
}
536536

537+
#[cfg(all(desktop, not(target_os = "windows")))]
538+
if pending.webview_attributes.zoom_hotkeys_enabled {
539+
#[derive(Template)]
540+
#[default_template("../webview/scripts/zoom-hotkey.js")]
541+
struct HotkeyZoom<'a> {
542+
os_name: &'a str,
543+
}
544+
545+
pending.webview_attributes.initialization_scripts.push(
546+
HotkeyZoom {
547+
os_name: std::env::consts::OS,
548+
}
549+
.render_default(&Default::default())?
550+
.into_string(),
551+
)
552+
}
553+
537554
#[cfg(feature = "isolation")]
538555
let pattern = app_manager.pattern.clone();
539556
let navigation_handler = pending.navigation_handler.take();

core/tauri/src/webview/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,15 @@ fn main() {
776776
self
777777
}
778778

779-
/// Whether page zooming by hotkeys is enabled **Windows Only**
779+
/// Whether page zooming by hotkeys is enabled
780+
///
781+
/// ## Platform-specific:
782+
///
783+
/// - **Windows**: Controls WebView2's [`IsZoomControlEnabled`](https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2settings?view=webview2-winrt-1.0.2420.47#iszoomcontrolenabled) setting.
784+
/// - **MacOS / Linux**: Injects a polyfill that zooms in and out with `ctrl/command` + `-/=`,
785+
/// 20% in each step, ranging from 20% to 1000%. Requires `webview:allow-set-webview-zoom` permission
786+
///
787+
/// - **Android / iOS**: Unsupported.
780788
#[must_use]
781789
pub fn zoom_hotkeys_enabled(mut self, enabled: bool) -> Self {
782790
self.webview_attributes.zoom_hotkeys_enabled = enabled;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
const OS_NAME = __TEMPLATE_os_name__
6+
7+
let zoomLevel = 1
8+
9+
const MAX_ZOOM_LEVEL = 10
10+
const MIN_ZOOM_LEVEL = 0.2
11+
12+
window.addEventListener('keydown', (event) => {
13+
if (OS_NAME === 'macos' ? event.metaKey : event.ctrlKey) {
14+
if (event.key === '-') {
15+
zoomLevel -= 0.2
16+
} else if (event.key === '=') {
17+
zoomLevel += 0.2
18+
} else if (event.key === '0') {
19+
zoomLevel = 1
20+
} else {
21+
return
22+
}
23+
zoomLevel = Math.min(Math.max(zoomLevel, MIN_ZOOM_LEVEL), MAX_ZOOM_LEVEL)
24+
window.__TAURI_INTERNALS__.invoke('plugin:webview|set_webview_zoom', {
25+
value: zoomLevel
26+
})
27+
}
28+
})

core/tauri/src/webview/webview_window.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,15 @@ fn main() {
839839
self
840840
}
841841

842-
/// Whether page zooming by hotkeys is enabled **Windows only**
842+
/// Whether page zooming by hotkeys is enabled
843+
///
844+
/// ## Platform-specific:
845+
///
846+
/// - **Windows**: Controls WebView2's [`IsZoomControlEnabled`](https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2settings?view=webview2-winrt-1.0.2420.47#iszoomcontrolenabled) setting.
847+
/// - **MacOS / Linux**: Injects a polyfill that zooms in and out with `ctrl/command` + `-/=`,
848+
/// 20% in each step, ranging from 20% to 1000%. Requires `webview:allow-set-webview-zoom` permission
849+
///
850+
/// - **Android / iOS**: Unsupported.
843851
#[must_use]
844852
pub fn zoom_hotkeys_enabled(mut self, enabled: bool) -> Self {
845853
self.webview_builder = self.webview_builder.zoom_hotkeys_enabled(enabled);

tooling/api/src/webview.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,15 @@ interface WebviewOptions {
666666
* */
667667
proxyUrl?: string
668668
/**
669-
* Whether page zooming by hotkeys or gestures is enabled **Windows Only**
669+
* Whether page zooming by hotkeys is enabled
670+
*
671+
* ## Platform-specific:
672+
*
673+
* - **Windows**: Controls WebView2's [`IsZoomControlEnabled`](https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2settings?view=webview2-winrt-1.0.2420.47#iszoomcontrolenabled) setting.
674+
* - **MacOS / Linux**: Injects a polyfill that zooms in and out with `ctrl/command` + `-/=`,
675+
* 20% in each step, ranging from 20% to 1000%. Requires `webview:allow-set-webview-zoom` permission
676+
*
677+
* - **Android / iOS**: Unsupported.
670678
*/
671679
zoomHotkeysEnabled?: boolean
672680
}

tooling/cli/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@
452452
"format": "uri"
453453
},
454454
"zoomHotkeysEnabled": {
455-
"description": "Whether page zooming by hotkeys is enabled **Windows Only**",
455+
"description": "Whether page zooming by hotkeys is enabled\n\n## Platform-specific:\n\n- **Windows**: Controls WebView2's [`IsZoomControlEnabled`](https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2settings?view=webview2-winrt-1.0.2420.47#iszoomcontrolenabled) setting. - **MacOS / Linux**: Injects a polyfill that zooms in and out with `ctrl/command` + `-/=`, 20% in each step, ranging from 20% to 1000%. Requires `webview:allow-set-webview-zoom` permission\n\n- **Android / iOS**: Unsupported.",
456456
"default": false,
457457
"type": "boolean"
458458
}

0 commit comments

Comments
 (0)