瀏覽代碼

feat: add `Webview::show` and `Webview::hide` (#11140)

closes #11126
Amr Bashir 10 月之前
父節點
當前提交
d9d2502b41

+ 6 - 0
.changes/webview-hide-show-api.md

@@ -0,0 +1,6 @@
+---
+"tauri": "patch:feat"
+---
+
+Add `Webview::hide` and `Webview::show` methods.
+

+ 7 - 0
.changes/webview-hide-show-runtime.md

@@ -0,0 +1,7 @@
+---
+"tauri-runtime": "patch:feat"
+"tauri-runtime-wry": "patch:feat"
+---
+
+Add `WebviewDispatch::hide` and `WebviewDispatch::show` methods.
+

+ 6 - 0
.changes/webview-hide-show.md

@@ -0,0 +1,6 @@
+---
+"@tauri-apps/api": "patch:feat"
+---
+
+Add `Webview.hide` and `Webview.show` methods.
+

+ 34 - 0
crates/tauri-runtime-wry/src/lib.rs

@@ -1237,6 +1237,8 @@ pub enum WebviewMessage {
   Navigate(Url),
   Print,
   Close,
+  Show,
+  Hide,
   SetPosition(Position),
   SetSize(Size),
   SetBounds(tauri_runtime::Rect),
@@ -1533,6 +1535,28 @@ impl<T: UserEvent> WebviewDispatch<T> for WryWebviewDispatcher<T> {
       ),
     )
   }
+
+  fn hide(&self) -> Result<()> {
+    send_user_message(
+      &self.context,
+      Message::Webview(
+        *self.window_id.lock().unwrap(),
+        self.webview_id,
+        WebviewMessage::Hide,
+      ),
+    )
+  }
+
+  fn show(&self) -> Result<()> {
+    send_user_message(
+      &self.context,
+      Message::Webview(
+        *self.window_id.lock().unwrap(),
+        self.webview_id,
+        WebviewMessage::Show,
+      ),
+    )
+  }
 }
 
 /// The Tauri [`WindowDispatch`] for [`Wry`].
@@ -3138,6 +3162,16 @@ fn handle_user_message<T: UserEvent>(
               log::error!("failed to navigate to url {}: {}", url, e);
             }
           }
+          WebviewMessage::Show => {
+            if let Err(e) = webview.set_visible(true) {
+              log::error!("failed to change webview visibility: {e}");
+            }
+          }
+          WebviewMessage::Hide => {
+            if let Err(e) = webview.set_visible(false) {
+              log::error!("failed to change webview visibility: {e}");
+            }
+          }
           WebviewMessage::Print => {
             let _ = webview.print();
           }

+ 6 - 0
crates/tauri-runtime/src/lib.rs

@@ -505,6 +505,12 @@ pub trait WebviewDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + '
   /// Bring the window to front and focus the webview.
   fn set_focus(&self) -> Result<()>;
 
+  /// Hide the webview
+  fn hide(&self) -> Result<()>;
+
+  /// Show the webview
+  fn show(&self) -> Result<()>;
+
   /// Executes javascript on the window this [`WindowDispatch`] represents.
   fn eval_script<S: Into<String>>(&self, script: S) -> Result<()>;
 

+ 1 - 1
crates/tauri-utils/src/acl/build.rs

@@ -306,7 +306,7 @@ commands.deny = ["{command}"]
 }
 
 const PERMISSION_TABLE_HEADER: &str =
