Skip to content

Commit a237f62

Browse files
authored
ndk/event: Add missing Axis variants and new MotionClassification API (#485)
1 parent 1fdce42 commit a237f62

File tree

1 file changed

+118
-5
lines changed

1 file changed

+118
-5
lines changed

ndk/src/event.rs

Lines changed: 118 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,63 @@ pub enum Axis {
413413
#[doc(alias = "AMOTION_EVENT_AXIS_GENERIC_16")]
414414
Generic16 = ffi::AMOTION_EVENT_AXIS_GENERIC_16 as i32,
415415

416+
/// Axis constant: X gesture offset axis of a motion event.
417+
///
418+
/// - For a touch pad, reports the distance that a swipe gesture has moved in the X axis, as a
419+
/// proportion of the touch pad's size. For example, if a touch pad is `1000` units wide, and
420+
/// a swipe gesture starts at `X = 500` then moves to `X = 400`, this axis would have a value
421+
/// of `-0.1`.
422+
///
423+
/// These values are relative to the state from the last event, not accumulated, so developers
424+
/// should make sure to process this axis value for all batched historical events.
425+
///
426+
/// This axis is only set on the first pointer in a motion event.
427+
#[doc(alias = "AMOTION_EVENT_AXIS_GESTURE_X_OFFSET")]
428+
GestureXOffset = ffi::AMOTION_EVENT_AXIS_GESTURE_X_OFFSET as i32,
429+
/// Axis constant: Y gesture offset axis of a motion event.
430+
///
431+
/// The same as [`Axis::GestureXOffset`], but for the Y axis.
432+
#[doc(alias = "AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET")]
433+
GestureYOffset = ffi::AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET as i32,
434+
/// Axis constant: X scroll distance axis of a motion event.
435+
///
436+
/// - For a touch pad, reports the distance that should be scrolled in the X axis as a result of
437+
/// the user's two-finger scroll gesture, in display pixels.
438+
///
439+
/// These values are relative to the state from the last event, not accumulated, so developers
440+
/// should make sure to process this axis value for all batched historical events.
441+
///
442+
/// This axis is only set on the first pointer in a motion event.
443+
#[doc(alias = "AMOTION_EVENT_AXIS_GESTURE_SCROLL_X_DISTANCE")]
444+
GestureScrollXDistance = ffi::AMOTION_EVENT_AXIS_GESTURE_SCROLL_X_DISTANCE as i32,
445+
/// Axis constant: Y scroll distance axis of a motion event.
446+
///
447+
/// The same as [`Axis::GestureScrollXDistance`], but for the Y axis.
448+
#[doc(alias = "AMOTION_EVENT_AXIS_GESTURE_SCROLL_Y_DISTANCE")]
449+
GestureScrollYDistance = ffi::AMOTION_EVENT_AXIS_GESTURE_SCROLL_Y_DISTANCE as i32,
450+
/// Axis constant: pinch scale factor of a motion event.
451+
///
452+
/// - For a touch pad, reports the change in distance between the fingers when the user is
453+
/// making a pinch gesture, as a proportion of that distance when the gesture was last
454+
/// reported. For example, if the fingers were `50` units apart and are now `52` units apart,
455+
/// the scale factor would be `1.04`.
456+
///
457+
/// These values are relative to the state from the last event, not accumulated, so developers
458+
/// should make sure to process this axis value for all batched historical events.
459+
///
460+
/// This axis is only set on the first pointer in a motion event.
461+
#[doc(alias = "AMOTION_EVENT_AXIS_GESTURE_PINCH_SCALE_FACTOR")]
462+
GesturePinchScaleFactor = ffi::AMOTION_EVENT_AXIS_GESTURE_PINCH_SCALE_FACTOR as i32,
463+
/// Axis constant: the number of fingers being used in a multi-finger swipe gesture.
464+
///
465+
/// - For a touch pad, reports the number of fingers being used in a multi-finger swipe gesture
466+
/// (with [`MotionClassification::MultiFingerSwipe`]).
467+
///
468+
/// Since [`MotionClassification::MultiFingerSwipe`] is a hidden API, so is this axis. It is
469+
/// only set on the first pointer in a motion event.
470+
#[doc(alias = "AMOTION_EVENT_AXIS_GESTURE_SWIPE_FINGER_COUNT")]
471+
GestureSwipeFingerCount = ffi::AMOTION_EVENT_AXIS_GESTURE_SWIPE_FINGER_COUNT as i32,
472+
416473
#[doc(hidden)]
417474
#[num_enum(catch_all)]
418475
__Unknown(i32),
@@ -441,6 +498,44 @@ pub enum ToolType {
441498
__Unknown(i32),
442499
}
443500

501+
/// Constants that identify different gesture classification types.
502+
#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)]
503+
#[repr(u32)]
504+
#[non_exhaustive]
505+
#[doc(alias = "AMotionClassification")]
506+
pub enum MotionClassification {
507+
/// No additional information is available about the current motion event stream.
508+
#[doc(alias = "AMOTION_EVENT_CLASSIFICATION_NONE")]
509+
None = 0,
510+
/// The user's intent with respect to the current event stream is not yet determined. Events
511+
/// starting in #AMOTION_EVENT_CLASSIFICATION_AMBIGUOUS_GESTURE will eventually resolve into
512+
/// either [`MotionClassification::DeepPress`] or [`MotionClassification::None`].
513+
/// Gestural actions, such as scrolling, should be inhibited until the classification resolves
514+
/// to another value or the event stream ends.
515+
#[doc(alias = "AMOTION_EVENT_CLASSIFICATION_AMBIGUOUS_GESTURE")]
516+
AmbiguousGesture = 1,
517+
/// The current event stream represents the user intentionally pressing harder on the screen.
518+
/// This classification type should be used to accelerate the long press behaviour.
519+
#[doc(alias = "AMOTION_EVENT_CLASSIFICATION_DEEP_PRESS")]
520+
DeepPress = 2,
521+
/// The current event stream represents the user swiping with two fingers on a touchpad.
522+
#[doc(alias = "AMOTION_EVENT_CLASSIFICATION_TWO_FINGER_SWIPE")]
523+
TwoFingerSwipe = 3,
524+
/// The current event stream represents the user swiping with three or more fingers on a
525+
/// touchpad. Unlike two-finger swipes, these are only to be handled by the system UI, which is
526+
/// why they have a separate constant from two-finger swipes.
527+
#[doc(alias = "AMOTION_EVENT_CLASSIFICATION_MULTI_FINGER_SWIPE")]
528+
MultiFingerSwipe = 4,
529+
/// The current event stream represents the user pinching with two fingers on a touchpad. The
530+
/// gesture is centered around the current cursor position.
531+
#[doc(alias = "AMOTION_EVENT_CLASSIFICATION_PINCH")]
532+
Pinch = 5,
533+
534+
#[doc(hidden)]
535+
#[num_enum(catch_all)]
536+
__Unknown(u32),
537+
}
538+
444539
/// A bitfield representing the state of buttons during a motion event.
445540
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
446541
pub struct ButtonState(pub u32);
@@ -510,7 +605,6 @@ impl EdgeFlags {
510605
}
511606
}
512607

513-
// TODO: bitflags
514608
/// Flags associated with this [`MotionEvent`].
515609
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
516610
pub struct MotionEventFlags(pub u32);
@@ -692,7 +786,7 @@ impl MotionEvent {
692786
#[inline]
693787
#[doc(alias = "AMotionEvent_getMetaState")]
694788
pub fn meta_state(&self) -> MetaState {
695-
unsafe { MetaState(ffi::AMotionEvent_getMetaState(self.ptr.as_ptr()) as u32) }
789+
MetaState(unsafe { ffi::AMotionEvent_getMetaState(self.ptr.as_ptr()) } as u32)
696790
}
697791

698792
/// Returns the button state during this event, as a bitfield.
@@ -702,7 +796,7 @@ impl MotionEvent {
702796
#[inline]
703797
#[doc(alias = "AMotionEvent_getButtonState")]
704798
pub fn button_state(&self) -> ButtonState {
705-
unsafe { ButtonState(ffi::AMotionEvent_getButtonState(self.ptr.as_ptr()) as u32) }
799+
ButtonState(unsafe { ffi::AMotionEvent_getButtonState(self.ptr.as_ptr()) } as u32)
706800
}
707801

708802
/// Returns the time of the start of this gesture, in the `java.lang.System.nanoTime()` time
@@ -723,7 +817,7 @@ impl MotionEvent {
723817
#[inline]
724818
#[doc(alias = "AMotionEvent_getEdgeFlags")]
725819
pub fn edge_flags(&self) -> EdgeFlags {
726-
unsafe { EdgeFlags(ffi::AMotionEvent_getEdgeFlags(self.ptr.as_ptr()) as u32) }
820+
EdgeFlags(unsafe { ffi::AMotionEvent_getEdgeFlags(self.ptr.as_ptr()) } as u32)
727821
}
728822

729823
/// Returns the time of this event, in the `java.lang.System.nanoTime()` time base
@@ -743,7 +837,7 @@ impl MotionEvent {
743837
#[inline]
744838
#[doc(alias = "AMotionEvent_getFlags")]
745839
pub fn flags(&self) -> MotionEventFlags {
746-
unsafe { MotionEventFlags(ffi::AMotionEvent_getFlags(self.ptr.as_ptr()) as u32) }
840+
MotionEventFlags(unsafe { ffi::AMotionEvent_getFlags(self.ptr.as_ptr()) } as u32)
747841
}
748842

749843
/// Returns the offset in the x direction between the coordinates and the raw coordinates
@@ -785,6 +879,25 @@ impl MotionEvent {
785879
pub fn y_precision(&self) -> f32 {
786880
unsafe { ffi::AMotionEvent_getYPrecision(self.ptr.as_ptr()) }
787881
}
882+
883+
/// Get the action button for the motion event. Returns a valid action button when the event is
884+
/// associated with a button press or button release action. For other actions the return value
885+
/// is undefined.
886+
#[cfg(feature = "api-level-33")]
887+
#[doc(alias = "AMotionEvent_getActionButton")]
888+
pub fn action_button(&self) -> ButtonState {
889+
ButtonState(unsafe { ffi::AMotionEvent_getActionButton(self.ptr.as_ptr()) } as u32)
890+
}
891+
892+
/// Returns the classification for the current gesture. The classification may change as more
893+
/// events become available for the same gesture.
894+
#[cfg(feature = "api-level-33")]
895+
#[doc(alias = "AMotionEvent_getClassification")]
896+
pub fn classification(&self) -> MotionClassification {
897+
u32::try_from(unsafe { ffi::AMotionEvent_getClassification(self.ptr.as_ptr()) })
898+
.unwrap()
899+
.into()
900+
}
788901
}
789902

790903
/// A view into the data of a specific pointer in a motion event.

0 commit comments

Comments
 (0)