BlueGit

ict rusty-teams

Files

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

overlay.rs

overlay.rs 27.1 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_window::{
22 CALL_WINDOW_MIN_SIZE, CALL_WINDOW_SIZE, CallWindowView, call_window_title,
23};
24use crate::views::call_share_gallery::ShareGalleryView;
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.update(cx, |_, window, _| window.activate_window()).ok();
288 }
289 }
290
291 fn stage_wanted(&self, cx: &App) -> bool {
292 let state = self.app.read(cx);
293 state.call.is_some() || state.call_end.is_some() || state.prejoin.is_some()
294 }
295
296 fn sync_stage(&mut self, cx: &mut Context<Self>) {
297 let wanted = self.stage_wanted(cx);
298 if self.stage_closed_by_user {
299 if !wanted {
300 self.stage_closed_by_user = false;
301 } else if self.app.read(cx).call.is_none() {
302 self.app.update(cx, |state, cx| state.dismiss_call_end(cx));
303 }
304 return;
305 }
306 match (wanted, self.stage) {
307 (true, None) => self.open_stage(cx),
308 (false, Some(stage)) => {
309 self.forget_stage();
310 stage.update(cx, |_, window, _| window.remove_window()).ok();
311 }
312 (true, Some(stage)) => {
313 if stage.update(cx, |_, _, _| ()).is_err() {
314 self.forget_stage();
315 self.stage_closed_by_user = true;
316 }
317 }
318 (false, None) => {}
319 }
320 }
321
322 fn forget_stage(&mut self) {
323 self.stage = None;
324 self.stage_native = None;
325 self.minimized = false;
326 self.minimized_for_share = false;
327 self.seen_viewing = None;
328 }
329
330 fn open_stage(&mut self, cx: &mut Context<Self>) {
331 if self.stage_opening {
332 return;
333 }
334 self.stage_opening = true;
335 let controller = cx.entity();
336 let app = self.app.clone();
337 let mut options = gpui_kit::component::TitleBar::window_options();
338 options.window_bounds = Some(WindowBounds::Windowed(Bounds::centered(
339 None,
340 size(px(CALL_WINDOW_SIZE.0), px(CALL_WINDOW_SIZE.1)),
341 cx,
342 )));
343 options.window_min_size = Some(size(px(CALL_WINDOW_MIN_SIZE.0), px(CALL_WINDOW_MIN_SIZE.1)));
344 if let Some(titlebar) = options.titlebar.as_mut() {
345 titlebar.title = Some(call_window_title(self.app.read(cx)).into());
346 }
347 // open_window draws right away, and the view reads the app entity while rendering.
348 cx.defer(move |cx| {
349 let opened = gpui_kit::open_window(options, cx, move |window, cx| {
350 cx.new(|cx| CallWindowView::new(app, window, cx))
351 });
352 let stage = opened.ok().map(|(handle, _)| handle);
353 let native = stage.and_then(|stage| {
354 stage
355 .update(cx, |_, window, _| platform::native_handle(window))
356 .ok()
357 .flatten()
358 });
359 controller.update(cx, |this, _| {
360 this.stage_opening = false;
361 this.stage = stage;
362 this.stage_native = native;
363 });
364 });
365 }
366
367 fn sync_viewing(&mut self, cx: &mut Context<Self>) {
368 let Some(viewing) = self.app.read(cx).call.as_ref().map(|call| call.viewing) else {
369 return;
370 };
371 let Some(stage) = self.stage else {
372 return;
373 };
374 match self.seen_viewing {
375 Some(seen) if seen == viewing => {
376 if self.stage_native.is_some() && viewing == self.minimized {
377 self.seen_viewing = Some(!viewing);
378 self.app.update(cx, |state, cx| state.show_call(!viewing, cx));
379 }
380 }
381 _ => {
382 self.seen_viewing = Some(viewing);
383 if viewing && self.minimized {
384 self.raise_stage(cx);
385 } else if !viewing && !self.minimized {
386 stage.update(cx, |_, window, _| window.minimize_window()).ok();
387 }
388 }
389 }
390 }
391
392 fn idle(&self, cx: &App) -> bool {
393 !self.stage_wanted(cx)
394 && self.stage.is_none()
395 && !self.stage_opening
396 && !self.stage_closed_by_user
397 && self.share.is_none()
398 && self.windows.is_empty()
399 && self.opening.is_empty()
400 }
401
402 fn tick(&mut self, cx: &mut Context<Self>) -> Duration {
403 if self.idle(cx) {
404 return IDLE_TICK;
405 }
406 self.ticks = self.ticks.wrapping_add(1);
407 let slow = self.ticks % SLOW_TICK_EVERY == 0;
408 self.sync_stage(cx);
409 self.refresh_stage_window();
410 self.sync_viewing(cx);
411 self.sync_share(slow, cx);
412 self.sync_pop_out(cx);
413 self.sync_monitor(cx);
414 if slow {
415 self.track_position(Role::Mini, cx);
416 self.track_position(Role::Gallery, cx);
417 }
418 TICK
419 }
420
421 fn refresh_stage_window(&mut self) {
422 let Some(native) = self.stage_native.or(self.main_native) else {
423 return;
424 };
425 let minimized = self.stage_native.is_some_and(platform::window_minimized);
426 if self.minimized && !minimized {
427 self.minimized_for_share = false;
428 }
429 self.minimized = minimized;
430 if !self.minimized {
431 self.work_area = platform::work_area(native).or(self.work_area);
432 }
433 }
434
435 fn local_share(&self, cx: &App) -> Option<Option<ShareSource>> {
436 let model = &self.app.read(cx).call.as_ref()?.model;
437 let label = model.local_share.as_ref()?;
438 Some(
439 model
440 .share_sources
441 .iter()
442 .find(|source| &source.label() == label)
443 .cloned(),
444 )
445 }
446
447 fn sync_share(&mut self, refresh_geometry: bool, cx: &mut Context<Self>) {
448 let Some(source) = self.local_share(cx) else {
449 self.end_share(cx);
450 return;
451 };
452 let mut refresh_geometry = refresh_geometry;
453 if self.share.is_none() {
454 self.bar_idle_since = Instant::now();
455 self.share = Some(ShareSession {
456 started: Instant::now(),
457 source: None,
458 geometry: None,
459 minimize_checked: false,
460 });
461 refresh_geometry = true;
462 }
463 let Some(session) = self.share.as_mut() else {
464 return;
465 };
466 session.source = source;
467 let shared_window = session
468 .source
469 .as_ref()
470 .filter(|source| source.kind == ShareKind::Window)
471 .map(|source| source.id as NativeHandle);
472 if refresh_geometry || shared_window.is_some() {
473 session.geometry = session
474 .source
475 .as_ref()
476 .and_then(|source| platform::share_geometry(source.kind, source.id));
477 }
478 let geometry = session.geometry;
479 let elapsed_seconds = session.started.elapsed().as_secs();
480 let check_minimize = !std::mem::replace(&mut session.minimize_checked, true);
481 if check_minimize {
482 self.minimize_for_share(geometry.map(|geometry| geometry.target), cx);
483 }
484 let Some(work) = geometry
485 .map(|geometry| geometry.work)
486 .or(self.work_area)
487 .or_else(|| display_work_area(cx))
488 else {
489 return;
490 };
491 let can_collapse = self
492 .windows
493 .get(&Role::Bar)
494 .is_some_and(|placed| placed.native.is_some());
495 let collapsed = can_collapse && bar_collapsed(self.pinned, self.bar_idle_since.elapsed());
496 if collapsed != self.bar_collapsed || elapsed_seconds != self.shown_second {
497 self.bar_collapsed = collapsed;
498 self.shown_second = elapsed_seconds;
499 cx.notify();
500 }
501 self.ensure_window(Role::Bar, bar_rect(work, collapsed), work.scale, true, cx);
502 match border_visibility(shared_window.is_some(), geometry.is_some()) {
503 BorderVisibility::Show => {
504 let thickness = border_thickness(work.scale);
505 let rects = geometry.map(|geometry| border_rects(geometry.target, thickness));
506 for (side, rect) in rects.into_iter().flatten().enumerate() {
507 if let Some(placed) = self.windows.get_mut(&Role::Border(side)) {
508 placed.anchor = shared_window;
509 placed.set_visible(true);
510 }
511 self.ensure_window(Role::Border(side), rect, work.scale, true, cx);
512 }
513 self.restack_borders(shared_window);
514 }
515 BorderVisibility::Hide => {
516 for side in 0..BORDER_SIDES {
517 if let Some(placed) = self.windows.get_mut(&Role::Border(side)) {
518 placed.set_visible(false);
519 }
520 }
521 }
522 BorderVisibility::Close => self.close_borders(cx),
523 }
524 }
525
526 fn local_share_mode(&self) -> LocalShare {
527 let kind = self
528 .share
529 .as_ref()
530 .and_then(|session| session.source.as_ref())
531 .map(|source| source.kind);
532 LocalShare::of(kind, self.share.is_some())
533 }
534
535 fn minimize_for_share(&mut self, shared: Option<ScreenRect>, cx: &mut Context<Self>) {
536 let Some(native) = self.stage_native.filter(|_| !self.minimized) else {
537 return;
538 };
539 if !minimize_on_share_start(
540 self.local_share_mode(),
541 platform::window_rect(native),
542 shared,
543 ) {
544 return;
545 }
546 self.minimized_for_share = true;
547 if let Some(stage) = self.stage {
548 stage.update(cx, |_, window, _| window.minimize_window()).ok();
549 }
550 }
551
552 fn end_share(&mut self, cx: &mut Context<Self>) {
553 if self.share.take().is_none() {
554 return;
555 }
556 if std::mem::take(&mut self.minimized_for_share) && self.minimized {
557 self.return_to_call(cx);
558 }
559 self.bar_collapsed = false;
560 self.close(Role::Bar, cx);
561 self.close_borders(cx);
562 }
563
564 fn close_borders(&mut self, cx: &mut Context<Self>) {
565 for side in 0..BORDER_SIDES {
566 self.close(Role::Border(side), cx);
567 }
568 }
569
570 fn sync_pop_out(&mut self, cx: &mut Context<Self>) {
571 let wanted = self
572 .app
573 .read(cx)
574 .call
575 .as_ref()
576 .is_some_and(|call| call.share_stage.popped && call.model.screen_sharer.is_some());
577 match (wanted, self.pop_out) {
578 (true, None) => self.open_pop_out(cx),
579 (false, Some(handle)) => {
580 self.pop_out = None;
581 handle
582 .update(cx, |_, window, _| window.remove_window())
583 .ok();
584 }
585 (true, Some(handle)) => {
586 if handle.update(cx, |_, _, _| ()).is_err() {
587 self.pop_out = None;
588 self.app
589 .update(cx, |state, cx| state.return_stage_from_pop_out(cx));
590 }
591 }
592 (false, None) => {}
593 }
594 }
595
596 fn open_pop_out(&mut self, cx: &mut Context<Self>) {
597 if self.pop_out_opening {
598 return;
599 }
600 self.pop_out_opening = true;
601 let controller = cx.entity();
602 let app = self.app.clone();
603 let options = WindowOptions {
604 window_bounds: Some(WindowBounds::Windowed(Bounds::centered(
605 None,
606 size(px(POP_OUT_SIZE.0), px(POP_OUT_SIZE.1)),
607 cx,
608 ))),
609 titlebar: Some(TitlebarOptions {
610 title: Some(POP_OUT_TITLE.into()),
611 ..Default::default()
612 }),
613 ..Default::default()
614 };
615 // open_window draws right away, and the view reads the app entity while rendering.
616 cx.defer(move |cx| {
617 let opened = gpui_kit::open_window(options, cx, move |window, cx| {
618 cx.new(|cx| SharePopOutView::new(app, window, cx))
619 });
620 controller.update(cx, |this, _| {
621 this.pop_out_opening = false;
622 this.pop_out = opened.ok().map(|(handle, _)| handle);
623 });
624 });
625 }
626
627 fn sync_monitor(&mut self, cx: &mut Context<Self>) {
628 let in_call = self.app.read(cx).call.is_some();
629 let plan = overlay_plan(
630 in_call,
631 self.local_share_mode(),
632 self.minimized,
633 self.speaker_only,
634 );
635 let work = self.work_area.or_else(|| display_work_area(cx));
636 match (plan.monitor, work) {
637 (Monitor::Mini, Some(main_work)) => {
638 self.close(Role::Gallery, cx);
639 let work = mini_work_area(main_work, self.mini_saved, platform::work_area_at);
640 let rect = mini_rect(work, self.mini_saved);
641 self.ensure_window(Role::Mini, rect, work.scale, false, cx);
642 }
643 (Monitor::Gallery, Some(main_work)) => {
644 self.close(Role::Mini, cx);
645 self.place_gallery(main_work, cx);
646 }
647 _ => {
648 self.close(Role::Mini, cx);
649 self.close(Role::Gallery, cx);
650 }
651 }
652 }
653
654 fn place_gallery(&mut self, main_work: WorkArea, cx: &mut Context<Self>) {
655 let work = mini_work_area(main_work, self.gallery_saved, platform::work_area_at);
656 let (others, self_tile) = self.app.read(cx).call.as_ref().map_or((0, true), |call| {
657 (
658 gallery_candidates(&call.model).len(),
659 !call.model.view.hide_me,
660 )
661 });
662 let layout = gallery_layout(others, self_tile, gallery_available_height(work));
663 if layout != self.gallery_layout {
664 self.gallery_layout = layout;
665 cx.notify();
666 }
667 let origin = self
668 .windows
669 .get(&Role::Gallery)
670 .and_then(|placed| placed.native)
671 .and_then(platform::window_rect)
672 .map(|rect| (rect.left, rect.top))
673 .or(self.gallery_saved);
674 let rect = gallery_rect(work, layout, origin);
675 match self.windows.get_mut(&Role::Gallery) {
676 Some(placed) => {
677 let size = |rect: ScreenRect| (rect.width(), rect.height());
678 if placed.rect.map(size) != Some(size(rect)) {
679 placed.place(rect, cx);
680 }
681 }
682 None => self.open(Role::Gallery, rect, work.scale, cx),
683 }
684 }
685
686 fn track_position(&mut self, role: Role, cx: &mut Context<Self>) {
687 let Some(native) = self.windows.get(&role).and_then(|placed| placed.native) else {
688 return;
689 };
690 let Some(rect) = platform::window_rect(native) else {
691 return;
692 };
693 let (key, saved, seen) = match role {
694 Role::Mini => (MINI_POSITION_KEY, &mut self.mini_saved, &mut self.mini_seen),
695 Role::Gallery => (
696 GALLERY_POSITION_KEY,
697 &mut self.gallery_saved,
698 &mut self.gallery_seen,
699 ),
700 Role::Bar | Role::Border(_) => return,
701 };
702 let position = (rect.left, rect.top);
703 let settled = *seen == Some(position);
704 *seen = Some(position);
705 if settled && *saved != Some(position) {
706 *saved = Some(position);
707 let _ = self
708 .app
709 .read(cx)
710 .store
711 .set_meta(key, &format_position(position.0, position.1));
712 }
713 }
714
715 fn restack_borders(&self, shared_window: Option<NativeHandle>) {
716 let Some(shared) = shared_window else {
717 return;
718 };
719 let group: Vec<NativeHandle> = (0..BORDER_SIDES)
720 .filter_map(|side| self.windows.get(&Role::Border(side))?.native)
721 .collect();
722 platform::stack_group_above(shared, &group);
723 }
724
725 fn ensure_window(
726 &mut self,
727 role: Role,
728 rect: ScreenRect,
729 scale: f32,
730 follow: bool,
731 cx: &mut Context<Self>,
732 ) {
733 match self.windows.get_mut(&role) {
734 Some(placed) => {
735 if placed.rect.is_none() || follow && placed.rect != Some(rect) {
736 placed.place(rect, cx);
737 }
738 }
739 None => self.open(role, rect, scale, cx),
740 }
741 }
742
743 fn close(&mut self, role: Role, cx: &mut Context<Self>) {
744 if let Some(placed) = self.windows.remove(&role) {
745 placed
746 .handle
747 .update(cx, |_, window, _| window.remove_window())
748 .ok();
749 }
750 }
751
752 fn open(&mut self, role: Role, rect: ScreenRect, scale: f32, cx: &mut Context<Self>) {
753 if !self.opening.insert(role) {
754 return;
755 }
756 let controller = cx.entity();
757 let options = overlay_options(role, rect, scale);
758 // open_window draws right away, and the views read this entity while rendering.
759 cx.defer(move |cx| {
760 let view_controller = controller.clone();
761 let opened = cx.open_window(options, move |window, cx| {
762 let view: AnyView = match role {
763 Role::Bar => cx.new(|cx| ShareBarView::new(view_controller, cx)).into(),
764 Role::Mini => cx.new(|cx| MiniWindowView::new(view_controller, cx)).into(),
765 Role::Gallery => cx
766 .new(|cx| ShareGalleryView::new(view_controller, cx))
767 .into(),
768 Role::Border(_) => cx.new(|_| ShareBorderView).into(),
769 };
770 cx.new(|cx| base::Root::new(view, window, cx).bg(transparent_black()))
771 });
772 let placed = opened.ok().map(|handle| {
773 let handle = AnyWindowHandle::from(handle);
774 let native = handle
775 .update(cx, |_, window, _| platform::native_handle(window))
776 .ok()
777 .flatten();
778 if let Some(native) = native {
779 platform::prepare_toast_window(native);
780 if excluded_from_capture(role.surface()) {
781 platform::exclude_from_capture(native);
782 }
783 }
784 if matches!(role, Role::Border(_)) {
785 handle
786 .update(cx, |_, window, _| {
787 platform::make_window_click_through(window)
788 })
789 .ok();
790 }
791 Placed {
792 handle,
793 native,
794 rect: None,
795 anchor: None,
796 visible: true,
797 }
798 });
799 controller.update(cx, |this, _| {
800 this.opening.remove(&role);
801 if let Some(placed) = placed {
802 this.windows.insert(role, placed);
803 }
804 });
805 });
806 }
807}