2020-03-20 14:07:44 +11:00
|
|
|
//! Implements a WebView, which wraps a number of different classes/delegates/controllers into one
|
|
|
|
//! useful interface. This encompasses...
|
2020-03-18 12:19:56 +11:00
|
|
|
//!
|
2020-03-20 14:07:44 +11:00
|
|
|
//! - `WKWebView`
|
|
|
|
//! - `WKUIDelegate`
|
|
|
|
//! - `WKScriptMessageHandler`
|
|
|
|
//! - `NSViewController`
|
2020-02-28 13:34:34 +11:00
|
|
|
|
2020-03-18 12:19:56 +11:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::cell::RefCell;
|
|
|
|
|
|
|
|
use objc_id::ShareId;
|
|
|
|
use objc::runtime::Object;
|
2020-03-20 14:07:44 +11:00
|
|
|
use objc::{class, msg_send, sel, sel_impl};
|
2020-03-18 12:19:56 +11:00
|
|
|
|
2020-03-20 14:07:44 +11:00
|
|
|
use crate::foundation::{id, nil, YES, NO, NSString};
|
|
|
|
use crate::constants::WEBVIEW_CONTROLLER_PTR;
|
|
|
|
use crate::webview::controller::register_controller_class;
|
2020-03-18 12:19:56 +11:00
|
|
|
|
2020-03-20 14:07:44 +11:00
|
|
|
pub mod actions;
|
|
|
|
|
|
|
|
pub(crate) mod controller;
|
|
|
|
//pub(crate) mod process_pool;
|
2020-03-05 13:33:11 +11:00
|
|
|
|
|
|
|
pub mod traits;
|
2020-03-20 14:07:44 +11:00
|
|
|
pub use traits::WebViewController;
|
2020-03-18 12:19:56 +11:00
|
|
|
|
2020-03-20 14:07:44 +11:00
|
|
|
pub mod config;
|
|
|
|
pub use config::WebViewConfig;
|
2020-03-18 12:19:56 +11:00
|
|
|
|
|
|
|
/// A `View` wraps two different controllers - one on the Objective-C/Cocoa side, which forwards
|
|
|
|
/// calls into your supplied `ViewController` trait object. This involves heap allocation, but all
|
|
|
|
/// of Cocoa is essentially Heap'd, so... well, enjoy.
|
|
|
|
#[derive(Clone)]
|
2020-03-20 14:07:44 +11:00
|
|
|
pub struct WebView<T> {
|
2020-03-18 12:19:56 +11:00
|
|
|
internal_callback_ptr: *const RefCell<T>,
|
2020-03-20 14:07:44 +11:00
|
|
|
pub objc_controller: WebViewHandle,
|
2020-03-18 12:19:56 +11:00
|
|
|
pub controller: Rc<RefCell<T>>
|
|
|
|
}
|
|
|
|
|
2020-03-20 14:07:44 +11:00
|
|
|
impl<T> WebView<T> where T: WebViewController + 'static {
|
2020-03-18 12:19:56 +11:00
|
|
|
/// Allocates and configures a `ViewController` in the Objective-C/Cocoa runtime that maps over
|
|
|
|
/// to your supplied view controller.
|
|
|
|
pub fn new(controller: T) -> Self {
|
2020-03-20 14:07:44 +11:00
|
|
|
let config = controller.config();
|
2020-03-18 12:19:56 +11:00
|
|
|
let controller = Rc::new(RefCell::new(controller));
|
|
|
|
|
|
|
|
let internal_callback_ptr = {
|
|
|
|
let cloned = Rc::clone(&controller);
|
|
|
|
Rc::into_raw(cloned)
|
|
|
|
};
|
|
|
|
|
2020-03-20 14:07:44 +11:00
|
|
|
let handle = WebViewHandle::new(unsafe {
|
2020-03-18 12:19:56 +11:00
|
|
|
let view_controller: id = msg_send![register_controller_class::<T>(), new];
|
2020-03-20 14:07:44 +11:00
|
|
|
(&mut *view_controller).set_ivar(WEBVIEW_CONTROLLER_PTR, internal_callback_ptr as usize);
|
2020-03-18 12:19:56 +11:00
|
|
|
|
2020-03-20 14:07:44 +11:00
|
|
|
// WKWebView isn't really great to subclass, so we don't bother here unlike other
|
|
|
|
// widgets in this framework. Just set and forget.
|
|
|
|
let frame: CGRect = Rect::zero().into();
|
|
|
|
let alloc: id = msg_send![class!(WKWebView), alloc];
|
|
|
|
let view: id = msg_send![alloc, initWithFrame:frame configuration:&*config.0];
|
|
|
|
let _: () = msg_send![&*view_controller, setView:view];
|
2020-03-18 12:19:56 +11:00
|
|
|
|
|
|
|
ShareId::from_ptr(view_controller)
|
2020-03-20 14:07:44 +11:00
|
|
|
});
|
2020-03-18 12:19:56 +11:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut vc = controller.borrow_mut();
|
|
|
|
(*vc).did_load(handle.clone());
|
|
|
|
}
|
|
|
|
|
2020-03-20 14:07:44 +11:00
|
|
|
WebView {
|
2020-03-18 12:19:56 +11:00
|
|
|
internal_callback_ptr: internal_callback_ptr,
|
|
|
|
objc_controller: handle,
|
|
|
|
controller: controller
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_background_color(&self, color: Color) {
|
|
|
|
self.objc_controller.set_background_color(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn register_for_dragged_types(&self, types: &[PasteboardType]) {
|
|
|
|
self.objc_controller.register_for_dragged_types(types);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn top(&self) -> &LayoutAnchorY {
|
|
|
|
&self.objc_controller.top
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn leading(&self) -> &LayoutAnchorX {
|
|
|
|
&self.objc_controller.leading
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn trailing(&self) -> &LayoutAnchorX {
|
|
|
|
&self.objc_controller.trailing
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bottom(&self) -> &LayoutAnchorY {
|
|
|
|
&self.objc_controller.bottom
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn width(&self) -> &LayoutAnchorDimension {
|
|
|
|
&self.objc_controller.width
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn height(&self) -> &LayoutAnchorDimension {
|
|
|
|
&self.objc_controller.height
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Layout for View<T> {
|
|
|
|
/// Returns the Objective-C object used for handling the view heirarchy.
|
|
|
|
fn get_backing_node(&self) -> Option<ShareId<Object>> {
|
|
|
|
self.objc_controller.objc.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_subview<V: Layout>(&self, subview: &V) {
|
|
|
|
self.objc_controller.add_subview(subview);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> std::fmt::Debug for View<T> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "View ({:p})", self)
|
|
|
|
}
|
|
|
|
}
|
2020-03-05 13:33:11 +11:00
|
|
|
|
2020-03-18 12:19:56 +11:00
|
|
|
impl<T> Drop for View<T> {
|
|
|
|
/// A bit of extra cleanup for delegate callback pointers.
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
let _ = Rc::from_raw(self.internal_callback_ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|