Skip to content

Commit

Permalink
fix(macos): remove webview ivar in WryWebView (#943)
Browse files Browse the repository at this point in the history
* fix(mac): remove redundant webview ivar

* chore: add change log

* Update .changes/remove-webview-ivar.md
  • Loading branch information
pewsheen authored Apr 26, 2023
1 parent 3e6e4c2 commit c9002c1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .changes/remove-webview-ivar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

On iOS, fix panic at runtime due to setting webview ivar.
2 changes: 0 additions & 2 deletions src/webview/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ impl InnerWebView {
Some(mut decl) => {
#[cfg(target_os = "macos")]
{
decl.add_ivar::<id>("webview");
add_file_drop_methods(&mut decl);
synthetic_mouse_events::setup(&mut decl);
decl.add_ivar::<bool>(ACCEPT_FIRST_MOUSE);
Expand All @@ -305,7 +304,6 @@ impl InnerWebView {
_ => class!(WryWebView),
};
let webview: id = msg_send![cls, alloc];
(*webview).set_ivar("webview", webview);

let () = msg_send![config, setWebsiteDataStore: data_store];
let _preference: id = msg_send![config, preferences];
Expand Down
71 changes: 32 additions & 39 deletions src/webview/wkwebview/synthetic_mouse_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,63 +12,56 @@ use std::{ffi::c_void, ptr::null};
pub unsafe fn setup(decl: &mut ClassDecl) {
decl.add_method(
sel!(otherMouseDown:),
other_mouse_down as extern "C" fn(&Object, Sel, id),
other_mouse_down as extern "C" fn(&mut Object, Sel, id),
);
decl.add_method(
sel!(otherMouseUp:),
other_mouse_up as extern "C" fn(&Object, Sel, id),
other_mouse_up as extern "C" fn(&mut Object, Sel, id),
);
}

extern "C" fn other_mouse_down(this: &Object, _sel: Sel, event: id) {
extern "C" fn other_mouse_down(this: &mut Object, _sel: Sel, event: id) {
unsafe {
if event.eventType() == NSEventType::NSOtherMouseDown {
let webview = this.get_ivar::<id>("webview");
if *webview != nil {
let button_number = event.buttonNumber();
match button_number {
// back button
3 => {
let js = create_js_mouse_event(*webview, event, true, true);
let _: id = msg_send![*webview, evaluateJavaScript:NSString::new(&js) completionHandler:null::<*const c_void>()];
return;
}
// forward button
4 => {
let js = create_js_mouse_event(*webview, event, true, false);
let _: id = msg_send![*webview, evaluateJavaScript:NSString::new(&js) completionHandler:null::<*const c_void>()];
return;
}

_ => {}
let button_number = event.buttonNumber();
match button_number {
// back button
3 => {
let js = create_js_mouse_event(this, event, true, true);
let _: id = msg_send![this, evaluateJavaScript:NSString::new(&js) completionHandler:null::<*const c_void>()];
return;
}
// forward button
4 => {
let js = create_js_mouse_event(this, event, true, false);
let _: id = msg_send![this, evaluateJavaScript:NSString::new(&js) completionHandler:null::<*const c_void>()];
return;
}
_ => {}
}
}

let _: () = msg_send![this, mouseDown: event];
}
}
extern "C" fn other_mouse_up(this: &Object, _sel: Sel, event: id) {
extern "C" fn other_mouse_up(this: &mut Object, _sel: Sel, event: id) {
unsafe {
if event.eventType() == NSEventType::NSOtherMouseUp {
let webview = this.get_ivar::<id>("webview");
if *webview != nil {
let button_number = event.buttonNumber();
match button_number {
// back button
3 => {
let js = create_js_mouse_event(*webview, event, false, true);
let _: id = msg_send![*webview, evaluateJavaScript:NSString::new(&js) completionHandler:null::<*const c_void>()];
return;
}
// forward button
4 => {
let js = create_js_mouse_event(*webview, event, false, false);
let _: id = msg_send![*webview, evaluateJavaScript:NSString::new(&js) completionHandler:null::<*const c_void>()];
return;
}
_ => {}
let button_number = event.buttonNumber();
match button_number {
// back button
3 => {
let js = create_js_mouse_event(this, event, false, true);
let _: id = msg_send![this, evaluateJavaScript:NSString::new(&js) completionHandler:null::<*const c_void>()];
return;
}
// forward button
4 => {
let js = create_js_mouse_event(this, event, false, false);
let _: id = msg_send![this, evaluateJavaScript:NSString::new(&js) completionHandler:null::<*const c_void>()];
return;
}
_ => {}
}
}

Expand Down

0 comments on commit c9002c1

Please sign in to comment.