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