channel_view.rs

  1use anyhow::{anyhow, Result};
  2use channel::{
  3    channel_buffer::{self, ChannelBuffer},
  4    ChannelId,
  5};
  6use client::proto;
  7use clock::ReplicaId;
  8use collections::HashMap;
  9use editor::Editor;
 10use gpui::{
 11    actions,
 12    elements::{ChildView, Label},
 13    geometry::vector::Vector2F,
 14    AnyElement, AnyViewHandle, AppContext, Element, Entity, ModelHandle, Subscription, Task, View,
 15    ViewContext, ViewHandle,
 16};
 17use project::Project;
 18use std::any::Any;
 19use workspace::{
 20    item::{FollowableItem, Item, ItemHandle},
 21    register_followable_item,
 22    searchable::SearchableItemHandle,
 23    ItemNavHistory, Pane, ViewId, Workspace, WorkspaceId,
 24};
 25
 26actions!(channel_view, [Deploy]);
 27
 28pub(crate) fn init(cx: &mut AppContext) {
 29    register_followable_item::<ChannelView>(cx)
 30}
 31
 32pub struct ChannelView {
 33    pub editor: ViewHandle<Editor>,
 34    project: ModelHandle<Project>,
 35    channel_buffer: ModelHandle<ChannelBuffer>,
 36    remote_id: Option<ViewId>,
 37    _editor_event_subscription: Subscription,
 38}
 39
 40impl ChannelView {
 41    pub fn open(
 42        channel_id: ChannelId,
 43        pane: ViewHandle<Pane>,
 44        workspace: ViewHandle<Workspace>,
 45        cx: &mut AppContext,
 46    ) -> Task<Result<ViewHandle<Self>>> {
 47        let workspace = workspace.read(cx);
 48        let project = workspace.project().to_owned();
 49        let channel_store = workspace.app_state().channel_store.clone();
 50        let markdown = workspace
 51            .app_state()
 52            .languages
 53            .language_for_name("Markdown");
 54        let channel_buffer =
 55            channel_store.update(cx, |store, cx| store.open_channel_buffer(channel_id, cx));
 56
 57        cx.spawn(|mut cx| async move {
 58            let channel_buffer = channel_buffer.await?;
 59            let markdown = markdown.await?;
 60            channel_buffer.update(&mut cx, |buffer, cx| {
 61                buffer.buffer().update(cx, |buffer, cx| {
 62                    buffer.set_language(Some(markdown), cx);
 63                })
 64            });
 65
 66            pane.update(&mut cx, |pane, cx| {
 67                pane.items_of_type::<Self>()
 68                    .find(|channel_view| channel_view.read(cx).channel_buffer == channel_buffer)
 69                    .unwrap_or_else(|| cx.add_view(|cx| Self::new(project, channel_buffer, cx)))
 70            })
 71            .ok_or_else(|| anyhow!("pane was dropped"))
 72        })
 73    }
 74
 75    pub fn new(
 76        project: ModelHandle<Project>,
 77        channel_buffer: ModelHandle<ChannelBuffer>,
 78        cx: &mut ViewContext<Self>,
 79    ) -> Self {
 80        let buffer = channel_buffer.read(cx).buffer();
 81        // buffer.update(cx, |buffer, cx| buffer.set_language(language, cx));
 82        let editor = cx.add_view(|cx| Editor::for_buffer(buffer, None, cx));
 83        let _editor_event_subscription = cx.subscribe(&editor, |_, _, e, cx| cx.emit(e.clone()));
 84
 85        cx.subscribe(&project, Self::handle_project_event).detach();
 86        cx.subscribe(&channel_buffer, Self::handle_channel_buffer_event)
 87            .detach();
 88
 89        let this = Self {
 90            editor,
 91            project,
 92            channel_buffer,
 93            remote_id: None,
 94            _editor_event_subscription,
 95        };
 96        this.refresh_replica_id_map(cx);
 97        this
 98    }
 99
100    fn handle_project_event(
101        &mut self,
102        _: ModelHandle<Project>,
103        event: &project::Event,
104        cx: &mut ViewContext<Self>,
105    ) {
106        match event {
107            project::Event::RemoteIdChanged(_) => {}
108            project::Event::DisconnectedFromHost => {}
109            project::Event::Closed => {}
110            project::Event::CollaboratorUpdated { .. } => {}
111            project::Event::CollaboratorLeft(_) => {}
112            project::Event::CollaboratorJoined(_) => {}
113            _ => return,
114        }
115        self.refresh_replica_id_map(cx);
116    }
117
118    fn handle_channel_buffer_event(
119        &mut self,
120        _: ModelHandle<ChannelBuffer>,
121        event: &channel_buffer::Event,
122        cx: &mut ViewContext<Self>,
123    ) {
124        match event {
125            channel_buffer::Event::CollaboratorsChanged => {
126                self.refresh_replica_id_map(cx);
127            }
128            channel_buffer::Event::Disconnected => self.editor.update(cx, |editor, cx| {
129                editor.set_read_only(true);
130                cx.notify();
131            }),
132        }
133    }
134
135    /// Build a mapping of channel buffer replica ids to the corresponding
136    /// replica ids in the current project.
137    ///
138    /// Using this mapping, a given user can be displayed with the same color
139    /// in the channel buffer as in other files in the project. Users who are
140    /// in the channel buffer but not the project will not have a color.
141    fn refresh_replica_id_map(&self, cx: &mut ViewContext<Self>) {
142        let mut project_replica_ids_by_channel_buffer_replica_id = HashMap::default();
143        let project = self.project.read(cx);
144        let channel_buffer = self.channel_buffer.read(cx);
145        project_replica_ids_by_channel_buffer_replica_id
146            .insert(channel_buffer.replica_id(cx), project.replica_id());
147        project_replica_ids_by_channel_buffer_replica_id.extend(
148            channel_buffer
149                .collaborators()
150                .iter()
151                .filter_map(|channel_buffer_collaborator| {
152                    project
153                        .collaborators()
154                        .values()
155                        .find_map(|project_collaborator| {
156                            (project_collaborator.user_id == channel_buffer_collaborator.user_id)
157                                .then_some((
158                                    channel_buffer_collaborator.replica_id as ReplicaId,
159                                    project_collaborator.replica_id,
160                                ))
161                        })
162                }),
163        );
164
165        self.editor.update(cx, |editor, cx| {
166            editor.set_replica_id_map(Some(project_replica_ids_by_channel_buffer_replica_id), cx)
167        });
168    }
169}
170
171impl Entity for ChannelView {
172    type Event = editor::Event;
173}
174
175impl View for ChannelView {
176    fn ui_name() -> &'static str {
177        "ChannelView"
178    }
179
180    fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
181        ChildView::new(self.editor.as_any(), cx).into_any()
182    }
183
184    fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
185        if cx.is_self_focused() {
186            cx.focus(self.editor.as_any())
187        }
188    }
189}
190
191impl Item for ChannelView {
192    fn tab_content<V: 'static>(
193        &self,
194        _: Option<usize>,
195        style: &theme::Tab,
196        cx: &gpui::AppContext,
197    ) -> AnyElement<V> {
198        let channel_name = &self.channel_buffer.read(cx).channel().name;
199        let label = if self.channel_buffer.read(cx).is_connected() {
200            format!("#{}", channel_name)
201        } else {
202            format!("#{} (disconnected)", channel_name)
203        };
204        Label::new(label, style.label.to_owned()).into_any()
205    }
206
207    fn clone_on_split(&self, _: WorkspaceId, cx: &mut ViewContext<Self>) -> Option<Self> {
208        Some(Self::new(
209            self.project.clone(),
210            self.channel_buffer.clone(),
211            cx,
212        ))
213    }
214
215    fn is_singleton(&self, _cx: &AppContext) -> bool {
216        true
217    }
218
219    fn navigate(&mut self, data: Box<dyn Any>, cx: &mut ViewContext<Self>) -> bool {
220        self.editor
221            .update(cx, |editor, cx| editor.navigate(data, cx))
222    }
223
224    fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
225        self.editor
226            .update(cx, |editor, cx| Item::deactivated(editor, cx))
227    }
228
229    fn set_nav_history(&mut self, history: ItemNavHistory, cx: &mut ViewContext<Self>) {
230        self.editor
231            .update(cx, |editor, cx| Item::set_nav_history(editor, history, cx))
232    }
233
234    fn as_searchable(&self, _: &ViewHandle<Self>) -> Option<Box<dyn SearchableItemHandle>> {
235        Some(Box::new(self.editor.clone()))
236    }
237
238    fn show_toolbar(&self) -> bool {
239        true
240    }
241
242    fn pixel_position_of_cursor(&self, cx: &AppContext) -> Option<Vector2F> {
243        self.editor.read(cx).pixel_position_of_cursor(cx)
244    }
245}
246
247impl FollowableItem for ChannelView {
248    fn remote_id(&self) -> Option<workspace::ViewId> {
249        self.remote_id
250    }
251
252    fn to_state_proto(&self, cx: &AppContext) -> Option<proto::view::Variant> {
253        let channel = self.channel_buffer.read(cx).channel();
254        Some(proto::view::Variant::ChannelView(
255            proto::view::ChannelView {
256                channel_id: channel.id,
257                editor: if let Some(proto::view::Variant::Editor(proto)) =
258                    self.editor.read(cx).to_state_proto(cx)
259                {
260                    Some(proto)
261                } else {
262                    None
263                },
264            },
265        ))
266    }
267
268    fn from_state_proto(
269        pane: ViewHandle<workspace::Pane>,
270        workspace: ViewHandle<workspace::Workspace>,
271        remote_id: workspace::ViewId,
272        state: &mut Option<proto::view::Variant>,
273        cx: &mut AppContext,
274    ) -> Option<gpui::Task<anyhow::Result<ViewHandle<Self>>>> {
275        let Some(proto::view::Variant::ChannelView(_)) = state else {
276            return None;
277        };
278        let Some(proto::view::Variant::ChannelView(state)) = state.take() else {
279            unreachable!()
280        };
281
282        let open = ChannelView::open(state.channel_id, pane, workspace, cx);
283
284        Some(cx.spawn(|mut cx| async move {
285            let this = open.await?;
286
287            let task = this
288                .update(&mut cx, |this, cx| {
289                    this.remote_id = Some(remote_id);
290
291                    if let Some(state) = state.editor {
292                        Some(this.editor.update(cx, |editor, cx| {
293                            editor.apply_update_proto(
294                                &this.project,
295                                proto::update_view::Variant::Editor(proto::update_view::Editor {
296                                    selections: state.selections,
297                                    pending_selection: state.pending_selection,
298                                    scroll_top_anchor: state.scroll_top_anchor,
299                                    scroll_x: state.scroll_x,
300                                    scroll_y: state.scroll_y,
301                                    ..Default::default()
302                                }),
303                                cx,
304                            )
305                        }))
306                    } else {
307                        None
308                    }
309                })
310                .ok_or_else(|| anyhow!("window was closed"))?;
311
312            if let Some(task) = task {
313                task.await?;
314            }
315
316            Ok(this)
317        }))
318    }
319
320    fn add_event_to_update_proto(
321        &self,
322        event: &Self::Event,
323        update: &mut Option<proto::update_view::Variant>,
324        cx: &AppContext,
325    ) -> bool {
326        self.editor
327            .read(cx)
328            .add_event_to_update_proto(event, update, cx)
329    }
330
331    fn apply_update_proto(
332        &mut self,
333        project: &ModelHandle<Project>,
334        message: proto::update_view::Variant,
335        cx: &mut ViewContext<Self>,
336    ) -> gpui::Task<anyhow::Result<()>> {
337        self.editor.update(cx, |editor, cx| {
338            editor.apply_update_proto(project, message, cx)
339        })
340    }
341
342    fn set_leader_replica_id(
343        &mut self,
344        leader_replica_id: Option<u16>,
345        cx: &mut ViewContext<Self>,
346    ) {
347        self.editor.update(cx, |editor, cx| {
348            editor.set_leader_replica_id(leader_replica_id, cx)
349        })
350    }
351
352    fn should_unfollow_on_event(event: &Self::Event, cx: &AppContext) -> bool {
353        Editor::should_unfollow_on_event(event, cx)
354    }
355}