| 1 | use std::time::Duration; |
| 2 | |
| 3 | use crate::notify::platform::{ScreenRect, WorkArea}; |
| 4 | |
| 5 | pub const BAR_WIDTH: f32 = 380.; |
| 6 | pub const BAR_HEIGHT: f32 = 40.; |
| 7 | pub const HANDLE_WIDTH: f32 = 120.; |
| 8 | pub const HANDLE_HEIGHT: f32 = 6.; |
| 9 | pub const BORDER_THICKNESS: f32 = 4.; |
| 10 | pub const MINI_WIDTH: f32 = 320.; |
| 11 | pub const MINI_HEIGHT: f32 = 180.; |
| 12 | pub const MINI_MARGIN: f32 = 16.; |
| 13 | pub const BAR_IDLE_BEFORE_COLLAPSE: Duration = Duration::from_secs(3); |
| 14 | pub const MINI_POSITION_KEY: &str = "call_mini_position"; |
| 15 | |
| 16 | fn scaled(logical: f32, scale: f32) -> i32 { |
| 17 | (logical * scale).round() as i32 |
| 18 | } |
| 19 | |
| 20 | pub fn border_thickness(scale: f32) -> i32 { |
| 21 | scaled(BORDER_THICKNESS, scale).max(1) |
| 22 | } |
| 23 | |
| 24 | pub fn bar_rect(work: WorkArea, collapsed: bool) -> ScreenRect { |
| 25 | let (width, height) = if collapsed { |
| 26 | (HANDLE_WIDTH, HANDLE_HEIGHT) |
| 27 | } else { |
| 28 | (BAR_WIDTH, BAR_HEIGHT) |
| 29 | }; |
| 30 | let width = scaled(width, work.scale); |
| 31 | let left = work.left + ((work.right - work.left) - width) / 2; |
| 32 | ScreenRect { |
| 33 | left, |
| 34 | top: work.top, |
| 35 | right: left + width, |
| 36 | bottom: work.top + scaled(height, work.scale), |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | pub fn bar_collapsed(pinned: bool, idle: Duration) -> bool { |
| 41 | !pinned && idle >= BAR_IDLE_BEFORE_COLLAPSE |
| 42 | } |
| 43 | |
| 44 | pub fn border_rects(target: ScreenRect, thickness: i32) -> [ScreenRect; 4] { |
| 45 | let thickness = thickness.clamp(1, (target.width().min(target.height()) / 2).max(1)); |
| 46 | let ScreenRect { |
| 47 | left, |
| 48 | top, |
| 49 | right, |
| 50 | bottom, |
| 51 | } = target; |
| 52 | [ |
| 53 | ScreenRect { |
| 54 | left, |
| 55 | top, |
| 56 | right, |
| 57 | bottom: top + thickness, |
| 58 | }, |
| 59 | ScreenRect { |
| 60 | left, |
| 61 | top: bottom - thickness, |
| 62 | right, |
| 63 | bottom, |
| 64 | }, |
| 65 | ScreenRect { |
| 66 | left, |
| 67 | top: top + thickness, |
| 68 | right: left + thickness, |
| 69 | bottom: bottom - thickness, |
| 70 | }, |
| 71 | ScreenRect { |
| 72 | left: right - thickness, |
| 73 | top: top + thickness, |
| 74 | right, |
| 75 | bottom: bottom - thickness, |
| 76 | }, |
| 77 | ] |
| 78 | } |
| 79 | |
| 80 | pub fn mini_rect(work: WorkArea, saved: Option<(i32, i32)>) -> ScreenRect { |
| 81 | let width = scaled(MINI_WIDTH, work.scale); |
| 82 | let height = scaled(MINI_HEIGHT, work.scale); |
| 83 | let margin = scaled(MINI_MARGIN, work.scale); |
| 84 | let (left, top) = saved.unwrap_or((work.right - width - margin, work.bottom - height - margin)); |
| 85 | let left = left.clamp(work.left, (work.right - width).max(work.left)); |
| 86 | let top = top.clamp(work.top, (work.bottom - height).max(work.top)); |
| 87 | ScreenRect { |
| 88 | left, |
| 89 | top, |
| 90 | right: left + width, |
| 91 | bottom: top + height, |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | pub fn mini_work_area( |
| 96 | main: WorkArea, |
| 97 | saved: Option<(i32, i32)>, |
| 98 | area_at: impl Fn(i32, i32) -> Option<WorkArea>, |
| 99 | ) -> WorkArea { |
| 100 | saved |
| 101 | .and_then(|(left, top)| area_at(left, top)) |
| 102 | .unwrap_or(main) |
| 103 | } |
| 104 | |
| 105 | pub fn mini_wanted(call_active: bool, main_minimized: bool) -> bool { |
| 106 | call_active && main_minimized |
| 107 | } |
| 108 | |
| 109 | pub fn parse_position(text: &str) -> Option<(i32, i32)> { |
| 110 | let (left, top) = text.split_once(',')?; |
| 111 | Some((left.trim().parse().ok()?, top.trim().parse().ok()?)) |
| 112 | } |
| 113 | |
| 114 | pub fn format_position(left: i32, top: i32) -> String { |
| 115 | format!("{left},{top}") |
| 116 | } |
| 117 | |
| 118 | #[cfg(test)] |
| 119 | mod tests { |
| 120 | use super::*; |
| 121 | |
| 122 | fn work(scale: f32) -> WorkArea { |
| 123 | WorkArea { |
| 124 | left: 0, |
| 125 | top: 0, |
| 126 | right: 1920, |
| 127 | bottom: 1040, |
| 128 | scale, |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | #[test] |
| 133 | fn bar_is_centred_at_the_top() { |
| 134 | let rect = bar_rect(work(1.), false); |
| 135 | assert_eq!( |
| 136 | (rect.left, rect.top, rect.right, rect.bottom), |
| 137 | (770, 0, 1150, 40) |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | #[test] |
| 142 | fn bar_follows_a_secondary_monitor_and_scale() { |
| 143 | let second = WorkArea { |
| 144 | left: 1920, |
| 145 | top: 100, |
| 146 | right: 3840, |
| 147 | bottom: 1140, |
| 148 | scale: 1.5, |
| 149 | }; |
| 150 | let rect = bar_rect(second, false); |
| 151 | assert_eq!(rect.width(), 570); |
| 152 | assert_eq!(rect.height(), 60); |
| 153 | assert_eq!(rect.top, 100); |
| 154 | assert_eq!(rect.left, 1920 + (1920 - 570) / 2); |
| 155 | } |
| 156 | |
| 157 | #[test] |
| 158 | fn collapsed_bar_is_a_thin_handle() { |
| 159 | let rect = bar_rect(work(1.), true); |
| 160 | assert_eq!((rect.width(), rect.height(), rect.top), (120, 6, 0)); |
| 161 | } |
| 162 | |
| 163 | #[test] |
| 164 | fn bar_collapses_only_when_unpinned_and_idle() { |
| 165 | assert!(!bar_collapsed(false, Duration::from_secs(2))); |
| 166 | assert!(bar_collapsed(false, Duration::from_secs(3))); |
| 167 | assert!(!bar_collapsed(true, Duration::from_secs(60))); |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn border_thickness_scales_but_stays_visible() { |
| 172 | assert_eq!(border_thickness(1.), 4); |
| 173 | assert_eq!(border_thickness(2.), 8); |
| 174 | assert_eq!(border_thickness(0.01), 1); |
| 175 | } |
| 176 | |
| 177 | #[test] |
| 178 | fn border_rects_frame_the_target_without_overlap() { |
| 179 | let target = ScreenRect { |
| 180 | left: 100, |
| 181 | top: 50, |
| 182 | right: 500, |
| 183 | bottom: 350, |
| 184 | }; |
| 185 | let [top, bottom, left, right] = border_rects(target, 4); |
| 186 | assert_eq!( |
| 187 | (top.left, top.top, top.right, top.bottom), |
| 188 | (100, 50, 500, 54) |
| 189 | ); |
| 190 | assert_eq!((bottom.top, bottom.bottom), (346, 350)); |
| 191 | assert_eq!( |
| 192 | (left.left, left.right, left.top, left.bottom), |
| 193 | (100, 104, 54, 346) |
| 194 | ); |
| 195 | assert_eq!((right.left, right.right), (496, 500)); |
| 196 | } |
| 197 | |
| 198 | #[test] |
| 199 | fn border_thickness_never_exceeds_half_the_target() { |
| 200 | let target = ScreenRect { |
| 201 | left: 0, |
| 202 | top: 0, |
| 203 | right: 6, |
| 204 | bottom: 100, |
| 205 | }; |
| 206 | let [top, _, left, right] = border_rects(target, 4); |
| 207 | assert_eq!(top.bottom, 3); |
| 208 | assert_eq!(left.right, 3); |
| 209 | assert_eq!(right.left, 3); |
| 210 | } |
| 211 | |
| 212 | #[test] |
| 213 | fn mini_defaults_to_the_bottom_right_corner() { |
| 214 | let rect = mini_rect(work(1.), None); |
| 215 | assert_eq!( |
| 216 | (rect.left, rect.top, rect.right, rect.bottom), |
| 217 | (1584, 844, 1904, 1024) |
| 218 | ); |
| 219 | } |
| 220 | |
| 221 | #[test] |
| 222 | fn mini_keeps_a_saved_position_inside_the_work_area() { |
| 223 | assert_eq!(mini_rect(work(1.), Some((300, 200))).left, 300); |
| 224 | let pushed = mini_rect(work(1.), Some((5000, -40))); |
| 225 | assert_eq!((pushed.right, pushed.top), (1920, 0)); |
| 226 | let pulled = mini_rect(work(1.), Some((-500, 5000))); |
| 227 | assert_eq!((pulled.left, pulled.bottom), (0, 1040)); |
| 228 | } |
| 229 | |
| 230 | #[test] |
| 231 | fn mini_stays_on_the_monitor_that_holds_the_saved_point() { |
| 232 | let second = WorkArea { |
| 233 | left: 1920, |
| 234 | top: 0, |
| 235 | right: 3840, |
| 236 | bottom: 1040, |
| 237 | scale: 1., |
| 238 | }; |
| 239 | let area_at = |left: i32, _: i32| (left >= 1920).then_some(second); |
| 240 | let saved = Some((3700, 900)); |
| 241 | let rect = mini_rect(mini_work_area(work(1.), saved, area_at), saved); |
| 242 | assert_eq!((rect.right, rect.bottom), (3840, 1040)); |
| 243 | assert_eq!( |
| 244 | mini_work_area(work(1.), Some((100, 100)), area_at), |
| 245 | work(1.) |
| 246 | ); |
| 247 | assert_eq!(mini_work_area(work(1.), None, area_at), work(1.)); |
| 248 | } |
| 249 | |
| 250 | #[test] |
| 251 | fn mini_scales_with_the_monitor() { |
| 252 | let rect = mini_rect(work(2.), None); |
| 253 | assert_eq!((rect.width(), rect.height()), (640, 360)); |
| 254 | } |
| 255 | |
| 256 | #[test] |
| 257 | fn mini_shows_only_in_a_call_with_the_main_window_minimized() { |
| 258 | assert!(mini_wanted(true, true)); |
| 259 | assert!(!mini_wanted(true, false)); |
| 260 | assert!(!mini_wanted(false, true)); |
| 261 | } |
| 262 | |
| 263 | #[test] |
| 264 | fn position_round_trips() { |
| 265 | assert_eq!( |
| 266 | parse_position(&format_position(-1200, 80)), |
| 267 | Some((-1200, 80)) |
| 268 | ); |
| 269 | assert_eq!(parse_position("garbage"), None); |
| 270 | } |
| 271 | } |