|
1 | | -use crate::{CAMetalLayer, Layer}; |
2 | 1 | use core::ffi::c_void; |
3 | | -use core_graphics::{base::CGFloat, geometry::CGRect}; |
4 | | -use objc::{ |
5 | | - msg_send, |
6 | | - runtime::{BOOL, YES}, |
7 | | -}; |
| 2 | +use objc2::rc::Retained; |
| 3 | +use objc2::ClassType; |
| 4 | +use objc2_foundation::{NSObject, NSObjectProtocol}; |
| 5 | +use objc2_quartz_core::CAMetalLayer; |
8 | 6 | use raw_window_handle::AppKitWindowHandle; |
9 | 7 | use std::ptr::NonNull; |
10 | 8 |
|
| 9 | +use crate::Layer; |
| 10 | + |
11 | 11 | /// |
12 | 12 | pub unsafe fn metal_layer_from_handle(handle: AppKitWindowHandle) -> Layer { |
13 | | - metal_layer_from_ns_view(handle.ns_view) |
| 13 | + unsafe { metal_layer_from_ns_view(handle.ns_view) } |
14 | 14 | } |
15 | 15 |
|
16 | 16 | /// |
17 | 17 | pub unsafe fn metal_layer_from_ns_view(view: NonNull<c_void>) -> Layer { |
18 | | - let view: cocoa::base::id = view.cast().as_ptr(); |
| 18 | + // SAFETY: Caller ensures that the view is valid. |
| 19 | + let obj = unsafe { view.cast::<NSObject>().as_ref() }; |
19 | 20 |
|
20 | 21 | // Check if the view is a CAMetalLayer |
21 | | - let class = class!(CAMetalLayer); |
22 | | - let is_actually_layer: BOOL = msg_send![view, isKindOfClass: class]; |
23 | | - if is_actually_layer == YES { |
24 | | - return Layer::Existing(view); |
| 22 | + if obj.is_kind_of::<CAMetalLayer>() { |
| 23 | + // SAFETY: Just checked that the view is a `CAMetalLayer`. |
| 24 | + let layer = unsafe { view.cast::<CAMetalLayer>().as_ref() }; |
| 25 | + return Layer { |
| 26 | + layer: layer.retain(), |
| 27 | + pre_existing: true, |
| 28 | + }; |
25 | 29 | } |
| 30 | + // Otherwise assume the view is `NSView` |
| 31 | + let view = unsafe { view.cast::<objc2_app_kit::NSView>().as_ref() }; |
26 | 32 |
|
27 | 33 | // Check if the view contains a valid CAMetalLayer |
28 | | - let existing: CAMetalLayer = msg_send![view, layer]; |
29 | | - let use_current = if existing.is_null() { |
30 | | - false |
31 | | - } else { |
32 | | - let result: BOOL = msg_send![existing, isKindOfClass: class]; |
33 | | - result == YES |
34 | | - }; |
35 | | - |
36 | | - let render_layer = if use_current { |
37 | | - Layer::Existing(existing) |
38 | | - } else { |
39 | | - // Allocate a new CAMetalLayer for the current view |
40 | | - let layer: CAMetalLayer = msg_send![class, new]; |
41 | | - let () = msg_send![view, setLayer: layer]; |
42 | | - let () = msg_send![view, setWantsLayer: YES]; |
43 | | - let bounds: CGRect = msg_send![view, bounds]; |
44 | | - let () = msg_send![layer, setBounds: bounds]; |
45 | | - |
46 | | - let window: cocoa::base::id = msg_send![view, window]; |
47 | | - if !window.is_null() { |
48 | | - let scale_factor: CGFloat = msg_send![window, backingScaleFactor]; |
49 | | - let () = msg_send![layer, setContentsScale: scale_factor]; |
| 34 | + let existing = unsafe { view.layer() }; |
| 35 | + if let Some(existing) = existing { |
| 36 | + if existing.is_kind_of::<CAMetalLayer>() { |
| 37 | + // SAFETY: Just checked that the layer is a `CAMetalLayer`. |
| 38 | + let layer = unsafe { Retained::cast::<CAMetalLayer>(existing) }; |
| 39 | + return Layer { |
| 40 | + layer, |
| 41 | + pre_existing: true, |
| 42 | + }; |
50 | 43 | } |
| 44 | + } |
51 | 45 |
|
52 | | - Layer::Allocated(layer) |
53 | | - }; |
| 46 | + // If the layer was not `CAMetalLayer`, allocate a new one for the view |
| 47 | + let layer = unsafe { CAMetalLayer::new() }; |
| 48 | + unsafe { view.setLayer(Some(&layer)) }; |
| 49 | + view.setWantsLayer(true); |
| 50 | + layer.setBounds(view.bounds()); |
54 | 51 |
|
55 | | - let _: *mut c_void = msg_send![view, retain]; |
56 | | - render_layer |
| 52 | + if let Some(window) = view.window() { |
| 53 | + let scale_factor = window.backingScaleFactor(); |
| 54 | + layer.setContentsScale(scale_factor); |
| 55 | + } |
| 56 | + |
| 57 | + Layer { |
| 58 | + layer, |
| 59 | + pre_existing: false, |
| 60 | + } |
57 | 61 | } |
0 commit comments