apple_compat.rs

 1use super::{BladeContext, BladeRenderer, BladeSurfaceConfig};
 2use blade_graphics as gpu;
 3use std::{ffi::c_void, ptr::NonNull};
 4
 5#[derive(Clone)]
 6pub struct Context {
 7    inner: BladeContext,
 8}
 9impl Default for Context {
10    fn default() -> Self {
11        Self {
12            inner: BladeContext::new().unwrap(),
13        }
14    }
15}
16
17pub type Renderer = BladeRenderer;
18
19pub unsafe fn new_renderer(
20    context: Context,
21    _native_window: *mut c_void,
22    native_view: *mut c_void,
23    bounds: crate::Size<f32>,
24    transparent: bool,
25) -> Renderer {
26    use raw_window_handle as rwh;
27    struct RawWindow {
28        view: *mut c_void,
29    }
30
31    impl rwh::HasWindowHandle for RawWindow {
32        fn window_handle(&self) -> Result<rwh::WindowHandle<'_>, rwh::HandleError> {
33            let view = NonNull::new(self.view).unwrap();
34            let handle = rwh::AppKitWindowHandle::new(view);
35            Ok(unsafe { rwh::WindowHandle::borrow_raw(handle.into()) })
36        }
37    }
38    impl rwh::HasDisplayHandle for RawWindow {
39        fn display_handle(&self) -> Result<rwh::DisplayHandle<'_>, rwh::HandleError> {
40            let handle = rwh::AppKitDisplayHandle::new();
41            Ok(unsafe { rwh::DisplayHandle::borrow_raw(handle.into()) })
42        }
43    }
44
45    BladeRenderer::new(
46        &context.inner,
47        &RawWindow {
48            view: native_view as *mut _,
49        },
50        BladeSurfaceConfig {
51            size: gpu::Extent {
52                width: bounds.width as u32,
53                height: bounds.height as u32,
54                depth: 1,
55            },
56            transparent,
57        },
58    )
59    .unwrap()
60}