BlueGit

ict rusty-teams

Files

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

overlay.rs

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