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

feat: multiple custom protocols #151

Merged
merged 14 commits into from
Apr 11, 2021
6 changes: 6 additions & 0 deletions .changes/protocols.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"wry": minor
---

Wry now accepts multiple custom protocol registerations.

2 changes: 1 addition & 1 deletion examples/custom_titlebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
2 changes: 1 addition & 1 deletion examples/dragndrop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion examples/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
2 changes: 1 addition & 1 deletion src/application/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
/// }
Expand Down
41 changes: 19 additions & 22 deletions src/application/general.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -54,34 +54,34 @@ pub struct InnerApplicationProxy {
receiver: Arc<Mutex<Receiver<WryEvent>>>,
}

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)
.map_err(|_| Error::MessageSender)?;
Ok(())
}

fn add_window(
pub fn add_window(
&self,
attributes: Attributes,
file_drop_handler: Option<WindowFileDropHandler>,
rpc_handler: Option<WindowRpcHandler>,
custom_protocol: Option<CustomProtocol>,
custom_protocols: Vec<CustomProtocol>,
) -> Result<WindowId> {
let (sender, receiver) = channel();
self.send_message(Message::NewWindow(
attributes,
sender,
file_drop_handler,
rpc_handler,
custom_protocol,
custom_protocols,
))?;
Ok(receiver.recv()?)
}

fn listen_event(&self) -> Result<WryEvent> {
pub fn listen_event(&self) -> Result<WryEvent> {
let rx = self.receiver.lock().unwrap();
Ok(rx.recv()?)
}
Expand Down Expand Up @@ -129,11 +129,8 @@ pub struct InnerApplication {
event_channel: (Sender<WryEvent>, Arc<Mutex<Receiver<WryEvent>>>),
}

impl App for InnerApplication {
type Id = WindowId;
type Proxy = InnerApplicationProxy;

fn new() -> Result<Self> {
impl InnerApplication {
pub fn new() -> Result<Self> {
let event_loop = EventLoop::<Message>::with_user_event();
let proxy = event_loop.create_proxy();
let (tx, rx) = channel();
Expand All @@ -145,20 +142,20 @@ impl App for InnerApplication {
})
}

fn create_webview(
pub fn create_webview(
&mut self,
attributes: Attributes,
file_drop_handler: Option<WindowFileDropHandler>,
rpc_handler: Option<WindowRpcHandler>,
custom_protocol: Option<CustomProtocol>,
) -> Result<Self::Id> {
custom_protocols: Vec<CustomProtocol>,
) -> Result<WindowId> {
let (window_attrs, webview_attrs) = attributes.split();

let window = _create_window(&self.event_loop, window_attrs)?;
let webview = _create_webview(
self.application_proxy(),
window,
custom_protocol,
custom_protocols,
rpc_handler,
file_drop_handler,
webview_attrs,
Expand All @@ -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;
Expand Down Expand Up @@ -217,7 +214,7 @@ impl App for 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) {
Expand All @@ -228,7 +225,7 @@ impl App for InnerApplication {
match _create_webview(
proxy.clone(),
window,
custom_protocol,
custom_protocols,
rpc_handler,
file_drop_handler,
webview_attrs,
Expand Down Expand Up @@ -403,7 +400,7 @@ fn _create_window(
fn _create_webview(
proxy: InnerApplicationProxy,
window: Window,
custom_protocol: Option<CustomProtocol>,
custom_protocols: Vec<CustomProtocol>,
rpc_handler: Option<WindowRpcHandler>,
file_drop_handler: Option<WindowFileDropHandler>,

Expand All @@ -419,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)
}

Expand Down
42 changes: 20 additions & 22 deletions src/application/gtkrs.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -38,8 +38,8 @@ pub struct InnerApplicationProxy {
receiver: Arc<Mutex<Receiver<WryEvent>>>,
}

impl AppProxy for InnerApplicationProxy {
fn send_message(&self, message: Message) -> Result<()> {
impl InnerApplicationProxy {
pub fn send_message(&self, message: Message) -> Result<()> {
self
.proxy
.0
Expand All @@ -48,25 +48,25 @@ impl AppProxy for InnerApplicationProxy {
Ok(())
}

fn add_window(
pub fn add_window(
&self,
attributes: Attributes,
file_drop_handler: Option<WindowFileDropHandler>,
rpc_handler: Option<WindowRpcHandler>,
custom_protocol: Option<CustomProtocol>,
custom_protocols: Vec<CustomProtocol>,
) -> Result<WindowId> {
let (sender, receiver): (Sender<WindowId>, Receiver<WindowId>) = channel();
self.send_message(Message::NewWindow(
attributes,
sender,
file_drop_handler,
rpc_handler,
custom_protocol,
custom_protocols,
))?;
Ok(receiver.recv()?)
}

fn listen_event(&self) -> Result<WryEvent> {
pub fn listen_event(&self) -> Result<WryEvent> {
let rx = self.receiver.lock().unwrap();
Ok(rx.recv()?)
}
Expand All @@ -80,11 +80,8 @@ pub struct InnerApplication {
event_channel: (Sender<WryEvent>, Arc<Mutex<Receiver<WryEvent>>>),
}

impl App for InnerApplication {
type Id = u32;
type Proxy = InnerApplicationProxy;

fn new() -> Result<Self> {
impl InnerApplication {
pub fn new() -> Result<Self> {
let app = GtkApp::new(None, Default::default())?;
let cancellable: Option<&Cancellable> = None;
app.register(cancellable)?;
Expand All @@ -101,20 +98,20 @@ impl App for InnerApplication {
})
}

fn create_webview(
pub fn create_webview(
&mut self,
attributes: Attributes,
file_drop_handler: Option<WindowFileDropHandler>,
rpc_handler: Option<WindowRpcHandler>,
custom_protocol: Option<CustomProtocol>,
) -> Result<Self::Id> {
custom_protocols: Vec<CustomProtocol>,
) -> Result<u32> {
let (window_attrs, webview_attrs) = attributes.split();
let window = _create_window(&self.app, window_attrs)?;

let webview = _create_webview(
self.application_proxy(),
window,
custom_protocol,
custom_protocols,
rpc_handler,
file_drop_handler,
webview_attrs,
Expand All @@ -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();
Expand Down Expand Up @@ -194,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) => {
Expand All @@ -204,7 +201,7 @@ async fn process_messages(
match _create_webview(
proxy.clone(),
window,
custom_protocol,
custom_protocols,
rpc_handler,
file_drop_handler,
webview_attrs,
Expand Down Expand Up @@ -450,7 +447,7 @@ fn _create_window(app: &GtkApp, attributes: InnerWindowAttributes) -> Result<App
fn _create_webview(
proxy: InnerApplicationProxy,
window: ApplicationWindow,
custom_protocol: Option<CustomProtocol>,
custom_protocols: Vec<CustomProtocol>,
rpc_handler: Option<WindowRpcHandler>,
file_drop_handler: Option<WindowFileDropHandler>,

Expand All @@ -466,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);
}

Expand Down
Loading