raw_window_handle/ohos.rs
1//! [OpenHarmony] OS Window Handles
2//!
3//! ## Background
4//!
5//! Applications on [OpenHarmony] use [ArkUI] for defining their UI. Applications can use an
6//! [XComponent] to render using native Code (e.g. Rust) via EGL.
7//! Native code will receive a callback `OnSurfaceCreatedCB(OH_NativeXComponent *component, void *window)`
8//! when the `XComponent` is created. The window argument has the type [`OHNativeWindow`] / `EGLNativeWindowType`.
9//! The window can then be used to create a surface with
10//! `eglCreateWindowSurface(eglDisplay_, eglConfig_, window, NULL)`
11//!
12//! [OpenHarmony]: https://gitee.com/openharmony/docs/blob/master/en/OpenHarmony-Overview.md
13//! [ArkUI]: https://gitee.com/openharmony/docs/blob/master/en/application-dev/ui/arkui-overview.md
14//! [XComponent]: https://gitee.com/openharmony/docs/blob/master/en/application-dev/ui/arkts-common-components-xcomponent.md
15//! [`OHNativeWindow`]: https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis-arkgraphics2d/_native_window.md
16
17use core::ffi::c_void;
18use core::ptr::NonNull;
19
20use super::DisplayHandle;
21
22/// Raw display handle for OpenHarmony.
23#[non_exhaustive]
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
25pub struct OhosDisplayHandle {}
26
27impl OhosDisplayHandle {
28 /// Create a new empty display handle.
29 ///
30 ///
31 /// # Example
32 ///
33 /// ```
34 /// # use raw_window_handle::OhosDisplayHandle;
35 /// let handle = OhosDisplayHandle::new();
36 /// ```
37 pub fn new() -> Self {
38 Self {}
39 }
40}
41
42impl DisplayHandle<'static> {
43 /// Create an OpenHarmony-based display handle.
44 ///
45 /// As no data is borrowed by this handle, it is completely safe to create. This function
46 /// may be useful to windowing framework implementations that want to avoid unsafe code.
47 ///
48 /// # Example
49 ///
50 /// ```
51 /// # use raw_window_handle::{DisplayHandle, HasDisplayHandle};
52 /// # fn do_something(rwh: impl HasDisplayHandle) { let _ = rwh; }
53 /// let handle = DisplayHandle::ohos();
54 /// do_something(handle);
55 /// ```
56 pub fn ohos() -> Self {
57 // SAFETY: No data is borrowed.
58 unsafe { Self::borrow_raw(OhosDisplayHandle::new().into()) }
59 }
60}
61
62/// Raw window handle for Ohos NDK.
63#[non_exhaustive]
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
65pub struct OhosNdkWindowHandle {
66 pub native_window: NonNull<c_void>,
67}
68
69impl OhosNdkWindowHandle {
70 /// Create a new handle to an [`OHNativeWindow`] on OpenHarmony.
71 ///
72 /// The handle will typically be created from an [`XComponent`], consult the
73 /// [native `XComponent` Guidelines] for more details.
74 ///
75 /// [`XComponent`]: https://gitee.com/openharmony/docs/blob/master/en/application-dev/ui/arkts-common-components-xcomponent.md
76 /// [native `XComponent` Guidelines]: https://gitee.com/openharmony/docs/blob/OpenHarmony-4.0-Release/en/application-dev/napi/xcomponent-guidelines.md
77 /// [`OHNativeWindow`]: https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis-arkgraphics2d/_native_window.md
78 /// # Example
79 ///
80 /// ```
81 /// # use core::ptr::NonNull;
82 /// # use core::ffi::c_void;
83 /// # use raw_window_handle::OhosNdkWindowHandle;
84 /// # #[allow(non_camel_case_types)]
85 /// # type OH_NativeXComponent = ();
86 ///
87 /// /// Called When the `XComponent` is created.
88 /// ///
89 /// /// See the [XComponent Guidelines](https://gitee.com/openharmony/docs/blob/OpenHarmony-4.0-Release/en/application-dev/napi/xcomponent-guidelines.md)
90 /// /// for more details
91 /// extern "C" fn on_surface_created_callback(component: *mut OH_NativeXComponent, window: *mut c_void) {
92 /// let handle = OhosNdkWindowHandle::new(NonNull::new(window).unwrap());
93 /// }
94 /// ```
95 pub fn new(native_window: NonNull<c_void>) -> Self {
96 Self { native_window }
97 }
98}