Skip to content

Commit

Permalink
webdriver support (#281)
Browse files Browse the repository at this point in the history
* Add `Application` back, require it for `WebviewBuilder`

Linux only so far. Until further notice, this entire branch is going to only
work on Linux until I can decide on a good interface to expose WebDriver stuff.

`Application` makes it so that multiple windows can share a single WebContext
and UserContentManager (data directory). This is required for WebDriver because
only a single `WebContext` is allowed to be marked to allow automation. It
seems that macOS and Windows have similar limitations to some extent.

Only the multi_window example has been updated so far.

* Create automated webviews from webdriver clients

again, linux only for now.

updated detect_js_ecma example also

* cargo +nightly fmt

* update env var value for automation

* allow closing the window from the webdriver client

* add support for win32 windows

* add wry::Application support to macOS

* change how ApplicationExt work to match std

* update all examples to use wry::Application

* remove now unneeded uuid dependency

* add webdriver support to new example

* cargo +nightly fmt

* remove automation flag from mac/win ApplicationInner

* allow dead_code in Application inner field

is there a better proper fix? probably - but we will be redoing this interface
soon I imagine.

* cargo +nightly fmt

* update all WebviewBuilder::new calls

* cargo +nightly fmt

* super builder

* update ecma example

* enable automation if specific build env is set

* change build env var to feature

* move the super builder to its own module

* cargo +nightly fmt

* update builder docs

* explicitly allow inlining on wry::Builder

* revert everything to dev

* update webkit2gtk to v2_18 to allow for automation

* set custom cfg on rustdocs to allow for nightly doc features

* simplify application module

* enable nightly doc features when cfg is set

* add WebContext as a required item to `WebViewBuilder`

* add changes file

* move web_context to build from new

* update WebContextData path for windows,macos

* mark WebContextData as private

* update examples and doc tests

* allow os impl WebContextImpl to be dead_code

* fix windows webview builder initialization

* add winrt note to add window close event handler

* add smart link to WebContext docs

Co-authored-by: Ngo Iok Ui (Wu Yu Wei) <[email protected]>

* update WebContext doc smartlink

* remove doc_cfg from empty non-linux WebContextImpls

* fix clippy error in unrelated code

* make clippy happy (new nightly lints)

Co-authored-by: Ngo Iok Ui (Wu Yu Wei) <[email protected]>
  • Loading branch information
chippers and Ngo Iok Ui (Wu Yu Wei) authored Jun 9, 2021
1 parent de4a5fa commit 761b2b5
Show file tree
Hide file tree
Showing 24 changed files with 275 additions and 78 deletions.
5 changes: 5 additions & 0 deletions .changes/web_context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Add `wry::webview::WebContext`. It's now a required argument on `WebViewBuilder::build`.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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 Expand Up @@ -50,7 +51,7 @@ chrono = "0.4.19"
tempfile = "3.2.0"

[target."cfg(target_os = \"linux\")".dependencies]
webkit2gtk = { version = "0.11", features = [ "v2_10" ] }
webkit2gtk = { version = "0.11", features = [ "v2_18" ] }
webkit2gtk-sys = "0.13.0"
gio = "0.9"
glib = "0.10"
Expand Down
6 changes: 3 additions & 3 deletions examples/custom_data_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn main() -> wry::Result<()> {
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
},
webview::WebViewBuilder,
webview::{WebContext, WebViewBuilder},
};

// Use a sample directory at the root of the project
Expand All @@ -24,15 +24,15 @@ fn main() -> wry::Result<()> {
println!("Webview storage path: {:#?}", &test_path);

let event_loop = EventLoop::new();
let web_context = WebContext::new(Some(test_path));
let window = WindowBuilder::new()
.with_title("Hello World")
.build(&event_loop)
.unwrap();
let _webview = WebViewBuilder::new(window)
.unwrap()
.with_url("https://tauri.studio")?
.with_data_directory(test_path)
.build()?;
.build(&web_context)?;

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()?;
.build(&Default::default())?;

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()?;
.build(&Default::default())?;
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()?;
.build(&Default::default())?;

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()?;
.build(&Default::default())?;

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()?;
.build(&Default::default())?;

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()?;
.build(&Default::default())?;

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()?;
.build(&Default::default())?;

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()?;
.build(&Default::default())?;
// launch WRY process
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
Expand Down
9 changes: 5 additions & 4 deletions examples/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ fn main() -> wry::Result<()> {
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
},
webview::{RpcRequest, WebViewBuilder},
webview::{RpcRequest, WebContext, WebViewBuilder},
};

