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