ndk icon indicating copy to clipboard operation
ndk copied to clipboard

Separate `InputEventJava` into `Key`/`Motion` `Java` structs to `Deref` into (and provide methods on) their native variants

Open yyy33 opened this issue 8 months ago • 2 comments

Currently InputEventJava doesn't have any available methods, can we modify it to an enumeration?

pub enum InputEventJava {
    MotionEvent(MotionEventJava),
    KeyEvent(KeyEventJava)
}

pub struct MotionEventJava(MotionEvent);

impl Deref for MotionEventJava {
    type Target = MotionEvent;

    fn deref(&self) -> &Self::Target {
        todo!()
    }
}
impl Drop for MotionEventJava {
    fn drop(&mut self) {
        todo!()
    }
}

pub struct KeyEventJava(KeyEvent);
impl Deref for KeyEventJava {
    type Target = KeyEvent;

    fn deref(&self) -> &Self::Target {
        todo!()
    }
}
impl Drop for KeyEventJava {
    fn drop(&mut self) {
        todo!()
    }
}

yyy33 avatar Apr 25 '25 14:04 yyy33

Good point, I didn't initially understand your request to use an enum here since that won't be changing/solving anything, but instead you need MotionEvent::from_java() and KeyEvent::from_java() to return two independent structures that Deref into the respective native wrapper to be able to call those methods on them.

I can do that.

MarijnS95 avatar Apr 25 '25 15:04 MarijnS95

Good point, I didn't initially understand your request to use an enum here since that won't be changing/solving anything, but instead you need MotionEvent::from_java() and KeyEvent::from_java() to return two independent structures that Deref into the respective native wrapper to be able to call those methods on them.

I can do that.

Can we add an unchecked version, in most of my project usage motion_event/key_event is always from java, and is unlikely to be null, giving the user the freedom to choose if they want to check for null pointers or not, and taking into account the fact that the from_java method has already been flagged as unsafe

  pub unsafe fn from_java_unchecked(env: *mut JNIEnv, key_event: jobject) -> MotionEventJava {
        let ptr = unsafe { ffi::AMotionEvent_fromJava(env, key_event) };
        MotionEventJava(Self::from_ptr(NonNull::new_unchecked(
            ptr.cast_mut(),
        )?))
    }

yyy33 avatar Apr 26 '25 01:04 yyy33