sharing_status_indicator.rs

 1use crate::toggle_screen_sharing;
 2use call::ActiveCall;
 3use gpui::{
 4    color::Color,
 5    elements::{MouseEventHandler, Svg},
 6    platform::{Appearance, MouseButton},
 7    AnyElement, AppContext, Element, Entity, View, ViewContext,
 8};
 9use workspace::WorkspaceSettings;
10
11pub fn init(cx: &mut AppContext) {
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()
19                    && settings::get::<WorkspaceSettings>(cx).show_call_status_icon
20                {
21                    status_indicator = Some(cx.add_status_bar_item(|_| SharingStatusIndicator));
22                }
23            } else if let Some(window) = status_indicator.take() {
24                window.update(cx, |cx| cx.remove_window());
25            }
26        } else if let Some(window) = status_indicator.take() {
27            window.update(cx, |cx| cx.remove_window());
28        }
29    })
30    .detach();
31}
32
33pub struct SharingStatusIndicator;
34
35impl Entity for SharingStatusIndicator {
36    type Event = ();
37}
38
39impl View for SharingStatusIndicator {
40    fn ui_name() -> &'static str {
41        "SharingStatusIndicator"
42    }
43
44    fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
45        let color = match cx.window_appearance() {
46            Appearance::Light | Appearance::VibrantLight => Color::black(),
47            Appearance::Dark | Appearance::VibrantDark => Color::white(),
48        };
49
50        MouseEventHandler::new::<Self, _>(0, cx, |_, _| {
51            Svg::new("icons/disable_screen_sharing_12.svg")
52                .with_color(color)
53                .constrained()
54                .with_width(18.)
55                .aligned()
56        })
57        .on_click(MouseButton::Left, |_, _, cx| {
58            toggle_screen_sharing(&Default::default(), cx)
59        })
60        .into_any()
61    }
62}