Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove Dispatcher and related methods, closes #290 #291

Merged
merged 8 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/webview/mimetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
75 changes: 9 additions & 66 deletions src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -100,22 +96,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 Down Expand Up @@ -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<F>(mut self, name: String, handler: F) -> Self
Expand Down Expand Up @@ -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 })
}
}

Expand All @@ -288,8 +264,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 @@ -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.
Expand All @@ -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<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