use cranpose_app_shell::PointerSource;
#[cfg_attr(not(all(feature = "android", target_os = "android")), allow(dead_code))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum AndroidToolKind {
Finger,
Mouse,
Stylus,
Indeterminate,
}
#[cfg_attr(not(all(feature = "android", target_os = "android")), allow(dead_code))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum AndroidSourceKind {
Touchscreen,
Stylus,
Mouse,
Other,
}
#[cfg_attr(not(all(feature = "android", target_os = "android")), allow(dead_code))]
pub(crate) fn resolve_pointer_source(
tool: AndroidToolKind,
source: AndroidSourceKind,
) -> PointerSource {
match tool {
AndroidToolKind::Finger => PointerSource::Touch,
AndroidToolKind::Mouse => PointerSource::Mouse,
AndroidToolKind::Stylus => PointerSource::Stylus,
AndroidToolKind::Indeterminate => match source {
AndroidSourceKind::Touchscreen => PointerSource::Touch,
AndroidSourceKind::Stylus => PointerSource::Stylus,
AndroidSourceKind::Mouse => PointerSource::Mouse,
AndroidSourceKind::Other => PointerSource::Unknown,
},
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tool_type_wins_when_it_identifies_the_device() {
assert_eq!(
resolve_pointer_source(AndroidToolKind::Finger, AndroidSourceKind::Mouse),
PointerSource::Touch
);
assert_eq!(
resolve_pointer_source(AndroidToolKind::Mouse, AndroidSourceKind::Touchscreen),
PointerSource::Mouse
);
assert_eq!(
resolve_pointer_source(AndroidToolKind::Stylus, AndroidSourceKind::Other),
PointerSource::Stylus
);
}
#[test]
fn unreported_tool_type_falls_back_to_touchscreen_source() {
assert_eq!(
resolve_pointer_source(
AndroidToolKind::Indeterminate,
AndroidSourceKind::Touchscreen
),
PointerSource::Touch
);
assert!(PointerSource::Touch.is_touch_like());
}
#[test]
fn unreported_tool_type_uses_the_source_class() {
assert_eq!(
resolve_pointer_source(AndroidToolKind::Indeterminate, AndroidSourceKind::Stylus),
PointerSource::Stylus
);
assert_eq!(
resolve_pointer_source(AndroidToolKind::Indeterminate, AndroidSourceKind::Mouse),
PointerSource::Mouse
);
assert_eq!(
resolve_pointer_source(AndroidToolKind::Indeterminate, AndroidSourceKind::Other),
PointerSource::Unknown
);
}
}