channel_view.rs

  1use anyhow::{anyhow, Result};
  2use call::report_call_event_for_channel;
  3use channel::{Channel, ChannelBuffer, ChannelBufferEvent, ChannelId, ChannelStore};
  4use client::{
  5    proto::{self, PeerId},
  6    Collaborator, ParticipantIndex,
  7};
  8use collections::HashMap;
  9use editor::{CollaborationHub, 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::{
 19    any::{Any, TypeId},
 20    sync::Arc,
 21};
 22use util::ResultExt;
 23use workspace::{
 24    item::{FollowableItem, Item, ItemHandle},
 25    register_followable_item,
 26    searchable::SearchableItemHandle,
 27    ItemNavHistory, Pane, ViewId, Workspace, WorkspaceId,
 28};
 29
 30actions!(channel_view, [Deploy]);
 31
 32pub fn init(cx: &mut AppContext) {
 33    register_followable_item::<ChannelView>(cx)
 34}
 35
 36pub struct ChannelView {
 37    pub editor: ViewHandle<Editor>,
 38    project: ModelHandle<Project>,
 39    channel_store: ModelHandle<ChannelStore>,
 40    channel_buffer: ModelHandle<ChannelBuffer>,
 41    remote_id: Option<ViewId>,
 42    _editor_event_subscription: Subscription,
 43}
 44
 45impl ChannelView {
 46    pub fn open(
 47        channel_id: ChannelId,
 48        workspace: ViewHandle<Workspace>,
 49        cx: &mut AppContext,
 50    ) -> Task<Result<ViewHandle<Self>>> {
 51        let pane = workspace.read(cx).active_pane().clone();
 52        let channel_view = Self::open_in_pane(channel_id, pane.clone(), workspace.clone(), cx);
 53        cx.spawn(|mut cx| async move {
 54            let channel_view = channel_view.await?;
 55            pane.update(&mut cx, |pane, cx| {
 56                report_call_event_for_channel(
 57                    "open channel notes",
 58                    channel_id,
 59                    &workspace.read(cx).app_state().client,
 60                    cx,
 61                );
 62                pane.add_item(Box::new(channel_view.clone()), true, true, None, cx);
 63            });
 64            anyhow::Ok(channel_view)
 65        })
 66    }
 67
 68    pub fn open_in_pane(
 69        channel_id: ChannelId,
 70        pane: ViewHandle<Pane>,
 71        workspace: ViewHandle<Workspace>,
 72        cx: &mut AppContext,
 73    ) -> Task<Result<ViewHandle<Self>>> {
 74        let workspace = workspace.read(cx);
 75        let project = workspace.project().to_owned();
 76        let channel_store = ChannelStore::global(cx);
 77        let markdown = workspace
 78            .app_state()
 79            .languages
 80            .language_for_name("Markdown");
 81        let channel_buffer =
 82            channel_store.update(cx, |store, cx| store.open_channel_buffer(channel_id, cx));
 83
 84        cx.spawn(|mut cx| async move {
 85            let channel_buffer = channel_buffer.await?;
 86
 87            if let Some(markdown) = markdown.await.log_err() {
 88                channel_buffer.update(&mut cx, |buffer, cx| {
 89                    buffer.buffer().update(cx, |buffer, cx| {
 90                        buffer.set_language(Some(markdown), cx);
 91                    })
 92                });
 93            }
 94
 95            pane.update(&mut cx, |pane, cx| {
 96                pane.items_of_type::<Self>()
 97                    .find(|channel_view| channel_view.read(cx).channel_buffer == channel_buffer)
 98                    .unwrap_or_else(|| {
 99                        cx.add_view(|cx| {
100                            let mut this = Self::new(project, channel_store, channel_buffer, cx);
101                            this.acknowledge_buffer_version(cx);
102                            this
103                        })
104                    })
105            })
106            .ok_or_else(|| anyhow!("pane was dropped"))
107        })
108    }
109
110    pub fn new(
111        project: ModelHandle<Project>,
112        channel_store: ModelHandle<ChannelStore>,
113        channel_buffer: ModelHandle<ChannelBuffer>,
114        cx: &mut ViewContext<Self>,
115    ) -> Self {
116        let buffer = channel_buffer.read(cx).buffer();
117        let editor = cx.add_view(|cx| {
118            let mut editor = Editor::for_buffer(buffer, None, cx);
119            editor.set_collaboration_hub(Box::new(ChannelBufferCollaborationHub(
120                channel_buffer.clone(),
121            )));
122            editor
123        });
124        let _editor_event_subscription = cx.subscribe(&editor, |_, _, e, cx| cx.emit(e.clone()));
125
126        cx.subscribe(&channel_buffer, Self::handle_channel_buffer_event)
127            .detach();
128
129        Self {
130            editor,
131            project,
132            channel_store,
133            channel_buffer,
134            remote_id: None,
135            _editor_event_subscription,
136        }
137    }
138
139    pub fn channel(&self, cx: &AppContext) -> Arc<Channel> {
140        self.channel_buffer.read(cx).channel()
141    }
142
143    fn handle_channel_buffer_event(
144        &mut self,
145        _: ModelHandle<ChannelBuffer>,
146        event: &ChannelBufferEvent,
147        cx: &mut ViewContext<Self>,
148    ) {
149        match event {
150            ChannelBufferEvent::Disconnected => self.editor.update(cx, |editor, cx| {
151                editor.set_read_only(true);
152                cx.notify();
153            }),
154            ChannelBufferEvent::BufferEdited => {
155                if cx.is_self_focused() || self.editor.is_focused(cx) {
156                    self.acknowledge_buffer_version(cx);
157                } else {
158                    self.channel_store.update(cx, |store, cx| {
159                        let channel_buffer = self.channel_buffer.read(cx);
160                        store.notes_changed(
161                            channel_buffer.channel().id,
162                            channel_buffer.epoch(),
163                            &channel_buffer.buffer().read(cx).version(),
164                            cx,
165                        )
166                    });
167                }
168            }
169            _ => {}
170        }
171    }
172
173    fn acknowledge_buffer_version(&mut self, cx: &mut ViewContext<'_, '_, ChannelView>) {
174        self.channel_store.update(cx, |store, cx| {
175            let channel_buffer = self.channel_buffer.read(cx);
176            store.acknowledge_notes_version(
177                channel_buffer.channel().id,
178                channel_buffer.epoch(),
179                &channel_buffer.buffer().read(cx).version(),
180                cx,
181            )
182        });
183        self.channel_buffer.update(cx, |buffer, cx| {
184            buffer.acknowledge_buffer_version(cx);
185        });
186    }
187}
188
189impl Entity for ChannelView {
190    type Event = editor::Event;
191}
192
193impl View for ChannelView {
194    fn ui_name() -> &'static str {
195        "ChannelView"
196    }
197
198    fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
199        ChildView::new(self.editor.as_any(), cx).into_any()
200    }
201
202    fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
203        if cx.is_self_focused() {
204            self.acknowledge_buffer_version(cx);
205            cx.focus(self.editor.as_any())
206        }
207    }
208}
209
210impl Item for ChannelView {
211    fn act_as_type<'a>(
212        &'a self,
213        type_id: TypeId,
214        self_handle: &'a ViewHandle<Self>,
215        _: &'a AppContext,
216    ) -> Option<&'a AnyViewHandle> {
217        if type_id == TypeId::of::<Self>() {
218            Some(self_handle)
219        } else if type_id == TypeId::of::<Editor>() {
220            Some(&self.editor)
221        } else {
222            None
223        }
224    }
225
226    fn tab_content<V: 'static>(
227        &self,
228        _: Option<usize>,
229        style: &theme::Tab,
230        cx: &gpui::AppContext,
231    ) -> AnyElement<V> {
232        let channel_name = &self.channel_buffer.read(cx).channel().name;
233        let label = if self.channel_buffer.read(cx).is_connected() {
234            format!("#{}", channel_name)
235        } else {
236            format!("#{} (disconnected)", channel_name)
237        };
238        Label::new(label, style.label.to_owned()).into_any()
239    }
240
241    fn clone_on_split(&self, _: WorkspaceId, cx: &mut ViewContext<Self>) -> Option<Self> {
242        Some(Self::new(
243            self.project.clone(),
244            self.channel_store.clone(),
245            self.channel_buffer.clone(),
246            cx,
247        ))
248    }
249
250    fn is_singleton(&self, _cx: &AppContext) -> bool {
251        false
252    }
253
254    fn navigate(&mut self, data: Box<dyn Any>, cx: &mut ViewContext<Self>) -> bool {
255        self.editor
256            .update(cx, |editor, cx| editor.navigate(data, cx))
257    }
258
259    fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
260        self.editor
261            .update(cx, |editor, cx| Item::deactivated(editor, cx))
262    }
263
264    fn set_nav_history(&mut self, history: ItemNavHistory, cx: &mut ViewContext<Self>) {
265        self.editor
266            .update(cx, |editor, cx| Item::set_nav_history(editor, history, cx))
267    }
268
269    fn as_searchable(&self, _: &ViewHandle<Self>) -> Option<Box<dyn SearchableItemHandle>> {
270        Some(Box::new(self.editor.clone()))
271    }
272
273    fn show_toolbar(&self) -> bool {
274        true
275    }
276
277    fn pixel_position_of_cursor(&self, cx: &AppContext) -> Option<Vector2F> {
278        self.editor.read(cx).pixel_position_of_cursor(cx)
279    }
280}
281
282impl FollowableItem for ChannelView {
283    fn remote_id(&self) -> Option<workspace::ViewId> {
284        self.remote_id
285    }
286
287    fn to_state_proto(&self, cx: &AppContext) -> Option<proto::view::Variant> {
288        let channel = self.channel_buffer.read(cx).channel();
289        Some(proto::view::Variant::ChannelView(
290            proto::view::ChannelView {
291                channel_id: channel.id,
292                editor: if let Some(proto::view::Variant::Editor(proto)) =
293                    self.editor.read(cx).to_state_proto(cx)
294                {
295                    Some(proto)
296                } else {
297                    None
298                },
299            },
300        ))
301    }
302
303    fn from_state_proto(
304        pane: ViewHandle<workspace::Pane>,
305        workspace: ViewHandle<workspace::Workspace>,
306        remote_id: workspace::ViewId,
307        state: &mut Option<proto::view::Variant>,
308        cx: &mut AppContext,
309    ) -> Option<gpui::Task<anyhow::Result<ViewHandle<Self>>>> {
310        let Some(proto::view::Variant::ChannelView(_)) = state else {
311            return None;
312        };
313        let Some(proto::view::Variant::ChannelView(state)) = state.take() else {
314            unreachable!()
315        };
316
317        let open = ChannelView::open_in_pane(state.channel_id, pane, workspace, cx);
318
319        Some(cx.spawn(|mut cx| async move {
320            let this = open.await?;
321
322            let task = this
323                .update(&mut cx, |this, cx| {
324                    this.remote_id = Some(remote_id);
325
326                    if let Some(state) = state.editor {
327                        Some(this.editor.update(cx, |editor, cx| {
328                            editor.apply_update_proto(
329                                &this.project,
330                                proto::update_view::Variant::Editor(proto::update_view::Editor {
331                                    selections: state.selections,
332                                    pending_selection: state.pending_selection,
333                                    scroll_top_anchor: state.scroll_top_anchor,
334                                    scroll_x: state.scroll_x,
335                                    scroll_y: state.scroll_y,
336                                    ..Default::default()
337                                }),
338                                cx,
339                            )
340                        }))
341                    } else {
342                        None
343                    }
344                })
345                .ok_or_else(|| anyhow!("window was closed"))?;
346
347            if let Some(task) = task {
348                task.await?;
349            }
350
351            Ok(this)
352        }))
353    }
354
355    fn add_event_to_update_proto(
356        &self,
357        event: &Self::Event,
358        update: &mut Option<proto::update_view::Variant>,
359        cx: &AppContext,
360    ) -> bool {
361        self.editor
362            .read(cx)
363            .add_event_to_update_proto(event, update, cx)
364    }
365
366    fn apply_update_proto(
367        &mut self,
368        project: &ModelHandle<Project>,
369        message: proto::update_view::Variant,
370        cx: &mut ViewContext<Self>,
371    ) -> gpui::Task<anyhow::Result<()>> {
372        self.editor.update(cx, |editor, cx| {
373            editor.apply_update_proto(project, message, cx)
374        })
375    }
376
377    fn set_leader_peer_id(&mut self, leader_peer_id: Option<PeerId>, cx: &mut ViewContext<Self>) {
378        self.editor.update(cx, |editor, cx| {
379            editor.set_leader_peer_id(leader_peer_id, cx)
380        })
381    }
382
383    fn should_unfollow_on_event(event: &Self::Event, cx: &AppContext) -> bool {
384        Editor::should_unfollow_on_event(event, cx)
385    }
386
387    fn is_project_item(&self, _cx: &AppContext) -> bool {
388        false
389    }
390}
391
392struct ChannelBufferCollaborationHub(ModelHandle<ChannelBuffer>);
393
394impl CollaborationHub for ChannelBufferCollaborationHub {
395    fn collaborators<'a>(&self, cx: &'a AppContext) -> &'a HashMap<PeerId, Collaborator> {
396        self.0.read(cx).collaborators()
397    }
398
399    fn user_participant_indices<'a>(
400        &self,
401        cx: &'a AppContext,
402    ) -> &'a HashMap<u64, ParticipantIndex> {
403        self.0.read(cx).user_store().read(cx).participant_indices()
404    }
405}