Restructure event-handling methods in workspace ItemView
This commit is contained in:
parent
a95d33f662
commit
ae57178f3e
2 changed files with 20 additions and 37 deletions
|
@ -2,11 +2,7 @@ use super::{
|
||||||
buffer, movement, Anchor, Bias, Buffer, BufferElement, DisplayMap, DisplayPoint, Point,
|
buffer, movement, Anchor, Bias, Buffer, BufferElement, DisplayMap, DisplayPoint, Point,
|
||||||
ToOffset, ToPoint,
|
ToOffset, ToPoint,
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{settings::Settings, watch, workspace};
|
||||||
settings::Settings,
|
|
||||||
watch,
|
|
||||||
workspace::{self, ItemViewEvent},
|
|
||||||
};
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use futures_core::future::LocalBoxFuture;
|
use futures_core::future::LocalBoxFuture;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
|
@ -86,18 +82,6 @@ pub enum SelectAction {
|
||||||
End,
|
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 {
|
pub struct BufferView {
|
||||||
handle: WeakViewHandle<Self>,
|
handle: WeakViewHandle<Self>,
|
||||||
buffer: ModelHandle<Buffer>,
|
buffer: ModelHandle<Buffer>,
|
||||||
|
@ -1155,12 +1139,12 @@ impl workspace::Item for Buffer {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl workspace::ItemView for BufferView {
|
impl workspace::ItemView for BufferView {
|
||||||
fn to_workspace_event(event: &Self::Event) -> Option<ItemViewEvent> {
|
fn should_activate_item_on_event(event: &Self::Event) -> bool {
|
||||||
match event {
|
matches!(event, Event::Activate)
|
||||||
Event::Activate => Some(ItemViewEvent::Activated),
|
}
|
||||||
Event::Dirtied | Event::Saved => Some(ItemViewEvent::TabStateChanged),
|
|
||||||
_ => None,
|
fn should_update_tab_on_event(event: &Self::Event) -> bool {
|
||||||
}
|
matches!(event, Event::Saved | Event::Dirtied)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn title(&self, app: &AppContext) -> std::string::String {
|
fn title(&self, app: &AppContext) -> std::string::String {
|
||||||
|
|
|
@ -13,13 +13,7 @@ pub fn init(app: &mut App) {
|
||||||
app.add_bindings(vec![Binding::new("cmd-s", "workspace:save", None)]);
|
app.add_bindings(vec![Binding::new("cmd-s", "workspace:save", None)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum ItemViewEvent {
|
|
||||||
TabStateChanged,
|
|
||||||
Activated,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait ItemView: View {
|
pub trait ItemView: View {
|
||||||
fn to_workspace_event(event: &Self::Event) -> Option<ItemViewEvent>;
|
|
||||||
fn title(&self, app: &AppContext) -> String;
|
fn title(&self, app: &AppContext) -> String;
|
||||||
fn entry_id(&self, app: &AppContext) -> Option<(usize, usize)>;
|
fn entry_id(&self, app: &AppContext) -> Option<(usize, usize)>;
|
||||||
fn clone_on_split(&self, _: &mut ViewContext<Self>) -> Option<Self>
|
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<()>> {
|
fn save(&self, _: &mut ViewContext<Self>) -> LocalBoxFuture<'static, anyhow::Result<()>> {
|
||||||
Box::pin(async { Ok(()) })
|
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 {
|
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) {
|
fn set_parent_pane(&self, pane: &ViewHandle<Pane>, app: &mut MutableAppContext) {
|
||||||
pane.update(app, |_, ctx| {
|
pane.update(app, |_, ctx| {
|
||||||
ctx.subscribe_to_view(self, |pane, item, event, ctx| {
|
ctx.subscribe_to_view(self, |pane, item, event, ctx| {
|
||||||
match T::to_workspace_event(event) {
|
if T::should_activate_item_on_event(event) {
|
||||||
Some(ItemViewEvent::Activated) => {
|
if let Some(ix) = pane.item_index(&item) {
|
||||||
if let Some(ix) = pane.item_index(&item) {
|
pane.activate_item(ix, ctx);
|
||||||
pane.activate_item(ix, ctx);
|
pane.activate(ctx);
|
||||||
pane.activate(ctx);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Some(ItemViewEvent::TabStateChanged) => ctx.notify(),
|
}
|
||||||
_ => {}
|
if T::should_update_tab_on_event(event) {
|
||||||
|
ctx.notify()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue