Allow the project panel to be docked right or left

Co-Authored-By: Joseph Lyons <joseph@zed.dev>
This commit is contained in:
Nathan Sobo 2023-05-09 17:26:54 -06:00
parent 0d78266ddb
commit 6a7feb4c4c
7 changed files with 66 additions and 24 deletions

View file

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

View file

@ -135,12 +135,25 @@ pub enum Event {
entry_id: ProjectEntryId, entry_id: ProjectEntryId,
focus_opened_item: bool, focus_opened_item: bool,
}, },
DockPositionChanged,
} }
impl ProjectPanel { impl ProjectPanel {
pub fn new(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> ViewHandle<Self> { pub fn new(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> ViewHandle<Self> {
let project = workspace.project().clone(); let project = workspace.project().clone();
let project_panel = cx.add_view(|cx: &mut ViewContext<Self>| { 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| { cx.observe(&project, |this, _, cx| {
this.update_visible_entries(None, cx); this.update_visible_entries(None, cx);
cx.notify(); cx.notify();
@ -242,7 +255,8 @@ impl ProjectPanel {
} }
} }
} }
} },
Event::DockPositionChanged => {},
} }
}) })
.detach(); .detach();
@ -1344,8 +1358,8 @@ impl workspace::dock::Panel for ProjectPanel {
"Project Panel".into() "Project Panel".into()
} }
fn should_change_position_on_event(&self, _: &Self::Event, _: &AppContext) -> bool { fn should_change_position_on_event(event: &Self::Event) -> bool {
todo!() matches!(event, Event::DockPositionChanged)
} }
fn should_activate_on_event(&self, _: &Self::Event, _: &AppContext) -> bool { fn should_activate_on_event(&self, _: &Self::Event, _: &AppContext) -> bool {

View file

@ -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")] #[serde(rename_all="lowercase")]
pub enum DockPosition { pub enum DockPosition {
Left, Left,
@ -407,6 +407,7 @@ pub struct SettingsFileContent {
pub autosave: Option<Autosave>, pub autosave: Option<Autosave>,
#[serde(flatten)] #[serde(flatten)]
pub editor: EditorSettings, pub editor: EditorSettings,
pub project_panel: ProjectPanelSettings,
#[serde(default)] #[serde(default)]
pub journal: JournalSettings, pub journal: JournalSettings,
#[serde(default)] #[serde(default)]
@ -609,6 +610,7 @@ impl Settings {
} }
} }
self.editor_overrides = data.editor; self.editor_overrides = data.editor;
self.project_panel_overrides = data.project_panel;
self.git_overrides = data.git.unwrap_or_default(); self.git_overrides = data.git.unwrap_or_default();
self.journal_overrides = data.journal; self.journal_overrides = data.journal;
self.terminal_defaults.font_size = data.terminal.font_size; self.terminal_defaults.font_size = data.terminal.font_size;

View file

@ -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!() todo!()
} }

View file

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

View file

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

View file

@ -836,10 +836,26 @@ impl Workspace {
pub fn add_panel<T: Panel>(&mut self, panel: ViewHandle<T>, cx: &mut ViewContext<Self>) { pub fn add_panel<T: Panel>(&mut self, panel: ViewHandle<T>, cx: &mut ViewContext<Self>) {
let dock = match panel.position(cx) { let dock = match panel.position(cx) {
DockPosition::Left => &mut self.left_dock, DockPosition::Left => &self.left_dock,
DockPosition::Bottom => &mut self.bottom_dock, DockPosition::Bottom => &self.bottom_dock,
DockPosition::Right => &mut self.right_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)); dock.update(cx, |dock, cx| dock.add_panel(panel, cx));
} }