raw_window_handle_ffi/handles/
web.rs

1use crate::{RawWindowHandle, RawWindowHandleData};
2
3/// raw_window_handle_ffi:
4///         This type is ABI-stable and FFI-compatible analogue for [`raw_window_handle::WebWindowHandle`].
5///         Can be converted to and from the referenced type.
6#[repr(C)]
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub struct WebWindowHandle {
9    /// An ID value inserted into the [data attributes] of the canvas element as '`raw-handle`'.
10    ///
11    /// When accessing from JS, the attribute will automatically be called `rawHandle`.
12    ///
13    /// Each canvas created by the windowing system should be assigned their own unique ID.
14    ///
15    /// [data attributes]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/data-*
16    pub id: u32,
17}
18
19unsafe impl Send for WebWindowHandle {}
20unsafe impl Sync for WebWindowHandle {}
21
22impl From<raw_window_handle::WebWindowHandle> for WebWindowHandle {
23    fn from(value: raw_window_handle::WebWindowHandle) -> Self {
24        Self { id: value.id }
25    }
26}
27
28impl WebWindowHandle {
29    /// Converts FFI type back to the original [`raw_window_handle::AppKitWindowHandle`].
30    ///
31    /// # Safety
32    /// Original type [`raw_window_handle::AppKitWindowHandle`] is marked as `non_exaustive`,
33    /// and therefore it is impossible to convert to it completely safely.
34    pub unsafe fn into(self) -> raw_window_handle::WebWindowHandle {
35        raw_window_handle::WebWindowHandle::new(self.id)
36    }
37}
38
39impl From<WebWindowHandle> for RawWindowHandle {
40    fn from(val: WebWindowHandle) -> RawWindowHandle {
41        RawWindowHandle {
42            kind: crate::RawWindowHandleKind::WebWindowHandle,
43            data: RawWindowHandleData { web: val },
44        }
45    }
46}