2020-02-28 13:34:34 +11:00
|
|
|
//! Hoists a basic NSView. In our current particular use case,
|
|
|
|
//! this is primarily used as the ContentView for a window. From there,
|
|
|
|
//! we configure an NSToolbar and WKWebview on top of them.
|
|
|
|
|
|
|
|
use std::ffi::c_void;
|
2020-03-22 10:46:35 +11:00
|
|
|
use std::sync::Once;
|
|
|
|
use std::rc::Rc;
|
2020-02-28 13:34:34 +11:00
|
|
|
|
|
|
|
use block::Block;
|
|
|
|
|
|
|
|
use objc::declare::ClassDecl;
|
2020-03-01 10:34:07 +11:00
|
|
|
use objc::runtime::{Class, Object, Sel};
|
2020-02-28 13:34:34 +11:00
|
|
|
use objc::{class, msg_send, sel, sel_impl};
|
|
|
|
|
2020-03-22 10:46:35 +11:00
|
|
|
use crate::foundation::{id, nil, YES, NO, NSString, NSArray, NSInteger};
|
2020-03-21 12:37:00 +11:00
|
|
|
use crate::webview::{WEBVIEW_DELEGATE_PTR, WebViewDelegate};
|
2020-03-22 10:46:35 +11:00
|
|
|
use crate::webview::actions::{NavigationAction, NavigationResponse};//, OpenPanelParameters};
|
|
|
|
//use crate::webview::enums::{NavigationPolicy, NavigationResponsePolicy};
|
|
|
|
use crate::utils::load;
|
2020-02-28 13:34:34 +11:00
|
|
|
|
|
|
|
/// Called when an `alert()` from the underlying `WKWebView` is fired. Will call over to your
|
|
|
|
/// `WebViewController`, where you should handle the event.
|
2020-03-21 12:37:00 +11:00
|
|
|
extern fn alert<T: WebViewDelegate>(_: &Object, _: Sel, _: id, s: id, _: id, complete: id) {
|
2020-03-20 14:07:44 +11:00
|
|
|
let alert = NSString::wrap(s).to_str();
|
2020-02-28 13:34:34 +11:00
|
|
|
|
|
|
|
// @TODO: This is technically (I think?) a private method, and there's some other dance that
|
|
|
|
// needs to be done here involving taking the pointer/invoke/casting... but this is fine for
|
|
|
|
// now as it's being exposed purely for debugging.
|
|
|
|
unsafe {
|
|
|
|
let _: () = msg_send![complete, invoke];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*unsafe {
|
2020-03-21 12:37:00 +11:00
|
|
|
let ptr: usize = *this.get_ivar(WEBVIEW_DELEGATE_PTR);
|
2020-03-22 10:46:35 +11:00
|
|
|
let delegate = ptr as *const T;
|
2020-02-28 13:34:34 +11:00
|
|
|
(*webview).alert(alert);
|
|
|
|
}*/
|
|
|
|
|
|
|
|
/*let queue = dispatch::Queue::main();
|
|
|
|
queue.exec_async(move || {
|
|
|
|
let a = Alert::new("Subatomic", message);
|
|
|
|
a.show();
|
|
|
|
});*/
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fires when a message has been passed from the underlying `WKWebView`.
|
2020-03-21 12:37:00 +11:00
|
|
|
extern fn on_message<T: WebViewDelegate>(this: &Object, _: Sel, _: id, script_message: id) {
|
2020-03-22 10:46:35 +11:00
|
|
|
let delegate = load::<T>(this, WEBVIEW_DELEGATE_PTR);
|
|
|
|
|
2020-02-28 13:34:34 +11:00
|
|
|
unsafe {
|
2020-03-20 14:07:44 +11:00
|
|
|
let name = NSString::wrap(msg_send![script_message, name]).to_str();
|
|
|
|
let body = NSString::wrap(msg_send![script_message, body]).to_str();
|
2020-03-22 10:46:35 +11:00
|
|
|
let d = delegate.borrow();
|
|
|
|
(*d).on_message(name, body);
|
2020-02-28 13:34:34 +11:00
|
|
|
}
|
2020-03-22 10:46:35 +11:00
|
|
|
|
|
|
|
Rc::into_raw(delegate);
|
2020-02-28 13:34:34 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Fires when deciding a navigation policy - i.e, should something be allowed or not.
|
2020-03-21 12:37:00 +11:00
|
|
|
extern fn decide_policy_for_action<T: WebViewDelegate>(this: &Object, _: Sel, _: id, action: id, handler: usize) {
|
2020-03-22 10:46:35 +11:00
|
|
|
let delegate = load::<T>(this, WEBVIEW_DELEGATE_PTR);
|
2020-02-28 13:34:34 +11:00
|
|
|
|
|
|
|
let action = NavigationAction::new(action);
|
2020-03-22 10:46:35 +11:00
|
|
|
|
|
|
|
{
|
|
|
|
let d = delegate.borrow();
|
|
|
|
|
|
|
|
(*d).policy_for_navigation_action(action, |policy| unsafe {
|
2020-02-28 13:34:34 +11:00
|
|
|
let handler = handler as *const Block<(NSInteger,), c_void>;
|
|
|
|
(*handler).call((policy.into(),));
|
2020-03-22 10:46:35 +11:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Rc::into_raw(delegate);
|
2020-02-28 13:34:34 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Fires when deciding a navigation policy - i.e, should something be allowed or not.
|
2020-03-21 12:37:00 +11:00
|
|
|
extern fn decide_policy_for_response<T: WebViewDelegate>(this: &Object, _: Sel, _: id, response: id, handler: usize) {
|
2020-03-22 10:46:35 +11:00
|
|
|
let delegate = load::<T>(this, WEBVIEW_DELEGATE_PTR);
|
2020-02-28 13:34:34 +11:00
|
|
|
|
|
|
|
let response = NavigationResponse::new(response);
|
2020-03-22 10:46:35 +11:00
|
|
|
|
|
|
|
{
|
|
|
|
let d = delegate.borrow();
|
|
|
|
|
|
|
|
(*d).policy_for_navigation_response(response, |policy| unsafe {
|
|
|
|
let handler = handler as *const Block<(NSInteger,), c_void>;
|
|
|
|
(*handler).call((policy.into(),));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Rc::into_raw(delegate);
|
2020-02-28 13:34:34 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Fires when deciding a navigation policy - i.e, should something be allowed or not.
|
2020-03-21 12:37:00 +11:00
|
|
|
extern fn run_open_panel<T: WebViewDelegate>(this: &Object, _: Sel, _: id, params: id, _: id, handler: usize) {
|
2020-03-22 10:46:35 +11:00
|
|
|
let delegate = load::<T>(this, WEBVIEW_DELEGATE_PTR);
|
2020-02-28 13:34:34 +11:00
|
|
|
|
2020-03-22 10:46:35 +11:00
|
|
|
{
|
|
|
|
let d = delegate.borrow();
|
2020-02-28 13:34:34 +11:00
|
|
|
|
2020-03-22 10:46:35 +11:00
|
|
|
(*d).run_open_panel(params.into(), move |urls| unsafe {
|
|
|
|
let handler = handler as *const Block<(id,), c_void>;
|
2020-02-28 13:34:34 +11:00
|
|
|
|
2020-03-22 10:46:35 +11:00
|
|
|
match urls {
|
|
|
|
Some(u) => {
|
|
|
|
let nsurls: NSArray = u.iter().map(|s| {
|
|
|
|
let s = NSString::new(s);
|
|
|
|
msg_send![class!(NSURL), URLWithString:s.into_inner()]
|
|
|
|
}).collect::<Vec<id>>().into();
|
2020-02-28 13:34:34 +11:00
|
|
|
|
2020-03-22 10:46:35 +11:00
|
|
|
(*handler).call((nsurls.into_inner(),));
|
|
|
|
},
|
|
|
|
|
|
|
|
None => { (*handler).call((nil,)); }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Rc::into_raw(delegate);
|
2020-02-28 13:34:34 +11:00
|
|
|
}
|
|
|
|
|
2020-03-01 10:34:07 +11:00
|
|
|
/// Called when a download has been initiated in the WebView, and when the navigation policy
|
|
|
|
/// response is upgraded to BecomeDownload. Only called when explicitly linked since it's a private
|
|
|
|
/// API.
|
2020-03-20 14:07:44 +11:00
|
|
|
#[cfg(feature = "webview-downloading")]
|
2020-03-21 12:37:00 +11:00
|
|
|
extern fn handle_download<T: WebViewDelegate>(this: &Object, _: Sel, download: id, suggested_filename: id, handler: usize) {
|
2020-03-22 10:46:35 +11:00
|
|
|
let delegate = load::<T>(this, WEBVIEW_DELEGATE_PTR);
|
2020-02-28 13:34:34 +11:00
|
|
|
|
2020-03-01 10:34:07 +11:00
|
|
|
let handler = handler as *const Block<(objc::runtime::BOOL, id), c_void>;
|
2020-03-20 14:07:44 +11:00
|
|
|
let filename = NSString::wrap(suggested_filename).to_str();
|
2020-02-28 13:34:34 +11:00
|
|
|
|
2020-03-22 10:46:35 +11:00
|
|
|
{
|
|
|
|
let d = delegate.borrow();
|
|
|
|
|
|
|
|
(*d).run_save_panel(filename, move |can_overwrite, path| unsafe {
|
|
|
|
if path.is_none() {
|
|
|
|
let _: () = msg_send![download, cancel];
|
|
|
|
}
|
|
|
|
|
|
|
|
let path = NSString::new(&path.unwrap());
|
|
|
|
|
|
|
|
(*handler).call((match can_overwrite {
|
|
|
|
true => YES,
|
|
|
|
false => NO
|
|
|
|
}, path.into_inner()));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Rc::into_raw(delegate);
|
2020-02-28 13:34:34 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Registers an `NSViewController` that we effectively turn into a `WebViewController`. Acts as
|
|
|
|
/// both a subclass of `NSViewController` and a delegate of the held `WKWebView` (for the various
|
|
|
|
/// varieties of delegates needed there).
|
2020-03-21 12:37:00 +11:00
|
|
|
pub fn register_webview_class() -> *const Class {
|
2020-02-28 13:34:34 +11:00
|
|
|
static mut VIEW_CLASS: *const Class = 0 as *const Class;
|
|
|
|
static INIT: Once = Once::new();
|
|
|
|
|
|
|
|
INIT.call_once(|| unsafe {
|
2020-03-21 12:37:00 +11:00
|
|
|
let superclass = class!(WKWebView);
|
|
|
|
let decl = ClassDecl::new("RSTWebView", superclass).unwrap();
|
|
|
|
VIEW_CLASS = decl.register();
|
|
|
|
});
|
2020-02-28 13:34:34 +11:00
|
|
|
|
2020-03-21 12:37:00 +11:00
|
|
|
unsafe { VIEW_CLASS }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Registers an `NSViewController` that we effectively turn into a `WebViewController`. Acts as
|
|
|
|
/// both a subclass of `NSViewController` and a delegate of the held `WKWebView` (for the various
|
|
|
|
/// varieties of delegates needed there).
|
|
|
|
pub fn register_webview_delegate_class<T: WebViewDelegate>() -> *const Class {
|
|
|
|
static mut VIEW_CLASS: *const Class = 0 as *const Class;
|
|
|
|
static INIT: Once = Once::new();
|
|
|
|
|
|
|
|
INIT.call_once(|| unsafe {
|
|
|
|
let superclass = class!(NSObject);
|
|
|
|
let mut decl = ClassDecl::new("RSTWebViewDelegate", superclass).unwrap();
|
2020-02-28 13:34:34 +11:00
|
|
|
|
2020-03-21 12:37:00 +11:00
|
|
|
decl.add_ivar::<usize>(WEBVIEW_DELEGATE_PTR);
|
2020-02-28 13:34:34 +11:00
|
|
|
|
|
|
|
// WKNavigationDelegate
|
|
|
|
decl.add_method(sel!(webView:decidePolicyForNavigationAction:decisionHandler:), decide_policy_for_action::<T> as extern fn(&Object, _, _, id, usize));
|
|
|
|
decl.add_method(sel!(webView:decidePolicyForNavigationResponse:decisionHandler:), decide_policy_for_response::<T> as extern fn(&Object, _, _, id, usize));
|
|
|
|
|
|
|
|
// WKScriptMessageHandler
|
|
|
|
decl.add_method(sel!(userContentController:didReceiveScriptMessage:), on_message::<T> as extern fn(&Object, _, _, id));
|
|
|
|
|
|
|
|
// WKUIDelegate
|
|
|
|
decl.add_method(sel!(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:), alert::<T> as extern fn(&Object, _, _, id, _, _));
|
|
|
|
decl.add_method(sel!(webView:runOpenPanelWithParameters:initiatedByFrame:completionHandler:), run_open_panel::<T> as extern fn(&Object, _, _, id, _, usize));
|
|
|
|
|
|
|
|
// WKDownloadDelegate is a private class on macOS that handles downloading (saving) files.
|
|
|
|
// It's absurd that this is still private in 2020. This probably couldn't get into the app
|
2020-03-21 12:37:00 +11:00
|
|
|
// store, so... screw it, feature-gate it.
|
2020-03-20 14:07:44 +11:00
|
|
|
#[cfg(feature = "webview-downloading")]
|
2020-02-28 13:34:34 +11:00
|
|
|
decl.add_method(sel!(_download:decideDestinationWithSuggestedFilename:completionHandler:), handle_download::<T> as extern fn(&Object, _, id, id, usize));
|
|
|
|
|
|
|
|
VIEW_CLASS = decl.register();
|
|
|
|
});
|
|
|
|
|
|
|
|
unsafe { VIEW_CLASS }
|
|
|
|
}
|