Skip to content

Commit d9d2502

Browse files
authored
feat: add Webview::show and Webview::hide (#11140)
closes #11126
1 parent 58bab8b commit d9d2502

File tree

22 files changed

+231
-12
lines changed

22 files changed

+231
-12
lines changed

.changes/webview-hide-show-api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": "patch:feat"
3+
---
4+
5+
Add `Webview::hide` and `Webview::show` methods.
6+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri-runtime": "patch:feat"
3+
"tauri-runtime-wry": "patch:feat"
4+
---
5+
6+
Add `WebviewDispatch::hide` and `WebviewDispatch::show` methods.
7+

.changes/webview-hide-show.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@tauri-apps/api": "patch:feat"
3+
---
4+
5+
Add `Webview.hide` and `Webview.show` methods.
6+

crates/tauri-runtime-wry/src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,8 @@ pub enum WebviewMessage {
12371237
Navigate(Url),
12381238
Print,
12391239
Close,
1240+
Show,
1241+
Hide,
12401242
SetPosition(Position),
12411243
SetSize(Size),
12421244
SetBounds(tauri_runtime::Rect),
@@ -1533,6 +1535,28 @@ impl<T: UserEvent> WebviewDispatch<T> for WryWebviewDispatcher<T> {
15331535
),
15341536
)
15351537
}
1538+
1539+
fn hide(&self) -> Result<()> {
1540+
send_user_message(
1541+
&self.context,
1542+
Message::Webview(
1543+
*self.window_id.lock().unwrap(),
1544+
self.webview_id,
1545+
WebviewMessage::Hide,
1546+
),
1547+
)
1548+
}
1549+
1550+
fn show(&self) -> Result<()> {
1551+
send_user_message(
1552+
&self.context,
1553+
Message::Webview(
1554+
*self.window_id.lock().unwrap(),
1555+
self.webview_id,
1556+
WebviewMessage::Show,
1557+
),
1558+
)
1559+
}
15361560
}
15371561

15381562
/// The Tauri [`WindowDispatch`] for [`Wry`].
@@ -3138,6 +3162,16 @@ fn handle_user_message<T: UserEvent>(
31383162
log::error!("failed to navigate to url {}: {}", url, e);
31393163
}
31403164
}
3165+
WebviewMessage::Show => {
3166+
if let Err(e) = webview.set_visible(true) {
3167+
log::error!("failed to change webview visibility: {e}");
3168+
}
3169+
}
3170+
WebviewMessage::Hide => {
3171+
if let Err(e) = webview.set_visible(false) {
3172+
log::error!("failed to change webview visibility: {e}");
3173+
}
3174+
}
31413175
WebviewMessage::Print => {
31423176
let _ = webview.print();
31433177
}

crates/tauri-runtime/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,12 @@ pub trait WebviewDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + '
505505
/// Bring the window to front and focus the webview.
506506
fn set_focus(&self) -> Result<()>;
507507

508+
/// Hide the webview
509+
fn hide(&self) -> Result<()>;
510+
511+
/// Show the webview
512+
fn show(&self) -> Result<()>;
513+
508514
/// Executes javascript on the window this [`WindowDispatch`] represents.
509515
fn eval_script<S: Into<String>>(&self, script: S) -> Result<()>;
510516

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ commands.deny = ["{command}"]
306306
}
307307

308308
const PERMISSION_TABLE_HEADER: &str =
309-
"## Permission Table \n\n<table>\n<tr>\n<th>Identifier</th>\n<th>Description</th>\n</tr>\n";
309+
"## Permission Table\n\n<table>\n<tr>\n<th>Identifier</th>\n<th>Description</th>\n</tr>\n";
310310

311311
/// Generate a markdown documentation page containing the list of permissions of the plugin.
312312
pub fn generate_docs(

crates/tauri/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ const PLUGINS: &[(&str, &[(&str, bool)])] = &[
126126
("set_webview_position", false),
127127
("set_webview_focus", false),
128128
("set_webview_zoom", false),
129+
("webview_hide", false),
130+
("webview_show", false),
129131
("print", false),
130132
("reparent", false),
131133
("clear_all_browsing_data", false),

crates/tauri/permissions/app/autogenerated/reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Default permissions for the plugin.
66
- `allow-name`
77
- `allow-tauri-version`
88

9-
## Permission Table
9+
## Permission Table
1010

1111
<table>
1212
<tr>

crates/tauri/permissions/event/autogenerated/reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Default permissions for the plugin.
77
- `allow-emit`
88
- `allow-emit-to`
99

10-
## Permission Table
10+
## Permission Table
1111

1212
<table>
1313
<tr>

crates/tauri/permissions/image/autogenerated/reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Default permissions for the plugin.
88
- `allow-rgba`
99
- `allow-size`
1010

11-
## Permission Table
11+
## Permission Table
1212

1313
<table>
1414
<tr>

0 commit comments

Comments
 (0)