Skip to content

Commit 5591a4f

Browse files
feature(linux) add mousewheel bindings for zoom functions (#12912)
* add mousewheel bindings commonly used for zooming * add change file * update docs * fmt --------- Co-authored-by: Lucas Nogueira <lucas@tauri.app>
1 parent 2fa33d5 commit 5591a4f

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

.changes/mousewheel-zoom.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:feat
3+
---
4+
5+
Change webview zoom on mousewheel when the `zoom_hotkeys_enabled` configuration is set to `true`.

crates/tauri/src/webview/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -778,13 +778,13 @@ fn main() {
778778
self
779779
}
780780

781-
/// Whether page zooming by hotkeys is enabled
781+
/// Whether page zooming by hotkeys and mousewheel should be enabled or not.
782782
///
783783
/// ## Platform-specific:
784784
///
785785
/// - **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.
786-
/// - **MacOS / Linux**: Injects a polyfill that zooms in and out with `ctrl/command` + `-/=`,
787-
/// 20% in each step, ranging from 20% to 1000%. Requires `webview:allow-set-webview-zoom` permission
786+
/// - **MacOS / Linux**: Injects a polyfill that zooms in and out with `Ctrl/Cmd + [- = +]` hotkeys or mousewheel events,
787+
/// 20% in each step, ranging from 20% to 1000%. Requires `core:webview:allow-set-webview-zoom` permission
788788
///
789789
/// - **Android / iOS**: Unsupported.
790790
#[must_use]

crates/tauri/src/webview/scripts/zoom-hotkey.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,18 @@ window.addEventListener('keydown', (event) => {
2626
})
2727
}
2828
})
29+
30+
window.addEventListener('mousewheel', (event) => {
31+
if (event.ctrlKey) {
32+
event.preventDefault()
33+
if (event.deltaY < 0) {
34+
zoomLevel += 0.2
35+
} else {
36+
zoomLevel -= 0.2
37+
}
38+
zoomLevel = Math.min(Math.max(zoomLevel, MIN_ZOOM_LEVEL), MAX_ZOOM_LEVEL)
39+
window.__TAURI_INTERNALS__.invoke('plugin:webview|set_webview_zoom', {
40+
value: zoomLevel
41+
})
42+
}
43+
})

crates/tauri/src/webview/webview_window.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -890,13 +890,13 @@ impl<R: Runtime, M: Manager<R>> WebviewWindowBuilder<'_, R, M> {
890890
self
891891
}
892892

893-
/// Whether page zooming by hotkeys is enabled
893+
/// Whether page zooming by hotkeys and mousewheel should be enabled or not.
894894
///
895895
/// ## Platform-specific:
896896
///
897897
/// - **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.
898-
/// - **MacOS / Linux**: Injects a polyfill that zooms in and out with `ctrl/command` + `-/=`,
899-
/// 20% in each step, ranging from 20% to 1000%. Requires `webview:allow-set-webview-zoom` permission
898+
/// - **MacOS / Linux**: Injects a polyfill that zooms in and out with `Ctrl/Cmd + [- = +]` hotkeys or mousewheel events,
899+
/// 20% in each step, ranging from 20% to 1000%. Requires `core:webview:allow-set-webview-zoom` permission
900900
///
901901
/// - **Android / iOS**: Unsupported.
902902
#[must_use]

0 commit comments

Comments
 (0)