Skip to content

Commit

Permalink
refactor(macos): migrate WryWebViewParent to objc2
Browse files Browse the repository at this point in the history
  • Loading branch information
pewsheen committed Jul 11, 2024
1 parent ce7b9d0 commit d2ff37d
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions src/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,27 +764,7 @@ r#"Object.defineProperty(window, 'ipc', {
if is_child {
ns_view.addSubview(&webview);
} else {
let parent_view_cls = match ClassBuilder::new("WryWebViewParent", NSView::class()) {
Some(mut decl) => {
decl.add_method(objc2::sel!(keyDown:), key_down as extern "C" fn(_, _, _));

extern "C" fn key_down(_this: &NSView, _sel: objc2::runtime::Sel, event: &NSEvent) {
unsafe {
let mtm = MainThreadMarker::new().unwrap();
let app = NSApp(mtm);
if let Some(menu) = app.mainMenu() {
menu.performKeyEquivalent(event);
}
}
}

decl.register()
}
None => class!(NSView),
};

let parent_view: Allocated<NSView> = objc2::msg_send_id![parent_view_cls, alloc];
let parent_view = NSView::init(parent_view);
let parent_view = WryWebViewParent::new(mtm);
parent_view.setAutoresizingMask(
NSAutoresizingMaskOptions::NSViewHeightSizable
| NSAutoresizingMaskOptions::NSViewWidthSizable,
Expand Down Expand Up @@ -1741,3 +1721,44 @@ impl WryWebViewUIDelegate {
unsafe { msg_send_id![super(delegate), init] }
}
}

struct WryWebViewParentIvars {}

declare_class!(
struct WryWebViewParent;

unsafe impl ClassType for WryWebViewParent {
type Super = NSView;
type Mutability = MainThreadOnly;
const NAME: &'static str = "WryWebViewParent";
}

impl DeclaredClass for WryWebViewParent {
type Ivars = WryWebViewParentIvars;
}

unsafe impl WryWebViewParent {
#[method(keyDown:)]
fn key_down(
&self,
event: &NSEvent,
) {
let mtm = MainThreadMarker::new().unwrap();
let app = NSApp(mtm);
unsafe {
if let Some(menu) = app.mainMenu() {
menu.performKeyEquivalent(event);
}
}
}
}
);

impl WryWebViewParent {
fn new(mtm: MainThreadMarker) -> Retained<Self> {
let delegate = mtm
.alloc::<WryWebViewParent>()
.set_ivars(WryWebViewParentIvars {});
unsafe { msg_send_id![super(delegate), init] }
}
}

0 comments on commit d2ff37d

Please sign in to comment.