Skip to content

Commit

Permalink
feat: Add zoom hotkey polyfill for non windows platforms (#9386)
Browse files Browse the repository at this point in the history
  • Loading branch information
Legend-Master committed Apr 8, 2024
1 parent 58a7a55 commit 4973d73
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changes/zoom-polyfill.md
@@ -0,0 +1,6 @@
---
"tauri": minor:feat
"tauri-runtime": minor:feat
---

Provide a basic zoom hotkey polyfill for non-Windows platforms
2 changes: 1 addition & 1 deletion core/tauri-config-schema/schema.json
Expand Up @@ -452,7 +452,7 @@
"format": "uri"
},
"zoomHotkeysEnabled": {
"description": "Whether page zooming by hotkeys is enabled **Windows Only**",
"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.",
"default": false,
"type": "boolean"
}
Expand Down
8 changes: 8 additions & 0 deletions core/tauri-runtime/src/webview.rs
Expand Up @@ -350,6 +350,14 @@ impl WebviewAttributes {
}

/// Whether page zooming by hotkeys is enabled
///
/// ## Platform-specific:
///
/// - **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
///
/// - **Android / iOS**: Unsupported.
#[must_use]
pub fn zoom_hotkeys_enabled(mut self, enabled: bool) -> Self {
self.zoom_hotkeys_enabled = enabled;
Expand Down
10 changes: 9 additions & 1 deletion core/tauri-utils/src/config.rs
Expand Up @@ -1293,7 +1293,15 @@ pub struct WindowConfig {
///
/// - **macOS**: Requires the `macos-proxy` feature flag and only compiles for macOS 14+.
pub proxy_url: Option<Url>,
/// Whether page zooming by hotkeys is enabled **Windows Only**
/// Whether page zooming by hotkeys is enabled
///
/// ## Platform-specific:
///
/// - **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
///
/// - **Android / iOS**: Unsupported.
#[serde(default)]
pub zoom_hotkeys_enabled: bool,
}
Expand Down
17 changes: 17 additions & 0 deletions core/tauri/src/manager/webview.rs
Expand Up @@ -534,6 +534,23 @@ impl<R: Runtime> WebviewManager<R> {
}
}

#[cfg(all(desktop, not(target_os = "windows")))]
if pending.webview_attributes.zoom_hotkeys_enabled {
#[derive(Template)]
#[default_template("../webview/scripts/zoom-hotkey.js")]
struct HotkeyZoom<'a> {
os_name: &'a str,
}

pending.webview_attributes.initialization_scripts.push(
HotkeyZoom {
os_name: std::env::consts::OS,
}
.render_default(&Default::default())?
.into_string(),
)
}

#[cfg(feature = "isolation")]
let pattern = app_manager.pattern.clone();
let navigation_handler = pending.navigation_handler.take();
Expand Down
10 changes: 9 additions & 1 deletion core/tauri/src/webview/mod.rs
Expand Up @@ -776,7 +776,15 @@ fn main() {
self
}

/// Whether page zooming by hotkeys is enabled **Windows Only**
/// Whether page zooming by hotkeys is enabled
///
/// ## Platform-specific:
///
/// - **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
///
/// - **Android / iOS**: Unsupported.
#[must_use]
pub fn zoom_hotkeys_enabled(mut self, enabled: bool) -> Self {
self.webview_attributes.zoom_hotkeys_enabled = enabled;
Expand Down
28 changes: 28 additions & 0 deletions core/tauri/src/webview/scripts/zoom-hotkey.js
@@ -0,0 +1,28 @@
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

const OS_NAME = __TEMPLATE_os_name__

let zoomLevel = 1

const MAX_ZOOM_LEVEL = 10
const MIN_ZOOM_LEVEL = 0.2

window.addEventListener('keydown', (event) => {
if (OS_NAME === 'macos' ? event.metaKey : event.ctrlKey) {
if (event.key === '-') {
zoomLevel -= 0.2
} else if (event.key === '=') {
zoomLevel += 0.2
} else if (event.key === '0') {
zoomLevel = 1
} else {
return
}
zoomLevel = Math.min(Math.max(zoomLevel, MIN_ZOOM_LEVEL), MAX_ZOOM_LEVEL)
window.__TAURI_INTERNALS__.invoke('plugin:webview|set_webview_zoom', {
value: zoomLevel
})
}
})
10 changes: 9 additions & 1 deletion core/tauri/src/webview/webview_window.rs
Expand Up @@ -839,7 +839,15 @@ fn main() {
self
}

/// Whether page zooming by hotkeys is enabled **Windows only**
/// Whether page zooming by hotkeys is enabled
///
/// ## Platform-specific:
///
/// - **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
///
/// - **Android / iOS**: Unsupported.
#[must_use]
pub fn zoom_hotkeys_enabled(mut self, enabled: bool) -> Self {
self.webview_builder = self.webview_builder.zoom_hotkeys_enabled(enabled);
Expand Down
10 changes: 9 additions & 1 deletion tooling/api/src/webview.ts
Expand Up @@ -666,7 +666,15 @@ interface WebviewOptions {
* */
proxyUrl?: string
/**
* Whether page zooming by hotkeys or gestures is enabled **Windows Only**
* Whether page zooming by hotkeys is enabled
*
* ## Platform-specific:
*
* - **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
*
* - **Android / iOS**: Unsupported.
*/
zoomHotkeysEnabled?: boolean
}
Expand Down
2 changes: 1 addition & 1 deletion tooling/cli/schema.json
Expand Up @@ -452,7 +452,7 @@
"format": "uri"
},
"zoomHotkeysEnabled": {
"description": "Whether page zooming by hotkeys is enabled **Windows Only**",
"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.",
"default": false,
"type": "boolean"
}
Expand Down

0 comments on commit 4973d73

Please sign in to comment.