Add call status indicator to the status bar

This commit is contained in:
Kay Simmons 2023-01-31 15:00:49 -08:00
parent a89cc22af4
commit e35db69dbd
4 changed files with 79 additions and 23 deletions

View file

@ -1,3 +1,4 @@
mod indicator;
pub mod participant;
pub mod room;
@ -5,18 +6,22 @@ use anyhow::{anyhow, Result};
use client::{proto, Client, TypedEnvelope, User, UserStore};
use collections::HashSet;
use gpui::{
AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext,
Subscription, Task, WeakModelHandle,
actions, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext,
Subscription, Task, ViewHandle, WeakModelHandle,
};
use indicator::SharingStatusIndicator;
pub use participant::ParticipantLocation;
use postage::watch;
use project::Project;
pub use room::Room;
use std::sync::Arc;
actions!(collab, [ToggleScreenSharing]);
pub fn init(client: Arc<Client>, user_store: ModelHandle<UserStore>, cx: &mut MutableAppContext) {
let active_call = cx.add_model(|cx| ActiveCall::new(client, user_store, cx));
cx.set_global(active_call);
cx.add_global_action(toggle_screen_sharing);
}
#[derive(Clone)]
@ -37,6 +42,7 @@ pub struct ActiveCall {
),
client: Arc<Client>,
user_store: ModelHandle<UserStore>,
sharing_status_indicator: Option<(usize, ViewHandle<SharingStatusIndicator>)>,
_subscriptions: Vec<client::Subscription>,
}
@ -61,6 +67,7 @@ impl ActiveCall {
],
client,
user_store,
sharing_status_indicator: None,
}
}
@ -279,6 +286,8 @@ impl ActiveCall {
this.set_room(None, cx).detach_and_log_err(cx);
}
this.set_sharing_status(room.read(cx).is_screen_sharing(), cx);
cx.notify();
}),
cx.subscribe(&room, |_, _, event, cx| cx.emit(event.clone())),
@ -303,4 +312,28 @@ impl ActiveCall {
pub fn pending_invites(&self) -> &HashSet<u64> {
&self.pending_invites
}
pub fn set_sharing_status(&mut self, is_screen_sharing: bool, cx: &mut MutableAppContext) {
if is_screen_sharing {
if self.sharing_status_indicator.is_none() {
self.sharing_status_indicator =
Some(cx.add_status_bar_item(|_| SharingStatusIndicator));
}
} else if let Some((window_id, _)) = self.sharing_status_indicator.take() {
cx.remove_status_bar_item(window_id);
}
}
}
pub fn toggle_screen_sharing(_: &ToggleScreenSharing, cx: &mut MutableAppContext) {
if let Some(room) = ActiveCall::global(cx).read(cx).room().cloned() {
let toggle_screen_sharing = room.update(cx, |room, cx| {
if room.is_screen_sharing() {
Task::ready(room.unshare_screen(cx))
} else {
room.share_screen(cx)
}
});
toggle_screen_sharing.detach_and_log_err(cx);
}
}

View file

@ -0,0 +1,39 @@
use gpui::{
color::Color,
elements::{MouseEventHandler, Svg},
Appearance, Element, ElementBox, Entity, MouseButton, RenderContext, View,
};
use crate::ToggleScreenSharing;
pub struct SharingStatusIndicator;
impl Entity for SharingStatusIndicator {
type Event = ();
}
impl View for SharingStatusIndicator {
fn ui_name() -> &'static str {
"SharingStatusIndicator"
}
fn render(&mut self, cx: &mut RenderContext<'_, Self>) -> ElementBox {
let color = match cx.appearance {
Appearance::Light | Appearance::VibrantLight => Color::black(),
Appearance::Dark | Appearance::VibrantDark => Color::white(),
};
MouseEventHandler::<Self>::new(0, cx, |_, _| {
Svg::new("icons/disable_screen_sharing_12.svg")
.with_color(color)
.constrained()
.with_width(18.)
.aligned()
.boxed()
})
.on_click(MouseButton::Left, |_, cx| {
cx.dispatch_action(ToggleScreenSharing);
})
.boxed()
}
}