Allow the project panel to be docked right or left

Nathan Sobo and Joseph Lyons created

Co-Authored-By: Joseph Lyons <joseph@zed.dev>

Change summary

assets/settings/default.json               |  5 +++
crates/project_panel/src/project_panel.rs  | 20 +++++++++++--
crates/settings/src/settings.rs            |  4 ++
crates/terminal_view/src/terminal_panel.rs |  2 
crates/workspace/src/dock.rs               | 35 +++++++++++++----------
crates/workspace/src/item.rs               |  2 
crates/workspace/src/workspace.rs          | 22 +++++++++++++--
7 files changed, 66 insertions(+), 24 deletions(-)

Detailed changes

assets/settings/default.json 🔗

@@ -107,6 +107,9 @@
   // Automatically update Zed
   "auto_update": true,
   // Git gutter behavior configuration.
+  "project_panel": {
+      "dock": "left"
+  },
   "git": {
     // Control whether the git gutter is shown. May take 2 values:
     // 1. Show the gutter
@@ -149,6 +152,8 @@
     //         }
     //     }
     "shell": "system",
+    // Where to dock terminals panel. Can be 'left', 'right', 'bottom'.
+    "dock": "bottom",
     // What working directory to use when launching the terminal.
     // May take 4 values:
     // 1. Use the current file's project directory.  Will Fallback to the

crates/project_panel/src/project_panel.rs 🔗

@@ -135,12 +135,25 @@ pub enum Event {
         entry_id: ProjectEntryId,
         focus_opened_item: bool,
     },
+    DockPositionChanged,
 }
 
 impl ProjectPanel {
     pub fn new(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> ViewHandle<Self> {
         let project = workspace.project().clone();
         let project_panel = cx.add_view(|cx: &mut ViewContext<Self>| {
+            // Update the dock position when the setting changes.
+            let mut old_dock_position = cx.global::<Settings>().project_panel_overrides.dock;
+            dbg!(old_dock_position);
+            cx.observe_global::<Settings, _>(move |_, cx| {
+                let new_dock_position = cx.global::<Settings>().project_panel_overrides.dock;
+                dbg!(new_dock_position);
+                if new_dock_position != old_dock_position {
+                    old_dock_position = new_dock_position;
+                    cx.emit(Event::DockPositionChanged);
+                }
+            }).detach();
+
             cx.observe(&project, |this, _, cx| {
                 this.update_visible_entries(None, cx);
                 cx.notify();
@@ -242,7 +255,8 @@ impl ProjectPanel {
                             }
                         }
                     }
-                }
+                },
+                Event::DockPositionChanged => {},
             }
         })
         .detach();
@@ -1344,8 +1358,8 @@ impl workspace::dock::Panel for ProjectPanel {
         "Project Panel".into()
     }
 
