Skip to main content

winio_ui_app_kit/widgets/
wgpu.rs

1use std::ptr::NonNull;
2
3use inherit_methods_macro::inherit_methods;
4use objc2::rc::Retained;
5use wgpu::{
6    CreateSurfaceError, Instance, Surface, SurfaceTarget,
7    rwh::{
8        AppKitWindowHandle, DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle,
9        RawWindowHandle, WindowHandle,
10    },
11};
12use winio_handle::{AsContainer, AsWidget};
13use winio_primitive::{MouseButton, Point, Size, Vector};
14
15use crate::{CanvasImpl, Result};
16
17#[derive(Debug)]
18pub struct WgpuCanvas {
19    handle: CanvasImpl,
20}
21
22#[inherit_methods(from = "self.handle")]
23impl WgpuCanvas {
24    pub fn new(parent: impl AsContainer) -> Result<Self> {
25        let handle = CanvasImpl::new(parent)?;
26        Ok(Self { handle })
27    }
28
29    pub fn is_visible(&self) -> Result<bool>;
30
31    pub fn set_visible(&mut self, v: bool) -> Result<()>;
32
33    pub fn is_enabled(&self) -> Result<bool>;
34
35    pub fn set_enabled(&mut self, v: bool) -> Result<()>;
36
37    pub fn loc(&self) -> Result<Point>;
38
39    pub fn set_loc(&mut self, p: Point) -> Result<()>;
40
41    pub fn size(&self) -> Result<Size>;
42
43    pub fn set_size(&mut self, v: Size) -> Result<()>;
44
45    pub fn tooltip(&self) -> Result<String>;
46
47    pub fn set_tooltip(&mut self, s: impl AsRef<str>) -> Result<()>;
48
49    pub async fn wait_mouse_down(&self) -> MouseButton {
50        self.handle.wait_mouse_down().await
51    }
52
53    pub async fn wait_mouse_up(&self) -> MouseButton {
54        self.handle.wait_mouse_up().await
55    }
56
57    pub async fn wait_mouse_move(&self) -> Point {
58        self.handle.wait_mouse_move().await
59    }
60
61    pub async fn wait_mouse_wheel(&self) -> Vector {
62        self.handle.wait_mouse_wheel().await
63    }
64
65    pub fn create_surface(
66        &self,
67        instance: &Instance,
68    ) -> std::result::Result<Surface<'static>, CreateSurfaceError> {
69        let handle = RawWindowHandle::AppKit(AppKitWindowHandle::new(
70            NonNull::new(
71                Retained::as_ptr(self.handle.as_widget().as_app_kit())
72                    .cast_mut()
73                    .cast(),
74            )
75            .unwrap(),
76        ));
77
78        struct WindowHandleWrapper(RawWindowHandle);
79
80        unsafe impl Send for WindowHandleWrapper {}
81        unsafe impl Sync for WindowHandleWrapper {}
82
83        impl HasWindowHandle for WindowHandleWrapper {
84            fn window_handle(&self) -> std::result::Result<WindowHandle<'_>, HandleError> {
85                Ok(unsafe { WindowHandle::borrow_raw(self.0) })
86            }
87        }
88
89        impl HasDisplayHandle for WindowHandleWrapper {
90            fn display_handle(&self) -> std::result::Result<DisplayHandle<'_>, HandleError> {
91                Ok(DisplayHandle::appkit())
92            }
93        }
94
95        let target = SurfaceTarget::from(WindowHandleWrapper(handle));
96
97        instance.create_surface(target)
98    }
99}
100
101winio_handle::impl_as_widget!(WgpuCanvas, handle);