From a5cb4c6d52dd61efe47925ad6cb2eb299420eee4 Mon Sep 17 00:00:00 2001 From: Mikayla Date: Wed, 9 Aug 2023 08:54:24 -0700 Subject: [PATCH] Fix selections and enter-to-create-file --- crates/collab_ui/src/collab_panel.rs | 61 ++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/crates/collab_ui/src/collab_panel.rs b/crates/collab_ui/src/collab_panel.rs index 85e0d80cce4dccf203b47a6d6e0af2ef3e3cefbf..b3b43dbabec735da5e7e338aab3629a4fc86d0f1 100644 --- a/crates/collab_ui/src/collab_panel.rs +++ b/crates/collab_ui/src/collab_panel.rs @@ -197,7 +197,7 @@ impl CollabPanel { if !query.is_empty() { this.selection.take(); } - this.update_entries(cx); + this.update_entries(false, cx); if !query.is_empty() { this.selection = this .entries @@ -220,7 +220,7 @@ impl CollabPanel { cx.subscribe(&channel_name_editor, |this, _, event, cx| { if let editor::Event::Blurred = event { this.take_editing_state(cx); - this.update_entries(cx); + this.update_entries(false, cx); cx.notify(); } }) @@ -358,7 +358,7 @@ impl CollabPanel { list_state, }; - this.update_entries(cx); + this.update_entries(false, cx); // Update the dock position when the setting changes. let mut old_dock_position = this.position(cx); @@ -376,13 +376,18 @@ impl CollabPanel { let active_call = ActiveCall::global(cx); this.subscriptions - .push(cx.observe(&this.user_store, |this, _, cx| this.update_entries(cx))); - this.subscriptions - .push(cx.observe(&this.channel_store, |this, _, cx| this.update_entries(cx))); + .push(cx.observe(&this.user_store, |this, _, cx| { + this.update_entries(false, cx) + })); this.subscriptions - .push(cx.observe(&active_call, |this, _, cx| this.update_entries(cx))); + .push(cx.observe(&this.channel_store, |this, _, cx| { + this.update_entries(false, cx) + })); this.subscriptions - .push(cx.observe_global::(move |this, cx| this.update_entries(cx))); + .push(cx.observe(&active_call, |this, _, cx| this.update_entries(false, cx))); + this.subscriptions.push( + cx.observe_global::(move |this, cx| this.update_entries(false, cx)), + ); this }) @@ -434,7 +439,7 @@ impl CollabPanel { ); } - fn update_entries(&mut self, cx: &mut ViewContext) { + fn update_entries(&mut self, select_editor: bool, cx: &mut ViewContext) { let channel_store = self.channel_store.read(cx); let user_store = self.user_store.read(cx); let query = self.filter_editor.read(cx).text(cx); @@ -743,14 +748,23 @@ impl CollabPanel { } } - if let Some(prev_selected_entry) = prev_selected_entry { - self.selection.take(); + if select_editor { for (ix, entry) in self.entries.iter().enumerate() { - if *entry == prev_selected_entry { + if matches!(*entry, ListEntry::ChannelEditor { .. }) { self.selection = Some(ix); break; } } + } else { + if let Some(prev_selected_entry) = prev_selected_entry { + self.selection.take(); + for (ix, entry) in self.entries.iter().enumerate() { + if *entry == prev_selected_entry { + self.selection = Some(ix); + break; + } + } + } } let old_scroll_top = self.list_state.logical_scroll_top(); @@ -1643,7 +1657,7 @@ impl CollabPanel { }); } - self.update_entries(cx); + self.update_entries(false, cx); } fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext) { @@ -1724,17 +1738,28 @@ impl CollabPanel { ListEntry::Channel(channel) => { self.join_channel(channel.id, cx); } + ListEntry::ChannelEditor { .. } => { + self.confirm_channel_edit(cx); + } _ => {} } } - } else if let Some((editing_state, channel_name)) = self.take_editing_state(cx) { + } else { + self.confirm_channel_edit(cx); + } + } + + fn confirm_channel_edit(&mut self, cx: &mut ViewContext<'_, '_, CollabPanel>) { + if let Some((editing_state, channel_name)) = self.take_editing_state(cx) { let create_channel = self.channel_store.update(cx, |channel_store, _| { channel_store.create_channel(&channel_name, editing_state.parent_id) }); + self.update_entries(false, cx); + cx.foreground() .spawn(async move { - create_channel.await.ok(); + create_channel.await.log_err(); }) .detach(); } @@ -1746,7 +1771,7 @@ impl CollabPanel { } else { self.collapsed_sections.push(section); } - self.update_entries(cx); + self.update_entries(false, cx); } fn leave_call(cx: &mut ViewContext) { @@ -1771,7 +1796,7 @@ impl CollabPanel { fn new_root_channel(&mut self, cx: &mut ViewContext) { self.channel_editing_state = Some(ChannelEditingState { parent_id: None }); - self.update_entries(cx); + self.update_entries(true, cx); cx.focus(self.channel_name_editor.as_any()); cx.notify(); } @@ -1780,7 +1805,7 @@ impl CollabPanel { self.channel_editing_state = Some(ChannelEditingState { parent_id: Some(action.channel_id), }); - self.update_entries(cx); + self.update_entries(true, cx); cx.focus(self.channel_name_editor.as_any()); cx.notify(); }