Skip to content

Commit 58a7a55

Browse files
feat: Expose webview zoom (#9378)
* Expose webview zoom * Add js side support * Generate bundle script * Format * Add change file
1 parent b231f4c commit 58a7a55

File tree

12 files changed

+81
-1
lines changed

12 files changed

+81
-1
lines changed

.changes/set-zoom.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@tauri-apps/api": minor:feat
3+
"tauri": minor:feat
4+
"tauri-runtime": minor:feat
5+
"tauri-runtime-wry": minor:feat
6+
---
7+
8+
Added the `set_zoom` function to the webview API.
File renamed without changes.

core/tauri-runtime-wry/src/lib.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,7 @@ pub enum WebviewMessage {
11851185
SetFocus,
11861186
Reparent(WindowId, Sender<Result<()>>),
11871187
SetAutoResize(bool),
1188+
SetZoom(f64),
11881189
// Getters
11891190
Url(Sender<Result<Url>>),
11901191
Bounds(Sender<Result<tauri_runtime::Rect>>),
@@ -1451,6 +1452,17 @@ impl<T: UserEvent> WebviewDispatch<T> for WryWebviewDispatcher<T> {
14511452
),
14521453
)
14531454
}
1455+
1456+
fn set_zoom(&self, scale_factor: f64) -> Result<()> {
1457+
send_user_message(
1458+
&self.context,
1459+
Message::Webview(
1460+
*self.window_id.lock().unwrap(),
1461+
self.webview_id,
1462+
WebviewMessage::SetZoom(scale_factor),
1463+
),
1464+
)
1465+
}
14541466
}
14551467

14561468
/// The Tauri [`WindowDispatch`] for [`Wry`].
@@ -2967,6 +2979,11 @@ fn handle_user_message<T: UserEvent>(
29672979
log::error!("failed to get webview bounds: {e}");
29682980
}
29692981
},
2982+
WebviewMessage::SetZoom(scale_factor) => {
2983+
if let Err(e) = webview.zoom(scale_factor) {
2984+
log::error!("failed to set webview zoom: {e}");
2985+
}
2986+
}
29702987
// Getters
29712988
WebviewMessage::Url(tx) => {
29722989
tx.send(

core/tauri-runtime/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,9 @@ pub trait WebviewDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + '
480480

481481
/// Sets whether the webview should automatically grow and shrink its size and position when the parent window resizes.
482482
fn set_auto_resize(&self, auto_resize: bool) -> Result<()>;
483+
484+
/// Set the webview zoom level
485+
fn set_zoom(&self, scale_factor: f64) -> Result<()>;
483486
}
484487

485488
/// Window dispatcher. A thread-safe handle to the window APIs.

core/tauri/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ const PLUGINS: &[(&str, &[(&str, bool)])] = &[
121121
("set_webview_size", false),
122122
("set_webview_position", false),
123123
("set_webview_focus", false),
124+
("set_webview_zoom", false),
124125
("print", false),
125126
("reparent", false),
126127
// internal

core/tauri/permissions/webview/autogenerated/reference.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
|`deny-set-webview-position`|Denies the set_webview_position command without any pre-configured scope.|
1717
|`allow-set-webview-size`|Enables the set_webview_size command without any pre-configured scope.|
1818
|`deny-set-webview-size`|Denies the set_webview_size command without any pre-configured scope.|
19+
|`allow-set-webview-zoom`|Enables the set_webview_zoom command without any pre-configured scope.|
20+
|`deny-set-webview-zoom`|Denies the set_webview_zoom command without any pre-configured scope.|
1921
|`allow-webview-close`|Enables the webview_close command without any pre-configured scope.|
2022
|`deny-webview-close`|Denies the webview_close command without any pre-configured scope.|
2123
|`allow-webview-position`|Enables the webview_position command without any pre-configured scope.|

core/tauri/scripts/bundle.global.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tauri/src/test/mock_runtime.rs

+4
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,10 @@ impl<T: UserEvent> WebviewDispatch<T> for MockWebviewDispatcher {
485485
Ok(false)
486486
}
487487

488+
fn set_zoom(&self, scale_factor: f64) -> Result<()> {
489+
Ok(())
490+
}
491+
488492
fn eval_script<S: Into<String>>(&self, script: S) -> Result<()> {
489493
self
490494
.last_evaluated_script

core/tauri/src/webview/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,21 @@ tauri::Builder::default()
14241424
.is_devtools_open()
14251425
.unwrap_or_default()
14261426
}
1427+
1428+
/// Set the webview zoom level
1429+
///
1430+
/// ## Platform-specific:
1431+
///
1432+
/// - **Android**: Not supported.
1433+
/// - **macOS**: available on macOS 11+ only.
1434+
/// - **iOS**: available on iOS 14+ only.
1435+
pub fn set_zoom(&self, scale_factor: f64) -> crate::Result<()> {
1436+
self
1437+
.webview
1438+
.dispatcher
1439+
.set_zoom(scale_factor)
1440+
.map_err(Into::into)
1441+
}
14271442
}
14281443

14291444
/// Event system APIs.

core/tauri/src/webview/plugin.rs

+2
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ mod desktop_commands {
157157
setter!(set_webview_size, set_size, Size);
158158
setter!(set_webview_position, set_position, Position);
159159
setter!(set_webview_focus, set_focus);
160+
setter!(set_webview_zoom, set_zoom, f64);
160161

161162
#[command(root = "crate")]
162163
pub async fn reparent<R: Runtime>(
@@ -238,6 +239,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
238239
desktop_commands::set_webview_size,
239240
desktop_commands::set_webview_position,
240241
desktop_commands::set_webview_focus,
242+
desktop_commands::set_webview_zoom,
241243
desktop_commands::print,
242244
desktop_commands::reparent,
243245
#[cfg(any(debug_assertions, feature = "devtools"))]

core/tauri/src/webview/webview_window.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,17 @@ tauri::Builder::default()
17131713
pub fn is_devtools_open(&self) -> bool {
17141714
self.webview.is_devtools_open()
17151715
}
1716+
1717+
/// Set the webview zoom level
1718+
///
1719+
/// ## Platform-specific:
1720+
///
1721+
/// - **Android**: Not supported.
1722+
/// - **macOS**: available on macOS 11+ only.
1723+
/// - **iOS**: available on iOS 14+ only.
1724+
pub fn set_zoom(&self, scale_factor: f64) -> crate::Result<()> {
1725+
self.webview.set_zoom(scale_factor)
1726+
}
17161727
}
17171728

17181729
/// Event system APIs.

tooling/api/src/webview.ts

+17
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,23 @@ class Webview {
480480
})
481481
}
482482

483+
/**
484+
* Set webview zoom level.
485+
* @example
486+
* ```typescript
487+
* import { getCurrent } from '@tauri-apps/api/webview';
488+
* await getCurrent().setZoom(1.5);
489+
* ```
490+
*
491+
* @returns A promise indicating the success or failure of the operation.
492+
*/
493+
async setZoom(scaleFactor: number): Promise<void> {
494+
return invoke('plugin:webview|set_webview_zoom', {
495+
label: this.label,
496+
value: scaleFactor
497+
})
498+
}
499+
483500
/**
484501
* Moves this webview to the given label.
485502
* @example

0 commit comments

Comments
 (0)