BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / notify / layout.rs

layout.rs

layout.rs 5.5 KiB Download Raw
1use super::platform::WorkArea;
2use super::rules::{Corner, Preview};
3use super::stack::{ReplyState, ToastModel};
4
5pub const TOAST_WIDTH: f32 = 360.;
6pub const HIDE_ALL_HEIGHT: f32 = 36.;
7pub const SIDE_MARGIN: f32 = 16.;
8pub const EDGE_MARGIN: f32 = 12.;
9pub const STACK_GAP: f32 = 8.;
10pub const ACTION_BUTTON: f32 = 28.;
11pub const SEND_BUTTON: f32 = 32.;
12const BASE_HEIGHT: f32 = 62.;
13const PREVIEW_LINE: f32 = 18.;
14const CHARS_PER_LINE: usize = 40;
15const ACTION_ROW_SPACING: f32 = 4.;
16const REPLY_FIELD_FRAME: f32 = 14.;
17const REPLY_HINT_ROW: f32 = 20.;
18const REPLY_SPACING: f32 = 8.;
19const CONFIRM_HEIGHT: f32 = 56.;
20
21#[derive(Debug, Clone, Copy, PartialEq)]
22pub struct Slot {
23 pub x: i32,
24 pub y: i32,
25 pub width: i32,
26 pub height: i32,
27}
28
29fn preview_lines(model: &ToastModel, preview_on: bool) -> usize {
30 match (&model.preview, preview_on) {
31 (Preview::Text(text), true) => text.chars().count().div_ceil(CHARS_PER_LINE).clamp(1, 2),
32 _ => 1,
33 }
34}
35
36pub fn toast_height(model: &ToastModel, preview_on: bool) -> f32 {
37 let base = BASE_HEIGHT + preview_lines(model, preview_on) as f32 * PREVIEW_LINE;
38 match model.reply {
39 ReplyState::Sent | ReplyState::Failed => CONFIRM_HEIGHT,
40 ReplyState::Open => {
41 base + REPLY_SPACING + SEND_BUTTON + REPLY_FIELD_FRAME + REPLY_HINT_ROW
42 }
43 ReplyState::Closed => base + ACTION_ROW_SPACING + ACTION_BUTTON,
44 }
45}
46
47pub struct StackSlots {
48 pub toasts: Vec<Slot>,
49 pub pill: Option<Slot>,
50}
51
52pub fn stack_slots(
53 area: WorkArea,
54 corner: Corner,
55 heights: &[f32],
56 with_pill: bool,
57) -> StackSlots {
58 let scale = area.scale;
59 let physical = |value: f32| (value * scale).round() as i32;
60 let width = physical(TOAST_WIDTH);
61 let x = if corner.is_right() {
62 area.right - physical(SIDE_MARGIN) - width
63 } else {
64 area.left + physical(SIDE_MARGIN)
65 };
66 let gap = physical(STACK_GAP);
67 let mut rows: Vec<(i32, i32)> = Vec::new();
68 if with_pill {
69 rows.push((width, physical(HIDE_ALL_HEIGHT)));
70 }
71 rows.extend(heights.iter().map(|height| (width, physical(*height))));
72 let total: i32 = rows.iter().map(|(_, height)| height).sum::<i32>()
73 + gap * rows.len().saturating_sub(1) as i32;
74 let mut y = if corner.is_bottom() {
75 area.bottom - physical(EDGE_MARGIN) - total
76 } else {
77 area.top + physical(EDGE_MARGIN)
78 };
79 let mut slots = Vec::new();
80 for (row_width, height) in rows {
81 let row_x = if corner.is_right() {
82 x + width - row_width
83 } else {
84 x
85 };
86 slots.push(Slot {
87 x: row_x,
88 y,
89 width: row_width,
90 height,
91 });
92 y += height + gap;
93 }
94 let pill = if with_pill && !slots.is_empty() {
95 Some(slots.remove(0))
96 } else {
97 None
98 };
99 StackSlots {
100 toasts: slots,
101 pill,
102 }
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108
109 fn area(scale: f32) -> WorkArea {
110 WorkArea {
111 left: 0,
112 top: 0,
113 right: 1920,
114 bottom: 1032,
115 scale,
116 }
117 }
118
119 #[test]
120 fn bottom_right_stack_hugs_corner_newest_at_bottom() {
121 let slots = stack_slots(area(1.), Corner::BottomRight, &[80., 98.], false);
122 assert_eq!(slots.toasts.len(), 2);
123 let newest = slots.toasts[1];
124 assert_eq!(newest.x, 1920 - 16 - 360);
125 assert_eq!(newest.y + newest.height, 1032 - 12);
126 let older = slots.toasts[0];
127 assert_eq!(older.y + older.height + 8, newest.y);
128 }
129
130 #[test]
131 fn pill_sits_above_oldest_toast() {
132 let slots = stack_slots(area(1.), Corner::BottomRight, &[80., 80., 80.], true);
133 let pill = slots.pill.unwrap();
134 assert_eq!(pill.height, 36);
135 assert_eq!(pill.y + pill.height + 8, slots.toasts[0].y);
136 assert_eq!((pill.x, pill.width), (slots.toasts[0].x, slots.toasts[0].width));
137 }
138
139 #[test]
140 fn top_left_anchors_to_top_edge() {
141 let slots = stack_slots(area(1.), Corner::TopLeft, &[80.], false);
142 assert_eq!((slots.toasts[0].x, slots.toasts[0].y), (16, 12));
143 }
144
145 #[test]
146 fn scale_applies_to_all_distances() {
147 let slots = stack_slots(area(1.5), Corner::BottomRight, &[80.], false);
148 let toast = slots.toasts[0];
149 assert_eq!(toast.width, 540);
150 assert_eq!(toast.height, 120);
151 assert_eq!(toast.y + toast.height, 1032 - 18);
152 }
153
154 #[test]
155 fn heights_stay_in_board_range() {
156 use super::super::rules::ChatKind;
157 use super::super::stack::{ReplyState, ToastTimer};
158 let model = |text: &str| ToastModel {
159 id: 1,
160 conversation_id: "a".into(),
161 message_id: "m".into(),
162 kind: ChatKind::Direct,
163 chat_title: "T".into(),
164 sender_id: None,
165 sender_name: "S".into(),
166 preview: Preview::Text(text.into()),
167 mentions_me: false,
168 count: 1,
169 timer: ToastTimer::new(std::time::Instant::now(), false),
170 reply: ReplyState::Closed,
171 reply_text: String::new(),
172 hovered: false,
173 time: String::new(),
174 queued: false,
175 dismissed: false,
176 fade: super::super::stack::Fade::shown(std::time::Instant::now()),
177 };
178 let short = toast_height(&model("Hi"), true);
179 let long = toast_height(&model(&"x".repeat(200)), true);
180 assert_eq!(short, 112.);
181 assert_eq!(long, 130.);
182 assert_eq!(toast_height(&model(&"x".repeat(200)), false), short);
183 }
184}