-  "## Permission Table \n\n<table>\n<tr>\n<th>Identifier</th>\n<th>Description</th>\n</tr>\n";
+  "## Permission Table\n\n<table>\n<tr>\n<th>Identifier</th>\n<th>Description</th>\n</tr>\n";
 
 /// Generate a markdown documentation page containing the list of permissions of the plugin.
 pub fn generate_docs(

+ 2 - 0
crates/tauri/build.rs

@@ -126,6 +126,8 @@ const PLUGINS: &[(&str, &[(&str, bool)])] = &[
       ("set_webview_position", false),
       ("set_webview_focus", false),
       ("set_webview_zoom", false),
+      ("webview_hide", false),
+      ("webview_show", false),
       ("print", false),
       ("reparent", false),
       ("clear_all_browsing_data", false),

+ 1 - 1
crates/tauri/permissions/app/autogenerated/reference.md

@@ -6,7 +6,7 @@ Default permissions for the plugin.
 - `allow-name`
 - `allow-tauri-version`
 
-## Permission Table 
+## Permission Table
 
 <table>
 <tr>

+ 1 - 1
crates/tauri/permissions/event/autogenerated/reference.md

@@ -7,7 +7,7 @@ Default permissions for the plugin.
 - `allow-emit`
 - `allow-emit-to`
 
-## Permission Table 
+## Permission Table
 
 <table>
 <tr>

+ 1 - 1
crates/tauri/permissions/image/autogenerated/reference.md

@@ -8,7 +8,7 @@ Default permissions for the plugin.
 - `allow-rgba`
 - `allow-size`
 
-## Permission Table 
+## Permission Table
 
 <table>
 <tr>

+ 1 - 1
crates/tauri/permissions/menu/autogenerated/reference.md

@@ -25,7 +25,7 @@ Default permissions for the plugin.
 - `allow-set-checked`
 - `allow-set-icon`
 
-## Permission Table 
+## Permission Table
 
 <table>
 <tr>

+ 1 - 1
crates/tauri/permissions/path/autogenerated/reference.md

@@ -11,7 +11,7 @@ Default permissions for the plugin.
 - `allow-basename`
 - `allow-is-absolute`
 
-## Permission Table 
+## Permission Table
 
 <table>
 <tr>

+ 1 - 1
crates/tauri/permissions/resources/autogenerated/reference.md

@@ -4,7 +4,7 @@ Default permissions for the plugin.
 
 - `allow-close`
 
-## Permission Table 
+## Permission Table
 
 <table>
 <tr>

+ 1 - 1
crates/tauri/permissions/tray/autogenerated/reference.md

@@ -14,7 +14,7 @@ Default permissions for the plugin.
 - `allow-set-icon-as-template`
 - `allow-set-show-menu-on-left-click`
 
-## Permission Table 
+## Permission Table
 
 <table>
 <tr>

+ 105 - 1
crates/tauri/permissions/webview/autogenerated/reference.md

@@ -7,7 +7,7 @@ Default permissions for the plugin.
 - `allow-webview-size`
 - `allow-internal-toggle-devtools`
 
-## Permission Table 
+## Permission Table
 
 <table>
 <tr>
@@ -123,6 +123,32 @@ Denies the get_all_webviews command without any pre-configured scope.
 <tr>
 <td>
 
+`core:webview:allow-hide-webview`
+
+</td>
+<td>
+
+Enables the hide_webview command without any pre-configured scope.
+
+</td>
+</tr>
+
+<tr>
+<td>
+
+`core:webview:deny-hide-webview`
+
+</td>
+<td>
+
+Denies the hide_webview command without any pre-configured scope.
+
+</td>
+</tr>
+
+<tr>
+<td>
+
 `core:webview:allow-internal-toggle-devtools`
 
 </td>
@@ -305,6 +331,32 @@ Denies the set_webview_zoom command without any pre-configured scope.
 <tr>
 <td>
 
+`core:webview:allow-show-webview`
+
+</td>
+<td>
+
+Enables the show_webview command without any pre-configured scope.
+
+</td>
+</tr>
+
+<tr>
+<td>
+
+`core:webview:deny-show-webview`
+
+</td>
+<td>
+
+Denies the show_webview command without any pre-configured scope.
+
+</td>
+</tr>
+
+<tr>
+<td>
+
 `core:webview:allow-webview-close`
 
 </td>
@@ -331,6 +383,32 @@ Denies the webview_close command without any pre-configured scope.
 <tr>
 <td>
 
+`core:webview:allow-webview-hide`
+
+</td>
+<td>
+
+Enables the webview_hide command without any pre-configured scope.
+
+</td>
+</tr>
+
+<tr>
+<td>
+
+`core:webview:deny-webview-hide`
+
+</td>
+<td>
+
+Denies the webview_hide command without any pre-configured scope.
+
+</td>
+</tr>
+
+<tr>
+<td>
+
 `core:webview:allow-webview-position`
 
 </td>
@@ -357,6 +435,32 @@ Denies the webview_position command without any pre-configured scope.
 <tr>
 <td>
 
+`core:webview:allow-webview-show`
+
+</td>
+<td>
+
+Enables the webview_show command without any pre-configured scope.
+
+</td>
+</tr>
+
+<tr>
+<td>
+
+`core:webview:deny-webview-show`
+
+</td>
+<td>
+
+Denies the webview_show command without any pre-configured scope.
+
+</td>
+</tr>
+
+<tr>
+<td>
+
 `core:webview:allow-webview-size`
 
 </td>

+ 1 - 1
crates/tauri/permissions/window/autogenerated/reference.md

@@ -27,7 +27,7 @@ Default permissions for the plugin.
 - `allow-theme`
 - `allow-internal-toggle-maximize`
 
-## Permission Table 
+## Permission Table
 
 <table>
 <tr>

文件差異過大導致無法顯示
+ 0 - 0
crates/tauri/scripts/bundle.global.js


+ 8 - 0
crates/tauri/src/test/mock_runtime.rs

@@ -580,6 +580,14 @@ impl<T: UserEvent> WebviewDispatch<T> for MockWebviewDispatcher {
   fn clear_all_browsing_data(&self) -> Result<()> {
     Ok(())
   }
+
+  fn hide(&self) -> Result<()> {
+    Ok(())
+  }
+
+  fn show(&self) -> Result<()> {
+    Ok(())
+  }
 }
 
 impl<T: UserEvent> WindowDispatch<T> for MockWindowDispatcher {

+ 10 - 0
crates/tauri/src/webview/mod.rs

@@ -943,6 +943,16 @@ impl<R: Runtime> Webview<R> {
     self.webview.dispatcher.set_focus().map_err(Into::into)
   }
 
+  /// Hide the webview.
+  pub fn hide(&self) -> crate::Result<()> {
+    self.webview.dispatcher.hide().map_err(Into::into)
+  }
+
+  /// Show the webview.
+  pub fn show(&self) -> crate::Result<()> {
+    self.webview.dispatcher.show().map_err(Into::into)
+  }
+
   /// Move the webview to the given window.
   pub fn reparent(&self, window: &Window<R>) -> crate::Result<()> {
     #[cfg(not(feature = "unstable"))]

+ 4 - 0
crates/tauri/src/webview/plugin.rs

@@ -176,6 +176,8 @@ mod desktop_commands {
   setter!(set_webview_size, set_size, Size);
   setter!(set_webview_position, set_position, Position);
   setter!(set_webview_focus, set_focus);
+  setter!(webview_hide, hide);
+  setter!(webview_show, show);
   setter!(set_webview_zoom, set_zoom, f64);
   setter!(clear_all_browsing_data, clear_all_browsing_data);
 
@@ -261,6 +263,8 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
             desktop_commands::set_webview_position,
             desktop_commands::set_webview_focus,
             desktop_commands::set_webview_zoom,
+            desktop_commands::webview_hide,
+            desktop_commands::webview_show,
             desktop_commands::print,
             desktop_commands::reparent,
             desktop_commands::clear_all_browsing_data,

+ 1 - 1
examples/api/src-tauri/tauri-plugin-sample/permissions/autogenerated/reference.md

@@ -1,5 +1,5 @@
 
-## Permission Table 
+## Permission Table
 
 <table>
 <tr>

+ 32 - 0
packages/api/src/webview.ts

@@ -483,6 +483,38 @@ class Webview {
     })
   }
 
+  /**
+   * Hide the webview.
+   * @example
+   * ```typescript
+   * import { getCurrentWebview } from '@tauri-apps/api/webview';
+   * await getCurrentWebview().hide();
+   * ```
+   *
+   * @returns A promise indicating the success or failure of the operation.
+   */
+  async hide(): Promise<void> {
+    return invoke('plugin:webview|webview_hide', {
+      label: this.label
+    })
+  }
+
+  /**
+   * Show the webview.
+   * @example
+   * ```typescript
+   * import { getCurrentWebview } from '@tauri-apps/api/webview';
+   * await getCurrentWebview().show();
+   * ```
+   *
+   * @returns A promise indicating the success or failure of the operation.
+   */
+  async show(): Promise<void> {
+    return invoke('plugin:webview|webview_show', {
+      label: this.label
+    })
+  }
+
   /**
    * Set webview zoom level.
    * @example

部分文件因文件數量過多而無法顯示