Restructure event-handling methods in workspace ItemView

This commit is contained in:
Max Brunsfeld 2021-04-07 15:04:08 -07:00
parent a95d33f662
commit ae57178f3e
2 changed files with 20 additions and 37 deletions

View file

@ -2,11 +2,7 @@ use super::{
buffer, movement, Anchor, Bias, Buffer, BufferElement, DisplayMap, DisplayPoint, Point,
ToOffset, ToPoint,
};
use crate::{
settings::Settings,
watch,
workspace::{self, ItemViewEvent},
};
use crate::{settings::Settings, watch, workspace};
use anyhow::Result;
use futures_core::future::LocalBoxFuture;
use gpui::{
@ -86,18 +82,6 @@ pub enum SelectAction {
End,
}
// impl workspace::Item for Buffer {
// type View = BufferView;
// fn build_view(
// buffer: ModelHandle<Self>,
// settings: watch::Receiver<Settings>,
// ctx: &mut ViewContext<Self::View>,
// ) -> Self::View {
// BufferView::for_buffer(buffer, settings, ctx)
// }
// }
pub struct BufferView {
handle: WeakViewHandle<Self>,
buffer: ModelHandle<Buffer>,
@ -1155,12 +1139,12 @@ impl workspace::Item for Buffer {
}
impl workspace::ItemView for BufferView {
fn to_workspace_event(event: &Self::Event) -> Option<ItemViewEvent> {
match event {
Event::Activate => Some(ItemViewEvent::Activated),
Event::Dirtied | Event::Saved => Some(ItemViewEvent::TabStateChanged),
_ => None,
}
fn should_activate_item_on_event(event: &Self::Event) -> bool {
matches!(event, Event::Activate)
}
fn should_update_tab_on_event(event: &Self::Event) -> bool {
matches!(event, Event::Saved | Event::Dirtied)
}
fn title(&self, app: &AppContext) -> std::string::String {

View file

@ -13,13 +13,7 @@ pub fn init(app: &mut App) {
app.add_bindings(vec![Binding::new("cmd-s", "workspace:save", None)]);
}
pub enum ItemViewEvent {
TabStateChanged,
Activated,
}
pub trait ItemView: View {
fn to_workspace_event(event: &Self::Event) -> Option<ItemViewEvent>;
fn title(&self, app: &AppContext) -> String;
fn entry_id(&self, app: &AppContext) -> Option<(usize, usize)>;
fn clone_on_split(&self, _: &mut ViewContext<Self>) -> Option<Self>
@ -34,6 +28,12 @@ pub trait ItemView: View {
fn save(&self, _: &mut ViewContext<Self>) -> LocalBoxFuture<'static, anyhow::Result<()>> {
Box::pin(async { Ok(()) })
}
fn should_activate_item_on_event(_: &Self::Event) -> bool {
false
}
fn should_update_tab_on_event(_: &Self::Event) -> bool {
false
}
}
pub trait ItemViewHandle: Send + Sync {
@ -71,15 +71,14 @@ impl<T: ItemView> ItemViewHandle for ViewHandle<T> {
fn set_parent_pane(&self, pane: &ViewHandle<Pane>, app: &mut MutableAppContext) {
pane.update(app, |_, ctx| {
ctx.subscribe_to_view(self, |pane, item, event, ctx| {
match T::to_workspace_event(event) {
Some(ItemViewEvent::Activated) => {
if let Some(ix) = pane.item_index(&item) {
pane.activate_item(ix, ctx);
pane.activate(ctx);
}
if T::should_activate_item_on_event(event) {
if let Some(ix) = pane.item_index(&item) {
pane.activate_item(ix, ctx);
pane.activate(ctx);
}
Some(ItemViewEvent::TabStateChanged) => ctx.notify(),
_ => {}
}
if T::should_update_tab_on_event(event) {
ctx.notify()
}
})
})