On macOS, fix wry window can crash if unfocused (#714)

This commit is contained in:
Ngo Iok Ui (Wu Yu Wei)
2023-03-24 17:13:46 +08:00
committed by GitHub
parent 407a9feaed
commit 6a03847f6d
2 changed files with 11 additions and 1 deletions

6
.changes/views.md Normal file
View File

@@ -0,0 +1,6 @@
---
"tao": patch
---
On macOS, fix wry window will crash if unfocused.

View File

@@ -34,6 +34,9 @@ use crate::{
pub struct WindowDelegateState {
ns_window: IdRef, // never changes
// We keep this ns_view because we still need its view state for some extern function
// like didResignKey
ns_view: IdRef, // never changes
window: Weak<UnownedWindow>,
@@ -60,6 +63,7 @@ impl WindowDelegateState {
let scale_factor = window.scale_factor();
let mut delegate_state = WindowDelegateState {
ns_window: window.ns_window.clone(),
ns_view: window.ns_view.clone(),
window: Arc::downgrade(window),
initial_fullscreen,
previous_position: None,
@@ -398,7 +402,7 @@ extern "C" fn window_did_resign_key(this: &Object, _: Sel, _: id) {
// Object referenced by state.ns_view (an IdRef, which is dereferenced
// to an id)
let view_state: &mut ViewState = unsafe {
let ns_view: &Object = state.ns_view().as_ref().expect("failed to deref");
let ns_view: &Object = (*state.ns_view).as_ref().expect("failed to deref");
let state_ptr: *mut c_void = *ns_view.get_ivar("taoState");
&mut *(state_ptr as *mut ViewState)
};