diff --git a/.changes/builder.md b/.changes/builder.md new file mode 100644 index 000000000..54ac51ea2 --- /dev/null +++ b/.changes/builder.md @@ -0,0 +1,5 @@ +--- +"wry": minor +--- + +Add `with_web_context` method that can work well with builder pattern. diff --git a/Cargo.toml b/Cargo.toml index b116c7ed1..f9c107ca8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,6 @@ categories = [ "gui" ] [package.metadata.docs.rs] features = [ "dox" ] -rustdoc-args = [ "--cfg", "doc_cfg" ] default-target = "x86_64-unknown-linux-gnu" targets = [ "x86_64-pc-windows-msvc", diff --git a/examples/custom_data_directory.rs b/examples/custom_data_directory.rs index a44517d07..85ea1e0cd 100644 --- a/examples/custom_data_directory.rs +++ b/examples/custom_data_directory.rs @@ -32,7 +32,8 @@ fn main() -> wry::Result<()> { let _webview = WebViewBuilder::new(window) .unwrap() .with_url("https://tauri.studio")? - .build(&web_context)?; + .with_web_context(&web_context) + .build()?; event_loop.run(move |event, _, control_flow| { *control_flow = ControlFlow::Wait; diff --git a/examples/custom_protocol.rs b/examples/custom_protocol.rs index 2dee82e1d..7861aa258 100644 --- a/examples/custom_protocol.rs +++ b/examples/custom_protocol.rs @@ -74,7 +74,7 @@ fn main() -> wry::Result<()> { }) // tell the webview to load the custom protocol .with_url("wry.dev://")? - .build(&Default::default())?; + .build()?; event_loop.run(move |event, _, control_flow| { *control_flow = ControlFlow::Wait; diff --git a/examples/custom_titlebar.rs b/examples/custom_titlebar.rs index fb64c8769..c07323605 100644 --- a/examples/custom_titlebar.rs +++ b/examples/custom_titlebar.rs @@ -119,7 +119,7 @@ fn main() -> wry::Result<()> { .with_url(url)? .with_initialization_script(script) .with_rpc_handler(handler) - .build(&Default::default())?; + .build()?; webviews.insert(webview.window().id(), webview); event_loop.run(move |event, _, control_flow| { diff --git a/examples/detect_js_ecma.rs b/examples/detect_js_ecma.rs index f45644793..a0c6517c0 100644 --- a/examples/detect_js_ecma.rs +++ b/examples/detect_js_ecma.rs @@ -128,7 +128,7 @@ fn main() -> wry::Result<()> { "#, )? - .build(&Default::default())?; + .build()?; event_loop.run(move |event, _, control_flow| { *control_flow = ControlFlow::Wait; diff --git a/examples/dragndrop.rs b/examples/dragndrop.rs index 07e6a1781..5ff3231fc 100644 --- a/examples/dragndrop.rs +++ b/examples/dragndrop.rs @@ -27,7 +27,7 @@ fn main() -> wry::Result<()> { println!("Window 1: {:?}", data); false // Returning true will block the OS default behaviour. }) - .build(&Default::default())?; + .build()?; event_loop.run(move |event, _, control_flow| { *control_flow = ControlFlow::Wait; diff --git a/examples/fullscreen.rs b/examples/fullscreen.rs index 3856302f2..f3b6c3900 100644 --- a/examples/fullscreen.rs +++ b/examples/fullscreen.rs @@ -21,7 +21,7 @@ fn main() -> wry::Result<()> { let webview = WebViewBuilder::new(window) .unwrap() .with_url("https://www.wirple.com/")? - .build(&Default::default())?; + .build()?; event_loop.run(move |event, _, control_flow| { *control_flow = ControlFlow::Wait; diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 5bcaefbbe..5187154ee 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -18,7 +18,7 @@ fn main() -> wry::Result<()> { .build(&event_loop)?; let _webview = WebViewBuilder::new(window)? .with_url("https://tauri.studio")? - .build(&Default::default())?; + .build()?; event_loop.run(move |event, _, control_flow| { *control_flow = ControlFlow::Wait; diff --git a/examples/html_test.rs b/examples/html_test.rs index 4f44df2a7..195f5376b 100644 --- a/examples/html_test.rs +++ b/examples/html_test.rs @@ -18,7 +18,7 @@ fn main() -> wry::Result<()> { .build(&event_loop)?; let _webview = WebViewBuilder::new(window)? .with_url("https://html5test.com")? - .build(&Default::default())?; + .build()?; event_loop.run(move |event, _, control_flow| { *control_flow = ControlFlow::Wait; diff --git a/examples/menu_bar.rs b/examples/menu_bar.rs index 642eaf856..a263335f5 100644 --- a/examples/menu_bar.rs +++ b/examples/menu_bar.rs @@ -93,7 +93,7 @@ fn main() -> wry::Result<()> { // Build the webview let webview = WebViewBuilder::new(window)? .with_url("https://tauri.studio")? - .build(&Default::default())?; + .build()?; // launch WRY process event_loop.run(move |event, _, control_flow| { *control_flow = ControlFlow::Wait; diff --git a/examples/multi_window.rs b/examples/multi_window.rs index e53672fd2..a11d0600f 100644 --- a/examples/multi_window.rs +++ b/examples/multi_window.rs @@ -51,7 +51,8 @@ fn main() -> wry::Result<()> { }"#, ) .with_rpc_handler(handler) - .build(&web_context)?; + .with_web_context(&web_context) + .build()?; let mut webviews = HashMap::new(); webviews.insert(id, webview1); @@ -72,7 +73,8 @@ fn main() -> wry::Result<()> { .unwrap() .with_url(&url) .unwrap() - .build(&web_context) + .with_web_context(&web_context) + .build() .unwrap(); webviews.insert(id, webview2); } else if trigger && instant.elapsed() >= eight_secs { diff --git a/examples/rpc.rs b/examples/rpc.rs index 3f5a713ee..e8d884ad8 100644 --- a/examples/rpc.rs +++ b/examples/rpc.rs @@ -80,7 +80,7 @@ async function getAsyncRpcResult() { .unwrap() .with_url(url)? .with_rpc_handler(handler) - .build(&Default::default())?; + .build()?; event_loop.run(move |event, _, control_flow| { *control_flow = ControlFlow::Wait; diff --git a/examples/system_tray.rs b/examples/system_tray.rs index 2eb589c64..59896c026 100644 --- a/examples/system_tray.rs +++ b/examples/system_tray.rs @@ -66,7 +66,7 @@ fn main() -> wry::Result<()> { .unwrap() .with_url("https://tauri.studio") .unwrap() - .build(&Default::default()) + .build() .unwrap(); webviews.insert(id, webview); } diff --git a/examples/transparent.rs b/examples/transparent.rs index da139aaa2..8d62b8196 100644 --- a/examples/transparent.rs +++ b/examples/transparent.rs @@ -37,7 +37,7 @@ fn main() -> wry::Result<()> { "#, )? - .build(&Default::default())?; + .build()?; event_loop.run(move |event, _, control_flow| { *control_flow = ControlFlow::Wait; diff --git a/examples/validate_webview.rs b/examples/validate_webview.rs index b717aca48..fd98644b6 100644 --- a/examples/validate_webview.rs +++ b/examples/validate_webview.rs @@ -26,7 +26,7 @@ fn main() -> wry::Result<()> { .build(&event_loop)?; let _webview = WebViewBuilder::new(window)? .with_url("https://tauri.studio")? - .build(&Default::default())?; + .build()?; event_loop.run(move |event, _, control_flow| { *control_flow = ControlFlow::Wait; diff --git a/src/lib.rs b/src/lib.rs index e633fc36d..0064fa5f9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,7 +26,7 @@ //! .build(&event_loop)?; //! let _webview = WebViewBuilder::new(window)? //! .with_url("https://tauri.studio")? -//! .build(&Default::default())?; +//! .build()?; //! //! event_loop.run(move |event, _, control_flow| { //! *control_flow = ControlFlow::Wait; @@ -77,7 +77,6 @@ //! [`with_file_drop_handler`]: crate::webview::WebView::with_file_drop_handler //! [`with_custom_protocol`]: crate::webview::WebView::with_custom_protocol -#![cfg_attr(doc_cfg, feature(doc_cfg))] #![allow(clippy::new_without_default)] #![allow(clippy::wrong_self_convention)] #![allow(clippy::type_complexity)] diff --git a/src/webview/mod.rs b/src/webview/mod.rs index 1521d6325..dcac8bf47 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -77,9 +77,6 @@ pub struct WebViewAttributes { pub file_drop_handler: Option bool>>, #[cfg(not(feature = "file-drop"))] file_drop_handler: Option bool>>, - /// Whether the WebView window should have a custom user data path. This is useful in Windows - /// when a bundled application can't have the webview data inside `Program Files`. - pub data_directory: Option, } impl Default for WebViewAttributes { @@ -92,7 +89,6 @@ impl Default for WebViewAttributes { custom_protocols: vec![], rpc_handler: None, file_drop_handler: None, - data_directory: None, } } } @@ -102,17 +98,23 @@ impl Default for WebViewAttributes { /// [`WebViewBuilder`] / [`WebView`] are the basic building blocks to constrcut WebView contents and /// scripts for those who prefer to control fine grained window creation and event handling. /// [`WebViewBuilder`] privides ability to setup initialization before web engine starts. -pub struct WebViewBuilder { +pub struct WebViewBuilder<'a> { pub webview: WebViewAttributes, + web_context: Option<&'a WebContext>, window: Window, } -impl WebViewBuilder { +impl<'a> WebViewBuilder<'a> { /// Create [`WebViewBuilder`] from provided [`Window`]. pub fn new(window: Window) -> Result { let webview = WebViewAttributes::default(); + let web_context = None; - Ok(Self { webview, window }) + Ok(Self { + webview, + web_context, + window, + }) } /// Sets whether the WebView should be transparent. @@ -193,6 +195,12 @@ impl WebViewBuilder { Ok(self) } + /// Set the web context that can share with multiple [`WebView`]s. + pub fn with_web_context(mut self, web_context: &'a WebContext) -> Self { + self.web_context = Some(web_context); + self + } + /// Consume the builder and create the [`WebView`]. /// /// Platform-specific behavior: @@ -201,7 +209,7 @@ impl WebViewBuilder { /// called in the same thread with the [`EventLoop`] you create. /// /// [`EventLoop`]: crate::application::event_loop::EventLoop - pub fn build(mut self, web_context: &WebContext) -> Result { + pub fn build(mut self) -> Result { if self.webview.rpc_handler.is_some() { let js = r#" (function() { @@ -256,7 +264,7 @@ impl WebViewBuilder { self.webview.initialization_scripts.push(js.to_string()); } let window = Rc::new(self.window); - let webview = InnerWebView::new(window.clone(), self.webview, web_context)?; + let webview = InnerWebView::new(window.clone(), self.webview, self.web_context)?; Ok(WebView { window, webview }) } } @@ -301,8 +309,8 @@ impl WebView { /// called in the same thread with the [`EventLoop`] you create. /// /// [`EventLoop`]: crate::application::event_loop::EventLoop - pub fn new(window: Window, web_context: &WebContext) -> Result { - WebViewBuilder::new(window)?.build(web_context) + pub fn new(window: Window) -> Result { + WebViewBuilder::new(window)?.build() } /// Get the [`Window`] associate with the [`WebView`]. This can let you perform window related diff --git a/src/webview/web_context.rs b/src/webview/web_context.rs index 4e0b8f149..d3333bde1 100644 --- a/src/webview/web_context.rs +++ b/src/webview/web_context.rs @@ -8,7 +8,7 @@ use std::path::{Path, PathBuf}; /// [`WebView`]: crate::webview::WebView pub struct WebContext { data: WebContextData, - #[allow(dead_code)] + #[allow(dead_code)] // It's no need on Windows and macOS. os: WebContextImpl, } diff --git a/src/webview/webkitgtk/mod.rs b/src/webview/webkitgtk/mod.rs index e4d6a1e95..ae86e17f6 100644 --- a/src/webview/webkitgtk/mod.rs +++ b/src/webview/webkitgtk/mod.rs @@ -37,13 +37,21 @@ impl InnerWebView { pub fn new( window: Rc, mut attributes: WebViewAttributes, - web_context: &WebContext, + web_context: Option<&WebContext>, ) -> Result { let window_rc = Rc::clone(&window); let window = &window.gtk_window(); // Webview widget let manager = UserContentManager::new(); + let default_context; + let web_context = match web_context { + Some(w) => w, + None => { + default_context = Default::default(); + &default_context + } + }; let context = web_context.context(); let mut webview = WebViewBuilder::new(); webview = webview.web_context(context); diff --git a/src/webview/webview2/win32/mod.rs b/src/webview/webview2/win32/mod.rs index 63a742c80..b099cea9b 100644 --- a/src/webview/webview2/win32/mod.rs +++ b/src/webview/webview2/win32/mod.rs @@ -39,7 +39,7 @@ impl InnerWebView { pub fn new( window: Rc, mut attributes: WebViewAttributes, - web_context: &WebContext, + web_context: Option<&WebContext>, ) -> Result { let hwnd = window.hwnd() as HWND; @@ -51,8 +51,10 @@ impl InnerWebView { let mut webview_builder = webview2::EnvironmentBuilder::new(); - if let Some(data_directory) = web_context.data_directory() { - webview_builder = webview_builder.with_user_data_folder(&data_directory); + if let Some(web_context) = web_context { + if let Some(data_directory) = web_context.data_directory() { + webview_builder = webview_builder.with_user_data_folder(&data_directory); + } } // Webview controller diff --git a/src/webview/wkwebview/mod.rs b/src/webview/wkwebview/mod.rs index abc3f6d14..3a59c710c 100644 --- a/src/webview/wkwebview/mod.rs +++ b/src/webview/wkwebview/mod.rs @@ -62,7 +62,7 @@ impl InnerWebView { pub fn new( window: Rc, attributes: WebViewAttributes, - _web_context: &WebContext, + _web_context: Option<&WebContext>, ) -> Result { // Function for rpc handler extern "C" fn did_receive(this: &Object, _: Sel, _: id, msg: id) {