diff --git a/examples/test/src/main.rs b/examples/test/src/main.rs index d6ef31e..dd6085e 100644 --- a/examples/test/src/main.rs +++ b/examples/test/src/main.rs @@ -185,7 +185,7 @@ fn main() { InteractiveTest(name="notification::show_notification",test=notification::show_notification()) InteractiveTest(name="global_shortcut::register_all",test=global_shortcut::register_all()) - // Test(name="window::WebviewWindow::new",test=window::create_window()) + Test(name="window::WebviewWindow::new",test=window::create_window()) Terminate // } diff --git a/examples/test/src/window.rs b/examples/test/src/window.rs index 8b1614d..44f26cc 100644 --- a/examples/test/src/window.rs +++ b/examples/test/src/window.rs @@ -2,10 +2,12 @@ use anyhow::ensure; use tauri_sys::window; pub async fn create_window() -> anyhow::Result<()> { - let win = window::WebviewWindowBuilder::new("foo").build()?; + let win = window::WebviewWindowBuilder::new("foo-win") + .set_url("/") + .build() + .await?; ensure!(win.is_visible().await?); - // ensure!(win.label() == "foo".to_string()); win.close().await?; diff --git a/src/window.rs b/src/window.rs index ccbacb5..4cdf86c 100644 --- a/src/window.rs +++ b/src/window.rs @@ -43,7 +43,10 @@ //! ``` //! It is recommended to allowlist only the APIs you use for optimal bundle size and security. -use crate::{event::{Event, Listen, Once}, utils::ArrayIterator}; +use crate::{ + event::{Event, Listen, Once}, + utils::ArrayIterator, +}; use futures::{ channel::{mpsc, oneshot}, Stream, @@ -182,8 +185,8 @@ impl Display for CursorIcon { } } -#[derive(Debug, Default, Clone, Serialize)] -#[serde(rename = "camelCase")] +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] struct WebviewWindowOptions<'a> { url: Option<&'a str>, center: bool, @@ -214,6 +217,40 @@ struct WebviewWindowOptions<'a> { user_agent: Option<&'a str>, } +impl<'a> Default for WebviewWindowOptions<'a> { + fn default() -> Self { + Self { + url: None, + center: false, + x: None, + y: None, + width: None, + height: None, + min_width: None, + min_height: None, + max_width: None, + max_height: None, + resizable: true, + title: None, + fullscreen: false, + focus: true, + transparent: false, + maximized: false, + visible: true, + decorations: true, + always_on_top: false, + skip_taskbar: false, + file_drop_enabled: true, + theme: None, + title_bar_style: None, + hidden_title: false, + accept_first_mouse: true, + tabbing_identifier: None, + user_agent: None, + } + } +} + #[derive(Debug, Default, Clone, Serialize)] pub struct WebviewWindowBuilder<'a> { label: &'a str, @@ -245,7 +282,7 @@ impl<'a> WebviewWindowBuilder<'a> { } /// The initial position. - pub fn set_position(&mut self, position: PhysicalPosition) -> &mut Self { + pub fn set_position(&mut self, position: PhysicalPosition) -> &mut Self { self.inner.x = Some(position.x()); self.inner.y = Some(position.y()); self @@ -387,10 +424,12 @@ impl<'a> WebviewWindowBuilder<'a> { /// Creates a new webview window. /// /// Requires [`allowlist > window > create`](https://tauri.app/v1/api/config#windowallowlistconfig.create) to be enabled. - pub fn build(&self) -> crate::Result { + pub async fn build(&self) -> crate::Result { let opts = serde_wasm_bindgen::to_value(&self.inner)?; - Ok(WebviewWindow(inner::WebviewWindow::new(self.label, opts))) + let win = WebviewWindow(inner::WebviewWindow::new(self.label, opts)); + win.once::<()>("tauri://created").await?; + Ok(win) } } @@ -751,7 +790,7 @@ impl WebviewWindow { } /// Listen to an event emitted by the backend that is tied to the webview window. - /// + /// /// The returned Future will automatically clean up it's underlying event listener when dropped, so no manual unlisten function needs to be called. /// See [Differences to the JavaScript API](../index.html#differences-to-the-javascript-api) for details. #[inline(always)] @@ -774,11 +813,11 @@ impl WebviewWindow { } /// Listen to an one-off event emitted by the backend that is tied to the webview window. - /// + /// /// The returned Future will automatically clean up it's underlying event listener when dropped, so no manual unlisten function needs to be called. /// See [Differences to the JavaScript API](../index.html#differences-to-the-javascript-api) for details. #[inline(always)] - pub async fn once(&self, event: &str) -> crate::Result> + pub async fn once(&self, event: &str) -> crate::Result> where T: DeserializeOwned + 'static, {