2023-08-23 20:32:16 +00:00
|
|
|
use channel::channel_buffer::{self, ChannelBuffer};
|
2023-08-24 18:25:20 +00:00
|
|
|
use client::proto;
|
2023-08-23 01:08:03 +00:00
|
|
|
use clock::ReplicaId;
|
|
|
|
use collections::HashMap;
|
2023-08-22 21:18:32 +00:00
|
|
|
use editor::Editor;
|
|
|
|
use gpui::{
|
|
|
|
actions,
|
|
|
|
elements::{ChildView, Label},
|
2023-08-24 18:25:20 +00:00
|
|
|
AnyElement, AnyViewHandle, AppContext, Element, Entity, ModelHandle, Subscription, View,
|
|
|
|
ViewContext, ViewHandle,
|
2023-08-22 21:18:32 +00:00
|
|
|
};
|
|
|
|
use language::Language;
|
2023-08-23 01:08:03 +00:00
|
|
|
use project::Project;
|
2023-08-22 21:18:32 +00:00
|
|
|
use std::sync::Arc;
|
2023-08-24 18:25:20 +00:00
|
|
|
use workspace::{
|
|
|
|
item::{FollowableItem, Item, ItemHandle},
|
|
|
|
register_followable_item, ViewId,
|
|
|
|
};
|
2023-08-22 21:18:32 +00:00
|
|
|
|
|
|
|
actions!(channel_view, [Deploy]);
|
|
|
|
|
|
|
|
pub(crate) fn init(cx: &mut AppContext) {
|
2023-08-24 18:25:20 +00:00
|
|
|
register_followable_item::<ChannelView>(cx)
|
2023-08-22 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ChannelView {
|
2023-08-23 20:32:16 +00:00
|
|
|
pub editor: ViewHandle<Editor>,
|
2023-08-23 01:08:03 +00:00
|
|
|
project: ModelHandle<Project>,
|
2023-08-22 21:18:32 +00:00
|
|
|
channel_buffer: ModelHandle<ChannelBuffer>,
|
2023-08-24 18:25:20 +00:00
|
|
|
remote_id: Option<ViewId>,
|
|
|
|
_editor_event_subscription: Subscription,
|
2023-08-22 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ChannelView {
|
|
|
|
pub fn new(
|
2023-08-23 01:08:03 +00:00
|
|
|
project: ModelHandle<Project>,
|
2023-08-22 21:18:32 +00:00
|
|
|
channel_buffer: ModelHandle<ChannelBuffer>,
|
2023-08-23 01:08:03 +00:00
|
|
|
language: Option<Arc<Language>>,
|
2023-08-22 21:18:32 +00:00
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) -> Self {
|
|
|
|
let buffer = channel_buffer.read(cx).buffer();
|
2023-08-23 01:08:03 +00:00
|
|
|
buffer.update(cx, |buffer, cx| buffer.set_language(language, cx));
|
2023-08-22 21:18:32 +00:00
|
|
|
let editor = cx.add_view(|cx| Editor::for_buffer(buffer, None, cx));
|
2023-08-24 18:25:20 +00:00
|
|
|
let _editor_event_subscription = cx.subscribe(&editor, |_, _, e, cx| cx.emit(e.clone()));
|
|
|
|
|
2023-08-23 20:32:16 +00:00
|
|
|
cx.subscribe(&project, Self::handle_project_event).detach();
|
|
|
|
cx.subscribe(&channel_buffer, Self::handle_channel_buffer_event)
|
|
|
|
.detach();
|
|
|
|
|
2023-08-23 01:08:03 +00:00
|
|
|
let this = Self {
|
2023-08-22 21:18:32 +00:00
|
|
|
editor,
|
2023-08-23 01:08:03 +00:00
|
|
|
project,
|
2023-08-22 21:18:32 +00:00
|
|
|
channel_buffer,
|
2023-08-24 18:25:20 +00:00
|
|
|
remote_id: None,
|
|
|
|
_editor_event_subscription,
|
2023-08-23 01:08:03 +00:00
|
|
|
};
|
2023-08-23 20:32:16 +00:00
|
|
|
this.refresh_replica_id_map(cx);
|
2023-08-23 01:08:03 +00:00
|
|
|
this
|
|
|
|
}
|
|
|
|
|
2023-08-23 20:32:16 +00:00
|
|
|
fn handle_project_event(
|
|
|
|
&mut self,
|
|
|
|
_: ModelHandle<Project>,
|
|
|
|
event: &project::Event,
|
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) {
|
|
|
|
match event {
|
|
|
|
project::Event::RemoteIdChanged(_) => {}
|
|
|
|
project::Event::DisconnectedFromHost => {}
|
|
|
|
project::Event::Closed => {}
|
|
|
|
project::Event::CollaboratorUpdated { .. } => {}
|
|
|
|
project::Event::CollaboratorLeft(_) => {}
|
|
|
|
project::Event::CollaboratorJoined(_) => {}
|
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
self.refresh_replica_id_map(cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_channel_buffer_event(
|
|
|
|
&mut self,
|
|
|
|
_: ModelHandle<ChannelBuffer>,
|
|
|
|
_: &channel_buffer::Event,
|
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) {
|
|
|
|
self.refresh_replica_id_map(cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Build a mapping of channel buffer replica ids to the corresponding
|
|
|
|
/// replica ids in the current project.
|
|
|
|
///
|
|
|
|
/// Using this mapping, a given user can be displayed with the same color
|
|
|
|
/// in the channel buffer as in other files in the project. Users who are
|
|
|
|
/// in the channel buffer but not the project will not have a color.
|
|
|
|
fn refresh_replica_id_map(&self, cx: &mut ViewContext<Self>) {
|
|
|
|
let mut project_replica_ids_by_channel_buffer_replica_id = HashMap::default();
|
2023-08-23 01:08:03 +00:00
|
|
|
let project = self.project.read(cx);
|
2023-08-23 20:32:16 +00:00
|
|
|
let channel_buffer = self.channel_buffer.read(cx);
|
|
|
|
project_replica_ids_by_channel_buffer_replica_id
|
|
|
|
.insert(channel_buffer.replica_id(cx), project.replica_id());
|
|
|
|
project_replica_ids_by_channel_buffer_replica_id.extend(
|
|
|
|
channel_buffer
|
|
|
|
.collaborators()
|
|
|
|
.iter()
|
|
|
|
.filter_map(|channel_buffer_collaborator| {
|
|
|
|
project
|
|
|
|
.collaborators()
|
|
|
|
.values()
|
|
|
|
.find_map(|project_collaborator| {
|
|
|
|
(project_collaborator.user_id == channel_buffer_collaborator.user_id)
|
|
|
|
.then_some((
|
|
|
|
channel_buffer_collaborator.replica_id as ReplicaId,
|
|
|
|
project_collaborator.replica_id,
|
|
|
|
))
|
|
|
|
})
|
|
|
|
}),
|
2023-08-23 01:08:03 +00:00
|
|
|
);
|
2023-08-23 20:32:16 +00:00
|
|
|
|
|
|
|
self.editor.update(cx, |editor, cx| {
|
|
|
|
editor.set_replica_id_map(Some(project_replica_ids_by_channel_buffer_replica_id), cx)
|
|
|
|
});
|
2023-08-22 21:18:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Entity for ChannelView {
|
|
|
|
type Event = editor::Event;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl View for ChannelView {
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
"ChannelView"
|
|
|
|
}
|
|
|
|
|
2023-08-24 18:25:20 +00:00
|
|
|
fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
|
2023-08-22 21:18:32 +00:00
|
|
|
ChildView::new(self.editor.as_any(), cx).into_any()
|
|
|
|
}
|
2023-08-24 18:25:20 +00:00
|
|
|
|
|
|
|
fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
|
|
|
|
if cx.is_self_focused() {
|
|
|
|
cx.focus(self.editor.as_any())
|
|
|
|
}
|
|
|
|
}
|
2023-08-22 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Item for ChannelView {
|
|
|
|
fn tab_content<V: 'static>(
|
|
|
|
&self,
|
|
|
|
_: Option<usize>,
|
|
|
|
style: &theme::Tab,
|
|
|
|
cx: &gpui::AppContext,
|
|
|
|
) -> AnyElement<V> {
|
|
|
|
let channel_name = self
|
|
|
|
.channel_buffer
|
|
|
|
.read(cx)
|
|
|
|
.channel(cx)
|
|
|
|
.map_or("[Deleted channel]".to_string(), |channel| {
|
|
|
|
format!("#{}", channel.name)
|
|
|
|
});
|
|
|
|
Label::new(channel_name, style.label.to_owned()).into_any()
|
|
|
|
}
|
|
|
|
}
|
2023-08-24 18:25:20 +00:00
|
|
|
|
|
|
|
impl FollowableItem for ChannelView {
|
|
|
|
fn remote_id(&self) -> Option<workspace::ViewId> {
|
|
|
|
self.remote_id
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_state_proto(&self, cx: &AppContext) -> Option<proto::view::Variant> {
|
|
|
|
self.channel_buffer.read(cx).channel(cx).map(|channel| {
|
|
|
|
proto::view::Variant::ChannelView(proto::view::ChannelView {
|
|
|
|
channel_id: channel.id,
|
2023-08-24 19:36:01 +00:00
|
|
|
editor: if let Some(proto::view::Variant::Editor(proto)) =
|
|
|
|
self.editor.read(cx).to_state_proto(cx)
|
|
|
|
{
|
|
|
|
Some(proto)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
2023-08-24 18:25:20 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_state_proto(
|
|
|
|
_: ViewHandle<workspace::Pane>,
|
|
|
|
workspace: ViewHandle<workspace::Workspace>,
|
|
|
|
remote_id: workspace::ViewId,
|
2023-08-24 19:36:01 +00:00
|
|
|
state: &mut Option<proto::view::Variant>,
|
2023-08-24 18:25:20 +00:00
|
|
|
cx: &mut AppContext,
|
|
|
|
) -> Option<gpui::Task<anyhow::Result<ViewHandle<Self>>>> {
|
2023-08-24 19:36:01 +00:00
|
|
|
let Some(proto::view::Variant::ChannelView(_)) = state else { return None };
|
|
|
|
let Some(proto::view::Variant::ChannelView(state)) = state.take() else { unreachable!() };
|
2023-08-24 18:25:20 +00:00
|
|
|
|
|
|
|
let channel_store = &workspace.read(cx).app_state().channel_store.clone();
|
|
|
|
let open_channel_buffer = channel_store.update(cx, |store, cx| {
|
|
|
|
store.open_channel_buffer(state.channel_id, cx)
|
|
|
|
});
|
|
|
|
let project = workspace.read(cx).project().to_owned();
|
|
|
|
let language = workspace.read(cx).app_state().languages.clone();
|
|
|
|
let get_markdown = language.language_for_name("Markdown");
|
|
|
|
|
|
|
|
Some(cx.spawn(|mut cx| async move {
|
|
|
|
let channel_buffer = open_channel_buffer.await?;
|
|
|
|
let markdown = get_markdown.await?;
|
|
|
|
|
|
|
|
let this = workspace
|
|
|
|
.update(&mut cx, move |_, cx| {
|
|
|
|
cx.add_view(|cx| {
|
|
|
|
let mut this = Self::new(project, channel_buffer, Some(markdown), cx);
|
|
|
|
this.remote_id = Some(remote_id);
|
|
|
|
this
|
|
|
|
})
|
|
|
|
})
|
2023-08-24 19:36:01 +00:00
|
|
|
.ok_or_else(|| anyhow::anyhow!("workspace dropped"))?;
|
|
|
|
|
|
|
|
if let Some(state) = state.editor {
|
|
|
|
let task = this.update(&mut cx, |this, cx| {
|
|
|
|
this.editor.update(cx, |editor, cx| {
|
|
|
|
editor.apply_update_proto(
|
|
|
|
&this.project,
|
|
|
|
proto::update_view::Variant::Editor(proto::update_view::Editor {
|
|
|
|
selections: state.selections,
|
|
|
|
pending_selection: state.pending_selection,
|
|
|
|
scroll_top_anchor: state.scroll_top_anchor,
|
|
|
|
scroll_x: state.scroll_x,
|
|
|
|
scroll_y: state.scroll_y,
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
cx,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
if let Some(task) = task {
|
|
|
|
task.await?;
|
|
|
|
}
|
|
|
|
}
|
2023-08-24 18:25:20 +00:00
|
|
|
|
|
|
|
Ok(this)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_event_to_update_proto(
|
|
|
|
&self,
|
2023-08-24 19:36:01 +00:00
|
|
|
event: &Self::Event,
|
|
|
|
update: &mut Option<proto::update_view::Variant>,
|
|
|
|
cx: &AppContext,
|
2023-08-24 18:25:20 +00:00
|
|
|
) -> bool {
|
2023-08-24 19:36:01 +00:00
|
|
|
self.editor
|
|
|
|
.read(cx)
|
|
|
|
.add_event_to_update_proto(event, update, cx)
|
2023-08-24 18:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_update_proto(
|
|
|
|
&mut self,
|
2023-08-24 19:36:01 +00:00
|
|
|
project: &ModelHandle<Project>,
|
|
|
|
message: proto::update_view::Variant,
|
|
|
|
cx: &mut ViewContext<Self>,
|
2023-08-24 18:25:20 +00:00
|
|
|
) -> gpui::Task<anyhow::Result<()>> {
|
2023-08-24 19:36:01 +00:00
|
|
|
self.editor.update(cx, |editor, cx| {
|
|
|
|
editor.apply_update_proto(project, message, cx)
|
|
|
|
})
|
2023-08-24 18:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn set_leader_replica_id(
|
|
|
|
&mut self,
|
|
|
|
leader_replica_id: Option<u16>,
|
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) {
|
|
|
|
self.editor.update(cx, |editor, cx| {
|
|
|
|
editor.set_leader_replica_id(leader_replica_id, cx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn should_unfollow_on_event(event: &Self::Event, cx: &AppContext) -> bool {
|
|
|
|
Editor::should_unfollow_on_event(event, cx)
|
|
|
|
}
|
|
|
|
}
|