BlueGit

ict rusty-teams

Files

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

layout.rs

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