-    fn should_change_position_on_event(&self, _: &Self::Event, _: &AppContext) -> bool {
-        todo!()
+    fn should_change_position_on_event(event: &Self::Event) -> bool {
+        matches!(event, Event::DockPositionChanged)
     }
 
     fn should_activate_on_event(&self, _: &Self::Event, _: &AppContext) -> bool {

crates/settings/src/settings.rs 🔗

@@ -131,7 +131,7 @@ impl TelemetrySettings {
     }
 }
 
-#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema)]
+#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, Eq, PartialEq)]
 #[serde(rename_all="lowercase")]
 pub enum DockPosition {
     Left,
@@ -407,6 +407,7 @@ pub struct SettingsFileContent {
     pub autosave: Option<Autosave>,
     #[serde(flatten)]
     pub editor: EditorSettings,
+    pub project_panel: ProjectPanelSettings,
     #[serde(default)]
     pub journal: JournalSettings,
     #[serde(default)]
@@ -609,6 +610,7 @@ impl Settings {
             }
         }
         self.editor_overrides = data.editor;
+        self.project_panel_overrides = data.project_panel;
         self.git_overrides = data.git.unwrap_or_default();
         self.journal_overrides = data.journal;
         self.terminal_defaults.font_size = data.terminal.font_size;

crates/terminal_view/src/terminal_panel.rs 🔗

@@ -160,7 +160,7 @@ impl Panel for TerminalPanel {
         }
     }
 
-    fn should_change_position_on_event(&self, _: &Self::Event, _: &AppContext) -> bool {
+    fn should_change_position_on_event(_: &Self::Event) -> bool {
         todo!()
     }
 

crates/workspace/src/dock.rs 🔗

@@ -15,7 +15,7 @@ pub trait Panel: View {
     fn icon_label(&self, _: &AppContext) -> Option<String> {
         None
     }
-    fn should_change_position_on_event(&self, _: &Self::Event, _: &AppContext) -> bool;
+    fn should_change_position_on_event(_: &Self::Event) -> bool;
     fn should_activate_on_event(&self, _: &Self::Event, _: &AppContext) -> bool;
     fn should_close_on_event(&self, _: &Self::Event, _: &AppContext) -> bool;
 }
@@ -80,7 +80,7 @@ pub enum Event {
 
 pub struct Dock {
     position: DockPosition,
-    items: Vec<Item>,
+    panels: Vec<PanelEntry>,
     is_open: bool,
     active_item_ix: usize,
 }
@@ -112,8 +112,8 @@ impl DockPosition {
     }
 }
 
-struct Item {
-    view: Rc<dyn PanelHandle>,
+struct PanelEntry {
+    panel: Rc<dyn PanelHandle>,
     _subscriptions: [Subscription; 2],
 }
 
@@ -134,7 +134,7 @@ impl Dock {
     pub fn new(position: DockPosition) -> Self {
         Self {
             position,
-            items: Default::default(),
+            panels: Default::default(),
             active_item_ix: 0,
             is_open: false,
         }
@@ -161,15 +161,15 @@ impl Dock {
         cx.notify();
     }
 
-    pub fn add_panel<T: Panel>(&mut self, view: ViewHandle<T>, cx: &mut ViewContext<Self>) {
+    pub fn add_panel<T: Panel>(&mut self, panel: ViewHandle<T>, cx: &mut ViewContext<Self>) {
         let subscriptions = [
-            cx.observe(&view, |_, _, cx| cx.notify()),
-            cx.subscribe(&view, |this, view, event, cx| {
+            cx.observe(&panel, |_, _, cx| cx.notify()),
+            cx.subscribe(&panel, |this, view, event, cx| {
                 if view.read(cx).should_activate_on_event(event, cx) {
                     if let Some(ix) = this
-                        .items
+                        .panels
                         .iter()
-                        .position(|item| item.view.id() == view.id())
+                        .position(|item| item.panel.id() == view.id())
                     {
                         this.activate_item(ix, cx);
                     }
@@ -179,13 +179,18 @@ impl Dock {
             }),
         ];
 
-        self.items.push(Item {
-            view: Rc::new(view),
+        self.panels.push(PanelEntry {
+            panel: Rc::new(panel),
             _subscriptions: subscriptions,
         });
         cx.notify()
     }
 
+    pub fn remove_panel<T: Panel>(&mut self, panel: &ViewHandle<T>, cx: &mut ViewContext<Self>) {
+        self.panels.retain(|entry| entry.panel.id() != panel.id());
+        cx.notify();
+    }
+
     pub fn activate_item(&mut self, item_ix: usize, cx: &mut ViewContext<Self>) {
         self.active_item_ix = item_ix;
         cx.notify();
@@ -202,7 +207,7 @@ impl Dock {
 
     pub fn active_item(&self) -> Option<&Rc<dyn PanelHandle>> {
         if self.is_open {
-            self.items.get(self.active_item_ix).map(|item| &item.view)
+            self.panels.get(self.active_item_ix).map(|item| &item.panel)
         } else {
             None
         }
@@ -275,9 +280,9 @@ impl View for PanelButtons {
         };
 
         let items = dock
-            .items
+            .panels
             .iter()
-            .map(|item| item.view.clone())
+            .map(|item| item.panel.clone())
             .collect::<Vec<_>>();
         Flex::row()
             .with_children(items.into_iter().enumerate().map(|(ix, view)| {

crates/workspace/src/item.rs 🔗

@@ -1077,7 +1077,7 @@ pub(crate) mod test {
             unimplemented!()
         }
 
-        fn should_change_position_on_event(&self, _: &Self::Event, _: &AppContext) -> bool {
+        fn should_change_position_on_event(_: &Self::Event) -> bool {
             unimplemented!()
         }
 

crates/workspace/src/workspace.rs 🔗

@@ -836,10 +836,26 @@ impl Workspace {
 
     pub fn add_panel<T: Panel>(&mut self, panel: ViewHandle<T>, cx: &mut ViewContext<Self>) {
         let dock = match panel.position(cx) {
-            DockPosition::Left => &mut self.left_dock,
-            DockPosition::Bottom => &mut self.bottom_dock,
-            DockPosition::Right => &mut self.right_dock,
+            DockPosition::Left => &self.left_dock,
+            DockPosition::Bottom => &self.bottom_dock,
+            DockPosition::Right => &self.right_dock,
         };
+
+        cx.subscribe(&panel, {
+            let mut dock = dock.clone();
+            move |this, panel, event, cx| {
+                if T::should_change_position_on_event(event) {
+                    dock.update(cx, |dock, cx| dock.remove_panel(&panel, cx));
+                    dock = match panel.read(cx).position(cx) {
+                        DockPosition::Left => &this.left_dock,
+                        DockPosition::Bottom => &this.bottom_dock,
+                        DockPosition::Right => &this.right_dock,
+                    }.clone();
+                    dock.update(cx, |dock, cx| dock.add_panel(panel, cx));
+                }
+            }
+        }).detach();
+
         dock.update(cx, |dock, cx| dock.add_panel(panel, cx));
     }