Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
yyy33 opened this issue Apr 25, 2025 · 2 comments · May be fixed by #503

Comments

@yyy33
Copy link

yyy33 commented Apr 25, 2025

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!()
    }
}
@MarijnS95 MarijnS95 changed the title Comments Off on Changes to InputEventJava Separate InputEventJava into Key/Motion Java structs to Deref into (and provide methods on) their native variants Apr 25, 2025
@MarijnS95
Copy link
Member

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 MarijnS95 added this to the 0.10.0 release milestone Apr 25, 2025
@yyy33
Copy link
Author

yyy33 commented Apr 26, 2025

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(),
        )?))
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants