| 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 | BorderVisibility, MINI_POSITION_KEY, bar_collapsed, bar_rect, border_rects, border_thickness, |
| 10 | border_visibility, format_position, mini_rect, mini_work_area, parse_position, |
| 11 | }; |
| 12 | use super::share_overlay::{ |
| 13 | GALLERY_POSITION_KEY, GalleryLayout, LocalShare, Monitor, Surface, excluded_from_capture, |
| 14 | gallery_available_height, gallery_candidates, gallery_layout, gallery_rect, |
| 15 | minimize_on_share_start, overlay_plan, |
| 16 | }; |
| 17 | use crate::app_state::AppState; |
| 18 | use crate::notify::platform::{self, NativeHandle, ScreenRect, ShareGeometry, WorkArea}; |
| 19 | use crate::views::call_mini_window::MiniWindowView; |
| 20 | use crate::views::call_pop_out::{POP_OUT_SIZE, POP_OUT_TITLE, SharePopOutView}; |
| 21 | use crate::views::call_share_gallery::ShareGalleryView; |
| 22 | use crate::views::call_window::{ |
| 23 | CALL_WINDOW_MIN_SIZE, CALL_WINDOW_SIZE, CallWindowView, call_window_title, |
| 24 | }; |
| 25 | use crate::views::share_bar::{ShareBarView, ShareBorderView}; |
| 26 | |
| 27 | const TICK: Duration = Duration::from_millis(100); |
| 28 | const IDLE_TICK: Duration = Duration::from_millis(250); |
| 29 | const SLOW_TICK_EVERY: u32 = 5; |
| 30 | const BORDER_SIDES: usize = 4; |
| 31 | |
| 32 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 33 | enum Role { |
| 34 | Bar, |
| 35 | Mini, |
| 36 | Gallery, |
| 37 | Border(usize), |
| 38 | } |
| 39 | |
| 40 | impl Role { |
| 41 | fn surface(self) -> Surface { |
| 42 | match self { |
| 43 | Role::Bar => Surface::ShareBar, |
| 44 | Role::Mini => Surface::Mini, |
| 45 | Role::Gallery => Surface::Gallery, |
| 46 | Role::Border(_) => Surface::Border, |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | struct Placed { |
| 52 | handle: AnyWindowHandle, |
| 53 | native: Option<NativeHandle>, |
| 54 | rect: Option<ScreenRect>, |
| 55 | anchor: Option<NativeHandle>, |
| 56 | visible: bool, |
| 57 | } |
| 58 | |
| 59 | impl Placed { |
| 60 | fn place(&mut self, rect: ScreenRect, cx: &mut App) { |
| 61 | if self.rect == Some(rect) { |
| 62 | return; |
| 63 | } |
| 64 | self.rect = Some(rect); |
| 65 | let Some(native) = self.native else { |
| 66 | return; |
| 67 | }; |
| 68 | match self.anchor { |
| 69 | Some(_) => { |
| 70 | platform::place_rect(native, rect.left, rect.top, rect.width(), rect.height()) |
| 71 | } |
| 72 | None => platform::place(native, rect.left, rect.top, rect.width(), rect.height()), |
| 73 | } |
| 74 | // WM_SIZE arrives while the app is borrowed, so GPUI drops its own resize report. |
| 75 | self.handle |
| 76 | .update(cx, |_, window, cx| window.bounds_changed(cx)) |
| 77 | .ok(); |
| 78 | } |
| 79 | |
| 80 | fn set_visible(&mut self, visible: bool) { |
| 81 | let Some(native) = self.native else { |
| 82 | return; |
| 83 | }; |
| 84 | if self.visible == visible { |
| 85 | return; |
| 86 | } |
| 87 | self.visible = visible; |
| 88 | if visible { |
| 89 | platform::show_window_inactive(native); |
| 90 | } else { |
| 91 | platform::hide_window(native); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | struct ShareSession { |
| 97 | started: Instant, |
| 98 | source: Option<ShareSource>, |
| 99 | geometry: Option<ShareGeometry>, |
| 100 | minimize_checked: bool, |
| 101 | } |
| 102 | |
| 103 | pub struct CallWindows { |
| 104 | app: Entity<AppState>, |
| 105 | main_native: Option<NativeHandle>, |
| 106 | stage: Option<AnyWindowHandle>, |
| 107 | stage_native: Option<NativeHandle>, |
| 108 | stage_opening: bool, |
| 109 | stage_closed_by_user: bool, |
| 110 | stage_closed_call: Option<u64>, |
| 111 | seen_viewing: Option<bool>, |
| 112 | work_area: Option<WorkArea>, |
| 113 | minimized: bool, |
| 114 | windows: HashMap<Role, Placed>, |
| 115 | opening: HashSet<Role>, |
| 116 | share: Option<ShareSession>, |
| 117 | pop_out: Option<AnyWindowHandle>, |
| 118 | pop_out_opening: bool, |
| 119 | pinned: bool, |
| 120 | bar_idle_since: Instant, |
| 121 | bar_collapsed: bool, |
| 122 | shown_second: u64, |
| 123 | mini_saved: Option<(i32, i32)>, |
| 124 | mini_seen: Option<(i32, i32)>, |
| 125 | gallery_saved: Option<(i32, i32)>, |
| 126 | gallery_seen: Option<(i32, i32)>, |
| 127 | gallery_layout: GalleryLayout, |
| 128 | speaker_only: bool, |
| 129 | minimized_for_share: bool, |
| 130 | ticks: u32, |
| 131 | } |
| 132 | |
| 133 | fn display_work_area(cx: &App) -> Option<WorkArea> { |
| 134 | let bounds = cx.primary_display()?.bounds(); |
| 135 | let left = f32::from(bounds.origin.x) as i32; |
| 136 | let top = f32::from(bounds.origin.y) as i32; |
| 137 | Some(WorkArea { |
| 138 | left, |
| 139 | top, |
| 140 | right: left + f32::from(bounds.size.width) as i32, |
| 141 | bottom: top + f32::from(bounds.size.height) as i32, |
| 142 | scale: 1., |
| 143 | }) |
| 144 | } |
| 145 | |
| 146 | fn overlay_options(role: Role, rect: ScreenRect, scale: f32) -> WindowOptions { |
| 147 | let bounds = Bounds::new( |
| 148 | point(px(rect.left as f32 / scale), px(rect.top as f32 / scale)), |
| 149 | size( |
| 150 | px(rect.width() as f32 / scale), |
| 151 | px(rect.height() as f32 / scale), |
| 152 | ), |
| 153 | ); |
| 154 | WindowOptions { |
| 155 | window_bounds: Some(WindowBounds::Windowed(bounds)), |
| 156 | titlebar: None, |
| 157 | focus: false, |
| 158 | show: true, |
| 159 | kind: WindowKind::PopUp, |
| 160 | is_movable: matches!(role, Role::Mini | Role::Gallery), |
| 161 | is_resizable: false, |
| 162 | is_minimizable: false, |
| 163 | window_background: match role { |
| 164 | Role::Border(_) => WindowBackgroundAppearance::Opaque, |
| 165 | Role::Bar | Role::Mini | Role::Gallery => WindowBackgroundAppearance::Transparent, |
| 166 | }, |
| 167 | ..Default::default() |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | impl CallWindows { |
| 172 | /// Points at a main window reopened from the tray. |
| 173 | pub fn attach_main_window(&mut self, window: &Window) { |
| 174 | self.main_native = platform::native_handle(window); |
| 175 | } |
| 176 | |
| 177 | pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self { |
| 178 | let saved_position = |key: &str, cx: &App| { |
| 179 | app.read(cx) |
| 180 | .store |
| 181 | .meta(key) |
| 182 | .ok() |
| 183 | .flatten() |
| 184 | .and_then(|text| parse_position(&text)) |
| 185 | }; |
| 186 | let mini_saved = saved_position(MINI_POSITION_KEY, cx); |
| 187 | let gallery_saved = saved_position(GALLERY_POSITION_KEY, cx); |
| 188 | let windows = CallWindows { |
| 189 | app, |
| 190 | main_native: platform::native_handle(window), |
| 191 | stage: None, |
| 192 | stage_native: None, |
| 193 | stage_opening: false, |
| 194 | stage_closed_by_user: false, |
| 195 | stage_closed_call: None, |
| 196 | seen_viewing: None, |
| 197 | work_area: None, |
| 198 | minimized: false, |
| 199 | windows: HashMap::new(), |
| 200 | opening: HashSet::new(), |
| 201 | share: None, |
| 202 | pop_out: None, |
| 203 | pop_out_opening: false, |
| 204 | pinned: false, |
| 205 | bar_idle_since: Instant::now(), |
| 206 | bar_collapsed: false, |
| 207 | shown_second: 0, |
| 208 | mini_saved, |
| 209 | mini_seen: None, |
| 210 | gallery_saved, |
| 211 | gallery_seen: None, |
| 212 | gallery_layout: gallery_layout(0, true, f32::MAX), |
| 213 | speaker_only: false, |
| 214 | minimized_for_share: false, |
| 215 | ticks: 0, |
| 216 | }; |
| 217 | cx.spawn(async move |this, cx| { |
| 218 | let mut pause = TICK; |
| 219 | loop { |
| 220 | cx.background_executor().timer(pause).await; |
| 221 | match this.update(cx, |windows, cx| windows.tick(cx)) { |
| 222 | Ok(next_pause) => pause = next_pause, |
| 223 | Err(_) => break, |
| 224 | } |
| 225 | } |
| 226 | }) |
| 227 | .detach(); |
| 228 | windows |
| 229 | } |
| 230 | |
| 231 | pub fn app(&self) -> &Entity<AppState> { |
| 232 | &self.app |
| 233 | } |
| 234 | |
| 235 | pub fn pinned(&self) -> bool { |
| 236 | self.pinned |
| 237 | } |
| 238 | |
| 239 | pub fn bar_collapsed(&self) -> bool { |
| 240 | self.bar_collapsed |
| 241 | } |
| 242 | |
| 243 | pub fn gallery_layout(&self) -> GalleryLayout { |
| 244 | self.gallery_layout |
| 245 | } |
| 246 | |
| 247 | pub fn sharing(&self) -> bool { |
| 248 | self.share.is_some() |
| 249 | } |
| 250 | |
| 251 | pub fn toggle_speaker_only(&mut self, cx: &mut Context<Self>) { |
| 252 | self.speaker_only = !self.speaker_only; |
| 253 | cx.notify(); |
| 254 | } |
| 255 | |
| 256 | pub fn share_text(&self) -> String { |
| 257 | let Some(session) = &self.share else { |
| 258 | return String::new(); |
| 259 | }; |
| 260 | let what = match session.source.as_ref().map(|source| source.kind) { |
| 261 | Some(ShareKind::Window) => "window", |
| 262 | _ => "screen", |
| 263 | }; |
| 264 | format!( |
| 265 | "Sharing {what} {}", |
| 266 | format_elapsed(session.started.elapsed()) |
| 267 | ) |
| 268 | } |
| 269 | |
| 270 | pub fn toggle_pin(&mut self, cx: &mut Context<Self>) { |
| 271 | self.pinned = !self.pinned; |
| 272 | self.bar_idle_since = Instant::now(); |
| 273 | cx.notify(); |
| 274 | } |
| 275 | |
| 276 | pub fn wake_bar(&mut self, cx: &mut Context<Self>) { |
| 277 | self.bar_idle_since = Instant::now(); |
| 278 | if self.bar_collapsed { |
| 279 | self.bar_collapsed = false; |
| 280 | cx.notify(); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | pub fn return_to_call(&mut self, cx: &mut Context<Self>) { |
| 285 | self.raise_stage(cx); |
| 286 | self.app.update(cx, |state, cx| state.show_call(true, cx)); |
| 287 | } |
| 288 | |
| 289 | fn raise_stage(&mut self, cx: &mut Context<Self>) { |
| 290 | if let Some(native) = self.stage_native { |
| 291 | platform::restore_and_raise(native); |
| 292 | } |
| 293 | if let Some(stage) = self.stage { |
| 294 | stage |
| 295 | .update(cx, |_, window, _| window.activate_window()) |
| 296 | .ok(); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | fn stage_wanted(&self, cx: &App) -> bool { |
| 301 | let state = self.app.read(cx); |
| 302 | state.call.is_some() || state.call_end.is_some() || state.prejoin.is_some() |
| 303 | } |
| 304 | |
| 305 | fn sync_stage(&mut self, cx: &mut Context<Self>) { |
| 306 | let wanted = self.stage_wanted(cx); |
| 307 | let current_call = self.app.read(cx).call.as_ref().map(|call| call.id); |
| 308 | let another_call = current_call.is_some() && current_call != self.stage_closed_call; |
| 309 | if self.stage_closed_by_user && !another_call { |
| 310 | if !wanted { |
| 311 | self.stage_closed_by_user = false; |
| 312 | } else if current_call.is_none() { |
| 313 | self.app.update(cx, |state, cx| state.dismiss_call_end(cx)); |
| 314 | } |
| 315 | return; |
| 316 | } |
| 317 | self.stage_closed_by_user = false; |
| 318 | match (wanted, self.stage) { |
| 319 | (true, None) => self.open_stage(cx), |
| 320 | (false, Some(stage)) => { |
| 321 | self.forget_stage(); |
| 322 | stage.update(cx, |_, window, _| window.remove_window()).ok(); |
| 323 | } |
| 324 | (true, Some(stage)) => { |
| 325 | if stage.update(cx, |_, _, _| ()).is_err() { |
| 326 | self.forget_stage(); |
| 327 | self.stage_closed_by_user = true; |
| 328 | self.stage_closed_call = current_call; |
| 329 | } |
| 330 | } |
| 331 | (false, None) => {} |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | fn forget_stage(&mut self) { |
| 336 | self.stage = None; |
| 337 | self.stage_native = None; |
| 338 | self.minimized = false; |
| 339 | self.minimized_for_share = false; |
| 340 | self.seen_viewing = None; |
| 341 | } |
| 342 | |
| 343 | fn open_stage(&mut self, cx: &mut Context<Self>) { |
| 344 | if self.stage_opening { |
| 345 | return; |
| 346 | } |
| 347 | self.stage_opening = true; |
| 348 | let controller = cx.entity(); |
| 349 | let app = self.app.clone(); |
| 350 | let mut options = crate::views::title_bar::window_options(); |
| 351 | options.window_bounds = Some(WindowBounds::Windowed(Bounds::centered( |
| 352 | None, |
| 353 | size(px(CALL_WINDOW_SIZE.0), px(CALL_WINDOW_SIZE.1)), |
| 354 | cx, |
| 355 | ))); |
| 356 | options.window_min_size = |
| 357 | Some(size(px(CALL_WINDOW_MIN_SIZE.0), px(CALL_WINDOW_MIN_SIZE.1))); |
| 358 | if let Some(titlebar) = options.titlebar.as_mut() { |
| 359 | titlebar.title = Some(call_window_title(self.app.read(cx)).into()); |
| 360 | } |
| 361 | // open_window draws right away, and the view reads the app entity while rendering. |
| 362 | cx.defer(move |cx| { |
| 363 | let opened = gpui_kit::open_window(options, cx, move |window, cx| { |
| 364 | cx.new(|cx| CallWindowView::new(app, window, cx)) |
| 365 | }); |
| 366 | let stage = opened.ok().map(|(handle, _)| handle); |
| 367 | let native = stage.and_then(|stage| { |
| 368 | stage |
| 369 | .update(cx, |_, window, _| platform::native_handle(window)) |
| 370 | .ok() |
| 371 | .flatten() |
| 372 | }); |
| 373 | controller.update(cx, |this, _| { |
| 374 | this.stage_opening = false; |
| 375 | this.stage = stage; |
| 376 | this.stage_native = native; |
| 377 | }); |
| 378 | }); |
| 379 | } |
| 380 | |
| 381 | fn sync_viewing(&mut self, cx: &mut Context<Self>) { |
| 382 | let Some(viewing) = self.app.read(cx).call.as_ref().map(|call| call.viewing) else { |
| 383 | return; |
| 384 | }; |
| 385 | let Some(stage) = self.stage else { |
| 386 | return; |
| 387 | }; |
| 388 | match self.seen_viewing { |
| 389 | Some(seen) if seen == viewing => { |
| 390 | if self.stage_native.is_some() && viewing == self.minimized { |
| 391 | self.seen_viewing = Some(!viewing); |
| 392 | self.app |
| 393 | .update(cx, |state, cx| state.show_call(!viewing, cx)); |
| 394 | } |
| 395 | } |
| 396 | _ => { |
| 397 | self.seen_viewing = Some(viewing); |
| 398 | if viewing && self.minimized { |
| 399 | self.raise_stage(cx); |
| 400 | } else if !viewing && !self.minimized { |
| 401 | stage |
| 402 | .update(cx, |_, window, _| window.minimize_window()) |
| 403 | .ok(); |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | fn idle(&self, cx: &App) -> bool { |
| 410 | !self.stage_wanted(cx) |
| 411 | && self.stage.is_none() |
| 412 | && !self.stage_opening |
| 413 | && !self.stage_closed_by_user |
| 414 | && self.share.is_none() |
| 415 | && self.windows.is_empty() |
| 416 | && self.opening.is_empty() |
| 417 | } |
| 418 | |
| 419 | fn tick(&mut self, cx: &mut Context<Self>) -> Duration { |
| 420 | if self.idle(cx) { |
| 421 | return IDLE_TICK; |
| 422 | } |
| 423 | self.ticks = self.ticks.wrapping_add(1); |
| 424 | let slow = self.ticks % SLOW_TICK_EVERY == 0; |
| 425 | self.sync_stage(cx); |
| 426 | self.refresh_stage_window(); |
| 427 | self.sync_viewing(cx); |
| 428 | self.sync_share(slow, cx); |
| 429 | self.sync_pop_out(cx); |
| 430 | self.sync_monitor(cx); |
| 431 | if slow { |
| 432 | self.track_position(Role::Mini, cx); |
| 433 | self.track_position(Role::Gallery, cx); |
| 434 | } |
| 435 | TICK |
| 436 | } |
| 437 | |
| 438 | fn refresh_stage_window(&mut self) { |
| 439 | let Some(native) = self.stage_native.or(self.main_native) else { |
| 440 | return; |
| 441 | }; |
| 442 | let minimized = self.stage_native.is_some_and(platform::window_minimized); |
| 443 | if self.minimized && !minimized { |
| 444 | self.minimized_for_share = false; |
| 445 | } |
| 446 | self.minimized = minimized; |
| 447 | if !self.minimized { |
| 448 | self.work_area = platform::work_area(native).or(self.work_area); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | fn local_share(&self, cx: &App) -> Option<Option<ShareSource>> { |
| 453 | let model = &self.app.read(cx).call.as_ref()?.model; |
| 454 | let label = model.local_share.as_ref()?; |
| 455 | if let Some(source) = model |
| 456 | .share_sources |
| 457 | .iter() |
| 458 | .find(|source| &source.label() == label) |
| 459 | .cloned() |
| 460 | { |
| 461 | return Some(Some(source)); |
| 462 | } |
| 463 | Some(model.share_kind.map(|kind| ShareSource { |
| 464 | id: 0, |
| 465 | kind, |
| 466 | title: label.clone(), |
| 467 | })) |
| 468 | } |
| 469 | |
| 470 | fn portal_picker(&self, cx: &App) -> bool { |
| 471 | self.app |
| 472 | .read(cx) |
| 473 | .call |
| 474 | .as_ref() |
| 475 | .is_some_and(|call| call.model.uses_portal_picker()) |
| 476 | } |
| 477 | |
| 478 | fn sync_share(&mut self, refresh_geometry: bool, cx: &mut Context<Self>) { |
| 479 | let Some(source) = self.local_share(cx) else { |
| 480 | self.end_share(cx); |
| 481 | return; |
| 482 | }; |
| 483 | let mut refresh_geometry = refresh_geometry; |
| 484 | if self.share.is_none() { |
| 485 | self.bar_idle_since = Instant::now(); |
| 486 | self.share = Some(ShareSession { |
| 487 | started: Instant::now(), |
| 488 | source: None, |
| 489 | geometry: None, |
| 490 | minimize_checked: false, |
| 491 | }); |
| 492 | refresh_geometry = true; |
| 493 | } |
| 494 | let Some(session) = self.share.as_mut() else { |
| 495 | return; |
| 496 | }; |
| 497 | session.source = source; |
| 498 | let shared_window = session |
| 499 | .source |
| 500 | .as_ref() |
| 501 | .filter(|source| source.kind == ShareKind::Window) |
| 502 | .map(|source| source.id as NativeHandle); |
| 503 | if refresh_geometry || shared_window.is_some() { |
| 504 | session.geometry = session |
| 505 | .source |
| 506 | .as_ref() |
| 507 | .and_then(|source| platform::share_geometry(source.kind, source.id)); |
| 508 | } |
| 509 | let geometry = session.geometry; |
| 510 | let elapsed_seconds = session.started.elapsed().as_secs(); |
| 511 | let check_minimize = !std::mem::replace(&mut session.minimize_checked, true); |
| 512 | if check_minimize { |
| 513 | self.minimize_for_share(geometry.map(|geometry| geometry.target), cx); |
| 514 | } |
| 515 | let Some(work) = geometry |
| 516 | .map(|geometry| geometry.work) |
| 517 | .or(self.work_area) |
| 518 | .or_else(|| display_work_area(cx)) |
| 519 | else { |
| 520 | return; |
| 521 | }; |
| 522 | let can_collapse = self |
| 523 | .windows |
| 524 | .get(&Role::Bar) |
| 525 | .is_some_and(|placed| placed.native.is_some()); |
| 526 | let collapsed = can_collapse && bar_collapsed(self.pinned, self.bar_idle_since.elapsed()); |
| 527 | if collapsed != self.bar_collapsed || elapsed_seconds != self.shown_second { |
| 528 | self.bar_collapsed = collapsed; |
| 529 | self.shown_second = elapsed_seconds; |
| 530 | cx.notify(); |
| 531 | } |
| 532 | self.ensure_window(Role::Bar, bar_rect(work, collapsed), work.scale, true, cx); |
| 533 | match border_visibility(shared_window.is_some(), geometry.is_some()) { |
| 534 | BorderVisibility::Show => { |
| 535 | let thickness = border_thickness(work.scale); |
| 536 | let rects = geometry.map(|geometry| border_rects(geometry.target, thickness)); |
| 537 | for (side, rect) in rects.into_iter().flatten().enumerate() { |
| 538 | if let Some(placed) = self.windows.get_mut(&Role::Border(side)) { |
| 539 | placed.anchor = shared_window; |
| 540 | placed.set_visible(true); |
| 541 | } |
| 542 | self.ensure_window(Role::Border(side), rect, work.scale, true, cx); |
| 543 | } |
| 544 | self.restack_borders(shared_window); |
| 545 | } |
| 546 | BorderVisibility::Hide => { |
| 547 | for side in 0..BORDER_SIDES { |
| 548 | if let Some(placed) = self.windows.get_mut(&Role::Border(side)) { |
| 549 | placed.set_visible(false); |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | BorderVisibility::Close => self.close_borders(cx), |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | fn local_share_mode(&self) -> LocalShare { |
| 558 | let kind = self |
| 559 | .share |
| 560 | .as_ref() |
| 561 | .and_then(|session| session.source.as_ref()) |
| 562 | .map(|source| source.kind); |
| 563 | LocalShare::of(kind, self.share.is_some()) |
| 564 | } |
| 565 | |
| 566 | fn minimize_for_share(&mut self, shared: Option<ScreenRect>, cx: &mut Context<Self>) { |
| 567 | let Some(native) = self.stage_native.filter(|_| !self.minimized) else { |
| 568 | return; |
| 569 | }; |
| 570 | if !minimize_on_share_start( |
| 571 | self.local_share_mode(), |
| 572 | platform::window_rect(native), |
| 573 | shared, |
| 574 | self.portal_picker(cx), |
| 575 | ) { |
| 576 | return; |
| 577 | } |
| 578 | self.minimized_for_share = true; |
| 579 | if let Some(stage) = self.stage { |
| 580 | stage |
| 581 | .update(cx, |_, window, _| window.minimize_window()) |
| 582 | .ok(); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | fn end_share(&mut self, cx: &mut Context<Self>) { |
| 587 | if self.share.take().is_none() { |
| 588 | return; |
| 589 | } |
| 590 | if std::mem::take(&mut self.minimized_for_share) && self.minimized { |
| 591 | self.return_to_call(cx); |
| 592 | } |
| 593 | self.bar_collapsed = false; |
| 594 | self.close(Role::Bar, cx); |
| 595 | self.close_borders(cx); |
| 596 | } |
| 597 | |
| 598 | fn close_borders(&mut self, cx: &mut Context<Self>) { |
| 599 | for side in 0..BORDER_SIDES { |
| 600 | self.close(Role::Border(side), cx); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | fn sync_pop_out(&mut self, cx: &mut Context<Self>) { |
| 605 | let wanted = self |
| 606 | .app |
| 607 | .read(cx) |
| 608 | .call |
| 609 | .as_ref() |
| 610 | .is_some_and(|call| call.share_stage.popped && call.model.screen_sharer.is_some()); |
| 611 | match (wanted, self.pop_out) { |
| 612 | (true, None) => self.open_pop_out(cx), |
| 613 | (false, Some(handle)) => { |
| 614 | self.pop_out = None; |
| 615 | handle |
| 616 | .update(cx, |_, window, _| window.remove_window()) |
| 617 | .ok(); |
| 618 | } |
| 619 | (true, Some(handle)) => { |
| 620 | if handle.update(cx, |_, _, _| ()).is_err() { |
| 621 | self.pop_out = None; |
| 622 | self.app |
| 623 | .update(cx, |state, cx| state.return_stage_from_pop_out(cx)); |
| 624 | } |
| 625 | } |
| 626 | (false, None) => {} |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | fn open_pop_out(&mut self, cx: &mut Context<Self>) { |
| 631 | if self.pop_out_opening { |
| 632 | return; |
| 633 | } |
| 634 | self.pop_out_opening = true; |
| 635 | let controller = cx.entity(); |
| 636 | let app = self.app.clone(); |
| 637 | let options = WindowOptions { |
| 638 | window_bounds: Some(WindowBounds::Windowed(Bounds::centered( |
| 639 | None, |
| 640 | size(px(POP_OUT_SIZE.0), px(POP_OUT_SIZE.1)), |
| 641 | cx, |
| 642 | ))), |
| 643 | titlebar: Some(TitlebarOptions { |
| 644 | title: Some(POP_OUT_TITLE.into()), |
| 645 | ..Default::default() |
| 646 | }), |
| 647 | ..Default::default() |
| 648 | }; |
| 649 | // open_window draws right away, and the view reads the app entity while rendering. |
| 650 | cx.defer(move |cx| { |
| 651 | let opened = gpui_kit::open_window(options, cx, move |window, cx| { |
| 652 | cx.new(|cx| SharePopOutView::new(app, window, cx)) |
| 653 | }); |
| 654 | controller.update(cx, |this, _| { |
| 655 | this.pop_out_opening = false; |
| 656 | this.pop_out = opened.ok().map(|(handle, _)| handle); |
| 657 | }); |
| 658 | }); |
| 659 | } |
| 660 | |
| 661 | fn sync_monitor(&mut self, cx: &mut Context<Self>) { |
| 662 | let in_call = self.app.read(cx).call.is_some(); |
| 663 | let plan = overlay_plan( |
| 664 | in_call, |
| 665 | self.local_share_mode(), |
| 666 | self.minimized, |
| 667 | self.speaker_only, |
| 668 | ); |
| 669 | let work = self.work_area.or_else(|| display_work_area(cx)); |
| 670 | match (plan.monitor, work) { |
| 671 | (Monitor::Mini, Some(main_work)) => { |
| 672 | self.close(Role::Gallery, cx); |
| 673 | let work = mini_work_area(main_work, self.mini_saved, platform::work_area_at); |
| 674 | let rect = mini_rect(work, self.mini_saved); |
| 675 | self.ensure_window(Role::Mini, rect, work.scale, false, cx); |
| 676 | } |
| 677 | (Monitor::Gallery, Some(main_work)) => { |
| 678 | self.close(Role::Mini, cx); |
| 679 | self.place_gallery(main_work, cx); |
| 680 | } |
| 681 | _ => { |
| 682 | self.close(Role::Mini, cx); |
| 683 | self.close(Role::Gallery, cx); |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | fn place_gallery(&mut self, main_work: WorkArea, cx: &mut Context<Self>) { |
| 689 | let work = mini_work_area(main_work, self.gallery_saved, platform::work_area_at); |
| 690 | let (others, self_tile) = self.app.read(cx).call.as_ref().map_or((0, true), |call| { |
| 691 | ( |
| 692 | gallery_candidates(&call.model).len(), |
| 693 | !call.model.view.hide_me, |
| 694 | ) |
| 695 | }); |
| 696 | let layout = gallery_layout(others, self_tile, gallery_available_height(work)); |
| 697 | if layout != self.gallery_layout { |
| 698 | self.gallery_layout = layout; |
| 699 | cx.notify(); |
| 700 | } |
| 701 | let origin = self |
| 702 | .windows |
| 703 | .get(&Role::Gallery) |
| 704 | .and_then(|placed| placed.native) |
| 705 | .and_then(platform::window_rect) |
| 706 | .map(|rect| (rect.left, rect.top)) |
| 707 | .or(self.gallery_saved); |
| 708 | let rect = gallery_rect(work, layout, origin); |
| 709 | match self.windows.get_mut(&Role::Gallery) { |
| 710 | Some(placed) => { |
| 711 | let size = |rect: ScreenRect| (rect.width(), rect.height()); |
| 712 | if placed.rect.map(size) != Some(size(rect)) { |
| 713 | placed.place(rect, cx); |
| 714 | } |
| 715 | } |
| 716 | None => self.open(Role::Gallery, rect, work.scale, cx), |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | fn track_position(&mut self, role: Role, cx: &mut Context<Self>) { |
| 721 | let Some(native) = self.windows.get(&role).and_then(|placed| placed.native) else { |
| 722 | return; |
| 723 | }; |
| 724 | let Some(rect) = platform::window_rect(native) else { |
| 725 | return; |
| 726 | }; |
| 727 | let (key, saved, seen) = match role { |
| 728 | Role::Mini => (MINI_POSITION_KEY, &mut self.mini_saved, &mut self.mini_seen), |
| 729 | Role::Gallery => ( |
| 730 | GALLERY_POSITION_KEY, |
| 731 | &mut self.gallery_saved, |
| 732 | &mut self.gallery_seen, |
| 733 | ), |
| 734 | Role::Bar | Role::Border(_) => return, |
| 735 | }; |
| 736 | let position = (rect.left, rect.top); |
| 737 | let settled = *seen == Some(position); |
| 738 | *seen = Some(position); |
| 739 | if settled && *saved != Some(position) { |
| 740 | *saved = Some(position); |
| 741 | let _ = self |
| 742 | .app |
| 743 | .read(cx) |
| 744 | .store |
| 745 | .set_meta(key, &format_position(position.0, position.1)); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | fn restack_borders(&self, shared_window: Option<NativeHandle>) { |
| 750 | let Some(shared) = shared_window else { |
| 751 | return; |
| 752 | }; |
| 753 | let group: Vec<NativeHandle> = (0..BORDER_SIDES) |
| 754 | .filter_map(|side| self.windows.get(&Role::Border(side))?.native) |
| 755 | .collect(); |
| 756 | platform::stack_group_above(shared, &group); |
| 757 | } |
| 758 | |
| 759 | fn ensure_window( |
| 760 | &mut self, |
| 761 | role: Role, |
| 762 | rect: ScreenRect, |
| 763 | scale: f32, |
| 764 | follow: bool, |
| 765 | cx: &mut Context<Self>, |
| 766 | ) { |
| 767 | match self.windows.get_mut(&role) { |
| 768 | Some(placed) => { |
| 769 | if placed.rect.is_none() || follow && placed.rect != Some(rect) { |
| 770 | placed.place(rect, cx); |
| 771 | } |
| 772 | } |
| 773 | None => self.open(role, rect, scale, cx), |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | fn close(&mut self, role: Role, cx: &mut Context<Self>) { |
| 778 | if let Some(placed) = self.windows.remove(&role) { |
| 779 | placed |
| 780 | .handle |
| 781 | .update(cx, |_, window, _| window.remove_window()) |
| 782 | .ok(); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | fn open(&mut self, role: Role, rect: ScreenRect, scale: f32, cx: &mut Context<Self>) { |
| 787 | if !self.opening.insert(role) { |
| 788 | return; |
| 789 | } |
| 790 | let controller = cx.entity(); |
| 791 | let options = overlay_options(role, rect, scale); |
| 792 | // open_window draws right away, and the views read this entity while rendering. |
| 793 | cx.defer(move |cx| { |
| 794 | let view_controller = controller.clone(); |
| 795 | let opened = cx.open_window(options, move |window, cx| { |
| 796 | let view: AnyView = match role { |
| 797 | Role::Bar => cx.new(|cx| ShareBarView::new(view_controller, cx)).into(), |
| 798 | Role::Mini => cx.new(|cx| MiniWindowView::new(view_controller, cx)).into(), |
| 799 | Role::Gallery => cx |
| 800 | .new(|cx| ShareGalleryView::new(view_controller, cx)) |
| 801 | .into(), |
| 802 | Role::Border(_) => cx.new(|_| ShareBorderView).into(), |
| 803 | }; |
| 804 | cx.new(|cx| base::Root::new(view, window, cx).bg(transparent_black())) |
| 805 | }); |
| 806 | let placed = opened.ok().map(|handle| { |
| 807 | let handle = AnyWindowHandle::from(handle); |
| 808 | let native = handle |
| 809 | .update(cx, |_, window, _| platform::native_handle(window)) |
| 810 | .ok() |
| 811 | .flatten(); |
| 812 | if let Some(native) = native { |
| 813 | platform::prepare_toast_window(native); |
| 814 | if excluded_from_capture(role.surface()) { |
| 815 | platform::exclude_from_capture(native); |
| 816 | } |
| 817 | } |
| 818 | if matches!(role, Role::Border(_)) { |
| 819 | handle |
| 820 | .update(cx, |_, window, _| { |
| 821 | platform::make_window_click_through(window) |
| 822 | }) |
| 823 | .ok(); |
| 824 | } |
| 825 | Placed { |
| 826 | handle, |
| 827 | native, |
| 828 | rect: None, |
| 829 | anchor: None, |
| 830 | visible: true, |
| 831 | } |
| 832 | }); |
| 833 | controller.update(cx, |this, _| { |
| 834 | this.opening.remove(&role); |
| 835 | if let Some(placed) = placed { |
| 836 | this.windows.insert(role, placed); |
| 837 | } |
| 838 | }); |
| 839 | }); |
| 840 | } |
| 841 | } |