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        }
25    })
26    .detach();
27}
28
29pub struct SharingStatusIndicator;
30
31impl Entity for SharingStatusIndicator {
32    type Event = ();
33}
34
35impl View for SharingStatusIndicator {
36    fn ui_name() -> &'static str {
37        "SharingStatusIndicator"
38    }
39
40    fn render(&mut self, cx: &mut RenderContext<'_, Self>) -> ElementBox {
41        let color = match cx.appearance {
42            Appearance::Light | Appearance::VibrantLight => Color::black(),
43            Appearance::Dark | Appearance::VibrantDark => Color::white(),
44        };
45
46        MouseEventHandler::<Self>::new(0, cx, |_, _| {
47            Svg::new("icons/disable_screen_sharing_12.svg")
48                .with_color(color)
49                .constrained()
50                .with_width(18.)
51                .aligned()
52                .boxed()
53        })
54        .on_click(MouseButton::Left, |_, cx| {
55            cx.dispatch_action(ToggleScreenSharing);
56        })
57        .boxed()
58    }
59}