Skip to content

Commit

Permalink
Add with_web_context method (#292)
Browse files Browse the repository at this point in the history
* Add with_web_context method

* Add change file

* cargo fmt
  • Loading branch information
Ngo Iok Ui (Wu Yu Wei) authored Jun 13, 2021
1 parent cc9fc4b commit 48f53a3
Show file tree
Hide file tree
Showing 22 changed files with 59 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .changes/builder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": minor
---

Add `with_web_context` method that can work well with builder pattern.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion examples/custom_data_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_titlebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
2 changes: 1 addition & 1 deletion examples/detect_js_ecma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ fn main() -> wry::Result<()> {
</html>
"#,
)?
.build(&Default::default())?;
.build()?;

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
Expand Down
2 changes: 1 addition & 1 deletion examples/dragndrop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/fullscreen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/html_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/menu_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions examples/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion examples/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn main() -> wry::Result<()> {
.unwrap()
.with_url("https://tauri.studio")
.unwrap()
.build(&Default::default())
.build()
.unwrap();
webviews.insert(id, webview);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn main() -> wry::Result<()> {
</script>
</html>"#,
)?
.build(&Default::default())?;
.build()?;

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
Expand Down
2 changes: 1 addition & 1 deletion examples/validate_webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)]
Expand Down
30 changes: 19 additions & 11 deletions src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ pub struct WebViewAttributes {
pub file_drop_handler: Option<Box<dyn Fn(&Window, FileDropEvent) -> bool>>,
#[cfg(not(feature = "file-drop"))]
file_drop_handler: Option<Box<dyn Fn(&Window, FileDropEvent) -> 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<PathBuf>,
}

impl Default for WebViewAttributes {
Expand All @@ -92,7 +89,6 @@ impl Default for WebViewAttributes {
custom_protocols: vec![],
rpc_handler: None,
file_drop_handler: None,
data_directory: None,
}
}
}
Expand All @@ -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<Self> {
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.
Expand Down Expand Up @@ -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:
Expand All @@ -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<WebView> {
pub fn build(mut self) -> Result<WebView> {
if self.webview.rpc_handler.is_some() {
let js = r#"
(function() {
Expand Down Expand Up @@ -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 })
}
}
Expand Down Expand Up @@ -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<Self> {
WebViewBuilder::new(window)?.build(web_context)
pub fn new(window: Window) -> Result<Self> {
WebViewBuilder::new(window)?.build()
}

/// Get the [`Window`] associate with the [`WebView`]. This can let you perform window related
Expand Down
2 changes: 1 addition & 1 deletion src/webview/web_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
10 changes: 9 additions & 1 deletion src/webview/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,21 @@ impl InnerWebView {
pub fn new(
window: Rc<Window>,
mut attributes: WebViewAttributes,
web_context: &WebContext,
web_context: Option<&WebContext>,
) -> Result<Self> {
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);
Expand Down
8 changes: 5 additions & 3 deletions src/webview/webview2/win32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl InnerWebView {
pub fn new(
window: Rc<Window>,
mut attributes: WebViewAttributes,
web_context: &WebContext,
web_context: Option<&WebContext>,
) -> Result<Self> {
let hwnd = window.hwnd() as HWND;

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/webview/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl InnerWebView {
pub fn new(
window: Rc<Window>,
attributes: WebViewAttributes,
_web_context: &WebContext,
_web_context: Option<&WebContext>,
) -> Result<Self> {
// Function for rpc handler
extern "C" fn did_receive(this: &Object, _: Sel, _: id, msg: id) {
Expand Down

0 comments on commit 48f53a3

Please sign in to comment.