BlueGit

ict rusty-teams

Files

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

overlay_layout.rs

overlay_layout.rs 6.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 { 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_wanted(call_active: bool, main_minimized: bool) -> bool {
71 call_active && main_minimized
72}
73
74pub fn parse_position(text: &str) -> Option<(i32, i32)> {
75 let (left, top) = text.split_once(',')?;
76 Some((left.trim().parse().ok()?, top.trim().parse().ok()?))
77}
78
79pub fn format_position(left: i32, top: i32) -> String {
80 format!("{left},{top}")
81}
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86
87 fn work(scale: f32) -> WorkArea {
88 WorkArea { left: 0, top: 0, right: 1920, bottom: 1040, scale }
89 }
90
91 #[test]
92 fn bar_is_centred_at_the_top() {
93 let rect = bar_rect(work(1.), false);
94 assert_eq!((rect.left, rect.top, rect.right, rect.bottom), (770, 0, 1150, 40));
95 }
96
97 #[test]
98 fn bar_follows_a_secondary_monitor_and_scale() {
99 let second = WorkArea { left: 1920, top: 100, right: 3840, bottom: 1140, scale: 1.5 };
100 let rect = bar_rect(second, false);
101 assert_eq!(rect.width(), 570);
102 assert_eq!(rect.height(), 60);
103 assert_eq!(rect.top, 100);
104 assert_eq!(rect.left, 1920 + (1920 - 570) / 2);
105 }
106
107 #[test]
108 fn collapsed_bar_is_a_thin_handle() {
109 let rect = bar_rect(work(1.), true);
110 assert_eq!((rect.width(), rect.height(), rect.top), (120, 6, 0));
111 }
112
113 #[test]
114 fn bar_collapses_only_when_unpinned_and_idle() {
115 assert!(!bar_collapsed(false, Duration::from_secs(2)));
116 assert!(bar_collapsed(false, Duration::from_secs(3)));
117 assert!(!bar_collapsed(true, Duration::from_secs(60)));
118 }
119
120 #[test]
121 fn border_thickness_scales_but_stays_visible() {
122 assert_eq!(border_thickness(1.), 4);
123 assert_eq!(border_thickness(2.), 8);
124 assert_eq!(border_thickness(0.01), 1);
125 }
126
127 #[test]
128 fn border_rects_frame_the_target_without_overlap() {
129 let target = ScreenRect { left: 100, top: 50, right: 500, bottom: 350 };
130 let [top, bottom, left, right] = border_rects(target, 4);
131 assert_eq!((top.left, top.top, top.right, top.bottom), (100, 50, 500, 54));
132 assert_eq!((bottom.top, bottom.bottom), (346, 350));
133 assert_eq!((left.left, left.right, left.top, left.bottom), (100, 104, 54, 346));
134 assert_eq!((right.left, right.right), (496, 500));
135 }
136
137 #[test]
138 fn border_thickness_never_exceeds_half_the_target() {
139 let target = ScreenRect { left: 0, top: 0, right: 6, bottom: 100 };
140 let [top, _, left, right] = border_rects(target, 4);
141 assert_eq!(top.bottom, 3);
142 assert_eq!(left.right, 3);
143 assert_eq!(right.left, 3);
144 }
145
146 #[test]
147 fn mini_defaults_to_the_bottom_right_corner() {
148 let rect = mini_rect(work(1.), None);
149 assert_eq!((rect.left, rect.top, rect.right, rect.bottom), (1584, 844, 1904, 1024));
150 }
151
152 #[test]
153 fn mini_keeps_a_saved_position_inside_the_work_area() {
154 assert_eq!(mini_rect(work(1.), Some((300, 200))).left, 300);
155 let pushed = mini_rect(work(1.), Some((5000, -40)));
156 assert_eq!((pushed.right, pushed.top), (1920, 0));
157 let pulled = mini_rect(work(1.), Some((-500, 5000)));
158 assert_eq!((pulled.left, pulled.bottom), (0, 1040));
159 }
160
161 #[test]
162 fn mini_scales_with_the_monitor() {
163 let rect = mini_rect(work(2.), None);
164 assert_eq!((rect.width(), rect.height()), (640, 360));
165 }
166
167 #[test]
168 fn mini_shows_only_in_a_call_with_the_main_window_minimized() {
169 assert!(mini_wanted(true, true));
170 assert!(!mini_wanted(true, false));
171 assert!(!mini_wanted(false, true));
172 }
173
174 #[test]
175 fn position_round_trips() {
176 assert_eq!(parse_position(&format_position(-1200, 80)), Some((-1200, 80)));
177 assert_eq!(parse_position("garbage"), None);
178 }
179}