Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Allow the project panel to be docked right or left
Nathan Sobo6a7feb4c4c0f
diff --git a/assets/settings/default.json b/assets/settings/default.jsonindex 58fb28bc625..5d85e751d48 100644--- a/assets/settings/default.json+++ b/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 thediff --git a/crates/project_panel/src/project_panel.rs b/crates/project_panel/src/project_panel.rsindex a595ec62255..8ee701c3cc5 100644--- a/crates/project_panel/src/project_panel.rs+++ b/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 {diff --git a/crates/settings/src/settings.rs b/crates/settings/src/settings.rsindex 528812af878..0eb9eb9e5db 100644--- a/crates/settings/src/settings.rs+++ b/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;diff --git a/crates/terminal_view/src/terminal_panel.rs b/crates/terminal_view/src/terminal_panel.rsindex e9cdb5a1ea5..e2bf2c9e32d 100644--- a/crates/terminal_view/src/terminal_panel.rs+++ b/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!()}diff --git a/crates/workspace/src/dock.rs b/crates/workspace/src/dock.rsindex 100b8e8661e..0e2d84e6267 100644--- a/crates/workspace/src/dock.rs+++ b/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)| {diff --git a/crates/workspace/src/item.rs b/crates/workspace/src/item.rsindex a4e034d7412..d2b9251a629 100644--- a/crates/workspace/src/item.rs+++ b/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!()}diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rsindex d86b25b7463..1708555f50c 100644--- a/crates/workspace/src/workspace.rs+++ b/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));}