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