BlueGit

ict rusty-teams

Files

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

layout.rs

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