BlueGit

ict rusty-teams

Files

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

overlay_layout.rs

d39bdfed History
overlay_layout.rs 8.4 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 {
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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
81pub enum BorderVisibility {
82 Show,
83 Hide,
84 Close,
85}
86
87pub fn border_visibility(window_share: bool, geometry_found: bool) -> BorderVisibility {
88 match (geometry_found, window_share) {
89 (true, _) => BorderVisibility::Show,
90 (false, true) => BorderVisibility::Hide,
91 (false, false) => BorderVisibility::Close,
92 }
93}
94
95pub fn mini_rect(work: WorkArea, saved: Option<(i32, i32)>) -> ScreenRect {
96 let width = scaled(MINI_WIDTH, work.scale);
97 let height = scaled(MINI_HEIGHT, work.scale);
98 let margin = scaled(MINI_MARGIN, work.scale);
99 let (left, top) = saved.unwrap_or((work.right - width - margin, work.bottom - height - margin));
100 let left = left.clamp(work.left, (work.right - width).max(work.left));
101 let top = top.clamp(work.top, (work.bottom - height).max(work.top));
102 ScreenRect {
103 left,
104 top,
105 right: left + width,
106 bottom: top + height,
107 }
108}
109
110pub fn mini_work_area(
111 main: WorkArea,
112 saved: Option<(i32, i32)>,
113 area_at: impl Fn(i32, i32) -> Option<WorkArea>,
114) -> WorkArea {
115 saved
116 .and_then(|(left, top)| area_at(left, top))
117 .unwrap_or(main)
118}
119
120pub fn mini_wanted(call_active: bool, main_minimized: bool) -> bool {
121 call_active && main_minimized
122}
123
124pub fn parse_position(text: &str) -> Option<(i32, i32)> {
125 let (left, top) = text.split_once(',')?;
126 Some((left.trim().parse().ok()?, top.trim().parse().ok()?))
127}
128
129pub fn format_position(left: i32, top: i32) -> String {
130 format!("{left},{top}")
131}
132
133#[cfg(test)]
134mod tests {
135 use super::*;
136
137 fn work(scale: f32) -> WorkArea {
138 WorkArea {
139 left: 0,
140 top: 0,
141 right: 1920,
142 bottom: 1040,
143 scale,
144 }
145 }
146
147 #[test]
148 fn bar_is_centred_at_the_top() {
149 let rect = bar_rect(work(1.), false);
150 assert_eq!(
151 (rect.left, rect.top, rect.right, rect.bottom),
152 (770, 0, 1150, 40)
153 );
154 }
155
156 #[test]
157 fn bar_follows_a_secondary_monitor_and_scale() {
158 let second = WorkArea {
159 left: 1920,
160 top: 100,
161 right: 3840,
162 bottom: 1140,
163 scale: 1.5,
164 };
165 let rect = bar_rect(second, false);
166 assert_eq!(rect.width(), 570);
167 assert_eq!(rect.height(), 60);
168 assert_eq!(rect.top, 100);
169 assert_eq!(rect.left, 1920 + (1920 - 570) / 2);
170 }
171
172 #[test]
173 fn collapsed_bar_is_a_thin_handle() {
174 let rect = bar_rect(work(1.), true);
175 assert_eq!((rect.width(), rect.height(), rect.top), (120, 6, 0));
176 }
177
178 #[test]
179 fn bar_collapses_only_when_unpinned_and_idle() {
180 assert!(!bar_collapsed(false, Duration::from_secs(2)));
181 assert!(bar_collapsed(false, Duration::from_secs(3)));
182 assert!(!bar_collapsed(true, Duration::from_secs(60)));
183 }
184
185 #[test]
186 fn border_thickness_scales_but_stays_visible() {
187 assert_eq!(border_thickness(1.), 4);
188 assert_eq!(border_thickness(2.), 8);
189 assert_eq!(border_thickness(0.01), 1);
190 }
191
192 #[test]
193 fn border_rects_frame_the_target_without_overlap() {
194 let target = ScreenRect {
195 left: 100,
196 top: 50,
197 right: 500,
198 bottom: 350,
199 };
200 let [top, bottom, left, right] = border_rects(target, 4);
201 assert_eq!(
202 (top.left, top.top, top.right, top.bottom),
203 (100, 50, 500, 54)
204 );
205 assert_eq!((bottom.top, bottom.bottom), (346, 350));
206 assert_eq!(
207 (left.left, left.right, left.top, left.bottom),
208 (100, 104, 54, 346)
209 );
210 assert_eq!((right.left, right.right), (496, 500));
211 }
212
213 #[test]
214 fn border_thickness_never_exceeds_half_the_target() {
215 let target = ScreenRect {
216 left: 0,
217 top: 0,
218 right: 6,
219 bottom: 100,
220 };
221 let [top, _, left, right] = border_rects(target, 4);
222 assert_eq!(top.bottom, 3);
223 assert_eq!(left.right, 3);
224 assert_eq!(right.left, 3);
225 }
226
227 #[test]
228 fn border_hides_for_a_window_without_frame_and_closes_for_a_screen() {
229 assert_eq!(border_visibility(true, true), BorderVisibility::Show);
230 assert_eq!(border_visibility(false, true), BorderVisibility::Show);
231 assert_eq!(border_visibility(true, false), BorderVisibility::Hide);
232 assert_eq!(border_visibility(false, false), BorderVisibility::Close);
233 }
234
235 #[test]
236 fn mini_defaults_to_the_bottom_right_corner() {
237 let rect = mini_rect(work(1.), None);
238 assert_eq!(
239 (rect.left, rect.top, rect.right, rect.bottom),
240 (1584, 844, 1904, 1024)
241 );
242 }
243
244 #[test]
245 fn mini_keeps_a_saved_position_inside_the_work_area() {
246 assert_eq!(mini_rect(work(1.), Some((300, 200))).left, 300);
247 let pushed = mini_rect(work(1.), Some((5000, -40)));
248 assert_eq!((pushed.right, pushed.top), (1920, 0));
249 let pulled = mini_rect(work(1.), Some((-500, 5000)));
250 assert_eq!((pulled.left, pulled.bottom), (0, 1040));
251 }
252
253 #[test]
254 fn mini_stays_on_the_monitor_that_holds_the_saved_point() {
255 let second = WorkArea {
256 left: 1920,
257 top: 0,
258 right: 3840,
259 bottom: 1040,
260 scale: 1.,
261 };
262 let area_at = |left: i32, _: i32| (left >= 1920).then_some(second);
263 let saved = Some((3700, 900));
264 let rect = mini_rect(mini_work_area(work(1.), saved, area_at), saved);
265 assert_eq!((rect.right, rect.bottom), (3840, 1040));
266 assert_eq!(
267 mini_work_area(work(1.), Some((100, 100)), area_at),
268 work(1.)
269 );
270 assert_eq!(mini_work_area(work(1.), None, area_at), work(1.));
271 }
272
273 #[test]
274 fn mini_scales_with_the_monitor() {
275 let rect = mini_rect(work(2.), None);
276 assert_eq!((rect.width(), rect.height()), (640, 360));
277 }
278
279 #[test]
280 fn mini_shows_only_in_a_call_with_the_main_window_minimized() {
281 assert!(mini_wanted(true, true));
282 assert!(!mini_wanted(true, false));
283 assert!(!mini_wanted(false, true));
284 }
285
286 #[test]
287 fn position_round_trips() {
288 assert_eq!(
289 parse_position(&format_position(-1200, 80)),
290 Some((-1200, 80))
291 );
292 assert_eq!(parse_position("garbage"), None);
293 }
294}