BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / call / overlay_layout.rs

overlay_layout.rs

overlay_layout.rs 6.8 KiB Download Raw
1use std::time::Duration;
2
3use crate::notify::platform::{ScreenRect, WorkArea};
4
5pub const BAR_WIDTH: f32 = 380.;
6pub const BAR_HEIGHT: f32 = 40.;
7pub const HANDLE_WIDTH: f32 = 120.;
8pub const HANDLE_HEIGHT: f32 = 6.;
9pub const BORDER_THICKNESS: f32 = 4.;
10pub const MINI_WIDTH: f32 = 320.;
11pub const MINI_HEIGHT: f32 = 180.;
12pub const MINI_MARGIN: f32 = 16.;
13pub const BAR_IDLE_BEFORE_COLLAPSE: Duration = Duration::from_secs(3);
14pub const MINI_POSITION_KEY: &str = "call_mini_position";
15
16fn scaled(logical: f32, scale: f32) -> i32 {
17 (logical * scale).round() as i32
18}
19
20pub fn border_thickness(scale: f32) -> i32 {
21 scaled(BORDER_THICKNESS, scale).max(1)
22}
23
24pub 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
40pub fn bar_collapsed(pinned: bool, idle: Duration) -> bool {
41 !pinned && idle >= BAR_IDLE_BEFORE_COLLAPSE
42}
43
44pub 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 { left, top, right, bottom } = target;
47 [
48 ScreenRect { left, top, right, bottom: top + thickness },
49 ScreenRect { left, top: bottom - thickness, right, bottom },
50 ScreenRect { left, top: top + thickness, right: left + thickness, bottom: bottom - thickness },
51 ScreenRect { left: right - thickness, top: top + thickness, right, bottom: bottom - thickness },
52 ]
53}
54
55pub fn mini_rect(work: WorkArea, saved: Option<(i32, i32)>) -> ScreenRect {
56 let width = scaled(MINI_WIDTH, work.scale);
57 let height = scaled(MINI_HEIGHT, work.scale);
58 let margin = scaled(MINI_MARGIN, work.scale);
59 let (left, top) = saved.unwrap_or((work.right - width - margin, work.bottom - height - margin));
60 let left = left.clamp(work.left, (work.right - width).max(work.left));
61 let top = top.clamp(work.top, (work.bottom - height).max(work.top));
62 ScreenRect {
63 left,
64 top,
65 right: left + width,
66 bottom: top + height,
67 }
68}
69
70pub fn mini_work_area(main: WorkArea, saved: Option<(i32, i32)>, area_at: impl Fn(i32, i32) -> Option<WorkArea>) -> WorkArea {
71 saved.and_then(|(left, top)| area_at(left, top)).unwrap_or(main)
72}
73
74pub fn mini_wanted(call_active: bool, main_minimized: bool) -> bool {
75 call_active && main_minimized
76}
77
78pub fn parse_position(text: &str) -> Option<(i32, i32)> {
79 let (left, top) = text.split_once(',')?;
80 Some((left.trim().parse().ok()?, top.trim().parse().ok()?))
81}
82
83pub fn format_position(left: i32, top: i32) -> String {
84 format!("{left},{top}")
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90
91 fn work(scale: f32) -> WorkArea {
92 WorkArea { left: 0, top: 0, right: 1920, bottom: 1040, scale }
93 }
94
95 #[test]
96 fn bar_is_centred_at_the_top() {
97 let rect = bar_rect(work(1.), false);
98 assert_eq!((rect.left, rect.top, rect.right, rect.bottom), (770, 0, 1150, 40));
99 }
100
101 #[test]
102 fn bar_follows_a_secondary_monitor_and_scale() {
103 let second = WorkArea { left: 1920, top: 100, right: 3840, bottom: 1140, scale: 1.5 };
104 let rect = bar_rect(second, false);
105 assert_eq!(rect.width(), 570);
106 assert_eq!(rect.height(), 60);
107 assert_eq!(rect.top, 100);
108 assert_eq!(rect.left, 1920 + (1920 - 570) / 2);
109 }
110
111 #[test]
112 fn collapsed_bar_is_a_thin_handle() {
113 let rect = bar_rect(work(1.), true);
114 assert_eq!((rect.width(), rect.height(), rect.top), (120, 6, 0));
115 }
116
117 #[test]
118 fn bar_collapses_only_when_unpinned_and_idle() {
119 assert!(!bar_collapsed(false, Duration::from_secs(2)));
120 assert!(bar_collapsed(false, Duration::from_secs(3)));
121 assert!(!bar_collapsed(true, Duration::from_secs(60)));
122 }
123
124 #[test]
125 fn border_thickness_scales_but_stays_visible() {
126 assert_eq!(border_thickness(1.), 4);
127 assert_eq!(border_thickness(2.), 8);
128 assert_eq!(border_thickness(0.01), 1);
129 }
130
131 #[test]
132 fn border_rects_frame_the_target_without_overlap() {
133 let target = ScreenRect { left: 100, top: 50, right: 500, bottom: 350 };
134 let [top, bottom, left, right] = border_rects(target, 4);
135 assert_eq!((top.left, top.top, top.right, top.bottom), (100, 50, 500, 54));
136 assert_eq!((bottom.top, bottom.bottom), (346, 350));
137 assert_eq!((left.left, left.right, left.top, left.bottom), (100, 104, 54, 346));
138 assert_eq!((right.left, right.right), (496, 500));
139 }
140
141 #[test]
142 fn border_thickness_never_exceeds_half_the_target() {
143 let target = ScreenRect { left: 0, top: 0, right: 6, bottom: 100 };
144 let [top, _, left, right] = border_rects(target, 4);
145 assert_eq!(top.bottom, 3);
146 assert_eq!(left.right, 3);
147 assert_eq!(right.left, 3);
148 }
149
150 #[test]
151 fn mini_defaults_to_the_bottom_right_corner() {
152 let rect = mini_rect(work(1.), None);
153 assert_eq!((rect.left, rect.top, rect.right, rect.bottom), (1584, 844, 1904, 1024));
154 }
155
156 #[test]
157 fn mini_keeps_a_saved_position_inside_the_work_area() {
158 assert_eq!(mini_rect(work(1.), Some((300, 200))).left, 300);
159 let pushed = mini_rect(work(1.), Some((5000, -40)));
160 assert_eq!((pushed.right, pushed.top), (1920, 0));
161 let pulled = mini_rect(work(1.), Some((-500, 5000)));
162 assert_eq!((pulled.left, pulled.bottom), (0, 1040));
163 }
164
165 #[test]
166 fn mini_stays_on_the_monitor_that_holds_the_saved_point() {
167 let second = WorkArea { left: 1920, top: 0, right: 3840, bottom: 1040, scale: 1. };
168 let area_at = |left: i32, _: i32| (left >= 1920).then_some(second);
169 let saved = Some((3700, 900));
170 let rect = mini_rect(mini_work_area(work(1.), saved, area_at), saved);
171 assert_eq!((rect.right, rect.bottom), (3840, 1040));
172 assert_eq!(mini_work_area(work(1.), Some((100, 100)), area_at), work(1.));
173 assert_eq!(mini_work_area(work(1.), None, area_at), work(1.));
174 }
175
176 #[test]
177 fn mini_scales_with_the_monitor() {
178 let rect = mini_rect(work(2.), None);
179 assert_eq!((rect.width(), rect.height()), (640, 360));
180 }
181
182 #[test]
183 fn mini_shows_only_in_a_call_with_the_main_window_minimized() {
184 assert!(mini_wanted(true, true));
185 assert!(!mini_wanted(true, false));
186 assert!(!mini_wanted(false, true));
187 }
188
189 #[test]
190 fn position_round_trips() {
191 assert_eq!(parse_position(&format_position(-1200, 80)), Some((-1200, 80)));
192 assert_eq!(parse_position("garbage"), None);
193 }
194}