浏览代码

feat(windows): Expose webview2 zoom hotkeys from wry (#9352)

* Expose webview2 zoom hotkeys from wry

* Add change file

* Apply suggestions from code review

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>

* Regenerate schema files

---------

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
Tony 1 年之前
父节点
当前提交
9273d7b379

+ 8 - 0
.changes/zoom_hotkeys_enabled.md

@@ -0,0 +1,8 @@
+---
+"@tauri-apps/api": minor:feat
+"tauri": minor:feat
+"tauri-runtime": minor:feat
+"tauri-runtime-wry": minor:feat
+---
+
+Add `zoom_hotkeys_enabled` to enable browser native zoom controls on creating webviews.

+ 5 - 0
core/tauri-config-schema/schema.json

@@ -450,6 +450,11 @@
             "null"
           ],
           "format": "uri"
+        },
+        "zoomHotkeysEnabled": {
+          "description": "Whether page zooming by hotkeys is enabled **Windows Only**",
+          "default": false,
+          "type": "boolean"
         }
       },
       "additionalProperties": false

+ 2 - 1
core/tauri-runtime-wry/src/lib.rs

@@ -3735,7 +3735,8 @@ fn create_webview<T: UserEvent>(
     .with_focused(window.is_focused())
     .with_url(&url)
     .with_transparent(webview_attributes.transparent)
-    .with_accept_first_mouse(webview_attributes.accept_first_mouse);
+    .with_accept_first_mouse(webview_attributes.accept_first_mouse)
+    .with_hotkeys_zoom(webview_attributes.zoom_hotkeys_enabled);
 
   #[cfg(windows)]
   if kind == WebviewKind::WindowContent {

+ 10 - 0
core/tauri-runtime/src/webview.rs

@@ -207,6 +207,7 @@ pub struct WebviewAttributes {
   pub bounds: Option<Rect>,
   pub auto_resize: bool,
   pub proxy_url: Option<Url>,
+  pub zoom_hotkeys_enabled: bool,
 }
 
 impl From<&WindowConfig> for WebviewAttributes {
@@ -233,6 +234,7 @@ impl From<&WindowConfig> for WebviewAttributes {
     if let Some(url) = &config.proxy_url {
       builder = builder.proxy_url(url.to_owned());
     }
+    builder = builder.zoom_hotkeys_enabled(config.zoom_hotkeys_enabled);
     builder
   }
 }
@@ -255,6 +257,7 @@ impl WebviewAttributes {
       bounds: None,
       auto_resize: false,
       proxy_url: None,
+      zoom_hotkeys_enabled: false,
     }
   }
 
@@ -345,6 +348,13 @@ impl WebviewAttributes {
     self.proxy_url = Some(url);
     self
   }
+
+  /// Whether page zooming by hotkeys is enabled
+  #[must_use]
+  pub fn zoom_hotkeys_enabled(mut self, enabled: bool) -> Self {
+    self.zoom_hotkeys_enabled = enabled;
+    self
+  }
 }
 
 /// IPC handler.

+ 7 - 1
core/tauri-utils/src/config.rs

@@ -1293,6 +1293,9 @@ pub struct WindowConfig {
   ///
   /// - **macOS**: Requires the `macos-proxy` feature flag and only compiles for macOS 14+.
   pub proxy_url: Option<Url>,
+  /// Whether page zooming by hotkeys is enabled **Windows Only**
+  #[serde(default)]
+  pub zoom_hotkeys_enabled: bool,
 }
 
 impl Default for WindowConfig {
@@ -1338,6 +1341,7 @@ impl Default for WindowConfig {
       incognito: false,
       parent: None,
       proxy_url: None,
+      zoom_hotkeys_enabled: false,
     }
   }
 }
@@ -2259,6 +2263,7 @@ mod build {
       let window_effects = opt_lit(self.window_effects.as_ref());
       let incognito = self.incognito;
       let parent = opt_str_lit(self.parent.as_ref());
+      let zoom_hotkeys_enabled = self.zoom_hotkeys_enabled;
 
       literal_struct!(
         tokens,
@@ -2302,7 +2307,8 @@ mod build {
         shadow,
         window_effects,
         incognito,
-        parent
+        parent,
+        zoom_hotkeys_enabled
       );
     }
   }

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

@@ -775,6 +775,13 @@ fn main() {
     self.webview_attributes.auto_resize = true;
     self
   }
+
+  /// Whether page zooming by hotkeys is enabled **Windows Only**
+  #[must_use]
+  pub fn zoom_hotkeys_enabled(mut self, enabled: bool) -> Self {
+    self.webview_attributes.zoom_hotkeys_enabled = enabled;
+    self
+  }
 }
 
 /// Webview.

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

@@ -40,6 +40,8 @@ mod desktop_commands {
     window_effects: Option<WindowEffectsConfig>,
     #[serde(default)]
     incognito: bool,
+    #[serde(default)]
+    zoom_hotkeys_enabled: bool,
   }
 
   #[command(root = "crate")]
@@ -77,6 +79,7 @@ mod desktop_commands {
     builder.webview_attributes.accept_first_mouse = options.accept_first_mouse;
     builder.webview_attributes.window_effects = options.window_effects;
     builder.webview_attributes.incognito = options.incognito;
+    builder.webview_attributes.zoom_hotkeys_enabled = options.zoom_hotkeys_enabled;
 
     window.add_child(
       builder,

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

@@ -838,6 +838,13 @@ fn main() {
     self.webview_builder = self.webview_builder.proxy_url(url);
     self
   }
+
+  /// Whether page zooming by hotkeys is enabled **Windows only**
+  #[must_use]
+  pub fn zoom_hotkeys_enabled(mut self, enabled: bool) -> Self {
+    self.webview_builder = self.webview_builder.zoom_hotkeys_enabled(enabled);
+    self
+  }
 }
 
 /// A type that wraps a [`Window`] together with a [`Webview`].

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

@@ -648,6 +648,10 @@ interface WebviewOptions {
    * - **macOS**: Requires the `macos-proxy` feature flag and only compiles for macOS 14+.
    * */
   proxyUrl?: string
+  /**
+   * Whether page zooming by hotkeys or gestures is enabled **Windows Only**
+   */
+  zoomHotkeysEnabled?: boolean
 }
 
 export { Webview, getCurrent, getAll }

+ 5 - 0
tooling/cli/schema.json

@@ -450,6 +450,11 @@
             "null"
           ],
           "format": "uri"
+        },
+        "zoomHotkeysEnabled": {
+          "description": "Whether page zooming by hotkeys is enabled **Windows Only**",
+          "default": false,
+          "type": "boolean"
         }
       },
       "additionalProperties": false