From 01f6b6af760a3cb1c01a2e07b0aeb2427973f52f Mon Sep 17 00:00:00 2001 From: Edgar Geier Date: Mon, 7 Jun 2021 01:26:43 +0200 Subject: [PATCH 1/8] feat: expose `eval` method --- src/webview/mod.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/webview/mod.rs b/src/webview/mod.rs index 277eaa4e6..638c55896 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -354,6 +354,11 @@ impl WebView { Ok(()) } + /// Evaluate javascript code immediately. + pub fn eval(&self, js: &str) -> Result<()> { + self.webview.eval(js) + } + /// Launch print modal for the webview content. pub fn print(&self) -> Result<()> { self.webview.print(); From 4ed6045defd1f83c9ca27000181f8100c2ca2c88 Mon Sep 17 00:00:00 2001 From: Edgar Geier Date: Mon, 7 Jun 2021 01:37:24 +0200 Subject: [PATCH 2/8] Add changefile --- .changes/eval.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changes/eval.md diff --git a/.changes/eval.md b/.changes/eval.md new file mode 100644 index 000000000..66e73f36b --- /dev/null +++ b/.changes/eval.md @@ -0,0 +1,5 @@ +--- +"wry": patch +--- + +Expose `eval` method in the `webview` module. From b9ecf002071ee4cacf0364be4e3ad92f65d916a0 Mon Sep 17 00:00:00 2001 From: Edgar Geier Date: Mon, 7 Jun 2021 15:50:05 +0200 Subject: [PATCH 3/8] Improve doc comment --- src/webview/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/webview/mod.rs b/src/webview/mod.rs index 638c55896..146831c90 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -354,7 +354,10 @@ impl WebView { Ok(()) } - /// Evaluate javascript code immediately. + /// 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 eval(&self, js: &str) -> Result<()> { self.webview.eval(js) } From 4b26923517ec707107cbf35f552cf7838f8076db Mon Sep 17 00:00:00 2001 From: Edgar Geier Date: Mon, 7 Jun 2021 16:15:04 +0200 Subject: [PATCH 4/8] refactor: remove `Dispatcher` and related methods --- .changes/remove-dispatcher.md | 5 +++ examples/multi_window.rs | 10 +---- src/webview/mod.rs | 71 ++--------------------------------- 3 files changed, 9 insertions(+), 77 deletions(-) create mode 100644 .changes/remove-dispatcher.md diff --git a/.changes/remove-dispatcher.md b/.changes/remove-dispatcher.md new file mode 100644 index 000000000..5e0e10266 --- /dev/null +++ b/.changes/remove-dispatcher.md @@ -0,0 +1,5 @@ +--- +"wry": minor +--- + +Remove `Dispatcher` struct and `dispatch_script`, `evaluate_script`, and `dispatcher` methods in the `webview` module. diff --git a/examples/multi_window.rs b/examples/multi_window.rs index eb7c4b60b..93114aa14 100644 --- a/examples/multi_window.rs +++ b/examples/multi_window.rs @@ -75,18 +75,10 @@ fn main() -> wry::Result<()> { .unwrap(); webviews.insert(id, webview2); } else if trigger && instant.elapsed() >= eight_secs { - webviews - .get_mut(&id) - .unwrap() - .dispatch_script("openWindow()") - .unwrap(); + webviews.get_mut(&id).unwrap().eval("openWindow()").unwrap(); trigger = false; } - for webview in webviews.values() { - webview.evaluate_script().unwrap(); - } - if let Event::WindowEvent { window_id, event: WindowEvent::CloseRequested, diff --git a/src/webview/mod.rs b/src/webview/mod.rs index 146831c90..6d6e7ad10 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -21,11 +21,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; @@ -100,22 +96,14 @@ impl Default for WebViewAttributes { pub struct WebViewBuilder { pub webview: WebViewAttributes, window: Window, - tx: Sender, - rx: Receiver, } impl WebViewBuilder { /// Create [`WebViewBuilder`] from provided [`Window`]. pub fn new(window: Window) -> Result { - 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. @@ -145,13 +133,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(mut self, name: String, handler: F) -> Self @@ -270,12 +251,7 @@ impl WebViewBuilder { } let window = Rc::new(self.window); let webview = InnerWebView::new(window.clone(), self.webview)?; - Ok(WebView { - window, - webview, - tx: self.tx, - rx: self.rx, - }) + Ok(WebView { window, webview }) } } @@ -288,8 +264,6 @@ impl WebViewBuilder { pub struct WebView { window: Rc, webview: InnerWebView, - tx: Sender, - rx: Receiver, } // Signal the Window to drop on Linux and Windows. On mac, we need to handle several unsafe code @@ -325,35 +299,12 @@ impl WebView { WebViewBuilder::new(window)?.build() } - /// 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. /// @@ -381,22 +332,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); - -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, From 61f93abfa61f64b9ce11a826c024cb78b721ae56 Mon Sep 17 00:00:00 2001 From: Edgar Geier Date: Tue, 8 Jun 2021 11:11:18 +0200 Subject: [PATCH 5/8] Rename `eval` to `evaluate_script` --- examples/multi_window.rs | 6 +++++- src/webview/mod.rs | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/multi_window.rs b/examples/multi_window.rs index 93114aa14..cee7e880b 100644 --- a/examples/multi_window.rs +++ b/examples/multi_window.rs @@ -75,7 +75,11 @@ fn main() -> wry::Result<()> { .unwrap(); webviews.insert(id, webview2); } else if trigger && instant.elapsed() >= eight_secs { - webviews.get_mut(&id).unwrap().eval("openWindow()").unwrap(); + webviews + .get_mut(&id) + .unwrap() + .evaluate_script("openWindow()") + .unwrap(); trigger = false; } diff --git a/src/webview/mod.rs b/src/webview/mod.rs index 6d6e7ad10..9db4ba915 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -309,7 +309,7 @@ impl WebView { /// [`WebView`]. Use [`EventLoopProxy`] and a custom event to send scripts from other threads. /// /// [`EventLoopProxy`]: crate::application::event_loop::EventLoopProxy - pub fn eval(&self, js: &str) -> Result<()> { + pub fn evaluate_script(&self, js: &str) -> Result<()> { self.webview.eval(js) } From 44af400513f1cd5fc09199e1ad25051435a99881 Mon Sep 17 00:00:00 2001 From: Edgar Geier Date: Tue, 8 Jun 2021 11:11:41 +0200 Subject: [PATCH 6/8] Remove eval.md --- .changes/eval.md | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .changes/eval.md diff --git a/.changes/eval.md b/.changes/eval.md deleted file mode 100644 index 66e73f36b..000000000 --- a/.changes/eval.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"wry": patch ---- - -Expose `eval` method in the `webview` module. From d703fbb52004346771c89a686bf4baad4eea025a Mon Sep 17 00:00:00 2001 From: Edgar Geier Date: Tue, 8 Jun 2021 11:11:59 +0200 Subject: [PATCH 7/8] Fix changefile --- .changes/remove-dispatcher.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changes/remove-dispatcher.md b/.changes/remove-dispatcher.md index 5e0e10266..70558e485 100644 --- a/.changes/remove-dispatcher.md +++ b/.changes/remove-dispatcher.md @@ -2,4 +2,4 @@ "wry": minor --- -Remove `Dispatcher` struct and `dispatch_script`, `evaluate_script`, and `dispatcher` methods in the `webview` module. +Remove `Dispatcher`, `dispatch_script` and `dispatcher` in the `webview` module and add a `js` parameter to `evaluate_script`. From f2a4b38158ea7cdff863716652668af234654461 Mon Sep 17 00:00:00 2001 From: Edgar Geier Date: Tue, 8 Jun 2021 17:51:28 +0200 Subject: [PATCH 8/8] Fix needless borrow --- src/webview/mimetype.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webview/mimetype.rs b/src/webview/mimetype.rs index 2d19a5403..8c7a45232 100644 --- a/src/webview/mimetype.rs +++ b/src/webview/mimetype.rs @@ -63,7 +63,7 @@ impl MimeType { /// infer mimetype from content (or) URI if needed. pub fn parse(content: &[u8], uri: &str) -> String { - let mime = match infer::get(&content) { + let mime = match infer::get(content) { Some(info) => info.mime_type(), None => MIMETYPE_PLAIN, };