From 2413e8db1bedbb1f96dabe5c6d217258da80bbf6 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 24 Dec 2024 23:02:46 -0800 Subject: [PATCH] metal: handle views without the main layer --- blade-graphics/src/metal/surface.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/blade-graphics/src/metal/surface.rs b/blade-graphics/src/metal/surface.rs index 30e8759..8c259c0 100644 --- a/blade-graphics/src/metal/surface.rs +++ b/blade-graphics/src/metal/surface.rs @@ -73,12 +73,17 @@ impl super::Context { raw_window_handle::RawWindowHandle::AppKit(handle) => unsafe { let view = Retained::retain(handle.ns_view.as_ptr() as *mut objc2_app_kit::NSView) .unwrap(); - let main_layer = view.layer().unwrap(); - let render_layer = if main_layer.is_kind_of::() { - Retained::cast(main_layer) + let main_layer = view.layer(); + let render_layer = if main_layer + .as_ref() + .map_or(false, |layer| layer.is_kind_of::()) + { + Retained::cast(main_layer.unwrap()) } else { let new_layer = CAMetalLayer::new(); - new_layer.setFrame(main_layer.frame()); + if let Some(layer) = main_layer { + new_layer.setFrame(layer.frame()); + } view.setLayer(Some(&new_layer)); view.setWantsLayer(true); new_layer.setContentsGravity(objc2_quartz_core::kCAGravityTopLeft);