BlueGit

ict rusty-teams

Files

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

overlay.rs

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