1use crate::{Bounds, Brush, Canvas, Color, FontSpec, FontWeight, RectF, SizeF, Stroke, TextAlign, TextStyle};
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14pub enum DeviceKind {
15 Phone,
16 Tablet,
17 Desktop,
18}
19
20#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22pub enum Cutout {
23 None,
24 Notch,
26 Island,
28 Hole,
30}
31
32#[derive(Clone, Copy, Debug, PartialEq, Eq)]
34pub enum DeviceOs {
35 Android,
36 Ios,
37 Desktop,
38}
39
40#[derive(Clone, Copy, Debug, PartialEq)]
42pub struct Device {
43 pub name: &'static str,
44 pub kind: DeviceKind,
45 pub os: DeviceOs,
46 pub width: f32,
48 pub height: f32,
49 pub radius: f32,
51 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
61const 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
78pub 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
114pub fn find(name: &str) -> Option<&'static Device> {
116 DEVICES.iter().find(|d| d.name.eq_ignore_ascii_case(name.trim()))
117}
118
119pub 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 pub fn rotates(&self) -> bool {
128 self.kind != DeviceKind::Desktop
129 }
130
131 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 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
149pub 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 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 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 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 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}