1use core::ffi::c_void;
2use core::ptr::NonNull;
3
4/// Raw display handle for the Web.
5#[non_exhaustive]
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub struct WebDisplayHandle {}
8
9impl WebDisplayHandle {
10 /// Create a new empty display handle.
11 ///
12 ///
13 /// # Example
14 ///
15 /// ```
16 /// # use raw_window_handle::WebDisplayHandle;
17 /// let handle = WebDisplayHandle::new();
18 /// ```
19 pub fn new() -> Self {
20 Self {}
21 }
22}
23
24/// Raw window handle for the Web.
25#[non_exhaustive]
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
27pub struct WebWindowHandle {
28 /// An ID value inserted into the [data attributes] of the canvas element as '`raw-handle`'.
29 ///
30 /// When accessing from JS, the attribute will automatically be called `rawHandle`.
31 ///
32 /// Each canvas created by the windowing system should be assigned their own unique ID.
33 ///
34 /// [data attributes]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/data-*
35 pub id: u32,
36}
37
38impl WebWindowHandle {
39 /// Create a new handle to a canvas element.
40 ///
41 ///
42 /// # Example
43 ///
44 /// ```
45 /// # use raw_window_handle::WebWindowHandle;
46 /// #
47 /// let id: u32 = 0; // canvas.rawHandle;
48 /// let handle = WebWindowHandle::new(id);
49 /// ```
50 pub fn new(id: u32) -> Self {
51 Self { id }
52 }
53}
54
55/// Raw window handle for a Web canvas registered via [`wasm-bindgen`].
56///
57/// [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen
58#[non_exhaustive]
59#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
60pub struct WebCanvasWindowHandle {
61 /// A pointer to the [`JsValue`] of an [`HtmlCanvasElement`].
62 ///
63 /// Note: This uses [`c_void`] to avoid depending on `wasm-bindgen`
64 /// directly.
65 ///
66 /// [`JsValue`]: https://docs.rs/wasm-bindgen/latest/wasm_bindgen/struct.JsValue.html
67 /// [`HtmlCanvasElement`]: https://docs.rs/web-sys/latest/web_sys/struct.HtmlCanvasElement.html
68 //
69 // SAFETY: Not using `JsValue` is sound because `wasm-bindgen` guarantees
70 // that there's only one version of itself in any given binary, and hence
71 // we can't have a type-confusion where e.g. one library used `JsValue`
72 // from `v0.2` of `wasm-bindgen`, and another used `JsValue` from `v1.0`;
73 // the binary will simply fail to compile!
74 //
75 // Reference: TODO
76 pub obj: NonNull<c_void>,
77}
78
79impl WebCanvasWindowHandle {
80 /// Create a new handle from a pointer to [`HtmlCanvasElement`].
81 ///
82 /// [`HtmlCanvasElement`]: https://docs.rs/web-sys/latest/web_sys/struct.HtmlCanvasElement.html
83 ///
84 /// # Example
85 ///
86 /// ```
87 /// # use core::ffi::c_void;
88 /// # use core::ptr::NonNull;
89 /// # use raw_window_handle::WebCanvasWindowHandle;
90 /// # type HtmlCanvasElement = ();
91 /// # type JsValue = ();
92 /// let canvas: &HtmlCanvasElement;
93 /// # canvas = &();
94 /// let value: &JsValue = &canvas; // Deref to `JsValue`
95 /// let obj: NonNull<c_void> = NonNull::from(value).cast();
96 /// let mut handle = WebCanvasWindowHandle::new(obj);
97 /// ```
98 pub fn new(obj: NonNull<c_void>) -> Self {
99 Self { obj }
100 }
101}
102
103#[cfg(all(target_family = "wasm", feature = "wasm-bindgen-0-2"))]
104#[cfg_attr(
105 docsrs,
106 doc(cfg(all(target_family = "wasm", feature = "wasm-bindgen-0-2")))
107)]
108/// These implementations are only available when `wasm-bindgen-0-2` is enabled.
109impl WebCanvasWindowHandle {
110 /// Create a new `WebCanvasWindowHandle` from a [`wasm_bindgen::JsValue`].
111 ///
112 /// The `JsValue` should refer to a `HtmlCanvasElement`, and the lifetime
113 /// of the value should be at least as long as the lifetime of this.
114 pub fn from_wasm_bindgen_0_2(js_value: &wasm_bindgen::JsValue) -> Self {
115 Self::new(NonNull::from(js_value).cast())
116 }
117
118 /// Convert to the underlying [`wasm_bindgen::JsValue`].
119 ///
120 /// # Safety
121 ///
122 /// The inner pointer must be valid. This is ensured if this handle was
123 /// borrowed from [`WindowHandle`][crate::WindowHandle].
124 pub unsafe fn as_wasm_bindgen_0_2(&self) -> &wasm_bindgen::JsValue {
125 unsafe { self.obj.cast().as_ref() }
126 }
127}
128
129/// Raw window handle for a Web offscreen canvas registered via
130/// [`wasm-bindgen`].
131///
132/// [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen
133#[non_exhaustive]
134#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
135pub struct WebOffscreenCanvasWindowHandle {
136 /// A pointer to the [`JsValue`] of an [`OffscreenCanvas`].
137 ///
138 /// Note: This uses [`c_void`] to avoid depending on `wasm-bindgen`
139 /// directly.
140 ///
141 /// [`JsValue`]: https://docs.rs/wasm-bindgen/latest/wasm_bindgen/struct.JsValue.html
142 /// [`OffscreenCanvas`]: https://docs.rs/web-sys/latest/web_sys/struct.OffscreenCanvas.html
143 //
144 // SAFETY: See WebCanvasWindowHandle.
145 pub obj: NonNull<c_void>,
146}
147
148impl WebOffscreenCanvasWindowHandle {
149 /// Create a new handle from a pointer to an [`OffscreenCanvas`].
150 ///
151 /// [`OffscreenCanvas`]: https://docs.rs/web-sys/latest/web_sys/struct.OffscreenCanvas.html
152 ///
153 /// # Example
154 ///
155 /// ```
156 /// # use core::ffi::c_void;
157 /// # use core::ptr::NonNull;
158 /// # use raw_window_handle::WebOffscreenCanvasWindowHandle;
159 /// # type OffscreenCanvas = ();
160 /// # type JsValue = ();
161 /// let canvas: &OffscreenCanvas;
162 /// # canvas = &();
163 /// let value: &JsValue = &canvas; // Deref to `JsValue`
164 /// let obj: NonNull<c_void> = NonNull::from(value).cast();
165 /// let mut handle = WebOffscreenCanvasWindowHandle::new(obj);
166 /// ```
167 pub fn new(obj: NonNull<c_void>) -> Self {
168 Self { obj }
169 }
170}
171
172#[cfg(all(target_family = "wasm", feature = "wasm-bindgen-0-2"))]
173#[cfg_attr(
174 docsrs,
175 doc(cfg(all(target_family = "wasm", feature = "wasm-bindgen-0-2")))
176)]
177/// These implementations are only available when `wasm-bindgen-0-2` is enabled.
178impl WebOffscreenCanvasWindowHandle {
179 /// Create a new `WebOffscreenCanvasWindowHandle` from a
180 /// [`wasm_bindgen::JsValue`].
181 ///
182 /// The `JsValue` should refer to a `HtmlCanvasElement`, and the lifetime
183 /// of the value should be at least as long as the lifetime of this.
184 pub fn from_wasm_bindgen_0_2(js_value: &wasm_bindgen::JsValue) -> Self {
185 Self::new(NonNull::from(js_value).cast())
186 }
187
188 /// Convert to the underlying [`wasm_bindgen::JsValue`].
189 ///
190 /// # Safety
191 ///
192 /// The inner pointer must be valid. This is ensured if this handle was
193 /// borrowed from [`WindowHandle`][crate::WindowHandle].
194 pub unsafe fn as_wasm_bindgen_0_2(&self) -> &wasm_bindgen::JsValue {
195 unsafe { self.obj.cast().as_ref() }
196 }
197}
198