| 1 | use super::platform::WorkArea; |
| 2 | use super::rules::{Corner, Preview}; |
| 3 | use super::stack::{ReplyState, ToastModel}; |
| 4 | |
| 5 | pub const TOAST_WIDTH: f32 = 360.; |
| 6 | pub const PILL_WIDTH: f32 = 132.; |
| 7 | pub const PILL_HEIGHT: f32 = 32.; |
| 8 | pub const SIDE_MARGIN: f32 = 16.; |
| 9 | pub const EDGE_MARGIN: f32 = 12.; |
| 10 | pub const STACK_GAP: f32 = 8.; |
| 11 | pub const ACTION_ROW: f32 = 36.; |
| 12 | const BASE_HEIGHT: f32 = 62.; |
| 13 | const PREVIEW_LINE: f32 = 18.; |
| 14 | const CHARS_PER_LINE: usize = 40; |
| 15 | const ACTION_ROW_SPACING: f32 = 4.; |
| 16 | const REPLY_HINT_ROW: f32 = 20.; |
| 17 | const REPLY_SPACING: f32 = 8.; |
| 18 | const CONFIRM_HEIGHT: f32 = 56.; |
| 19 | |
| 20 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 21 | pub struct Slot { |
| 22 | pub x: i32, |
| 23 | pub y: i32, |
| 24 | pub width: i32, |
| 25 | pub height: i32, |
| 26 | } |
| 27 | |
| 28 | fn preview_lines(model: &ToastModel, preview_on: bool) -> usize { |
| 29 | match (&model.preview, preview_on) { |
| 30 | (Preview::Text(text), true) => text.chars().count().div_ceil(CHARS_PER_LINE).clamp(1, 2), |
| 31 | _ => 1, |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | pub fn toast_height(model: &ToastModel, hovered: bool, preview_on: bool) -> f32 { |
| 36 | match model.reply { |
| 37 | ReplyState::Sent | ReplyState::Failed => CONFIRM_HEIGHT, |
| 38 | ReplyState::Open => { |
| 39 | let base = BASE_HEIGHT + preview_lines(model, preview_on) as f32 * PREVIEW_LINE; |
| 40 | base + REPLY_SPACING + ACTION_ROW + REPLY_HINT_ROW |
| 41 | } |
| 42 | ReplyState::Closed => { |
| 43 | let base = BASE_HEIGHT + preview_lines(model, preview_on) as f32 * PREVIEW_LINE; |
| 44 | if hovered { |
| 45 | base + ACTION_ROW + ACTION_ROW_SPACING |
| 46 | } else { |
| 47 | base |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | pub struct StackSlots { |
| 54 | pub toasts: Vec<Slot>, |
| 55 | pub pill: Option<Slot>, |
| 56 | } |
| 57 | |
| 58 | pub 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((physical(PILL_WIDTH), physical(PILL_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)] |
| 112 | mod 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, 32); |
| 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 heights_stay_in_board_range() { |
| 162 | use super::super::rules::ChatKind; |
| 163 | use super::super::stack::{ReplyState, ToastTimer}; |
| 164 | let model = |text: &str| ToastModel { |
| 165 | id: 1, |
| 166 | conversation_id: "a".into(), |
| 167 | message_id: "m".into(), |
| 168 | kind: ChatKind::Direct, |
| 169 | chat_title: "T".into(), |
| 170 | sender_id: None, |
| 171 | sender_name: "S".into(), |
| 172 | preview: Preview::Text(text.into()), |
| 173 | mentions_me: false, |
| 174 | count: 1, |
| 175 | timer: ToastTimer::new(std::time::Instant::now(), false), |
| 176 | reply: ReplyState::Closed, |
| 177 | reply_text: String::new(), |
| 178 | hovered: false, |
| 179 | time: String::new(), |
| 180 | }; |
| 181 | let short = toast_height(&model("Hi"), false, true); |
| 182 | let long = toast_height(&model(&"x".repeat(200)), false, true); |
| 183 | assert!((76. ..=104.).contains(&short)); |
| 184 | assert!((76. ..=104.).contains(&long)); |
| 185 | assert!(toast_height(&model("Hi"), true, true) > short); |
| 186 | assert_eq!(toast_height(&model(&"x".repeat(200)), false, false), short); |
| 187 | } |
| 188 | } |