Skip to main content

cranpose_platform_android/
lib.rs

1use cranpose_ui_graphics::Point;
2
3/// Platform abstraction for Android.
4///
5/// This type manages platform-specific conversions (e.g., density, pointer coordinates)
6/// and provides a bridge between Android's event system and Compose's logical coordinate space.
7#[derive(Debug, Clone)]
8pub struct AndroidPlatform {
9    scale_factor: f64,
10}
11
12impl Default for AndroidPlatform {
13    fn default() -> Self {
14        Self { scale_factor: 1.0 }
15    }
16}
17
18impl AndroidPlatform {
19    /// Creates a new Android platform with default configuration.
20    pub fn new() -> Self {
21        Self::default()
22    }
23
24    /// Updates the platform's scale factor.
25    ///
26    /// This should be called when the device density changes.
27    pub fn set_scale_factor(&mut self, scale_factor: f64) {
28        self.scale_factor = scale_factor;
29    }
30
31    /// Converts a physical pointer position into logical coordinates.
32    ///
33    /// Android provides pointer positions in physical pixels; this method
34    /// scales them back to logical pixels using the platform's current scale factor.
35    pub fn pointer_position(&self, physical_x: f64, physical_y: f64) -> Point {
36        let scale = self.scale_factor;
37        Point {
38            x: (physical_x / scale) as f32,
39            y: (physical_y / scale) as f32,
40        }
41    }
42
43    /// Returns the current scale factor (density).
44    pub fn scale_factor(&self) -> f32 {
45        self.scale_factor as f32
46    }
47}