mirror of
https://github.com/zed-industries/zed.git
synced 2025-02-05 18:25:57 +00:00
WIP: Start on ActiveCall
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
46b61feb9a
commit
04d194924e
2 changed files with 56 additions and 0 deletions
55
crates/room/src/active_call.rs
Normal file
55
crates/room/src/active_call.rs
Normal file
|
@ -0,0 +1,55 @@
|
|||
use crate::Room;
|
||||
use gpui::{Entity, ModelHandle, MutableAppContext};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ActiveCall {
|
||||
room: Option<ModelHandle<Room>>,
|
||||
}
|
||||
|
||||
impl Entity for ActiveCall {
|
||||
type Event = ();
|
||||
}
|
||||
|
||||
impl ActiveCall {
|
||||
pub fn global(cx: &mut MutableAppContext) -> ModelHandle<Self> {
|
||||
if cx.has_global::<ModelHandle<Self>>() {
|
||||
let active_call = cx.add_model(|_| ActiveCall::default());
|
||||
cx.set_global(active_call.clone());
|
||||
active_call
|
||||
} else {
|
||||
cx.global::<ModelHandle<Self>>().clone()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn observe<F>(cx: &mut MutableAppContext, mut callback: F) -> gpui::Subscription
|
||||
where
|
||||
F: 'static + FnMut(Option<ModelHandle<Room>>, &mut MutableAppContext),
|
||||
{
|
||||
cx.observe_default_global::<Option<ModelHandle<Room>>, _>(move |cx| {
|
||||
let room = cx.global::<Option<ModelHandle<Room>>>().clone();
|
||||
callback(room, cx);
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_or_create(
|
||||
client: &Arc<Client>,
|
||||
user_store: &ModelHandle<UserStore>,
|
||||
cx: &mut MutableAppContext,
|
||||
) -> Task<Result<ModelHandle<Room>>> {
|
||||
if let Some(room) = cx.global::<Option<ModelHandle<Room>>>() {
|
||||
Task::ready(Ok(room.clone()))
|
||||
} else {
|
||||
let client = client.clone();
|
||||
let user_store = user_store.clone();
|
||||
cx.spawn(|mut cx| async move {
|
||||
let room = cx.update(|cx| Room::create(client, user_store, cx)).await?;
|
||||
cx.update(|cx| cx.set_global(Some(room.clone())));
|
||||
Ok(room)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear(cx: &mut MutableAppContext) {
|
||||
cx.set_global::<Option<ModelHandle<Room>>>(None);
|
||||
}
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
mod active_call;
|
||||
mod participant;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
|
|
Loading…
Reference in a new issue