sharing_status_indicator.rs

 1use call::ActiveCall;
 2use gpui::{
 3    color::Color,
 4    elements::{MouseEventHandler, Svg},
 5    Appearance, Element, ElementBox, Entity, MouseButton, MutableAppContext, RenderContext, View,
 6};
 7use settings::Settings;
 8
 9use crate::ToggleScreenSharing;
10
11pub fn init(cx: &mut MutableAppContext) {
12    let active_call = ActiveCall::global(cx);
13
14    let mut status_indicator = None;
15    cx.observe(&active_call, move |call, cx| {
16        if let Some(room) = call.read(cx).room() {
17            if room.read(cx).is_screen_sharing() {
18                if status_indicator.is_none() && cx.global::<Settings>().show_call_status_icon {
19                    status_indicator = Some(cx.add_status_bar_item(|_| SharingStatusIndicator));
20                }
21            } else if let Some((window_id, _)) = status_indicator.take() {
22                cx.remove_status_bar_item(window_id);
23            }
24        } else if let Some((window_id, _)) = status_indicator.take() {
25            cx.remove_status_bar_item(window_id);
26        }
27    })
28    .detach();
29}
30
31pub struct SharingStatusIndicator;
32
33impl Entity for SharingStatusIndicator {
34    type Event = ();
35}
36
37impl View for SharingStatusIndicator {
38    fn ui_name() -> &'static str {
39        "SharingStatusIndicator"
40    }
41
42    fn render(&mut self, cx: &mut RenderContext<'_, Self>) -> ElementBox {
43        let color = match cx.appearance {
44            Appearance::Light | Appearance::VibrantLight => Color::black(),
45            Appearance::Dark | Appearance::VibrantDark => Color::white(),
46        };
47
48        MouseEventHandler::<Self>::new(0, cx, |_, _| {
49            Svg::new("icons/disable_screen_sharing_12.svg")
50                .with_color(color)
51                .constrained()
52                .with_width(18.)
53                .aligned()
54                .boxed()
55        })
56        .on_click(MouseButton::Left, |_, cx| {
57            cx.dispatch_action(ToggleScreenSharing);
58        })
59        .boxed()
60    }
61}