Skip to content

Commit

Permalink
Add DPI info, content_rect, and various small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mb64 committed Oct 26, 2019
1 parent ddafe90 commit 695c85f
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 14 deletions.
4 changes: 4 additions & 0 deletions android-ndk/src/android_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ impl AndroidApp {
}
}

pub fn content_rect(&self) -> ffi::ARect {
unsafe { self.ptr.as_ref().contentRect }
}

// The looper will also never change
pub fn looper(&self) -> ForeignLooper {
unsafe { ForeignLooper::from_ptr(NonNull::new(self.ptr.as_ref().looper).unwrap()) }
Expand Down
47 changes: 46 additions & 1 deletion android-ndk/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use std::ptr::NonNull;

/// A native `AConfiguration *`.
///
/// This stores information about configuration.
/// This stores information about configuration. See [the NDK
/// docs](https://developer.android.com/ndk/reference/group/configuration)
pub struct Configuration {
ptr: NonNull<ffi::AConfiguration>,
}
Expand Down Expand Up @@ -392,6 +393,50 @@ pub enum Density {
None = ffi::ACONFIGURATION_DENSITY_NONE,
}

impl Density {
/// The DPI associated with the density class.
/// See [the Android screen density
/// docs](https://developer.android.com/training/multiscreen/screendensities#TaskProvideAltBmp)
///
/// There are some `Density` values that have no associated DPI; these values return `None`.
pub fn approx_dpi(self) -> Option<u32> {
match self {
Self::Default => Some(160), // Or should it be None?
Self::Low => Some(120),
Self::Medium => Some(160),
Self::High => Some(240),
Self::XHigh => Some(320),
Self::XXHigh => Some(480),
Self::XXXHigh => Some(640),
Self::TV => Some(213),
Self::Any => None,
Self::None => None,
}
}

/// The Hi-DPI factor associated with the density class. This is the factor by which an
/// image/resource should be scaled to match its size across devices. The baseline is a 160dpi
/// screen (i.e., Hi-DPI factor = DPI / 160).
/// See [the Android screen density
/// docs](https://developer.android.com/training/multiscreen/screendensities#TaskProvideAltBmp)
///
/// There are some `Density` values that have no associated DPI; these values return `None`.
pub fn approx_hidpi_factor(self) -> Option<f64> {
match self {
Self::Default => Some(1.), // Or should it be None?
Self::Low => Some(0.75),
Self::Medium => Some(1.),
Self::High => Some(1.5),
Self::XHigh => Some(2.),
Self::XXHigh => Some(3.),
Self::XXXHigh => Some(4.),
Self::TV => Some(4. / 3.),
Self::Any => None,
Self::None => None,
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum Keyboard {
Expand Down
64 changes: 51 additions & 13 deletions android-ndk/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,25 @@ impl MotionEvent {
self.ptr
}

/// Get the source of the event.
///
/// See [the NDK
/// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getsource)
#[inline]
pub fn source(&self) -> Source {
let source = unsafe { ffi::AInputEvent_getSource(self.ptr.as_ptr()) as u32 };
source.try_into().unwrap_or(Source::Unknown)
}

/// Get the device id associated with the event.
///
/// See [the NDK
/// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getdeviceid)
#[inline]
pub fn device_id(&self) -> i32 {
unsafe { ffi::AInputEvent_getDeviceId(self.ptr.as_ptr()) }
}

/// Returns the motion action associated with this event.
///
/// See [the NDK
Expand Down Expand Up @@ -1301,8 +1320,27 @@ impl KeyEvent {
action.try_into().unwrap()
}

/// Get the source of the event.
///
/// See [the NDK
/// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getsource)
#[inline]
pub fn source(&self) -> Source {
let source = unsafe { ffi::AInputEvent_getSource(self.ptr.as_ptr()) as u32 };
source.try_into().unwrap_or(Source::Unknown)
}

/// Get the device id associated with the event.
///
/// See [the NDK
/// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getdeviceid)
#[inline]
pub fn device_id(&self) -> i32 {
unsafe { ffi::AInputEvent_getDeviceId(self.ptr.as_ptr()) }
}

/// Returns the last time the key was pressed. This is on the scale of
/// `java.lang.System.nanoTime()`, which has nanosecond accuracy, but no defined start time.
/// `java.lang.System.nanoTime()`, which has nanosecond precision, but no defined start time.
///
/// See [the NDK
/// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getdowntime)
Expand All @@ -1312,7 +1350,7 @@ impl KeyEvent {
}

/// Returns the time this event occured. This is on the scale of
/// `java.lang.System.nanoTime()`, which has nanosecond accuracy, but no defined start time.
/// `java.lang.System.nanoTime()`, which has nanosecond precision, but no defined start time.
///
/// See [the NDK
/// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_geteventtime)
Expand Down Expand Up @@ -1358,47 +1396,47 @@ pub struct KeyEventFlags(pub u32);

impl KeyEventFlags {
#[inline]
pub fn cancelled_flag(&self) -> bool {
pub fn cancelled(&self) -> bool {
self.0 & ffi::AKEY_EVENT_FLAG_CANCELED != 0
}
#[inline]
pub fn cancelled_long_press_flag(&self) -> bool {
pub fn cancelled_long_press(&self) -> bool {
self.0 & ffi::AKEY_EVENT_FLAG_CANCELED_LONG_PRESS != 0
}
#[inline]
pub fn editor_action_flag(&self) -> bool {
pub fn editor_action(&self) -> bool {
self.0 & ffi::AKEY_EVENT_FLAG_EDITOR_ACTION != 0
}
#[inline]
pub fn fallback_flag(&self) -> bool {
pub fn fallback(&self) -> bool {
self.0 & ffi::AKEY_EVENT_FLAG_FALLBACK != 0
}
#[inline]
pub fn from_system_flag(&self) -> bool {
pub fn from_system(&self) -> bool {
self.0 & ffi::AKEY_EVENT_FLAG_FROM_SYSTEM != 0
}
#[inline]
pub fn keep_touch_mode_flag(&self) -> bool {
pub fn keep_touch_mode(&self) -> bool {
self.0 & ffi::AKEY_EVENT_FLAG_KEEP_TOUCH_MODE != 0
}
#[inline]
pub fn long_press_flag(&self) -> bool {
pub fn long_press(&self) -> bool {
self.0 & ffi::AKEY_EVENT_FLAG_LONG_PRESS != 0
}
#[inline]
pub fn soft_keyboard_flag(&self) -> bool {
pub fn soft_keyboard(&self) -> bool {
self.0 & ffi::AKEY_EVENT_FLAG_SOFT_KEYBOARD != 0
}
#[inline]
pub fn tracking_flag(&self) -> bool {
pub fn tracking(&self) -> bool {
self.0 & ffi::AKEY_EVENT_FLAG_TRACKING != 0
}
#[inline]
pub fn virtual_hard_key_flag(&self) -> bool {
pub fn virtual_hard_key(&self) -> bool {
self.0 & ffi::AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY != 0
}
#[inline]
pub fn woke_here_flag(&self) -> bool {
pub fn woke_here(&self) -> bool {
self.0 & ffi::AKEY_EVENT_FLAG_WOKE_HERE != 0
}
}
Expand Down

0 comments on commit 695c85f

Please sign in to comment.