From de4a5fa820b1938532223677913e73720885cb54 Mon Sep 17 00:00:00 2001 From: Edgar Geier Date: Wed, 9 Jun 2021 04:32:21 +0200 Subject: [PATCH] refactor: remove `Dispatcher` and related methods, closes #290 (#291) * feat: expose `eval` method * Add changefile * Improve doc comment * refactor: remove `Dispatcher` and related methods * Rename `eval` to `evaluate_script` * Remove eval.md * Fix changefile * Fix needless borrow --- .changes/remove-dispatcher.md | 5 +++ examples/multi_window.rs | 6 +-- src/webview/mimetype.rs | 2 +- src/webview/mod.rs | 75 +++++------------------------------ 4 files changed, 16 insertions(+), 72 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 0000000000..70558e4852 --- /dev/null +++ b/.changes/remove-dispatcher.md @@ -0,0 +1,5 @@ +--- +"wry": minor +--- + +Remove `Dispatcher`, `dispatch_script` and `dispatcher` in the `webview` module and add a `js` parameter to `evaluate_script`. diff --git a/examples/multi_window.rs b/examples/multi_window.rs index eb7c4b60b7..cee7e880b8 100644 --- a/examples/multi_window.rs +++ b/examples/multi_window.rs @@ -78,15 +78,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, diff --git a/src/webview/mimetype.rs b/src/webview/mimetype.rs index 2d19a5403d..8c7a45232a 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, }; diff --git a/src/webview/mod.rs b/src/webview/mod.rs index 277eaa4e65..9db4ba915b 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,33 +299,18 @@ 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. + /// + /// [`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. @@ -373,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,