Re-emit notifications and events from ActiveCall

This lets us only observe and subscribe to the active call without
needing to track the underlying `Room` if it changes, which implies
writing the same boilerplate over and over.
This commit is contained in:
Antonio Scandurra 2022-10-04 18:15:56 +02:00
parent 41240351d3
commit ebee2168fc
4 changed files with 35 additions and 71 deletions

View file

@ -30,7 +30,6 @@ pub fn init(cx: &mut MutableAppContext) {
pub struct CollabTitlebarItem {
workspace: WeakViewHandle<Workspace>,
contacts_popover: Option<ViewHandle<ContactsPopover>>,
room_subscription: Option<Subscription>,
_subscriptions: Vec<Subscription>,
}
@ -73,24 +72,12 @@ impl CollabTitlebarItem {
let active_call = ActiveCall::global(cx);
let mut subscriptions = Vec::new();
subscriptions.push(cx.observe(workspace, |_, _, cx| cx.notify()));
subscriptions.push(cx.observe(&active_call, |this, _, cx| this.active_call_changed(cx)));
let mut this = Self {
subscriptions.push(cx.observe(&active_call, |_, _, cx| cx.notify()));
Self {
workspace: workspace.downgrade(),
contacts_popover: None,
room_subscription: None,
_subscriptions: subscriptions,
};
this.active_call_changed(cx);
this
}
fn active_call_changed(&mut self, cx: &mut ViewContext<Self>) {
if let Some(room) = ActiveCall::global(cx).read(cx).room().cloned() {
self.room_subscription = Some(cx.observe(&room, |_, _, cx| cx.notify()));
} else {
self.room_subscription = None;
}
cx.notify();
}
fn share_project(&mut self, _: &ShareProject, cx: &mut ViewContext<Self>) {

View file

@ -80,7 +80,6 @@ pub enum Event {
}
pub struct ContactsPopover {
room_subscription: Option<Subscription>,
entries: Vec<ContactEntry>,
match_candidates: Vec<StringMatchCandidate>,
list_state: ListState,
@ -159,10 +158,9 @@ impl ContactsPopover {
let active_call = ActiveCall::global(cx);
let mut subscriptions = Vec::new();
subscriptions.push(cx.observe(&user_store, |this, _, cx| this.update_entries(cx)));
subscriptions.push(cx.observe(&active_call, |this, _, cx| this.active_call_changed(cx)));
subscriptions.push(cx.observe(&active_call, |_, _, cx| cx.notify()));
let mut this = Self {
room_subscription: None,
list_state,
selection: None,
collapsed_sections: Default::default(),
@ -173,19 +171,9 @@ impl ContactsPopover {
user_store,
};
this.update_entries(cx);
this.active_call_changed(cx);
this
}
fn active_call_changed(&mut self, cx: &mut ViewContext<Self>) {
if let Some(room) = ActiveCall::global(cx).read(cx).room().cloned() {
self.room_subscription = Some(cx.observe(&room, |_, _, cx| cx.notify()));
} else {
self.room_subscription = None;
}
cx.notify();
}
fn clear_filter(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
let did_clear = self.filter_editor.update(cx, |editor, cx| {
if editor.buffer().read(cx).len(cx) > 0 {

View file

@ -19,35 +19,18 @@ pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
cx.add_action(ProjectSharedNotification::dismiss);
let active_call = ActiveCall::global(cx);
let mut _room_subscription = None;
cx.observe(&active_call, move |active_call, cx| {
if let Some(room) = active_call.read(cx).room().cloned() {
let app_state = app_state.clone();
_room_subscription = Some(cx.subscribe(&room, move |_, event, cx| match event {
room::Event::RemoteProjectShared { owner, project_id } => {
cx.add_window(
WindowOptions {
bounds: WindowBounds::Fixed(RectF::new(
vec2f(0., 0.),
vec2f(300., 400.),
)),
titlebar: None,
center: true,
kind: WindowKind::PopUp,
is_movable: false,
},
|_| {
ProjectSharedNotification::new(
*project_id,
owner.clone(),
app_state.clone(),
)
},
);
}
}));
} else {
_room_subscription = None;
cx.subscribe(&active_call, move |_, event, cx| match event {
room::Event::RemoteProjectShared { owner, project_id } => {
cx.add_window(
WindowOptions {
bounds: WindowBounds::Fixed(RectF::new(vec2f(0., 0.), vec2f(300., 400.))),
titlebar: None,
center: true,
kind: WindowKind::PopUp,
is_movable: false,
},
|_| ProjectSharedNotification::new(*project_id, owner.clone(), app_state.clone()),
);
}
})
.detach();