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

closes #11126
This commit is contained in:
Amr Bashir 2024-09-27 08:30:29 +03:00 committed by GitHub
parent 58bab8b35b
commit d9d2502b41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
22 changed files with 231 additions and 12 deletions

View File

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

View File

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

View File

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

View File

@ -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();
}

View File

@ -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<()>;

View File

@ -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),

View File

@ -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>

File diff suppressed because one or more lines are too long

View File

@ -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 {

View File

@ -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"))]

View File

@ -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,

View File

@ -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