Update webview implementation to use load_or_register_class (Issue #63)

This commit is contained in:
Ryan McGrath 2022-10-12 14:32:23 -04:00
parent b187b1dc49
commit 2525cbfee4
No known key found for this signature in database
GPG key ID: DA6CBD9233593DEA

View file

@ -12,7 +12,7 @@ use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel};
use objc::{class, msg_send, sel, sel_impl};
use crate::foundation::{id, nil, NSArray, NSInteger, NSString, NO, YES};
use crate::foundation::{id, nil, NSArray, NSInteger, NSString, NO, YES, load_or_register_class};
use crate::webview::actions::{NavigationAction, NavigationResponse};
use crate::webview::{mimetype::MimeType, WebViewDelegate, WEBVIEW_DELEGATE_PTR}; //, OpenPanelParameters};
//use crate::webview::enums::{NavigationPolicy, NavigationResponsePolicy};
@ -186,13 +186,7 @@ pub fn register_webview_class() -> *const Class {
/// 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();
load_or_register_class("NSObject", "RSTWebViewDelegate", |decl| unsafe {
decl.add_ivar::<usize>(WEBVIEW_DELEGATE_PTR);
// WKNavigationDelegate
@ -239,9 +233,5 @@ pub fn register_webview_delegate_class<T: WebViewDelegate>() -> *const Class {
sel!(_download:decideDestinationWithSuggestedFilename:completionHandler:),
handle_download::<T> as extern "C" fn(&Object, _, id, id, usize)
);
VIEW_CLASS = decl.register();
});
unsafe { VIEW_CLASS }
})
}