Skip to content

Commit c84a001

Browse files
fixed parented_window crash
1 parent 237d323 commit c84a001

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/macos/window.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ impl WindowInner {
109109

110110
fn raw_window_handle(&self) -> RawWindowHandle {
111111
if self.open.get() {
112-
let ns_window = self.ns_window.get().unwrap_or(ptr::null_mut()) as *mut c_void;
112+
let ns_window = self.ns_window.get().unwrap_or_else(|| unsafe {
113+
// If ns_window is None (e.g., for parented windows), try to get it from the view
114+
let window: id = msg_send![self.ns_view, window];
115+
window as *mut Object
116+
}) as *mut c_void;
113117

114118
let mut handle = AppKitWindowHandle::empty();
115119
handle.ns_window = ns_window;
@@ -149,12 +153,24 @@ impl<'a> Window<'a> {
149153
panic!("Not a macOS window");
150154
};
151155

156+
// Get the parent window - prefer ns_window from handle if available,
157+
// otherwise try to get it from the parent view
158+
let parent_window = if !handle.ns_window.is_null() {
159+
handle.ns_window as *mut Object
160+
} else {
161+
unsafe {
162+
let parent_view = handle.ns_view as id;
163+
let window: id = msg_send![parent_view, window];
164+
window as *mut Object
165+
}
166+
};
167+
152168
let ns_view = unsafe { create_view(&options) };
153169

154170
let window_inner = WindowInner {
155171
open: Cell::new(true),
156172
ns_app: Cell::new(None),
157-
ns_window: Cell::new(None),
173+
ns_window: Cell::new(Some(parent_window)),
158174
ns_view,
159175

160176
#[cfg(feature = "opengl")]
@@ -163,11 +179,13 @@ impl<'a> Window<'a> {
163179
.map(|gl_config| Self::create_gl_context(None, ns_view, gl_config)),
164180
};
165181

166-
let window_handle = Self::init(window_inner, window_info, build);
167-
168182
unsafe {
169183
let _: id = msg_send![handle.ns_view as *mut Object, addSubview: ns_view];
184+
}
185+
186+
let window_handle = Self::init(window_inner, window_info, build);
170187

188+
unsafe {
171189
let () = msg_send![pool, drain];
172190
}
173191

@@ -345,6 +363,7 @@ impl<'a> Window<'a> {
345363

346364
#[cfg(feature = "opengl")]
347365
fn create_gl_context(ns_window: Option<id>, ns_view: id, config: GlConfig) -> GlContext {
366+
348367
let mut handle = AppKitWindowHandle::empty();
349368
handle.ns_window = ns_window.unwrap_or(ptr::null_mut()) as *mut c_void;
350369
handle.ns_view = ns_view as *mut c_void;

0 commit comments

Comments
 (0)