Skip to content

Commit

Permalink
feat: add Webview::show and Webview::hide (#11140)
Browse files Browse the repository at this point in the history
closes #11126
  • Loading branch information
amrbashir authored Sep 27, 2024
1 parent 58bab8b commit d9d2502
Show file tree
Hide file tree
Showing 22 changed files with 231 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changes/webview-hide-show-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri": "patch:feat"
---

Add `Webview::hide` and `Webview::show` methods.

7 changes: 7 additions & 0 deletions .changes/webview-hide-show-runtime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"tauri-runtime": "patch:feat"
"tauri-runtime-wry": "patch:feat"
---

Add `WebviewDispatch::hide` and `WebviewDispatch::show` methods.

6 changes: 6 additions & 0 deletions .changes/webview-hide-show.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@tauri-apps/api": "patch:feat"
---

Add `Webview.hide` and `Webview.show` methods.

34 changes: 34 additions & 0 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,8 @@ pub enum WebviewMessage {
Navigate(Url),
Print,
Close,
Show,
Hide,
SetPosition(Position),
SetSize(Size),
SetBounds(tauri_runtime::Rect),
Expand Down Expand Up @@ -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`].
Expand Down Expand Up @@ -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();
}
Expand Down
6 changes: 6 additions & 0 deletions crates/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()>;

Expand Down
2 changes: 1 addition & 1 deletion crates/tauri-utils/src/acl/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions crates/tauri/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri/permissions/app/autogenerated/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Default permissions for the plugin.
- `allow-name`
- `allow-tauri-version`

## Permission Table
## Permission Table

<table>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri/permissions/event/autogenerated/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Default permissions for the plugin.
- `allow-emit`
- `allow-emit-to`

## Permission Table
## Permission Table

<table>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri/permissions/image/autogenerated/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Default permissions for the plugin.
- `allow-rgba`
- `allow-size`

## Permission Table
## Permission Table

<table>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri/permissions/menu/autogenerated/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Default permissions for the plugin.
- `allow-set-checked`
- `allow-set-icon`

## Permission Table
## Permission Table

<table>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri/permissions/path/autogenerated/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Default permissions for the plugin.
- `allow-basename`
- `allow-is-absolute`

## Permission Table
## Permission Table

<table>
<tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Default permissions for the plugin.

- `allow-close`

## Permission Table
## Permission Table

<table>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri/permissions/tray/autogenerated/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
106 changes: 105 additions & 1 deletion crates/tauri/permissions/webview/autogenerated/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Default permissions for the plugin.
- `allow-webview-size`
- `allow-internal-toggle-devtools`

## Permission Table
## Permission Table

<table>
<tr>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri/permissions/window/autogenerated/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Default permissions for the plugin.
- `allow-theme`
- `allow-internal-toggle-maximize`

## Permission Table
## Permission Table

<table>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions crates/tauri/src/test/mock_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions crates/tauri/src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down
4 changes: 4 additions & 0 deletions crates/tauri/src/webview/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

## Permission Table
## Permission Table

<table>
<tr>
Expand Down
Loading

0 comments on commit d9d2502

Please sign in to comment.