mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-12-25 03:33:36 +03:00
feat: Expose webview zoom (#9378)
* Expose webview zoom * Add js side support * Generate bundle script * Format * Add change file
This commit is contained in:
parent
b231f4c2e5
commit
58a7a552d7
8
.changes/set-zoom.md
Normal file
8
.changes/set-zoom.md
Normal file
@ -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.
|
@ -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>>),
|
||||
@ -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`].
|
||||
@ -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(
|
||||
|
@ -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.
|
||||
|
@ -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
|
||||
|
@ -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.|
|
||||
|
File diff suppressed because one or more lines are too long
@ -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
|
||||
|
@ -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.
|
||||
|
@ -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>(
|
||||
@ -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"))]
|
||||
|
@ -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.
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user