Skip to content

Commit

Permalink
feat: Expose webview zoom (#9378)
Browse files Browse the repository at this point in the history
* Expose webview zoom

* Add js side support

* Generate bundle script

* Format

* Add change file
  • Loading branch information
Legend-Master committed Apr 5, 2024
1 parent b231f4c commit 58a7a55
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .changes/set-zoom.md
@@ -0,0 +1,8 @@
---
"@tauri-apps/api": minor:feat
"tauri": minor:feat
"tauri-runtime": minor:feat
"tauri-runtime-wry": minor:feat
---

Added the `set_zoom` function to the webview API.
File renamed without changes.
17 changes: 17 additions & 0 deletions core/tauri-runtime-wry/src/lib.rs
Expand Up @@ -1185,6 +1185,7 @@ pub enum WebviewMessage {
SetFocus,
Reparent(WindowId, Sender<Result<()>>),
SetAutoResize(bool),
SetZoom(f64),
// Getters
Url(Sender<Result<Url>>),
Bounds(Sender<Result<tauri_runtime::Rect>>),
Expand Down Expand Up @@ -1451,6 +1452,17 @@ impl<T: UserEvent> WebviewDispatch<T> for WryWebviewDispatcher<T> {
),
)
}

fn set_zoom(&self, scale_factor: f64) -> Result<()> {
send_user_message(
&self.context,
Message::Webview(
*self.window_id.lock().unwrap(),
self.webview_id,
WebviewMessage::SetZoom(scale_factor),
),
)
}
}

/// The Tauri [`WindowDispatch`] for [`Wry`].
Expand Down Expand Up @@ -2967,6 +2979,11 @@ fn handle_user_message<T: UserEvent>(
log::error!("failed to get webview bounds: {e}");
}
},
WebviewMessage::SetZoom(scale_factor) => {
if let Err(e) = webview.zoom(scale_factor) {
log::error!("failed to set webview zoom: {e}");
}
}
// Getters
WebviewMessage::Url(tx) => {
tx.send(
Expand Down
3 changes: 3 additions & 0 deletions core/tauri-runtime/src/lib.rs
Expand Up @@ -480,6 +480,9 @@ pub trait WebviewDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + '

/// Sets whether the webview should automatically grow and shrink its size and position when the parent window resizes.
fn set_auto_resize(&self, auto_resize: bool) -> Result<()>;

/// Set the webview zoom level
fn set_zoom(&self, scale_factor: f64) -> Result<()>;
}

/// Window dispatcher. A thread-safe handle to the window APIs.
Expand Down
1 change: 1 addition & 0 deletions core/tauri/build.rs
Expand Up @@ -121,6 +121,7 @@ const PLUGINS: &[(&str, &[(&str, bool)])] = &[
("set_webview_size", false),
("set_webview_position", false),
("set_webview_focus", false),
("set_webview_zoom", false),
("print", false),
("reparent", false),
// internal
Expand Down
2 changes: 2 additions & 0 deletions core/tauri/permissions/webview/autogenerated/reference.md
Expand Up @@ -16,6 +16,8 @@
|`deny-set-webview-position`|Denies the set_webview_position command without any pre-configured scope.|
|`allow-set-webview-size`|Enables the set_webview_size command without any pre-configured scope.|
|`deny-set-webview-size`|Denies the set_webview_size command without any pre-configured scope.|
|`allow-set-webview-zoom`|Enables the set_webview_zoom command without any pre-configured scope.|
|`deny-set-webview-zoom`|Denies the set_webview_zoom command without any pre-configured scope.|
|`allow-webview-close`|Enables the webview_close command without any pre-configured scope.|
|`deny-webview-close`|Denies the webview_close command without any pre-configured scope.|
|`allow-webview-position`|Enables the webview_position command without any pre-configured scope.|
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions core/tauri/src/test/mock_runtime.rs
Expand Up @@ -485,6 +485,10 @@ impl<T: UserEvent> WebviewDispatch<T> for MockWebviewDispatcher {
Ok(false)
}

fn set_zoom(&self, scale_factor: f64) -> Result<()> {
Ok(())
}

fn eval_script<S: Into<String>>(&self, script: S) -> Result<()> {
self
.last_evaluated_script
Expand Down
15 changes: 15 additions & 0 deletions core/tauri/src/webview/mod.rs
Expand Up @@ -1424,6 +1424,21 @@ tauri::Builder::default()
.is_devtools_open()
.unwrap_or_default()
}

/// Set the webview zoom level
///
/// ## Platform-specific:
///
/// - **Android**: Not supported.
/// - **macOS**: available on macOS 11+ only.
/// - **iOS**: available on iOS 14+ only.
pub fn set_zoom(&self, scale_factor: f64) -> crate::Result<()> {
self
.webview
.dispatcher
.set_zoom(scale_factor)
.map_err(Into::into)
}
}

/// Event system APIs.
Expand Down
2 changes: 2 additions & 0 deletions core/tauri/src/webview/plugin.rs
Expand Up @@ -157,6 +157,7 @@ mod desktop_commands {
setter!(set_webview_size, set_size, Size);
setter!(set_webview_position, set_position, Position);
setter!(set_webview_focus, set_focus);
setter!(set_webview_zoom, set_zoom, f64);

#[command(root = "crate")]
pub async fn reparent<R: Runtime>(
Expand Down Expand Up @@ -238,6 +239,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
desktop_commands::set_webview_size,
desktop_commands::set_webview_position,
desktop_commands::set_webview_focus,
desktop_commands::set_webview_zoom,
desktop_commands::print,
desktop_commands::reparent,
#[cfg(any(debug_assertions, feature = "devtools"))]
Expand Down
11 changes: 11 additions & 0 deletions core/tauri/src/webview/webview_window.rs
Expand Up @@ -1713,6 +1713,17 @@ tauri::Builder::default()
pub fn is_devtools_open(&self) -> bool {
self.webview.is_devtools_open()
}

/// Set the webview zoom level
///
/// ## Platform-specific:
///
/// - **Android**: Not supported.
/// - **macOS**: available on macOS 11+ only.
/// - **iOS**: available on iOS 14+ only.
pub fn set_zoom(&self, scale_factor: f64) -> crate::Result<()> {
self.webview.set_zoom(scale_factor)
}
}

/// Event system APIs.
Expand Down
17 changes: 17 additions & 0 deletions tooling/api/src/webview.ts
Expand Up @@ -480,6 +480,23 @@ class Webview {
})
}

/**
* Set webview zoom level.
* @example
* ```typescript
* import { getCurrent } from '@tauri-apps/api/webview';
* await getCurrent().setZoom(1.5);
* ```
*
* @returns A promise indicating the success or failure of the operation.
*/
async setZoom(scaleFactor: number): Promise<void> {
return invoke('plugin:webview|set_webview_zoom', {
label: this.label,
value: scaleFactor
})
}

/**
* Moves this webview to the given label.
* @example
Expand Down

0 comments on commit 58a7a55

Please sign in to comment.