mirror of
https://github.com/zed-industries/zed.git
synced 2025-02-08 19:43:11 +00:00
Remove model and view specific observe/subscribe methods
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
5ecedd894d
commit
fa0739ee2e
5 changed files with 24 additions and 58 deletions
|
@ -2097,40 +2097,6 @@ impl<'a, T: View> ViewContext<'a, T> {
|
||||||
self.app.add_option_view(self.window_id, build_view)
|
self.app.add_option_view(self.window_id, build_view)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn subscribe_to_model<E, F>(&mut self, handle: &ModelHandle<E>, callback: F)
|
|
||||||
where
|
|
||||||
E: Entity,
|
|
||||||
E::Event: 'static,
|
|
||||||
F: 'static + FnMut(&mut T, ModelHandle<E>, &E::Event, &mut ViewContext<T>),
|
|
||||||
{
|
|
||||||
self.subscribe(handle, callback)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn subscribe_to_view<V, F>(&mut self, handle: &ViewHandle<V>, callback: F)
|
|
||||||
where
|
|
||||||
V: View,
|
|
||||||
V::Event: 'static,
|
|
||||||
F: 'static + FnMut(&mut T, ViewHandle<V>, &V::Event, &mut ViewContext<T>),
|
|
||||||
{
|
|
||||||
self.subscribe(handle, callback)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn observe_model<S, F>(&mut self, handle: &ModelHandle<S>, callback: F)
|
|
||||||
where
|
|
||||||
S: Entity,
|
|
||||||
F: 'static + FnMut(&mut T, ModelHandle<S>, &mut ViewContext<T>),
|
|
||||||
{
|
|
||||||
self.observe(handle, callback)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn observe_view<S, F>(&mut self, handle: &ViewHandle<S>, callback: F)
|
|
||||||
where
|
|
||||||
S: View,
|
|
||||||
F: 'static + FnMut(&mut T, ViewHandle<S>, &mut ViewContext<T>),
|
|
||||||
{
|
|
||||||
self.observe(handle, callback)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn subscribe<E, H, F>(&mut self, handle: &H, mut callback: F)
|
pub fn subscribe<E, H, F>(&mut self, handle: &H, mut callback: F)
|
||||||
where
|
where
|
||||||
E: Entity,
|
E: Entity,
|
||||||
|
@ -2152,7 +2118,7 @@ impl<'a, T: View> ViewContext<'a, T> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn observe<E, F, H>(&mut self, handle: &H, mut callback: F)
|
pub fn observe<E, F, H>(&mut self, handle: &H, mut callback: F)
|
||||||
where
|
where
|
||||||
E: Entity,
|
E: Entity,
|
||||||
H: Handle<E>,
|
H: Handle<E>,
|
||||||
|
@ -2594,14 +2560,14 @@ impl<T: View> ViewHandle<T> {
|
||||||
|
|
||||||
let mut cx = cx.cx.borrow_mut();
|
let mut cx = cx.cx.borrow_mut();
|
||||||
self.update(&mut *cx, |_, cx| {
|
self.update(&mut *cx, |_, cx| {
|
||||||
cx.observe_view(self, {
|
cx.observe(self, {
|
||||||
let mut tx = tx.clone();
|
let mut tx = tx.clone();
|
||||||
move |_, _, _| {
|
move |_, _, _| {
|
||||||
tx.blocking_send(()).ok();
|
tx.blocking_send(()).ok();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
cx.subscribe_to_view(self, {
|
cx.subscribe(self, {
|
||||||
let mut tx = tx.clone();
|
let mut tx = tx.clone();
|
||||||
move |_, _, _, _| {
|
move |_, _, _, _| {
|
||||||
tx.blocking_send(()).ok();
|
tx.blocking_send(()).ok();
|
||||||
|
@ -3153,7 +3119,7 @@ mod tests {
|
||||||
impl View {
|
impl View {
|
||||||
fn new(other: Option<ViewHandle<View>>, cx: &mut ViewContext<Self>) -> Self {
|
fn new(other: Option<ViewHandle<View>>, cx: &mut ViewContext<Self>) -> Self {
|
||||||
if let Some(other) = other.as_ref() {
|
if let Some(other) = other.as_ref() {
|
||||||
cx.subscribe_to_view(other, |me, _, event, _| {
|
cx.subscribe(other, |me, _, event, _| {
|
||||||
me.events.push(format!("observed event {}", event));
|
me.events.push(format!("observed event {}", event));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -3327,15 +3293,15 @@ mod tests {
|
||||||
let handle_3 = cx.add_model(|_| Model);
|
let handle_3 = cx.add_model(|_| Model);
|
||||||
|
|
||||||
handle_1.update(cx, |_, c| {
|
handle_1.update(cx, |_, c| {
|
||||||
c.subscribe_to_view(&handle_2, move |me, _, event, c| {
|
c.subscribe(&handle_2, move |me, _, event, c| {
|
||||||
me.events.push(*event);
|
me.events.push(*event);
|
||||||
|
|
||||||
c.subscribe_to_view(&handle_2b, |me, _, event, _| {
|
c.subscribe(&handle_2b, |me, _, event, _| {
|
||||||
me.events.push(*event * 2);
|
me.events.push(*event * 2);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
c.subscribe_to_model(&handle_3, |me, _, event, _| {
|
c.subscribe(&handle_3, |me, _, event, _| {
|
||||||
me.events.push(*event);
|
me.events.push(*event);
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
@ -3381,8 +3347,8 @@ mod tests {
|
||||||
let observed_model = cx.add_model(|_| Model);
|
let observed_model = cx.add_model(|_| Model);
|
||||||
|
|
||||||
observing_view.update(cx, |_, cx| {
|
observing_view.update(cx, |_, cx| {
|
||||||
cx.subscribe_to_view(&emitting_view, |_, _, _, _| {});
|
cx.subscribe(&emitting_view, |_, _, _, _| {});
|
||||||
cx.subscribe_to_model(&observed_model, |_, _, _, _| {});
|
cx.subscribe(&observed_model, |_, _, _, _| {});
|
||||||
});
|
});
|
||||||
observing_model.update(cx, |_, cx| {
|
observing_model.update(cx, |_, cx| {
|
||||||
cx.subscribe(&observed_model, |_, _, _, _| {});
|
cx.subscribe(&observed_model, |_, _, _, _| {});
|
||||||
|
@ -3431,7 +3397,7 @@ mod tests {
|
||||||
let model = cx.add_model(|_| Model::default());
|
let model = cx.add_model(|_| Model::default());
|
||||||
|
|
||||||
view.update(cx, |_, c| {
|
view.update(cx, |_, c| {
|
||||||
c.observe_model(&model, |me, observed, c| {
|
c.observe(&model, |me, observed, c| {
|
||||||
me.events.push(observed.read(c).count)
|
me.events.push(observed.read(c).count)
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -3473,7 +3439,7 @@ mod tests {
|
||||||
let observed_model = cx.add_model(|_| Model);
|
let observed_model = cx.add_model(|_| Model);
|
||||||
|
|
||||||
observing_view.update(cx, |_, cx| {
|
observing_view.update(cx, |_, cx| {
|
||||||
cx.observe_model(&observed_model, |_, _, _| {});
|
cx.observe(&observed_model, |_, _, _| {});
|
||||||
});
|
});
|
||||||
observing_model.update(cx, |_, cx| {
|
observing_model.update(cx, |_, cx| {
|
||||||
cx.observe(&observed_model, |_, _, _| {});
|
cx.observe(&observed_model, |_, _, _| {});
|
||||||
|
|
|
@ -324,9 +324,9 @@ impl Editor {
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let display_map =
|
let display_map =
|
||||||
cx.add_model(|cx| DisplayMap::new(buffer.clone(), settings.borrow().clone(), None, cx));
|
cx.add_model(|cx| DisplayMap::new(buffer.clone(), settings.borrow().clone(), None, cx));
|
||||||
cx.observe_model(&buffer, Self::on_buffer_changed);
|
cx.observe(&buffer, Self::on_buffer_changed);
|
||||||
cx.subscribe_to_model(&buffer, Self::on_buffer_event);
|
cx.subscribe(&buffer, Self::on_buffer_event);
|
||||||
cx.observe_model(&display_map, Self::on_display_map_changed);
|
cx.observe(&display_map, Self::on_display_map_changed);
|
||||||
|
|
||||||
let mut next_selection_id = 0;
|
let mut next_selection_id = 0;
|
||||||
let selection_set_id = buffer.update(cx, |buffer, cx| {
|
let selection_set_id = buffer.update(cx, |buffer, cx| {
|
||||||
|
|
|
@ -252,7 +252,7 @@ impl FileFinder {
|
||||||
workspace.toggle_modal(cx, |cx, workspace| {
|
workspace.toggle_modal(cx, |cx, workspace| {
|
||||||
let handle = cx.handle();
|
let handle = cx.handle();
|
||||||
let finder = cx.add_view(|cx| Self::new(workspace.settings.clone(), handle, cx));
|
let finder = cx.add_view(|cx| Self::new(workspace.settings.clone(), handle, cx));
|
||||||
cx.subscribe_to_view(&finder, Self::on_event);
|
cx.subscribe(&finder, Self::on_event);
|
||||||
finder
|
finder
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -281,10 +281,10 @@ impl FileFinder {
|
||||||
workspace: ViewHandle<Workspace>,
|
workspace: ViewHandle<Workspace>,
|
||||||
cx: &mut ViewContext<Self>,
|
cx: &mut ViewContext<Self>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
cx.observe_view(&workspace, Self::workspace_updated);
|
cx.observe(&workspace, Self::workspace_updated);
|
||||||
|
|
||||||
let query_buffer = cx.add_view(|cx| Editor::single_line(settings.clone(), cx));
|
let query_buffer = cx.add_view(|cx| Editor::single_line(settings.clone(), cx));
|
||||||
cx.subscribe_to_view(&query_buffer, Self::on_query_editor_event);
|
cx.subscribe(&query_buffer, Self::on_query_editor_event);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
handle: cx.handle().downgrade(),
|
handle: cx.handle().downgrade(),
|
||||||
|
|
|
@ -61,7 +61,7 @@ impl ThemeSelector {
|
||||||
cx: &mut ViewContext<Self>,
|
cx: &mut ViewContext<Self>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let query_buffer = cx.add_view(|cx| Editor::single_line(settings.clone(), cx));
|
let query_buffer = cx.add_view(|cx| Editor::single_line(settings.clone(), cx));
|
||||||
cx.subscribe_to_view(&query_buffer, Self::on_query_editor_event);
|
cx.subscribe(&query_buffer, Self::on_query_editor_event);
|
||||||
|
|
||||||
let mut this = Self {
|
let mut this = Self {
|
||||||
settings,
|
settings,
|
||||||
|
@ -86,7 +86,7 @@ impl ThemeSelector {
|
||||||
cx,
|
cx,
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
cx.subscribe_to_view(&selector, Self::on_event);
|
cx.subscribe(&selector, Self::on_event);
|
||||||
selector
|
selector
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -279,7 +279,7 @@ impl<T: ItemView> ItemViewHandle for ViewHandle<T> {
|
||||||
|
|
||||||
fn set_parent_pane(&self, pane: &ViewHandle<Pane>, cx: &mut MutableAppContext) {
|
fn set_parent_pane(&self, pane: &ViewHandle<Pane>, cx: &mut MutableAppContext) {
|
||||||
pane.update(cx, |_, cx| {
|
pane.update(cx, |_, cx| {
|
||||||
cx.subscribe_to_view(self, |pane, item, event, cx| {
|
cx.subscribe(self, |pane, item, event, cx| {
|
||||||
if T::should_activate_item_on_event(event) {
|
if T::should_activate_item_on_event(event) {
|
||||||
if let Some(ix) = pane.item_index(&item) {
|
if let Some(ix) = pane.item_index(&item) {
|
||||||
pane.activate_item(ix, cx);
|
pane.activate_item(ix, cx);
|
||||||
|
@ -358,7 +358,7 @@ impl Workspace {
|
||||||
pub fn new(app_state: &AppState, cx: &mut ViewContext<Self>) -> Self {
|
pub fn new(app_state: &AppState, cx: &mut ViewContext<Self>) -> Self {
|
||||||
let pane = cx.add_view(|_| Pane::new(app_state.settings.clone()));
|
let pane = cx.add_view(|_| Pane::new(app_state.settings.clone()));
|
||||||
let pane_id = pane.id();
|
let pane_id = pane.id();
|
||||||
cx.subscribe_to_view(&pane, move |me, _, event, cx| {
|
cx.subscribe(&pane, move |me, _, event, cx| {
|
||||||
me.handle_pane_event(pane_id, event, cx)
|
me.handle_pane_event(pane_id, event, cx)
|
||||||
});
|
});
|
||||||
cx.focus(&pane);
|
cx.focus(&pane);
|
||||||
|
@ -526,7 +526,7 @@ impl Workspace {
|
||||||
cx.spawn(|this, mut cx| async move {
|
cx.spawn(|this, mut cx| async move {
|
||||||
let worktree = Worktree::open_local(path, languages, fs, &mut cx).await?;
|
let worktree = Worktree::open_local(path, languages, fs, &mut cx).await?;
|
||||||
this.update(&mut cx, |this, cx| {
|
this.update(&mut cx, |this, cx| {
|
||||||
cx.observe_model(&worktree, |_, _, cx| cx.notify());
|
cx.observe(&worktree, |_, _, cx| cx.notify());
|
||||||
this.worktrees.insert(worktree.clone());
|
this.worktrees.insert(worktree.clone());
|
||||||
cx.notify();
|
cx.notify();
|
||||||
});
|
});
|
||||||
|
@ -835,7 +835,7 @@ impl Workspace {
|
||||||
Worktree::open_remote(rpc.clone(), worktree_id, access_token, languages, &mut cx)
|
Worktree::open_remote(rpc.clone(), worktree_id, access_token, languages, &mut cx)
|
||||||
.await?;
|
.await?;
|
||||||
this.update(&mut cx, |workspace, cx| {
|
this.update(&mut cx, |workspace, cx| {
|
||||||
cx.observe_model(&worktree, |_, _, cx| cx.notify());
|
cx.observe(&worktree, |_, _, cx| cx.notify());
|
||||||
workspace.worktrees.insert(worktree);
|
workspace.worktrees.insert(worktree);
|
||||||
cx.notify();
|
cx.notify();
|
||||||
});
|
});
|
||||||
|
@ -854,7 +854,7 @@ impl Workspace {
|
||||||
fn add_pane(&mut self, cx: &mut ViewContext<Self>) -> ViewHandle<Pane> {
|
fn add_pane(&mut self, cx: &mut ViewContext<Self>) -> ViewHandle<Pane> {
|
||||||
let pane = cx.add_view(|_| Pane::new(self.settings.clone()));
|
let pane = cx.add_view(|_| Pane::new(self.settings.clone()));
|
||||||
let pane_id = pane.id();
|
let pane_id = pane.id();
|
||||||
cx.subscribe_to_view(&pane, move |me, _, event, cx| {
|
cx.subscribe(&pane, move |me, _, event, cx| {
|
||||||
me.handle_pane_event(pane_id, event, cx)
|
me.handle_pane_event(pane_id, event, cx)
|
||||||
});
|
});
|
||||||
self.panes.push(pane.clone());
|
self.panes.push(pane.clone());
|
||||||
|
|
Loading…
Reference in a new issue