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