| 1 | use std::collections::{HashMap, HashSet}; |
| 2 | use std::time::{Duration, Instant}; |
| 3 | |
| 4 | use calling::{ShareKind, ShareSource}; |
| 5 | use gpui_kit::*; |
| 6 | |
| 7 | use super::model::format_elapsed; |
| 8 | use super::overlay_layout::{ |
| 9 | MINI_POSITION_KEY, bar_collapsed, bar_rect, border_rects, border_thickness, format_position, mini_rect, |
| 10 | mini_wanted, mini_work_area, parse_position, |
| 11 | }; |
| 12 | use crate::app_state::AppState; |
| 13 | use crate::notify::platform::{self, NativeHandle, ScreenRect, ShareGeometry, WorkArea}; |
| 14 | use crate::views::call_mini_window::MiniWindowView; |
| 15 | use crate::views::share_bar::{ShareBarView, ShareBorderView}; |
| 16 | |
| 17 | const TICK: Duration = Duration::from_millis(100); |
| 18 | const IDLE_TICK: Duration = Duration::from_millis(250); |
| 19 | const SLOW_TICK_EVERY: u32 = 5; |
| 20 | const BORDER_SIDES: usize = 4; |
| 21 | |
| 22 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 23 | enum Role { |
| 24 | Bar, |
| 25 | Mini, |
| 26 | Border(usize), |
| 27 | } |
| 28 | |
| 29 | struct Placed { |
| 30 | handle: AnyWindowHandle, |
| 31 | native: Option<NativeHandle>, |
| 32 | rect: Option<ScreenRect>, |
| 33 | } |
| 34 | |
| 35 | impl Placed { |
| 36 | fn place(&mut self, rect: ScreenRect, cx: &mut App) { |
| 37 | self.rect = Some(rect); |
| 38 | let Some(native) = self.native else { |
| 39 | return; |
| 40 | }; |
| 41 | platform::place(native, rect.left, rect.top, rect.width(), rect.height()); |
| 42 | // WM_SIZE arrives while the app is borrowed, so GPUI drops its own resize report. |
| 43 | self.handle.update(cx, |_, window, cx| window.bounds_changed(cx)).ok(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | struct ShareSession { |
| 48 | started: Instant, |
| 49 | source: Option<ShareSource>, |
| 50 | geometry: Option<ShareGeometry>, |
| 51 | } |
| 52 | |
| 53 | pub struct CallWindows { |
| 54 | app: Entity<AppState>, |
| 55 | main_window: AnyWindowHandle, |
| 56 | main_native: Option<NativeHandle>, |
| 57 | work_area: Option<WorkArea>, |
| 58 | minimized: bool, |
| 59 | windows: HashMap<Role, Placed>, |
| 60 | opening: HashSet<Role>, |
| 61 | share: Option<ShareSession>, |
| 62 | pinned: bool, |
| 63 | bar_idle_since: Instant, |
| 64 | bar_collapsed: bool, |
| 65 | shown_second: u64, |
| 66 | mini_saved: Option<(i32, i32)>, |
| 67 | mini_seen: Option<(i32, i32)>, |
| 68 | ticks: u32, |
| 69 | } |
| 70 | |
| 71 | fn display_work_area(cx: &App) -> Option<WorkArea> { |
| 72 | let bounds = cx.primary_display()?.bounds(); |
| 73 | let left = f32::from(bounds.origin.x) as i32; |
| 74 | let top = f32::from(bounds.origin.y) as i32; |
| 75 | Some(WorkArea { |
| 76 | left, |
| 77 | top, |
| 78 | right: left + f32::from(bounds.size.width) as i32, |
| 79 | bottom: top + f32::from(bounds.size.height) as i32, |
| 80 | scale: 1., |
| 81 | }) |
| 82 | } |
| 83 | |
| 84 | fn overlay_options(role: Role, rect: ScreenRect, scale: f32) -> WindowOptions { |
| 85 | let bounds = Bounds::new( |
| 86 | point(px(rect.left as f32 / scale), px(rect.top as f32 / scale)), |
| 87 | size(px(rect.width() as f32 / scale), px(rect.height() as f32 / scale)), |
| 88 | ); |
| 89 | WindowOptions { |
| 90 | window_bounds: Some(WindowBounds::Windowed(bounds)), |
| 91 | titlebar: None, |
| 92 | focus: false, |
| 93 | show: true, |
| 94 | kind: WindowKind::PopUp, |
| 95 | is_movable: role == Role::Mini, |
| 96 | is_resizable: false, |
| 97 | is_minimizable: false, |
| 98 | window_background: match role { |
| 99 | Role::Border(_) => WindowBackgroundAppearance::Opaque, |
| 100 | Role::Bar | Role::Mini => WindowBackgroundAppearance::Transparent, |
| 101 | }, |
| 102 | ..Default::default() |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | impl CallWindows { |
| 107 | pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self { |
| 108 | let mini_saved = app |
| 109 | .read(cx) |
| 110 | .store |
| 111 | .meta(MINI_POSITION_KEY) |
| 112 | .ok() |
| 113 | .flatten() |
| 114 | .and_then(|text| parse_position(&text)); |
| 115 | let windows = CallWindows { |
| 116 | app, |
| 117 | main_window: window.window_handle(), |
| 118 | main_native: platform::native_handle(window), |
| 119 | work_area: None, |
| 120 | minimized: false, |
| 121 | windows: HashMap::new(), |
| 122 | opening: HashSet::new(), |
| 123 | share: None, |
| 124 | pinned: false, |
| 125 | bar_idle_since: Instant::now(), |
| 126 | bar_collapsed: false, |
| 127 | shown_second: 0, |
| 128 | mini_saved, |
| 129 | mini_seen: None, |
| 130 | ticks: 0, |
| 131 | }; |
| 132 | cx.spawn(async move |this, cx| { |
| 133 | let mut pause = TICK; |
| 134 | loop { |
| 135 | cx.background_executor().timer(pause).await; |
| 136 | match this.update(cx, |windows, cx| windows.tick(cx)) { |
| 137 | Ok(next_pause) => pause = next_pause, |
| 138 | Err(_) => break, |
| 139 | } |
| 140 | } |
| 141 | }) |
| 142 | .detach(); |
| 143 | windows |
| 144 | } |
| 145 | |
| 146 | pub fn app(&self) -> &Entity<AppState> { |
| 147 | &self.app |
| 148 | } |
| 149 | |
| 150 | pub fn pinned(&self) -> bool { |
| 151 | self.pinned |
| 152 | } |
| 153 | |
| 154 | pub fn bar_collapsed(&self) -> bool { |
| 155 | self.bar_collapsed |
| 156 | } |
| 157 | |
| 158 | pub fn share_text(&self) -> String { |
| 159 | let Some(session) = &self.share else { |
| 160 | return String::new(); |
| 161 | }; |
| 162 | let what = match session.source.as_ref().map(|source| source.kind) { |
| 163 | Some(ShareKind::Window) => "window", |
| 164 | _ => "screen", |
| 165 | }; |
| 166 | format!("Sharing {what} {}", format_elapsed(session.started.elapsed())) |
| 167 | } |
| 168 | |
| 169 | pub fn toggle_pin(&mut self, cx: &mut Context<Self>) { |
| 170 | self.pinned = !self.pinned; |
| 171 | self.bar_idle_since = Instant::now(); |
| 172 | cx.notify(); |
| 173 | } |
| 174 | |
| 175 | pub fn wake_bar(&mut self, cx: &mut Context<Self>) { |
| 176 | self.bar_idle_since = Instant::now(); |
| 177 | if self.bar_collapsed { |
| 178 | self.bar_collapsed = false; |
| 179 | cx.notify(); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | pub fn return_to_call(&mut self, cx: &mut Context<Self>) { |
| 184 | if let Some(native) = self.main_native { |
| 185 | platform::restore_and_raise(native); |
| 186 | } |
| 187 | self.main_window.update(cx, |_, window, _| window.activate_window()).ok(); |
| 188 | self.app.update(cx, |state, cx| state.show_call(true, cx)); |
| 189 | } |
| 190 | |
| 191 | fn idle(&self, cx: &App) -> bool { |
| 192 | self.app.read(cx).call.is_none() && self.share.is_none() && self.windows.is_empty() && self.opening.is_empty() |
| 193 | } |
| 194 | |
| 195 | fn tick(&mut self, cx: &mut Context<Self>) -> Duration { |
| 196 | if self.idle(cx) { |
| 197 | return IDLE_TICK; |
| 198 | } |
| 199 | self.ticks = self.ticks.wrapping_add(1); |
| 200 | let slow = self.ticks % SLOW_TICK_EVERY == 0; |
| 201 | self.refresh_main_window(); |
| 202 | self.sync_share(slow, cx); |
| 203 | self.sync_mini(cx); |
| 204 | if slow { |
| 205 | self.track_mini_position(cx); |
| 206 | } |
| 207 | TICK |
| 208 | } |
| 209 | |
| 210 | fn refresh_main_window(&mut self) { |
| 211 | let Some(native) = self.main_native else { |
| 212 | return; |
| 213 | }; |
| 214 | self.minimized = platform::window_minimized(native); |
| 215 | if !self.minimized { |
| 216 | self.work_area = platform::work_area(native).or(self.work_area); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | fn local_share(&self, cx: &App) -> Option<Option<ShareSource>> { |
| 221 | let model = &self.app.read(cx).call.as_ref()?.model; |
| 222 | let label = model.local_share.as_ref()?; |
| 223 | Some(model.share_sources.iter().find(|source| &source.label() == label).cloned()) |
| 224 | } |
| 225 | |
| 226 | fn sync_share(&mut self, refresh_geometry: bool, cx: &mut Context<Self>) { |
| 227 | let Some(source) = self.local_share(cx) else { |
| 228 | self.end_share(cx); |
| 229 | return; |
| 230 | }; |
| 231 | let mut refresh_geometry = refresh_geometry; |
| 232 | if self.share.is_none() { |
| 233 | self.bar_idle_since = Instant::now(); |
| 234 | self.share = Some(ShareSession { started: Instant::now(), source: None, geometry: None }); |
| 235 | refresh_geometry = true; |
| 236 | } |
| 237 | let Some(session) = self.share.as_mut() else { |
| 238 | return; |
| 239 | }; |
| 240 | session.source = source; |
| 241 | if refresh_geometry { |
| 242 | session.geometry = session.source.as_ref().and_then(|source| platform::share_geometry(source.kind, source.id)); |
| 243 | } |
| 244 | let geometry = session.geometry; |
| 245 | let elapsed_seconds = session.started.elapsed().as_secs(); |
| 246 | let Some(work) = geometry.map(|geometry| geometry.work).or(self.work_area).or_else(|| display_work_area(cx)) else { |
| 247 | return; |
| 248 | }; |
| 249 | let can_collapse = self.windows.get(&Role::Bar).is_some_and(|placed| placed.native.is_some()); |
| 250 | let collapsed = can_collapse && bar_collapsed(self.pinned, self.bar_idle_since.elapsed()); |
| 251 | if collapsed != self.bar_collapsed || elapsed_seconds != self.shown_second { |
| 252 | self.bar_collapsed = collapsed; |
| 253 | self.shown_second = elapsed_seconds; |
| 254 | cx.notify(); |
| 255 | } |
| 256 | self.ensure_window(Role::Bar, bar_rect(work, collapsed), work.scale, true, cx); |
| 257 | match geometry { |
| 258 | Some(geometry) => { |
| 259 | let thickness = border_thickness(geometry.work.scale); |
| 260 | for (side, rect) in border_rects(geometry.target, thickness).into_iter().enumerate() { |
| 261 | self.ensure_window(Role::Border(side), rect, geometry.work.scale, true, cx); |
| 262 | } |
| 263 | } |
| 264 | None => self.close_borders(cx), |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | fn end_share(&mut self, cx: &mut Context<Self>) { |
| 269 | if self.share.take().is_none() { |
| 270 | return; |
| 271 | } |
| 272 | self.bar_collapsed = false; |
| 273 | self.close(Role::Bar, cx); |
| 274 | self.close_borders(cx); |
| 275 | } |
| 276 | |
| 277 | fn close_borders(&mut self, cx: &mut Context<Self>) { |
| 278 | for side in 0..BORDER_SIDES { |
| 279 | self.close(Role::Border(side), cx); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | fn sync_mini(&mut self, cx: &mut Context<Self>) { |
| 284 | let in_call = self.app.read(cx).call.is_some(); |
| 285 | let work = self.work_area.or_else(|| display_work_area(cx)); |
| 286 | match work.filter(|_| mini_wanted(in_call, self.minimized)) { |
| 287 | Some(main_work) => { |
| 288 | let work = mini_work_area(main_work, self.mini_saved, platform::work_area_at); |
| 289 | let rect = mini_rect(work, self.mini_saved); |
| 290 | self.ensure_window(Role::Mini, rect, work.scale, false, cx); |
| 291 | } |
| 292 | None => self.close(Role::Mini, cx), |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | fn track_mini_position(&mut self, cx: &mut Context<Self>) { |
| 297 | let Some(native) = self.windows.get(&Role::Mini).and_then(|placed| placed.native) else { |
| 298 | return; |
| 299 | }; |
| 300 | let Some(rect) = platform::window_rect(native) else { |
| 301 | return; |
| 302 | }; |
| 303 | let position = (rect.left, rect.top); |
| 304 | let settled = self.mini_seen == Some(position); |
| 305 | self.mini_seen = Some(position); |
| 306 | if settled && self.mini_saved != Some(position) { |
| 307 | self.mini_saved = Some(position); |
| 308 | let _ = self.app.read(cx).store.set_meta(MINI_POSITION_KEY, &format_position(position.0, position.1)); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | fn ensure_window(&mut self, role: Role, rect: ScreenRect, scale: f32, follow: bool, cx: &mut Context<Self>) { |
| 313 | match self.windows.get_mut(&role) { |
| 314 | Some(placed) => { |
| 315 | if placed.rect.is_none() || follow && placed.rect != Some(rect) { |
| 316 | placed.place(rect, cx); |
| 317 | } |
| 318 | } |
| 319 | None => self.open(role, rect, scale, cx), |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | fn close(&mut self, role: Role, cx: &mut Context<Self>) { |
| 324 | if let Some(placed) = self.windows.remove(&role) { |
| 325 | placed.handle.update(cx, |_, window, _| window.remove_window()).ok(); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | fn open(&mut self, role: Role, rect: ScreenRect, scale: f32, cx: &mut Context<Self>) { |
| 330 | if !self.opening.insert(role) { |
| 331 | return; |
| 332 | } |
| 333 | let controller = cx.entity(); |
| 334 | let options = overlay_options(role, rect, scale); |
| 335 | // open_window draws right away, and the views read this entity while rendering. |
| 336 | cx.defer(move |cx| { |
| 337 | let view_controller = controller.clone(); |
| 338 | let opened = cx.open_window(options, move |window, cx| { |
| 339 | let view: AnyView = match role { |
| 340 | Role::Bar => cx.new(|cx| ShareBarView::new(view_controller, cx)).into(), |
| 341 | Role::Mini => cx.new(|cx| MiniWindowView::new(view_controller, cx)).into(), |
| 342 | Role::Border(_) => cx.new(|_| ShareBorderView).into(), |
| 343 | }; |
| 344 | cx.new(|cx| base::Root::new(view, window, cx).bg(transparent_black())) |
| 345 | }); |
| 346 | let placed = opened.ok().map(|handle| { |
| 347 | let handle = AnyWindowHandle::from(handle); |
| 348 | let native = handle.update(cx, |_, window, _| platform::native_handle(window)).ok().flatten(); |
| 349 | if let Some(native) = native { |
| 350 | platform::prepare_toast_window(native); |
| 351 | platform::exclude_from_capture(native); |
| 352 | if matches!(role, Role::Border(_)) { |
| 353 | platform::make_click_through(native); |
| 354 | } |
| 355 | } |
| 356 | Placed { handle, native, rect: None } |
| 357 | }); |
| 358 | controller.update(cx, |this, _| { |
| 359 | this.opening.remove(&role); |
| 360 | if let Some(placed) = placed { |
| 361 | this.windows.insert(role, placed); |
| 362 | } |
| 363 | }); |
| 364 | }); |
| 365 | } |
| 366 | } |