Skip to main content

ohos_xcomponent_binding/events/
raw_window.rs

1use raw_window_handle::{OhosNdkWindowHandle, RawWindowHandle};
2use std::{
3    os::raw::c_void,
4    ptr::NonNull,
5    sync::{LazyLock, RwLock},
6};
7
8// Same with WindowRaw, but thread safe.
9#[derive(Debug, Clone, Copy)]
10pub struct RawWindow {
11    pub(crate) raw: *mut c_void,
12}
13
14unsafe impl Send for RawWindow {}
15unsafe impl Sync for RawWindow {}
16
17impl RawWindow {
18    pub fn new(window: *mut c_void) -> Self {
19        RawWindow { raw: window }
20    }
21
22    pub fn raw(&self) -> *mut c_void {
23        self.raw
24    }
25
26    /// Get window handle
27    pub fn raw_window_handle(&self) -> Option<RawWindowHandle> {
28        let guard = (*RAW_WINDOW).read();
29        if let Ok(guard) = guard {
30            if let Some(win) = &*guard {
31                let win = NonNull::new(win.raw);
32                if let Some(win) = win {
33                    return Some(RawWindowHandle::OhosNdk(OhosNdkWindowHandle::new(win)));
34                }
35                return None;
36            }
37            return None;
38        }
39        None
40    }
41}
42
43pub(crate) static RAW_WINDOW: LazyLock<RwLock<Option<RawWindow>>> =
44    LazyLock::new(|| RwLock::new(None));