Skip to content

Commit

Permalink
refactor: signature of closure types (#167)
Browse files Browse the repository at this point in the history
* Refactor rpc handler on windows

* Refactor rpc handler on linux and mac

* Fix handler param on each platform

* Move file drop type into webview mod

* Refactor file drop handler

* Refactor custom protocol handler

* cargo fmt

* Add .changes file
  • Loading branch information
Ngo Iok Ui (Wu Yu Wei) authored Apr 19, 2021
1 parent fa15076 commit b8823fe
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 140 deletions.
6 changes: 6 additions & 0 deletions .changes/closure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"wry": minor
---

Refactor signatures of most closure types

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ targets = [
]

[features]
default = [ "file-drop", "protocol", "win32" ]
default = [ "file-drop", "protocol", "winrt" ]
file-drop = [ ]
protocol = [ ]
winrt = [ "windows-webview2", "windows" ]
Expand Down
7 changes: 3 additions & 4 deletions examples/custom_titlebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ fn main() -> wry::Result<()> {
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
},
webview::WebViewBuilder,
RpcRequest,
webview::{RpcRequest, WebViewBuilder},
};

let event_loop = EventLoop::new();
Expand Down Expand Up @@ -50,7 +49,7 @@ fn main() -> wry::Result<()> {
</script>
"#;

let handler = Box::new(|mut _req: RpcRequest| {
let handler = |mut _req: RpcRequest| {
/* TODO window setter
if req.method == "minimize" {
proxy.minimize().unwrap();
Expand All @@ -67,7 +66,7 @@ fn main() -> wry::Result<()> {
}
*/
None
});
};
let _webview = WebViewBuilder::new(window)
.unwrap()
.with_url(url)?
Expand Down
7 changes: 3 additions & 4 deletions examples/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ fn main() -> wry::Result<()> {
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
},
webview::WebViewBuilder,
RpcRequest,
webview::{RpcRequest, WebViewBuilder},
};

let event_loop = EventLoop::new();
let window = WindowBuilder::new().build(&event_loop).unwrap();

