Skip to content

Commit

Permalink
Merge branch 'dev' into feat/webdriver
Browse files Browse the repository at this point in the history
  • Loading branch information
Ngo Iok Ui committed Jun 9, 2021
2 parents 08c646c + de4a5fa commit 03f60ad
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 72 deletions.
5 changes: 5 additions & 0 deletions .changes/remove-dispatcher.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": minor
---

Remove `Dispatcher`, `dispatch_script` and `dispatcher` in the `webview` module and add a `js` parameter to `evaluate_script`.
6 changes: 1 addition & 5 deletions examples/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,11 @@ fn main() -> wry::Result<()> {
webviews
.get_mut(&id)
.unwrap()
.dispatch_script("openWindow()")
.evaluate_script("openWindow()")
.unwrap();
trigger = false;
}

for webview in webviews.values() {
webview.evaluate_script().unwrap();
}

if let Event::WindowEvent {
window_id,
event: WindowEvent::CloseRequested,
Expand Down
77 changes: 10 additions & 67 deletions src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ use self::webview2::*;

use crate::{Error, Result};

use std::{
path::PathBuf,
rc::Rc,
sync::mpsc::{channel, Receiver, Sender},
};
use std::{path::PathBuf, rc::Rc};

use serde_json::Value;
use url::Url;
Expand Down Expand Up @@ -103,22 +99,14 @@ impl Default for WebViewAttributes {
pub struct WebViewBuilder {
pub webview: WebViewAttributes,
window: Window,
tx: Sender<String>,
rx: Receiver<String>,
}

impl WebViewBuilder {
/// Create [`WebViewBuilder`] from provided [`Window`].
pub fn new(window: Window) -> Result<Self> {
let (tx, rx) = channel();
let webview = WebViewAttributes::default();

Ok(Self {
webview,
window,
tx,
rx,
})
Ok(Self { webview, window })
}

/// Sets whether the WebView should be transparent.
Expand All @@ -141,13 +129,6 @@ impl WebViewBuilder {
self
}

/// Create a [`Dispatcher`] to send evaluation scripts to the WebView. [`WebView`] is not thread
/// safe because it must be run on the main thread who creates it. [`Dispatcher`] can let you
/// send the scripts from other threads.
pub fn dispatcher(&self) -> Dispatcher {
Dispatcher(self.tx.clone())
}

/// 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 @@ -265,13 +246,8 @@ 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)?;
Ok(WebView {
window,
webview,
tx: self.tx,
rx: self.rx,
})
let webview = InnerWebView::new(window.clone(), self.webview)?;
Ok(WebView { window, webview })
}
}

Expand All @@ -284,8 +260,6 @@ impl WebViewBuilder {
pub struct WebView {
window: Rc<Window>,
webview: InnerWebView,
tx: Sender<String>,
rx: Receiver<String>,
}

// Signal the Window to drop on Linux and Windows. On mac, we need to handle several unsafe code
Expand Down Expand Up @@ -321,33 +295,18 @@ impl WebView {
WebViewBuilder::new(window)?.build(web_context)
}

/// Dispatch javascript code to be evaluated later. Note this will not actually run the
/// scripts being dispatched. Users need to call [`WebView::evaluate_script`] to execute them.
pub fn dispatch_script(&mut self, js: &str) -> Result<()> {
self.tx.send(js.to_string())?;
Ok(())
}

/// Create a [`Dispatcher`] to send evaluation scripts to the WebView. [`WebView`] is not thread
/// safe because it must be run on the main thread who creates it. [`Dispatcher`] can let you
/// send the scripts from other threads.
pub fn dispatcher(&self) -> Dispatcher {
Dispatcher(self.tx.clone())
}

/// Get the [`Window`] associate with the [`WebView`]. This can let you perform window related
/// actions.
pub fn window(&self) -> &Window {
&self.window
}

/// Evaluate the scripts sent from [`Dispatcher`]s.
pub fn evaluate_script(&self) -> Result<()> {
while let Ok(js) = self.rx.try_recv() {
self.webview.eval(&js)?;
}

Ok(())
/// Evaluate and run javascript code. Must be called on the same thread who created the
/// [`WebView`]. Use [`EventLoopProxy`] and a custom event to send scripts from other threads.
///
/// [`EventLoopProxy`]: crate::application::event_loop::EventLoopProxy
pub fn evaluate_script(&self, js: &str) -> Result<()> {
self.webview.eval(js)
}

/// Launch print modal for the webview content.
Expand All @@ -369,22 +328,6 @@ impl WebView {
}
}

#[derive(Clone)]
/// A channel sender to dispatch javascript code to for the [`WebView`] to evaluate it.
///
/// [`WebView`] is not thread safe because it must be run on main thread who creates it.
/// [`Dispatcher`] can let you send scripts from other thread.
pub struct Dispatcher(Sender<String>);

impl Dispatcher {
/// Dispatch javascript code to be evaluated later. Note this will not actually run the
/// scripts being dispatched. Users need to call [`WebView::evaluate_script`] to execute them.
pub fn dispatch_script(&self, js: &str) -> Result<()> {
self.0.send(js.to_string())?;
Ok(())
}
}

// Helper so all platforms handle RPC messages consistently.
fn rpc_proxy(
window: &Window,
Expand Down

0 comments on commit 03f60ad

Please sign in to comment.