From db880a61e385bc91489035b5106f98d7165e5e74 Mon Sep 17 00:00:00 2001 From: Ngo Iok Ui Date: Sat, 10 Apr 2021 04:37:21 +0800 Subject: [PATCH 01/14] Remove WV trait on windows --- src/webview/mod.rs | 17 ----------------- src/webview/win32/mod.rs | 12 ++++-------- 2 files changed, 4 insertions(+), 25 deletions(-) diff --git a/src/webview/mod.rs b/src/webview/mod.rs index 86a1ed0a2..3ef0d4f52 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -373,23 +373,6 @@ impl Dispatcher { } } -pub(crate) trait WV: Sized { - type Window; - - fn new Result>>( - window: &Self::Window, - scripts: Vec, - url: Option, - transparent: bool, - custom_protocol: Option<(String, F)>, - rpc_handler: Option, - file_drop_handler: Option, - user_data_path: Option, - ) -> Result; - - fn eval(&self, js: &str) -> Result<()>; -} - const RPC_VERSION: &str = "2.0"; /// RPC request message. diff --git a/src/webview/win32/mod.rs b/src/webview/win32/mod.rs index 81436cfe5..ec2ac64c6 100644 --- a/src/webview/win32/mod.rs +++ b/src/webview/win32/mod.rs @@ -1,7 +1,7 @@ mod file_drop; use crate::{ - webview::{mimetype::MimeType, WV}, + webview::mimetype::MimeType, FileDropHandler, Result, RpcHandler, }; @@ -24,10 +24,8 @@ pub struct InnerWebView { file_drop_controller: Rc>, } -impl WV for InnerWebView { - type Window = Window; - - fn new Result>>( +impl InnerWebView { + pub fn new Result>>( window: &Window, scripts: Vec, url: Option, @@ -200,16 +198,14 @@ impl WV for InnerWebView { }) } - fn eval(&self, js: &str) -> Result<()> { + pub fn eval(&self, js: &str) -> Result<()> { if let Some(c) = self.controller.get() { let webview = c.get_webview()?; webview.execute_script(js, |_| (Ok(())))?; } Ok(()) } -} -impl InnerWebView { pub fn resize(&self, hwnd: *mut c_void) -> Result<()> { let hwnd = hwnd as HWND; From 3255dda494a0a95681f96f755711b7d66e017d3c Mon Sep 17 00:00:00 2001 From: Ngo Iok Ui Date: Sat, 10 Apr 2021 12:39:56 +0800 Subject: [PATCH 02/14] Remove WV trait on Linux --- src/webview/linux/mod.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/webview/linux/mod.rs b/src/webview/linux/mod.rs index bd7d2d26b..3060035ec 100644 --- a/src/webview/linux/mod.rs +++ b/src/webview/linux/mod.rs @@ -12,7 +12,7 @@ use webkit2gtk::{ }; use crate::{ - webview::{mimetype::MimeType, FileDropHandler, WV}, + webview::{mimetype::MimeType, FileDropHandler}, Error, Result, RpcHandler, }; @@ -22,10 +22,8 @@ pub struct InnerWebView { webview: Rc, } -impl WV for InnerWebView { - type Window = Window; - - fn new Result>>( +impl InnerWebView { + pub fn new Result>>( window: &Window, scripts: Vec, url: Option, @@ -155,14 +153,12 @@ impl WV for InnerWebView { Ok(w) } - fn eval(&self, js: &str) -> Result<()> { + pub fn eval(&self, js: &str) -> Result<()> { let cancellable: Option<&Cancellable> = None; self.webview.run_javascript(js, cancellable, |_| ()); Ok(()) } -} -impl InnerWebView { fn init(&self, js: &str) -> Result<()> { if let Some(manager) = self.webview.get_user_content_manager() { let script = UserScript::new( From ac4e1a4002c9e99d9c43459304da30f56bede28c Mon Sep 17 00:00:00 2001 From: Yu-Wei Wu Date: Sat, 10 Apr 2021 12:44:34 +0800 Subject: [PATCH 03/14] Remove WV trait on macOS --- src/webview/macos/mod.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/webview/macos/mod.rs b/src/webview/macos/mod.rs index be0520fcb..e624a0974 100644 --- a/src/webview/macos/mod.rs +++ b/src/webview/macos/mod.rs @@ -22,7 +22,7 @@ use winit::{platform::macos::WindowExtMacOS, window::Window}; use file_drop::{add_file_drop_methods, set_file_drop_handler}; use crate::{ - webview::{mimetype::MimeType, FileDropHandler, WV}, + webview::{mimetype::MimeType, FileDropHandler}, Result, RpcHandler, }; @@ -33,10 +33,8 @@ pub struct InnerWebView { manager: id, } -impl WV for InnerWebView { - type Window = Window; - - fn new Result>>( +impl InnerWebView { + pub fn new Result>>( window: &Window, scripts: Vec, url: Option, @@ -261,16 +259,14 @@ impl WV for InnerWebView { } } - fn eval(&self, js: &str) -> Result<()> { + pub fn eval(&self, js: &str) -> Result<()> { // Safety: objc runtime calls are unsafe unsafe { let _: id = msg_send![self.webview, evaluateJavaScript:NSString::new(js) completionHandler:null::<*const c_void>()]; } Ok(()) } -} -impl InnerWebView { fn init(&self, js: &str) { // Safety: objc runtime calls are unsafe // Equivalent Obj-C: From 328e8a53585049b29b279953ce7c109fe97fb0cf Mon Sep 17 00:00:00 2001 From: Ngo Iok Ui Date: Sat, 10 Apr 2021 12:55:39 +0800 Subject: [PATCH 04/14] Update custom protocol parameter to Vec on Linux --- src/webview/linux/mod.rs | 4 ++-- src/webview/mod.rs | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/webview/linux/mod.rs b/src/webview/linux/mod.rs index 3060035ec..169d11af4 100644 --- a/src/webview/linux/mod.rs +++ b/src/webview/linux/mod.rs @@ -28,7 +28,7 @@ impl InnerWebView { scripts: Vec, url: Option, transparent: bool, - custom_protocol: Option<(String, F)>, + custom_protocols: Vec<(String, F)>, rpc_handler: Option, file_drop_handler: Option, _user_data_path: Option, @@ -116,7 +116,7 @@ impl InnerWebView { } // Custom protocol - if let Some((name, handler)) = custom_protocol { + for (name, handler) in custom_protocols { context .get_security_manager() .ok_or(Error::MissingManager)? diff --git a/src/webview/mod.rs b/src/webview/mod.rs index 3ef0d4f52..004650e86 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -109,7 +109,7 @@ pub struct WebViewBuilder { initialization_scripts: Vec, window: Window, url: Option, - custom_protocol: Option<(String, Box Result>>)>, + custom_protocols: Vec<(String, Box Result>>)>, rpc_handler: Option, file_drop_handler: Option, user_data_path: Option, @@ -134,7 +134,7 @@ impl WebViewBuilder { window, url: None, transparent: false, - custom_protocol: None, + custom_protocols: vec![], rpc_handler: None, file_drop_handler: None, user_data_path: None, @@ -175,7 +175,7 @@ impl WebViewBuilder { where F: Fn(&str) -> Result> + 'static, { - self.custom_protocol = Some((name, Box::new(handler))); + self.custom_protocols.push((name, Box::new(handler))); self } @@ -253,7 +253,7 @@ impl WebViewBuilder { self.initialization_scripts, self.url, self.transparent, - self.custom_protocol, + self.custom_protocols, self.rpc_handler, self.file_drop_handler, self.user_data_path, @@ -293,14 +293,14 @@ impl WebView { /// many more before starting WebView. To benefit from above features, create a /// [`WebViewBuilder`] instead. pub fn new_with_configs(window: Window, transparent: bool) -> Result { - let picky_none: Option<(String, Box Result>>)> = None; + let picky_vec: Vec<(String, Box Result>>)> = Vec::new(); let webview = InnerWebView::new( &window, vec![], None, transparent, - picky_none, + picky_vec, None, None, None, From 566e71a3b0e035699513cfbcf6a20833eecbc51c Mon Sep 17 00:00:00 2001 From: Yu-Wei Wu Date: Sat, 10 Apr 2021 13:04:43 +0800 Subject: [PATCH 05/14] Update custom protocol parameter to Vec on macOS --- src/webview/macos/mod.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/webview/macos/mod.rs b/src/webview/macos/mod.rs index e624a0974..e0c330814 100644 --- a/src/webview/macos/mod.rs +++ b/src/webview/macos/mod.rs @@ -39,7 +39,7 @@ impl InnerWebView { scripts: Vec, url: Option, transparent: bool, - custom_protocol: Option<(String, F)>, + custom_protocols: Vec<(String, F)>, rpc_handler: Option, file_drop_handler: Option, _user_data_path: Option, @@ -110,8 +110,9 @@ impl InnerWebView { unsafe { // Config and custom protocol let config: id = msg_send![class!(WKWebViewConfiguration), new]; - if let Some((name, function)) = custom_protocol { - let cls = ClassDecl::new("CustomURLSchemeHandler", class!(NSObject)); + for (name, function) in custom_protocols { + let scheme_name = format!("{}URLSchemeHandler", name); + let cls = ClassDecl::new(&scheme_name, class!(NSObject)); let cls = match cls { Some(mut cls) => { cls.add_ivar::<*mut c_void>("function"); @@ -125,7 +126,7 @@ impl InnerWebView { ); cls.register() } - None => class!(CustomURLSchemeHandler), + None => class!(scheme_name), }; let handler: id = msg_send![cls, new]; let function: Box Result>>> = Box::new(Box::new(function)); From 701b294b7303c1f10ac00200bec730db945b8bb9 Mon Sep 17 00:00:00 2001 From: Ngo Iok Ui Date: Sat, 10 Apr 2021 13:22:46 +0800 Subject: [PATCH 06/14] Update custom protocol parameter to Vec on Windows --- src/webview/win32/mod.rs | 30 +++++++++++++++--------------- src/webview/winrt/mod.rs | 38 +++++++++++++++++--------------------- 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/src/webview/win32/mod.rs b/src/webview/win32/mod.rs index ec2ac64c6..8234b9b57 100644 --- a/src/webview/win32/mod.rs +++ b/src/webview/win32/mod.rs @@ -7,7 +7,7 @@ use crate::{ use file_drop::FileDropController; -use std::{os::raw::c_void, path::PathBuf, rc::Rc}; +use std::{os::raw::c_void, path::PathBuf, rc::Rc, collections::HashSet}; use once_cell::unsync::OnceCell; use url::Url; @@ -32,7 +32,7 @@ impl InnerWebView { // TODO default background color option just adds to webview2 recently and it requires // canary build. Implement this once it's in official release. transparent: bool, - custom_protocol: Option<(String, F)>, + custom_protocols: Vec<(String, F)>, rpc_handler: Option, file_drop_handler: Option, user_data_path: Option, @@ -107,15 +107,16 @@ impl InnerWebView { Ok(()) })?; - let mut custom_protocol_name = None; - if let Some((name, function)) = custom_protocol { + let mut custom_protocol_names = HashSet::new(); + for (name, function) in custom_protocols { // WebView2 doesn't support non-standard protocols yet, so we have to use this workaround // See https://github.com/MicrosoftEdge/WebView2Feedback/issues/73 - custom_protocol_name = Some(name.clone()); + custom_protocol_names.insert(name.clone()); w.add_web_resource_requested_filter( &format!("file://custom-protocol-{}*", name), webview2::WebResourceContext::All, )?; + let env_clone = env_.clone(); w.add_web_resource_requested(move |_, args| { let uri = args.get_request()?.get_uri()?; // Undo the protocol workaround when giving path to resolver @@ -128,7 +129,7 @@ impl InnerWebView { Ok(content) => { let mime = MimeType::parse(&content, &uri); let stream = webview2::Stream::from_bytes(&content); - let response = env_.create_web_resource_response( + let response = env_clone.create_web_resource_response( stream, 200, "OK", @@ -164,15 +165,14 @@ impl InnerWebView { } } else { let mut url_string = String::from(url.as_str()); - if let Some(name) = custom_protocol_name { - if name == url.scheme() { - // WebView2 doesn't support non-standard protocols yet, so we have to use this workaround - // See https://github.com/MicrosoftEdge/WebView2Feedback/issues/73 - url_string = url.as_str().replace( - &format!("{}://", name), - &format!("file://custom-protocol-{}", name), - ) - } + let name = url.scheme(); + if custom_protocol_names.contains(name) { + // WebView2 doesn't support non-standard protocols yet, so we have to use this workaround + // See https://github.com/MicrosoftEdge/WebView2Feedback/issues/73 + url_string = url.as_str().replace( + &format!("{}://", name), + &format!("file://custom-protocol-{}", name), + ) } w.navigate(&url_string)?; } diff --git a/src/webview/winrt/mod.rs b/src/webview/winrt/mod.rs index 4e8ff3751..d38c64c6a 100644 --- a/src/webview/winrt/mod.rs +++ b/src/webview/winrt/mod.rs @@ -13,13 +13,14 @@ use windows_webview2::{ }; use crate::{ - webview::{mimetype::MimeType, WV}, + webview::mimetype::MimeType, FileDropHandler, Result, RpcHandler, }; use file_drop::FileDropController; use std::{ + collections::HashSet, path::PathBuf, rc::Rc, sync::mpsc::{self, RecvError}, @@ -43,17 +44,15 @@ pub struct InnerWebView { file_drop_controller: Rc>, } -impl WV for InnerWebView { - type Window = Window; - - fn new Result>>( +impl InnerWebView { + pub fn new Result>>( window: &Window, scripts: Vec, url: Option, // TODO default background color option just adds to webview2 recently and it requires // canary build. Implement this once it's in official release. #[allow(unused_variables)] transparent: bool, - custom_protocol: Option<(String, F)>, + custom_protocols: Vec<(String, F)>, rpc_handler: Option, file_drop_handler: Option, user_data_path: Option, @@ -132,11 +131,11 @@ impl WV for InnerWebView { Ok(()) }))?; - let mut custom_protocol_name = None; - if let Some((name, function)) = custom_protocol { + let mut custom_protocol_names = HashSet::new(); + for (name, function) in custom_protocols { // WebView2 doesn't support non-standard protocols yet, so we have to use this workaround // See https://github.com/MicrosoftEdge/WebView2Feedback/issues/73 - custom_protocol_name = Some(name.clone()); + custom_protocol_names.insert(name.clone()); w.AddWebResourceRequestedFilter( format!("file://custom-protocol-{}*", name).as_str(), webview2::CoreWebView2WebResourceContext::All, @@ -199,15 +198,14 @@ impl WV for InnerWebView { } } else { let mut url_string = String::from(url.as_str()); - if let Some(name) = custom_protocol_name { - if name == url.scheme() { - // WebView2 doesn't support non-standard protocols yet, so we have to use this workaround - // See https://github.com/MicrosoftEdge/WebView2Feedback/issues/73 - url_string = url.as_str().replace( - &format!("{}://", name), - &format!("file://custom-protocol-{}", name), - ) - } + let name = url.scheme(); + if custom_protocol_names.contains(name) { + // WebView2 doesn't support non-standard protocols yet, so we have to use this workaround + // See https://github.com/MicrosoftEdge/WebView2Feedback/issues/73 + url_string = url.as_str().replace( + &format!("{}://", name), + &format!("file://custom-protocol-{}", name), + ) } w.Navigate(url_string.as_str())?; } @@ -231,15 +229,13 @@ impl WV for InnerWebView { }) } - fn eval(&self, js: &str) -> Result<()> { + pub fn eval(&self, js: &str) -> Result<()> { if let Some(w) = self.webview.get() { let _ = w.ExecuteScriptAsync(js)?; } Ok(()) } -} -impl InnerWebView { pub fn resize(&self, hwnd: HWND) -> Result<()> { // Safety: System calls are unsafe unsafe { From ba109173369fd45e3ee4353bd0d1abde29e563b7 Mon Sep 17 00:00:00 2001 From: Ngo Iok Ui Date: Sat, 10 Apr 2021 13:47:13 +0800 Subject: [PATCH 07/14] Remove traits in application layer on winit --- src/application/general.rs | 25 +++++++++++-------------- src/application/mod.rs | 31 ------------------------------- 2 files changed, 11 insertions(+), 45 deletions(-) diff --git a/src/application/general.rs b/src/application/general.rs index 9e91561a4..da33bb883 100644 --- a/src/application/general.rs +++ b/src/application/general.rs @@ -1,5 +1,5 @@ use crate::{ - application::{App, AppProxy, InnerWebViewAttributes, InnerWindowAttributes}, + application::{InnerWebViewAttributes, InnerWindowAttributes}, ApplicationProxy, Attributes, CustomProtocol, Error, Event as WryEvent, Icon, Message, Result, WebView, WebViewBuilder, WindowEvent as WryWindowEvent, WindowFileDropHandler, WindowMessage, WindowProxy, WindowRpcHandler, @@ -54,8 +54,8 @@ pub struct InnerApplicationProxy { receiver: Arc>>, } -impl AppProxy for InnerApplicationProxy { - fn send_message(&self, message: Message) -> Result<()> { +impl InnerApplicationProxy { + pub fn send_message(&self, message: Message) -> Result<()> { self .proxy .send_event(message) @@ -63,7 +63,7 @@ impl AppProxy for InnerApplicationProxy { Ok(()) } - fn add_window( + pub fn add_window( &self, attributes: Attributes, file_drop_handler: Option, @@ -81,7 +81,7 @@ impl AppProxy for InnerApplicationProxy { Ok(receiver.recv()?) } - fn listen_event(&self) -> Result { + pub fn listen_event(&self) -> Result { let rx = self.receiver.lock().unwrap(); Ok(rx.recv()?) } @@ -129,11 +129,8 @@ pub struct InnerApplication { event_channel: (Sender, Arc>>), } -impl App for InnerApplication { - type Id = WindowId; - type Proxy = InnerApplicationProxy; - - fn new() -> Result { +impl InnerApplication { + pub fn new() -> Result { let event_loop = EventLoop::::with_user_event(); let proxy = event_loop.create_proxy(); let (tx, rx) = channel(); @@ -145,13 +142,13 @@ impl App for InnerApplication { }) } - fn create_webview( + pub fn create_webview( &mut self, attributes: Attributes, file_drop_handler: Option, rpc_handler: Option, custom_protocol: Option, - ) -> Result { + ) -> Result { let (window_attrs, webview_attrs) = attributes.split(); let window = _create_window(&self.event_loop, window_attrs)?; @@ -169,14 +166,14 @@ impl App for InnerApplication { Ok(id) } - fn application_proxy(&self) -> Self::Proxy { + pub fn application_proxy(&self) -> InnerApplicationProxy { InnerApplicationProxy { proxy: self.event_loop_proxy.clone(), receiver: self.event_channel.1.clone(), } } - fn run(self) { + pub fn run(self) { let proxy = self.application_proxy(); let mut windows = self.webviews; let event_sender = self.event_channel.0; diff --git a/src/application/mod.rs b/src/application/mod.rs index 090d00e9b..8c10c611c 100644 --- a/src/application/mod.rs +++ b/src/application/mod.rs @@ -111,18 +111,6 @@ impl ApplicationProxy { } } -trait AppProxy { - fn send_message(&self, message: Message) -> Result<()>; - fn add_window( - &self, - attributes: Attributes, - file_drop_handler: Option, - rpc_handler: Option, - custom_protocol: Option, - ) -> Result; - fn listen_event(&self) -> Result; -} - /// A proxy to customize its corresponding WebView window. /// /// Whenever [`Application::add_window`] creates a WebView Window, it will return this for you. But @@ -382,22 +370,3 @@ impl Application { self.inner.run() } } - -trait App: Sized { - type Proxy: AppProxy; - type Id: Copy; - - fn new() -> Result; - - fn create_webview( - &mut self, - attributes: Attributes, - file_drop_handler: Option, - rpc_handler: Option, - custom_protocol: Option, - ) -> Result; - - fn application_proxy(&self) -> Self::Proxy; - - fn run(self); -} From 20f94c830531792fb740a9749d43cec32178fea9 Mon Sep 17 00:00:00 2001 From: Ngo Iok Ui Date: Sat, 10 Apr 2021 13:49:55 +0800 Subject: [PATCH 08/14] Remove traits in application layer for gtk --- src/application/gtkrs.rs | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/application/gtkrs.rs b/src/application/gtkrs.rs index ac91d91f0..f720822dd 100644 --- a/src/application/gtkrs.rs +++ b/src/application/gtkrs.rs @@ -1,5 +1,5 @@ use crate::{ - application::{App, AppProxy, InnerWebViewAttributes, InnerWindowAttributes}, + application::{InnerWebViewAttributes, InnerWindowAttributes}, ApplicationProxy, Attributes, CustomProtocol, Error, Event as WryEvent, Icon, Message, Result, WebView, WebViewBuilder, WindowEvent as WryWindowEvent, WindowFileDropHandler, WindowMessage, WindowProxy, WindowRpcHandler, @@ -38,8 +38,8 @@ pub struct InnerApplicationProxy { receiver: Arc>>, } -impl AppProxy for InnerApplicationProxy { - fn send_message(&self, message: Message) -> Result<()> { +impl InnerApplicationProxy { + pub fn send_message(&self, message: Message) -> Result<()> { self .proxy .0 @@ -48,7 +48,7 @@ impl AppProxy for InnerApplicationProxy { Ok(()) } - fn add_window( + pub fn add_window( &self, attributes: Attributes, file_drop_handler: Option, @@ -66,7 +66,7 @@ impl AppProxy for InnerApplicationProxy { Ok(receiver.recv()?) } - fn listen_event(&self) -> Result { + pub fn listen_event(&self) -> Result { let rx = self.receiver.lock().unwrap(); Ok(rx.recv()?) } @@ -80,11 +80,8 @@ pub struct InnerApplication { event_channel: (Sender, Arc>>), } -impl App for InnerApplication { - type Id = u32; - type Proxy = InnerApplicationProxy; - - fn new() -> Result { +impl InnerApplication { + pub fn new() -> Result { let app = GtkApp::new(None, Default::default())?; let cancellable: Option<&Cancellable> = None; app.register(cancellable)?; @@ -101,13 +98,13 @@ impl App for InnerApplication { }) } - fn create_webview( + pub fn create_webview( &mut self, attributes: Attributes, file_drop_handler: Option, rpc_handler: Option, custom_protocol: Option, - ) -> Result { + ) -> Result { let (window_attrs, webview_attrs) = attributes.split(); let window = _create_window(&self.app, window_attrs)?; @@ -126,14 +123,14 @@ impl App for InnerApplication { Ok(id) } - fn application_proxy(&self) -> Self::Proxy { + pub fn application_proxy(&self) -> InnerApplicationProxy { InnerApplicationProxy { proxy: self.event_loop_proxy.clone(), receiver: self.event_channel.1.clone(), } } - fn run(self) { + pub fn run(self) { let proxy = self.application_proxy(); let shared_webviews = Rc::new(RefCell::new(self.webviews)); let shared_webviews_ = shared_webviews.clone(); From fd10bfffac0af9b4075e991a7412a9d25ed6bbac Mon Sep 17 00:00:00 2001 From: Ngo Iok Ui Date: Sat, 10 Apr 2021 14:03:01 +0800 Subject: [PATCH 09/14] Accepct multiple protocols on gtk --- src/application/gtkrs.rs | 17 +++++++++-------- src/application/mod.rs | 14 +++++++------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/application/gtkrs.rs b/src/application/gtkrs.rs index f720822dd..445d1d752 100644 --- a/src/application/gtkrs.rs +++ b/src/application/gtkrs.rs @@ -53,7 +53,7 @@ impl InnerApplicationProxy { attributes: Attributes, file_drop_handler: Option, rpc_handler: Option, - custom_protocol: Option, + custom_protocols: Vec, ) -> Result { let (sender, receiver): (Sender, Receiver) = channel(); self.send_message(Message::NewWindow( @@ -61,7 +61,7 @@ impl InnerApplicationProxy { sender, file_drop_handler, rpc_handler, - custom_protocol, + custom_protocols, ))?; Ok(receiver.recv()?) } @@ -103,7 +103,7 @@ impl InnerApplication { attributes: Attributes, file_drop_handler: Option, rpc_handler: Option, - custom_protocol: Option, + custom_protocols: Vec, ) -> Result { let (window_attrs, webview_attrs) = attributes.split(); let window = _create_window(&self.app, window_attrs)?; @@ -111,7 +111,7 @@ impl InnerApplication { let webview = _create_webview( self.application_proxy(), window, - custom_protocol, + custom_protocols, rpc_handler, file_drop_handler, webview_attrs, @@ -191,7 +191,7 @@ async fn process_messages( ) { while let Ok(message) = event_loop_proxy_rx.recv().await { match message { - Message::NewWindow(attributes, sender, file_drop_handler, rpc_handler, custom_protocol) => { + Message::NewWindow(attributes, sender, file_drop_handler, rpc_handler, custom_protocols) => { let (window_attrs, webview_attrs) = attributes.split(); match _create_window(&app, window_attrs) { Ok(window) => { @@ -201,7 +201,7 @@ async fn process_messages( match _create_webview( proxy.clone(), window, - custom_protocol, + custom_protocols, rpc_handler, file_drop_handler, webview_attrs, @@ -447,7 +447,7 @@ fn _create_window(app: &GtkApp, attributes: InnerWindowAttributes) -> Result, + custom_protocols: Vec, rpc_handler: Option, file_drop_handler: Option, @@ -463,7 +463,8 @@ fn _create_webview( Some(url) => webview.load_url(&url)?, None => webview, }; - if let Some(protocol) = custom_protocol { + + for protocol in custom_protocols { webview = webview.register_protocol(protocol.name, protocol.handler); } diff --git a/src/application/mod.rs b/src/application/mod.rs index 8c10c611c..e39f16d78 100644 --- a/src/application/mod.rs +++ b/src/application/mod.rs @@ -66,7 +66,7 @@ pub enum Message { Sender, Option, Option, - Option, + Vec, ), } @@ -87,7 +87,7 @@ impl ApplicationProxy { } /// Adds another WebView window to the application. Returns its [`WindowProxy`] after created. pub fn add_window(&self, attributes: Attributes) -> Result { - let id = self.inner.add_window(attributes, None, None, None)?; + let id = self.inner.add_window(attributes, None, None, vec![])?; Ok(WindowProxy::new(self.clone(), id)) } @@ -96,12 +96,12 @@ impl ApplicationProxy { &self, attributes: Attributes, rpc_handler: Option, - custom_protocol: Option, + custom_protocols: Vec, file_drop_handler: Option, ) -> Result { let id = self .inner - .add_window(attributes, file_drop_handler, rpc_handler, custom_protocol)?; + .add_window(attributes, file_drop_handler, rpc_handler, custom_protocols)?; Ok(WindowProxy::new(self.clone(), id)) } @@ -324,7 +324,7 @@ impl Application { /// /// To create a default window, you could just pass `.add_window(Default::default(), None)`. pub fn add_window(&mut self, attributes: Attributes) -> Result { - let id = self.inner.create_webview(attributes, None, None, None)?; + let id = self.inner.create_webview(attributes, None, None, vec![])?; Ok(self.window_proxy(id)) } @@ -340,13 +340,13 @@ impl Application { &mut self, attributes: Attributes, rpc_handler: Option, - custom_protocol: Option, + custom_protocols: Vec, file_drop_handler: Option, ) -> Result { let id = self .inner - .create_webview(attributes, file_drop_handler, rpc_handler, custom_protocol)?; + .create_webview(attributes, file_drop_handler, rpc_handler, custom_protocols)?; Ok(self.window_proxy(id)) } From 1519223234815bed59a71494d7d4554f4ff9cd94 Mon Sep 17 00:00:00 2001 From: Yu-Wei Wu Date: Sat, 10 Apr 2021 14:09:04 +0800 Subject: [PATCH 10/14] Accepct multiple protocols on winit --- src/application/general.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/application/general.rs b/src/application/general.rs index da33bb883..52d237f07 100644 --- a/src/application/general.rs +++ b/src/application/general.rs @@ -68,7 +68,7 @@ impl InnerApplicationProxy { attributes: Attributes, file_drop_handler: Option, rpc_handler: Option, - custom_protocol: Option, + custom_protocols: Vec, ) -> Result { let (sender, receiver) = channel(); self.send_message(Message::NewWindow( @@ -76,7 +76,7 @@ impl InnerApplicationProxy { sender, file_drop_handler, rpc_handler, - custom_protocol, + custom_protocols, ))?; Ok(receiver.recv()?) } @@ -147,7 +147,7 @@ impl InnerApplication { attributes: Attributes, file_drop_handler: Option, rpc_handler: Option, - custom_protocol: Option, + custom_protocols: Vec, ) -> Result { let (window_attrs, webview_attrs) = attributes.split(); @@ -155,7 +155,7 @@ impl InnerApplication { let webview = _create_webview( self.application_proxy(), window, - custom_protocol, + custom_protocols, rpc_handler, file_drop_handler, webview_attrs, @@ -214,7 +214,7 @@ impl InnerApplication { sender, file_drop_handler, rpc_handler, - custom_protocol, + custom_protocols, ) => { let (window_attrs, webview_attrs) = attributes.split(); match _create_window(&event_loop, window_attrs) { @@ -225,7 +225,7 @@ impl InnerApplication { match _create_webview( proxy.clone(), window, - custom_protocol, + custom_protocols, rpc_handler, file_drop_handler, webview_attrs, @@ -400,7 +400,7 @@ fn _create_window( fn _create_webview( proxy: InnerApplicationProxy, window: Window, - custom_protocol: Option, + custom_protocols: Vec, rpc_handler: Option, file_drop_handler: Option, @@ -416,7 +416,7 @@ fn _create_webview( webview = webview.initialize_script(&js); } - if let Some(protocol) = custom_protocol { + for protocol in custom_protocols { webview = webview.register_protocol(protocol.name, protocol.handler) } From 32c70c07e99836aa6ee86de94e5aac5bbe10cebf Mon Sep 17 00:00:00 2001 From: Yu-Wei Wu Date: Sat, 10 Apr 2021 14:09:36 +0800 Subject: [PATCH 11/14] cargo fmt --- src/webview/macos/mod.rs | 2 +- src/webview/win32/mod.rs | 7 ++----- src/webview/winrt/mod.rs | 5 +---- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/webview/macos/mod.rs b/src/webview/macos/mod.rs index e0c330814..6f59a17f8 100644 --- a/src/webview/macos/mod.rs +++ b/src/webview/macos/mod.rs @@ -111,7 +111,7 @@ impl InnerWebView { // Config and custom protocol let config: id = msg_send![class!(WKWebViewConfiguration), new]; for (name, function) in custom_protocols { - let scheme_name = format!("{}URLSchemeHandler", name); + let scheme_name = format!("{}URLSchemeHandler", name); let cls = ClassDecl::new(&scheme_name, class!(NSObject)); let cls = match cls { Some(mut cls) => { diff --git a/src/webview/win32/mod.rs b/src/webview/win32/mod.rs index 8234b9b57..9ec1b5b5d 100644 --- a/src/webview/win32/mod.rs +++ b/src/webview/win32/mod.rs @@ -1,13 +1,10 @@ mod file_drop; -use crate::{ - webview::mimetype::MimeType, - FileDropHandler, Result, RpcHandler, -}; +use crate::{webview::mimetype::MimeType, FileDropHandler, Result, RpcHandler}; use file_drop::FileDropController; -use std::{os::raw::c_void, path::PathBuf, rc::Rc, collections::HashSet}; +use std::{collections::HashSet, os::raw::c_void, path::PathBuf, rc::Rc}; use once_cell::unsync::OnceCell; use url::Url; diff --git a/src/webview/winrt/mod.rs b/src/webview/winrt/mod.rs index d38c64c6a..81540a7d5 100644 --- a/src/webview/winrt/mod.rs +++ b/src/webview/winrt/mod.rs @@ -12,10 +12,7 @@ use windows_webview2::{ }, }; -use crate::{ - webview::mimetype::MimeType, - FileDropHandler, Result, RpcHandler, -}; +use crate::{webview::mimetype::MimeType, FileDropHandler, Result, RpcHandler}; use file_drop::FileDropController; From 251f10b06fa0edf27c73a53f3ee9c229e636cf4b Mon Sep 17 00:00:00 2001 From: Yu-Wei Wu Date: Sat, 10 Apr 2021 14:11:23 +0800 Subject: [PATCH 12/14] Add .change file --- .changes/protocols.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/protocols.md diff --git a/.changes/protocols.md b/.changes/protocols.md new file mode 100644 index 000000000..3aec8f6b3 --- /dev/null +++ b/.changes/protocols.md @@ -0,0 +1,6 @@ +--- +"wry": patch +--- + +Wry now accepts multiple custom protocol registerations. + From 584bb43e8cd38dbf498d56f955d83979903edc8f Mon Sep 17 00:00:00 2001 From: Ngo Iok Ui Date: Sat, 10 Apr 2021 14:25:24 +0800 Subject: [PATCH 13/14] Fix tests --- examples/custom_titlebar.rs | 2 +- examples/dragndrop.rs | 2 +- examples/multi_window.rs | 2 +- examples/rpc.rs | 2 +- src/application/attributes.rs | 2 +- src/file_drop.rs | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/custom_titlebar.rs b/examples/custom_titlebar.rs index ee62d1d6e..fb679f72d 100644 --- a/examples/custom_titlebar.rs +++ b/examples/custom_titlebar.rs @@ -97,7 +97,7 @@ fn main() -> Result<()> { None }); - let _window1 = app.add_window_with_configs(attributes, Some(handler), None, None)?; + let _window1 = app.add_window_with_configs(attributes, Some(handler), vec![], None)?; app.run(); Ok(()) diff --git a/examples/dragndrop.rs b/examples/dragndrop.rs index a7deebd6b..25076455f 100644 --- a/examples/dragndrop.rs +++ b/examples/dragndrop.rs @@ -15,7 +15,7 @@ fn main() -> Result<()> { ..Default::default() }, None, - None, + vec![], Some(Box::new(|_, data| { println!("Window 1: {:?}", data); false // Returning true will block the OS default behaviour. diff --git a/examples/multi_window.rs b/examples/multi_window.rs index 5e1b022f2..99dbaacce 100644 --- a/examples/multi_window.rs +++ b/examples/multi_window.rs @@ -26,7 +26,7 @@ fn main() -> Result<()> { None }); - let window_proxy = app.add_window_with_configs(attributes, Some(handler), None, None)?; + let window_proxy = app.add_window_with_configs(attributes, Some(handler), vec![], None)?; let app_proxy = app.application_proxy(); std::thread::spawn(move || { let mut count = 1; diff --git a/examples/rpc.rs b/examples/rpc.rs index 29713ffd9..313721d81 100644 --- a/examples/rpc.rs +++ b/examples/rpc.rs @@ -67,7 +67,7 @@ async function getAsyncRpcResult() { response }); - app.add_window_with_configs(attributes, Some(handler), None, None)?; + app.add_window_with_configs(attributes, Some(handler), vec![], None)?; app.run(); Ok(()) diff --git a/src/application/attributes.rs b/src/application/attributes.rs index ba5d0e15c..f342539c3 100644 --- a/src/application/attributes.rs +++ b/src/application/attributes.rs @@ -43,7 +43,7 @@ use std::{ /// // from the cache. /// None /// }); -/// app.add_window_with_configs(Default::default(), Some(handler), None, None)?; +/// app.add_window_with_configs(Default::default(), Some(handler), vec![], None)?; /// app.run(); /// Ok(()) /// } diff --git a/src/file_drop.rs b/src/file_drop.rs index 8a90f0fc6..cb27b2b7f 100644 --- a/src/file_drop.rs +++ b/src/file_drop.rs @@ -39,7 +39,7 @@ pub enum FileDropEvent { /// // Return `true` to block the default file drop behavior of the OS. /// false /// }); -/// app.add_window_with_configs(Default::default(), None, None, Some(file_drop))?; +/// app.add_window_with_configs(Default::default(), None, vec![], Some(file_drop))?; /// app.run(); /// Ok(()) /// } From 3890c745d99503a6b3221db6d657d0b621de7989 Mon Sep 17 00:00:00 2001 From: Ngo Iok Ui Date: Sat, 10 Apr 2021 14:26:37 +0800 Subject: [PATCH 14/14] Update change file to minor update --- .changes/protocols.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changes/protocols.md b/.changes/protocols.md index 3aec8f6b3..36c0de42f 100644 --- a/.changes/protocols.md +++ b/.changes/protocols.md @@ -1,5 +1,5 @@ --- -"wry": patch +"wry": minor --- Wry now accepts multiple custom protocol registerations.