Эх сурвалжийг харах

feat: Expose webview zoom (#9378)

* Expose webview zoom

* Add js side support

* Generate bundle script

* Format

* Add change file
Tony 1 жил өмнө
parent
commit
58a7a552d7

+ 8 - 0
.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.

+ 0 - 0
.changes/zoom_hotkeys_enabled.md → .changes/zoom-hotkeys-enabled.md


+ 17 - 0
core/tauri-runtime-wry/src/lib.rs

@@ -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(

+ 3 - 0
core/tauri-runtime/src/lib.rs

@@ -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.

+ 1 - 0
core/tauri/build.rs

@@ -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

+ 2 - 0
core/tauri/permissions/webview/autogenerated/reference.md

@@ -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.|

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
core/tauri/scripts/bundle.global.js


+ 4 - 0
core/tauri/src/test/mock_runtime.rs

@@ -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

+ 15 - 0
core/tauri/src/webview/mod.rs

@@ -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.

+ 2 - 0
core/tauri/src/webview/plugin.rs

@@ -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"))]

+ 11 - 0
core/tauri/src/webview/webview_window.rs

@@ -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.

+ 17 - 0
tooling/api/src/webview.ts

@@ -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

Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно