BlueGit

ict rusty-teams

Files

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