浏览代码

feat(core): add Webview::show and Webview::hide

Lucas Nogueira 1 年之前
父节点
当前提交
12104c2da0

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

@@ -1181,6 +1181,7 @@ pub enum WebviewMessage {
   Close,
   SetPosition(Position),
   SetSize(Size),
+  SetVisible(bool),
   SetBounds(tauri_runtime::Rect),
   SetFocus,
   Reparent(WindowId, Sender<Result<()>>),
@@ -1353,6 +1354,17 @@ impl<T: UserEvent> WebviewDispatch<T> for WryWebviewDispatcher<T> {
     )
   }
 
+  fn set_visible(&self, visible: bool) -> Result<()> {
+    send_user_message(
+      &self.context,
+      Message::Webview(
+        *self.window_id.lock().unwrap(),
+        self.webview_id,
+        WebviewMessage::SetVisible(visible),
+      ),
+    )
+  }
+
   fn set_bounds(&self, bounds: tauri_runtime::Rect) -> Result<()> {
     send_user_message(
       &self.context,
@@ -2910,6 +2922,11 @@ fn handle_user_message<T: UserEvent>(
               window
             });
           }
+          WebviewMessage::SetVisible(visible) => {
+            if let Err(e) = webview.set_visible(visible) {
+              log::error!("failed to change webview visibility: {e}");
+            }
+          }
           WebviewMessage::SetBounds(bounds) => {
             let bounds: RectWrapper = bounds.into();
             let bounds = bounds.0;

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

@@ -460,6 +460,9 @@ pub trait WebviewDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + '
   /// Closes the webview.
   fn close(&self) -> Result<()>;
 
+  /// Sets the webview's visibility flag.
+  fn set_visible(&self, visible: bool) -> Result<()>;
+
   /// Sets the webview's bounds.
   fn set_bounds(&self, bounds: Rect) -> Result<()>;
 

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

@@ -208,6 +208,7 @@ pub struct WebviewAttributes {
   pub auto_resize: bool,
   pub proxy_url: Option<Url>,
   pub zoom_hotkeys_enabled: bool,
+  pub visible: bool,
 }
 
 impl From<&WindowConfig> for WebviewAttributes {
@@ -235,6 +236,7 @@ impl From<&WindowConfig> for WebviewAttributes {
       builder = builder.proxy_url(url.to_owned());
     }
     builder = builder.zoom_hotkeys_enabled(config.zoom_hotkeys_enabled);
+    builder = builder.visible(config.visible);
     builder
   }
 }
@@ -258,6 +260,7 @@ impl WebviewAttributes {
       auto_resize: false,
       proxy_url: None,
       zoom_hotkeys_enabled: false,
+      visible: true,
     }
   }
 
@@ -363,6 +366,13 @@ impl WebviewAttributes {
     self.zoom_hotkeys_enabled = enabled;
     self
   }
+
+  /// Whether the webview is visible or not.
+  #[must_use]
+  pub fn visible(mut self, visible: bool) -> Self {
+    self.visible = visible;
+    self
+  }
 }
 
 /// IPC handler.

+ 2 - 0
core/tauri/build.rs

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

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

@@ -20,8 +20,12 @@
 |`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-hide`|Enables the webview_hide command without any pre-configured scope.|
+|`deny-webview-hide`|Denies the webview_hide command without any pre-configured scope.|
 |`allow-webview-position`|Enables the webview_position command without any pre-configured scope.|
 |`deny-webview-position`|Denies the webview_position command without any pre-configured scope.|
+|`allow-webview-show`|Enables the webview_show command without any pre-configured scope.|
+|`deny-webview-show`|Denies the webview_show command without any pre-configured scope.|
 |`allow-webview-size`|Enables the webview_size command without any pre-configured scope.|
 |`deny-webview-size`|Denies the webview_size command without any pre-configured scope.|
 |`default`|Default permissions for the plugin.|

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

@@ -631,6 +631,7 @@ tauri::Builder::default()
 }
 
 /// Webview attributes.
+#[cfg_attr(not(feature = "unstable"), allow(dead_code))]
 impl<R: Runtime> WebviewBuilder<R> {
   /// Sets whether clicking an inactive window also clicks through to the webview.
   #[must_use]
@@ -790,6 +791,13 @@ fn main() {
     self.webview_attributes.zoom_hotkeys_enabled = enabled;
     self
   }
+
+  /// Whether the webview is visible or not. Defaults to `true`.
+  #[must_use]
+  pub fn visible(mut self, visible: bool) -> Self {
+    self.webview_attributes.visible = visible;
+    self
+  }
 }
 
 /// Webview.
@@ -902,6 +910,24 @@ impl<R: Runtime> Webview<R> {
     Ok(())
   }
 
+  /// Show this window.
+  pub fn show(&self) -> crate::Result<()> {
+    self
+      .webview
+      .dispatcher
+      .set_visible(true)
+      .map_err(Into::into)
+  }
+
+  /// Hide this window.
+  pub fn hide(&self) -> crate::Result<()> {
+    self
+      .webview
+      .dispatcher
+      .set_visible(false)
+      .map_err(Into::into)
+  }
+
   /// Resizes this webview.
   pub fn set_bounds(&self, bounds: Rect) -> crate::Result<()> {
     self

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

@@ -42,6 +42,7 @@ mod desktop_commands {
     incognito: bool,
     #[serde(default)]
     zoom_hotkeys_enabled: bool,
+    visible: Option<bool>,
   }
 
   #[command(root = "crate")]
@@ -80,6 +81,7 @@ mod desktop_commands {
     builder.webview_attributes.window_effects = options.window_effects;
     builder.webview_attributes.incognito = options.incognito;
     builder.webview_attributes.zoom_hotkeys_enabled = options.zoom_hotkeys_enabled;
+    builder.webview_attributes.visible = options.visible.unwrap_or(true);
 
     window.add_child(
       builder,
@@ -157,6 +159,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_show, show);
+  setter!(webview_hide, hide);
   setter!(set_webview_zoom, set_zoom, f64);
 
   #[command(root = "crate")]
@@ -238,6 +242,8 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
             desktop_commands::webview_close,
             desktop_commands::set_webview_size,
             desktop_commands::set_webview_position,
+            desktop_commands::webview_show,
+            desktop_commands::webview_hide,
             desktop_commands::set_webview_focus,
             desktop_commands::set_webview_zoom,
             desktop_commands::print,

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

@@ -463,6 +463,39 @@ class Webview {
       value
     })
   }
+  
+  /**
+   * Sets the webview visibility to true.
+   * @example
+   * ```typescript
+   * import { getCurrent } from '@tauri-apps/api/webview';
+   * await getCurrent().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
+    })
+  }
+
+  /**
+   * Sets the webview visibility to false.
+   * @example
+   * ```typescript
+   * import { getCurrent } from '@tauri-apps/api/webview';
+   * await getCurrent().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
+    })
+  }
+
 
   /**
    * Bring the webview to front and focus.
@@ -677,6 +710,10 @@ interface WebviewOptions {
    * - **Android / iOS**: Unsupported.
    */
   zoomHotkeysEnabled?: boolean
+  /**
+   * Whether the webview should be visible or not. Defaults to `true`.
+   */
+  visible?: boolean
 }
 
 export { Webview, getCurrent, getAll }