Skip to main content

aylis_core/
devices.rs

1//! Os aparelhos de referência para ver um form como ele fica em cada um: o tamanho da tela
2//! em pontos (em pé), os cantos, o recorte da câmera e as faixas cobertas pelo sistema (a
3//! barra de status em cima, a barra de gestos embaixo). A IDE desenha a moldura no designer
4//! e o app rodando no desktop abre do tamanho do aparelho (`AYLIS_DEVICE`), com a área segura
5//! simulada e a mesma barra de status por cima.
6//!
7//! Os números são os pontos lógicos de cada aparelho (o que o layout enxerga), não pixels.
8
9use crate::{Bounds, Brush, Canvas, Color, FontSpec, FontWeight, RectF, SizeF, Stroke, TextAlign, TextStyle};
10
11/// O tipo de aparelho: celular e tablet giram; a tela de computador mostra o form como uma
12/// janela nela.
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14pub enum DeviceKind {
15    Phone,
16    Tablet,
17    Desktop,
18}
19
20/// O recorte da câmera no alto da tela.
21#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22pub enum Cutout {
23    None,
24    /// O entalhe largo (iPhone X a 14).
25    Notch,
26    /// A ilha (iPhone 14 Pro em diante).
27    Island,
28    /// O furo no meio (a maioria dos Android).
29    Hole,
30}
31
32/// O sistema do aparelho: separa os celulares e tablets em Android e iOS no seletor da IDE.
33#[derive(Clone, Copy, Debug, PartialEq, Eq)]
34pub enum DeviceOs {
35    Android,
36    Ios,
37    Desktop,
38}
39
40/// Um aparelho de referência.
41#[derive(Clone, Copy, Debug, PartialEq)]
42pub struct Device {
43    pub name: &'static str,
44    pub kind: DeviceKind,
45    pub os: DeviceOs,
46    /// A tela em pé, em pontos.
47    pub width: f32,
48    pub height: f32,
49    /// O raio dos cantos da tela.
50    pub radius: f32,
51    /// A barra de status (com o recorte) e a barra de gestos, em pé.
52    pub top: f32,
53    pub bottom: f32,
54    pub cutout: Cutout,
55}
56
57const fn iphone(name: &'static str, width: f32, height: f32, radius: f32, top: f32, bottom: f32, cutout: Cutout) -> Device {
58    Device { name, kind: DeviceKind::Phone, os: DeviceOs::Ios, width, height, radius, top, bottom, cutout }
59}
60
61/// Celular Android: o furo da câmera no meio, barra de status de 32 e gestos de 24.
62const fn android(name: &'static str, width: f32, height: f32, radius: f32) -> Device {
63    Device { name, kind: DeviceKind::Phone, os: DeviceOs::Android, width, height, radius, top: 32.0, bottom: 24.0, cutout: Cutout::Hole }
64}
65
66const fn ipad(name: &'static str, width: f32, height: f32) -> Device {
67    Device { name, kind: DeviceKind::Tablet, os: DeviceOs::Ios, width, height, radius: 18.0, top: 24.0, bottom: 20.0, cutout: Cutout::None }
68}
69
70const fn android_tablet(name: &'static str, width: f32, height: f32) -> Device {
71    Device { name, kind: DeviceKind::Tablet, os: DeviceOs::Android, width, height, radius: 16.0, top: 24.0, bottom: 24.0, cutout: Cutout::None }
72}
73
74const fn screen(name: &'static str, width: f32, height: f32) -> Device {
75    Device { name, kind: DeviceKind::Desktop, os: DeviceOs::Desktop, width, height, radius: 0.0, top: 0.0, bottom: 0.0, cutout: Cutout::None }
76}
77
78/// Os aparelhos, dos celulares às telas de computador (as telas de PC a 100%).
79pub const DEVICES: &[Device] = &[
80    iphone("iPhone SE", 375.0, 667.0, 0.0, 20.0, 0.0, Cutout::None),
81    iphone("iPhone 13 mini", 375.0, 812.0, 44.0, 50.0, 34.0, Cutout::Notch),
82    iphone("iPhone 14", 390.0, 844.0, 47.0, 47.0, 34.0, Cutout::Notch),
83    iphone("iPhone 15", 393.0, 852.0, 55.0, 59.0, 34.0, Cutout::Island),
84    iphone("iPhone 15 Pro Max", 430.0, 932.0, 55.0, 59.0, 34.0, Cutout::Island),
85    iphone("iPhone 16", 393.0, 852.0, 55.0, 59.0, 34.0, Cutout::Island),
86    iphone("iPhone 16 Pro", 402.0, 874.0, 62.0, 62.0, 34.0, Cutout::Island),
87    iphone("iPhone 16 Pro Max", 440.0, 956.0, 62.0, 62.0, 34.0, Cutout::Island),
88    android("Galaxy S24", 360.0, 780.0, 36.0),
89    android("Galaxy S24 Ultra", 412.0, 891.0, 24.0),
90    android("Galaxy A15", 411.0, 891.0, 30.0),
91    android("Galaxy A54", 412.0, 915.0, 32.0),
92    android("Pixel 8", 412.0, 915.0, 40.0),
93    android("Pixel 8 Pro", 448.0, 998.0, 40.0),
94    android("Pixel 9", 411.0, 923.0, 40.0),
95    android("Moto G84", 393.0, 873.0, 30.0),
96    android("Redmi Note 13", 393.0, 873.0, 30.0),
97    ipad("iPad", 820.0, 1180.0),
98    ipad("iPad mini", 744.0, 1133.0),
99    ipad("iPad Air 11", 820.0, 1180.0),
100    ipad("iPad Pro 11", 834.0, 1210.0),
101    ipad("iPad Pro 13", 1032.0, 1376.0),
102    android_tablet("Galaxy Tab S9", 800.0, 1280.0),
103    android_tablet("Galaxy Tab S9 Ultra", 924.0, 1480.0),
104    android_tablet("Galaxy Tab A9+", 800.0, 1280.0),
105    android_tablet("Pixel Tablet", 800.0, 1280.0),
106    android_tablet("Xiaomi Pad 6", 900.0, 1440.0),
107    screen("HD (1366×768)", 1366.0, 768.0),
108    screen("Full HD (1920×1080)", 1920.0, 1080.0),
109    screen("2,5K (2560×1440)", 2560.0, 1440.0),
110    screen("4K (3840×2160)", 3840.0, 2160.0),
111    screen("8K (7680×4320)", 7680.0, 4320.0),
112];
113
114/// O aparelho pelo nome (sem diferença de maiúsculas).
115pub fn find(name: &str) -> Option<&'static Device> {
116    DEVICES.iter().find(|d| d.name.eq_ignore_ascii_case(name.trim()))
117}
118
119/// O que vem no `AYLIS_DEVICE` e no designer: `Nome` ou `Nome;deitado`.
120pub fn parse(spec: &str) -> Option<(&'static Device, bool)> {
121    let (name, landscape) = spec.split_once(';').map_or((spec, false), |(n, rest)| (n, rest.trim() == "deitado" || rest.trim() == "landscape"));
122    find(name).map(|d| (d, landscape))
123}
124
125impl Device {
126    /// Gira (celular e tablet).
127    pub fn rotates(&self) -> bool {
128        self.kind != DeviceKind::Desktop
129    }
130
131    /// A tela, em pé ou deitada.
132    pub fn size(&self, landscape: bool) -> SizeF {
133        if landscape && self.rotates() { SizeF::new(self.height, self.width) } else { SizeF::new(self.width, self.height) }
134    }
135
136    /// As faixas cobertas pelo sistema. Deitado, o recorte vai para o lado (o iPhone reserva
137    /// os dois lados) e a barra de status do iPhone some; o Android mantém a de cima.
138    pub fn safe_area(&self, landscape: bool) -> Bounds {
139        if !landscape || !self.rotates() {
140            return Bounds::new(0.0, self.top, 0.0, self.bottom);
141        }
142        match self.cutout {
143            Cutout::Notch | Cutout::Island => Bounds::new(self.top, 0.0, self.top, if self.bottom > 0.0 { 21.0 } else { 0.0 }),
144            _ => Bounds::new(0.0, self.top.min(24.0), 0.0, self.bottom),
145        }
146    }
147}
148
149/// Pinta por cima da tela `rect` (já do tamanho do aparelho) o que o sistema desenha: a hora e
150/// a bateria na barra de status, o recorte da câmera e a barra de gestos. `dark`: o fundo do
151/// app é escuro (os textos ficam claros).
152pub fn paint_overlay(canvas: &mut dyn Canvas, device: &Device, landscape: bool, rect: RectF, dark: bool) {
153    if device.kind == DeviceKind::Desktop {
154        return;
155    }
156    let ink = if dark { Color::WHITE } else { Color::rgb(0x11, 0x12, 0x16) };
157    let safe = device.safe_area(landscape);
158    let bar = if landscape && matches!(device.cutout, Cutout::Notch | Cutout::Island) { 24.0 } else { safe.top.max(20.0) };
159    let style = TextStyle {
160        font: FontSpec { size: 14.0, weight: FontWeight::SemiBold, ..FontSpec::default() },
161        color: ink,
162        h_align: TextAlign::Center,
163        v_align: TextAlign::Center,
164        ..TextStyle::default()
165    };
166    let side = (rect.width * 0.22).min(110.0);
167    let status = RectF::new(rect.x + (safe.left.max(12.0)), rect.y, side, bar);
168    canvas.draw_text("9:41", status, &style);
169    // A bateria: um contorno com a carga dentro e o polo.
170    let b = RectF::new(rect.right() - safe.right.max(12.0) - 34.0, rect.y + bar / 2.0 - 6.0, 25.0, 12.0);
171    canvas.stroke_rect(b, 3.5, &Stroke::new(ink.with_opacity(0.5), 1.0));
172    canvas.fill_rect(b.inflate(-2.0), 2.0, &Brush::Solid(ink));
173    canvas.fill_rect(RectF::new(b.right() + 1.5, b.y + 4.0, 2.0, 4.0), 1.0, &Brush::Solid(ink.with_opacity(0.5)));
174    // O recorte, no alto em pé ou no lado esquerdo deitado.
175    let black = Brush::Solid(Color::BLACK);
176    let (cx, cy, along) = if landscape { (rect.x + 14.0, rect.y + rect.height / 2.0, false) } else { (rect.x + rect.width / 2.0, rect.y + 14.0, true) };
177    let (long, short) = match device.cutout {
178        Cutout::Notch => (rect.width.min(rect.height) * 0.42, 30.0),
179        Cutout::Island => (122.0, 36.0),
180        Cutout::Hole => (12.0, 12.0),
181        Cutout::None => (0.0, 0.0),
182    };
183    if long > 0.0 {
184        let r = if along {
185            let top = if device.cutout == Cutout::Notch { rect.y } else { cy - short / 2.0 + 2.0 };
186            RectF::new(cx - long / 2.0, top, long, short)
187        } else {
188            let left = if device.cutout == Cutout::Notch { rect.x } else { cx - short / 2.0 + 2.0 };
189            RectF::new(left, cy - long / 2.0, short, long)
190        };
191        canvas.fill_rect(r, short / 2.0, &black);
192    }
193    // A barra de gestos.
194    if safe.bottom > 0.0 {
195        let w = (rect.width * 0.36).min(140.0);
196        let y = rect.bottom() - safe.bottom / 2.0 - 2.5;
197        canvas.fill_rect(RectF::new(rect.x + (rect.width - w) / 2.0, y, w, 5.0), 2.5, &Brush::Solid(ink.with_opacity(0.85)));
198    }
199}
200
201#[cfg(test)]
202mod tests {
203    use super::*;
204
205    #[test]
206    fn devices_rotate_and_move_the_safe_area() {
207        let (iphone, landscape) = parse("iPhone 15;deitado").unwrap();
208        assert!(landscape);
209        assert_eq!(iphone.size(false), SizeF::new(393.0, 852.0));
210        assert_eq!(iphone.size(true), SizeF::new(852.0, 393.0));
211        assert_eq!(iphone.safe_area(false), Bounds::new(0.0, 59.0, 0.0, 34.0));
212        assert_eq!(iphone.safe_area(true), Bounds::new(59.0, 0.0, 59.0, 21.0));
213        let pixel = find("pixel 8").unwrap();
214        assert_eq!(pixel.safe_area(true), Bounds::new(0.0, 24.0, 0.0, 24.0));
215        let hd = find("HD (1366×768)").unwrap();
216        assert!(!hd.rotates() && hd.size(true) == SizeF::new(1366.0, 768.0) && hd.safe_area(true) == Bounds::default());
217        assert!(parse("Nenhum").is_none());
218        // Os nomes não se repetem.
219        for (i, d) in DEVICES.iter().enumerate() {
220            assert!(DEVICES[i + 1..].iter().all(|o| o.name != d.name), "{} repetido", d.name);
221        }
222    }
223}