BlueGit

ict rusty-teams

Files

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

overlay_layout.rs

overlay_layout.rs 8.0 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 parse_position(text: &str) -> Option<(i32, i32)> {
121 let (left, top) = text.split_once(',')?;
122 Some((left.trim().parse().ok()?, top.trim().parse().ok()?))
123}
124
125pub fn format_position(left: i32, top: i32) -> String {
126 format!("{left},{top}")
127}
128
129#[cfg(test)]
130mod tests {
131 use super::*;
132
133 fn work(scale: f32) -> WorkArea {
134 WorkArea {
135 left: 0,
136 top: 0,
137 right: 1920,
138 bottom: 1040,
139 scale,
140 }
141 }
142
143 #[test]
144 fn bar_is_centred_at_the_top() {
145 let rect = bar_rect(work(1.), false);
146 assert_eq!(
147 (rect.left, rect.top, rect.right, rect.bottom),
148 (770, 0, 1150, 40)
149 );
150 }
151
152 #[test]
153 fn bar_follows_a_secondary_monitor_and_scale() {
154 let second = WorkArea {
155 left: 1920,
156 top: 100,
157 right: 3840,
158 bottom: 1140,
159 scale: 1.5,
160 };
161 let rect = bar_rect(second, false);
162 assert_eq!(rect.width(), 570);
163 assert_eq!(rect.height(), 60);
164 assert_eq!(rect.top, 100);
165 assert_eq!(rect.left, 1920 + (1920 - 570) / 2);
166 }
167
168 #[test]
169 fn collapsed_bar_is_a_thin_handle() {
170 let rect = bar_rect(work(1.), true);
171 assert_eq!((rect.width(), rect.height(), rect.top), (120, 6, 0));
172 }
173
174 #[test]
175 fn bar_collapses_only_when_unpinned_and_idle() {
176 assert!(!bar_collapsed(false, Duration::from_secs(2)));
177 assert!(bar_collapsed(false, Duration::from_secs(3)));
178 assert!(!bar_collapsed(true, Duration::from_secs(60)));
179 }
180
181 #[test]
182 fn border_thickness_scales_but_stays_visible() {
183 assert_eq!(border_thickness(1.), 4);
184 assert_eq!(border_thickness(2.), 8);
185 assert_eq!(border_thickness(0.01), 1);
186 }
187
188 #[test]
189 fn border_rects_frame_the_target_without_overlap() {
190 let target = ScreenRect {
191 left: 100,
192 top: 50,
193 right: 500,
194 bottom: 350,
195 };
196 let [top, bottom, left, right] = border_rects(target, 4);
197 assert_eq!(
198 (top.left, top.top, top.right, top.bottom),
199 (100, 50, 500, 54)
200 );
201 assert_eq!((bottom.top, bottom.bottom), (346, 350));
202 assert_eq!(
203 (left.left, left.right, left.top, left.bottom),
204 (100, 104, 54, 346)
205 );
206 assert_eq!((right.left, right.right), (496, 500));
207 }
208
209 #[test]
210 fn border_thickness_never_exceeds_half_the_target() {
211 let target = ScreenRect {
212 left: 0,
213 top: 0,
214 right: 6,
215 bottom: 100,
216 };
217 let [top, _, left, right] = border_rects(target, 4);
218 assert_eq!(top.bottom, 3);
219 assert_eq!(left.right, 3);
220 assert_eq!(right.left, 3);
221 }
222
223 #[test]
224 fn border_hides_for_a_window_without_frame_and_closes_for_a_screen() {
225 assert_eq!(border_visibility(true, true), BorderVisibility::Show);
226 assert_eq!(border_visibility(false, true), BorderVisibility::Show);
227 assert_eq!(border_visibility(true, false), BorderVisibility::Hide);
228 assert_eq!(border_visibility(false, false), BorderVisibility::Close);
229 }
230
231 #[test]
232 fn mini_defaults_to_the_bottom_right_corner() {
233 let rect = mini_rect(work(1.), None);
234 assert_eq!(
235 (rect.left, rect.top, rect.right, rect.bottom),
236 (1584, 844, 1904, 1024)
237 );
238 }
239
240 #[test]
241 fn mini_keeps_a_saved_position_inside_the_work_area() {
242 assert_eq!(mini_rect(work(1.), Some((300, 200))).left, 300);
243 let pushed = mini_rect(work(1.), Some((5000, -40)));
244 assert_eq!((pushed.right, pushed.top), (1920, 0));
245 let pulled = mini_rect(work(1.), Some((-500, 5000)));
246 assert_eq!((pulled.left, pulled.bottom), (0, 1040));
247 }
248
249 #[test]
250 fn mini_stays_on_the_monitor_that_holds_the_saved_point() {
251 let second = WorkArea {
252 left: 1920,
253 top: 0,
254 right: 3840,
255 bottom: 1040,
256 scale: 1.,
257 };
258 let area_at = |left: i32, _: i32| (left >= 1920).then_some(second);
259 let saved = Some((3700, 900));
260 let rect = mini_rect(mini_work_area(work(1.), saved, area_at), saved);
261 assert_eq!((rect.right, rect.bottom), (3840, 1040));
262 assert_eq!(
263 mini_work_area(work(1.), Some((100, 100)), area_at),
264 work(1.)
265 );
266 assert_eq!(mini_work_area(work(1.), None, area_at), work(1.));
267 }
268
269 #[test]
270 fn mini_scales_with_the_monitor() {
271 let rect = mini_rect(work(2.), None);
272 assert_eq!((rect.width(), rect.height()), (640, 360));
273 }
274
275 #[test]
276 fn position_round_trips() {
277 assert_eq!(
278 parse_position(&format_position(-1200, 80)),
279 Some((-1200, 80))
280 );
281 assert_eq!(parse_position("garbage"), None);
282 }
283}