Skip to content

Commit

Permalink
ndk: Implement HasRawWindowHandle directly on NativeWindow
Browse files Browse the repository at this point in the history
Crates wrapping the NDK (winit and others), and `NativeWindow` in
particular have to implement `HasRawWindowHandle` on their own wrapper
types, yet the `ndk` should also implement this trait on the
`NativeWindow` type directly for them to reuse.  Or more importantly,
when the "raw" `ndk` crate is used directly.
  • Loading branch information
MarijnS95 committed May 13, 2022
1 parent b3b9a95 commit eb2d7cf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions ndk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ test = ["ffi/test", "jni", "jni-glue", "all"]
bitflags = "1.2.1"
jni-sys = "0.3.0"
num_enum = "0.5.1"
raw-window-handle = "0.4"
thiserror = "1.0.23"

[dependencies.jni]
Expand Down
11 changes: 10 additions & 1 deletion ndk/src/native_window.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Bindings for [`ffi::ANativeWindow`]
use std::ptr::NonNull;
use raw_window_handle::{AndroidNdkHandle, HasRawWindowHandle, RawWindowHandle};
use std::{ffi::c_void, ptr::NonNull};

#[derive(Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct NativeWindow {
Expand All @@ -24,6 +25,14 @@ impl Clone for NativeWindow {
}
}

unsafe impl HasRawWindowHandle for NativeWindow {
fn raw_window_handle(&self) -> RawWindowHandle {
let mut handle = AndroidNdkHandle::empty();
handle.a_native_window = self.ptr.as_ptr() as *mut c_void;
RawWindowHandle::AndroidNdk(handle)
}
}

impl NativeWindow {
/// Assumes ownership of `ptr`
///
Expand Down

0 comments on commit eb2d7cf

Please sign in to comment.