let (window_tx, window_rx) = std::sync::mpsc::channel::<String>();
let handler = Box::new(move |req: RpcRequest| {
let handler = move |req: RpcRequest| {
if &req.method == "openWindow" {
if let Some(params) = req.params {
if let Value::String(url) = &params[0] {
Expand All @@ -36,7 +35,7 @@ fn main() -> wry::Result<()> {
}
}
None
});
};

let mut webview = WebViewBuilder::new(window)
.unwrap()
Expand Down
7 changes: 3 additions & 4 deletions examples/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ fn main() -> wry::Result<()> {
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
},
webview::WebViewBuilder,
RpcRequest, RpcResponse,
webview::{RpcRequest, RpcResponse, WebViewBuilder},
};

let event_loop = EventLoop::new();
Expand All @@ -44,7 +43,7 @@ async function getAsyncRpcResult() {
<div id="rpc-result"></div>
"#;

let handler = Box::new(|mut req: RpcRequest| {
let handler = |mut req: RpcRequest| {
let mut response = None;
if &req.method == "fullscreen" {
if let Some(params) = req.params.take() {
Expand Down Expand Up @@ -74,7 +73,7 @@ async function getAsyncRpcResult() {
}

response
});
};
let _webview = WebViewBuilder::new(window)
.unwrap()
.with_url(url)?
Expand Down
11 changes: 1 addition & 10 deletions src/file_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,4 @@

use std::path::PathBuf;

/// An event enumeration sent to [`FileDropHandler`].
#[derive(Debug, Serialize, Clone)]
pub enum FileDropEvent {
/// The file(s) have been dragged onto the window, but have not been dropped yet.
Hovered(Vec<PathBuf>),
/// The file(s) have been dropped onto the window.
Dropped(Vec<PathBuf>),
/// The file drop was aborted.
Cancelled,
}

12 changes: 0 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,8 @@ use url::ParseError;
#[cfg(not(target_os = "linux"))]
use winit::window::BadIcon;

#[cfg(not(feature = "file-drop"))]
pub(crate) use file_drop::FileDropEvent;
#[cfg(feature = "file-drop")]
pub use file_drop::FileDropEvent;
#[cfg(not(feature = "file-drop"))]
pub(crate) use webview::FileDropHandler;
#[cfg(feature = "file-drop")]
pub use webview::FileDropHandler;
pub(crate) use webview::RpcHandler;
pub use webview::{RpcRequest, RpcResponse};

#[cfg(target_os = "linux")]
pub mod application;
mod file_drop;
pub mod webview;
#[cfg(not(target_os = "linux"))]
pub use winit as application;
Expand Down
7 changes: 5 additions & 2 deletions src/webview/linux/file_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ use std::{cell::Cell, path::PathBuf, rc::Rc};
use gtk::WidgetExt;
use webkit2gtk::WebView;

use crate::{webview::FileDropHandler, FileDropEvent};
use crate::webview::FileDropEvent;

pub(crate) fn connect_drag_event(webview: Rc<WebView>, handler: FileDropHandler) {
pub(crate) fn connect_drag_event(
webview: Rc<WebView>,
handler: Box<dyn Fn(FileDropEvent) -> bool>,
) {
let listener = Rc::new((handler, Cell::new(None)));

let listener_ref = listener.clone();
Expand Down
12 changes: 6 additions & 6 deletions src/webview/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use webkit2gtk::{

use crate::{
application::window::Window,
webview::{mimetype::MimeType, FileDropHandler},
Error, Result, RpcHandler,
webview::{mimetype::MimeType, FileDropEvent, RpcRequest, RpcResponse},
Error, Result,
};

mod file_drop;
Expand All @@ -28,14 +28,14 @@ pub struct InnerWebView {
}

impl InnerWebView {
pub fn new<F: 'static + Fn(&str) -> Result<Vec<u8>>>(
pub fn new(
window: &Window,
scripts: Vec<String>,
url: Option<Url>,
transparent: bool,
custom_protocols: Vec<(String, F)>,
rpc_handler: Option<RpcHandler>,
file_drop_handler: Option<FileDropHandler>,
custom_protocols: Vec<(String, Box<dyn Fn(&str) -> Result<Vec<u8>> + 'static>)>,
rpc_handler: Option<Box<dyn Fn(RpcRequest) -> Option<RpcResponse>>>,
file_drop_handler: Option<Box<dyn Fn(FileDropEvent) -> bool>>,
_user_data_path: Option<PathBuf>,
) -> Result<Self> {
let window = &window.window;
Expand Down
11 changes: 7 additions & 4 deletions src/webview/macos/file_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use objc::{
};
use once_cell::sync::Lazy;

use crate::{webview::FileDropHandler, FileDropEvent};
use crate::webview::FileDropEvent;

pub(crate) type NSDragOperation = cocoa::foundation::NSUInteger;
#[allow(non_upper_case_globals)]
Expand Down Expand Up @@ -52,14 +52,17 @@ static OBJC_DRAGGING_UPDATED: Lazy<extern "C" fn(*const Object, Sel, id) -> NSDr
});

// Safety: objc runtime calls are unsafe
pub(crate) unsafe fn set_file_drop_handler(webview: *mut Object, handler: FileDropHandler) {
pub(crate) unsafe fn set_file_drop_handler(
webview: *mut Object,
handler: Box<dyn Fn(FileDropEvent) -> bool>,
) {
let listener = Box::into_raw(Box::new(handler));
(*webview).set_ivar("FileDropHandler", listener as *mut _ as *mut c_void);
}

unsafe fn get_handler(this: &Object) -> &mut FileDropHandler {
unsafe fn get_handler(this: &Object) -> &mut Box<dyn Fn(FileDropEvent) -> bool> {
let delegate: *mut c_void = *this.get_ivar("FileDropHandler");
&mut *(delegate as *mut FileDropHandler)
&mut *(delegate as *mut Box<dyn Fn(FileDropEvent) -> bool>)
}

unsafe fn collect_paths(drag_info: id) -> Vec<PathBuf> {
Expand Down
17 changes: 9 additions & 8 deletions src/webview/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use winit::{platform::macos::WindowExtMacOS, window::Window};
use file_drop::{add_file_drop_methods, set_file_drop_handler};

use crate::{
webview::{mimetype::MimeType, FileDropHandler},
Result, RpcHandler,
webview::{mimetype::MimeType, FileDropEvent, RpcRequest, RpcResponse},
Result,
};

mod file_drop;
Expand All @@ -38,22 +38,23 @@ pub struct InnerWebView {
}

impl InnerWebView {
pub fn new<F: 'static + Fn(&str) -> Result<Vec<u8>>>(
pub fn new(
window: &Window,
scripts: Vec<String>,
url: Option<Url>,
transparent: bool,
custom_protocols: Vec<(String, F)>,
rpc_handler: Option<RpcHandler>,
file_drop_handler: Option<FileDropHandler>,
custom_protocols: Vec<(String, Box<dyn Fn(&str) -> Result<Vec<u8>> + 'static>)>,
rpc_handler: Option<Box<dyn Fn(RpcRequest) -> Option<RpcResponse>>>,
file_drop_handler: Option<Box<dyn Fn(FileDropEvent) -> bool>>,
_user_data_path: Option<PathBuf>,
) -> Result<Self> {
// Function for rpc handler
extern "C" fn did_receive(this: &Object, _: Sel, _: id, msg: id) {
// Safety: objc runtime calls are unsafe
unsafe {
let function = this.get_ivar::<*mut c_void>("function");
let function: &mut RpcHandler = std::mem::transmute(*function);
let function: &mut Box<dyn Fn(RpcRequest) -> Option<RpcResponse>> =
std::mem::transmute(*function);
let body: id = msg_send![msg, body];
let utf8: *const c_char = msg_send![body, UTF8String];
let js = CStr::from_ptr(utf8).to_str().expect("Invalid UTF8 string");
Expand Down Expand Up @@ -190,7 +191,7 @@ impl InnerWebView {
None => class!(WebViewDelegate),
};
let handler: id = msg_send![cls, new];
let function: Box<RpcHandler> = Box::new(rpc_handler);
let function = Box::new(rpc_handler);

(*handler).set_ivar("function", Box::into_raw(function) as *mut _ as *mut c_void);
let external = NSString::new("external");
Expand Down
Loading

0 comments on commit b8823fe

Please sign in to comment.