let event_loop = EventLoop::new();
let web_context = WebContext::default();
let window1 = WindowBuilder::new().build(&event_loop).unwrap();

let (window_tx, window_rx) = std::sync::mpsc::channel::<String>();
Expand All @@ -50,7 +51,7 @@ fn main() -> wry::Result<()> {
}"#,
)
.with_rpc_handler(handler)
.build()?;
.build(&web_context)?;
let mut webviews = HashMap::new();
webviews.insert(id, webview1);

Expand All @@ -64,14 +65,14 @@ fn main() -> wry::Result<()> {
let window2 = WindowBuilder::new()
.with_title("RODA RORA DA")
.with_inner_size(PhysicalSize::new(426, 197))
.build(&event_loop)
.build(event_loop)
.unwrap();
let id = window2.id();
let webview2 = WebViewBuilder::new(window2)
.unwrap()
.with_url(&url)
.unwrap()
.build()
.build(&web_context)
.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()?;
.build(&Default::default())?;

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
Expand Down
4 changes: 2 additions & 2 deletions examples/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ fn main() -> wry::Result<()> {
origin: MenuType::SystemTray,
} => {
if menu_id == open_new_window_id {
let window = Window::new(&event_loop).unwrap();
let window = Window::new(event_loop).unwrap();
let id = window.id();
let webview = WebViewBuilder::new(window)
.unwrap()
.with_url("https://tauri.studio")
.unwrap()
.build()
.build(&Default::default())
.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()?;
.build(&Default::default())?;

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()?;
.build(&Default::default())?;

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion 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()?;
//! .build(&Default::default())?;
//!
//! event_loop.run(move |event, _, control_flow| {
//! *control_flow = ControlFlow::Wait;
Expand Down Expand Up @@ -77,6 +77,7 @@
//! [`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
18 changes: 7 additions & 11 deletions src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
//! [`WebView`] struct and associated types.
mod mimetype;
mod web_context;

pub use web_context::WebContext;

#[cfg(target_os = "linux")]
mod webkitgtk;
Expand Down Expand Up @@ -126,13 +129,6 @@ impl WebViewBuilder {
self
}

/// 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 fn with_data_directory(mut self, data_directory: PathBuf) -> Self {
self.webview.data_directory.replace(data_directory);
self
}

/// Register custom file loading protocol
#[cfg(feature = "protocol")]
pub fn with_custom_protocol<F>(mut self, name: String, handler: F) -> Self
Expand Down Expand Up @@ -195,7 +191,7 @@ impl WebViewBuilder {
/// called in the same thread with the [`EventLoop`] you create.
///
/// [`EventLoop`]: crate::application::event_loop::EventLoop
pub fn build(mut self) -> Result<WebView> {
pub fn build(mut self, web_context: &WebContext) -> Result<WebView> {
if self.webview.rpc_handler.is_some() {
let js = r#"
(function() {
Expand Down Expand Up @@ -250,7 +246,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)?;
let webview = InnerWebView::new(window.clone(), self.webview, web_context)?;
Ok(WebView { window, webview })
}
}
Expand Down Expand Up @@ -295,8 +291,8 @@ impl WebView {
/// called in the same thread with the [`EventLoop`] you create.
///
/// [`EventLoop`]: crate::application::event_loop::EventLoop
pub fn new(window: Window) -> Result<Self> {
WebViewBuilder::new(window)?.build()
pub fn new(window: Window, web_context: &WebContext) -> Result<Self> {
WebViewBuilder::new(window)?.build(web_context)
}

/// Get the [`Window`] associate with the [`WebView`]. This can let you perform window related
Expand Down
Loading

0 comments on commit 761b2b5

Please sign in to comment.