WIP: Allow panels to be moved

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2023-05-09 18:57:25 +02:00
parent 45df09245b
commit 8f12489937
6 changed files with 157 additions and 92 deletions

View file

@ -28,7 +28,7 @@ use std::{
}; };
use theme::ProjectPanelEntry; use theme::ProjectPanelEntry;
use unicase::UniCase; use unicase::UniCase;
use workspace::Workspace; use workspace::{dock::DockPosition, Workspace};
const NEW_ENTRY_ID: ProjectEntryId = ProjectEntryId::MAX; const NEW_ENTRY_ID: ProjectEntryId = ProjectEntryId::MAX;
@ -1327,7 +1327,35 @@ impl Entity for ProjectPanel {
type Event = Event; type Event = Event;
} }
impl workspace::dock::Panel for ProjectPanel {} impl workspace::dock::Panel for ProjectPanel {
fn position(&self, cx: &gpui::WindowContext) -> DockPosition {
todo!()
}
fn position_is_valid(&self, position: DockPosition) -> bool {
matches!(position, DockPosition::Left | DockPosition::Right)
}
fn icon_path(&self) -> &'static str {
"icons/folder_tree_16.svg"
}
fn icon_tooltip(&self) -> String {
"Project Panel".into()
}
fn should_change_position_on_event(&self, _: &Self::Event, _: &AppContext) -> bool {
todo!()
}
fn should_activate_on_event(&self, _: &Self::Event, _: &AppContext) -> bool {
false
}
fn should_close_on_event(&self, _: &Self::Event, _: &AppContext) -> bool {
false
}
}
impl ClipboardEntry { impl ClipboardEntry {
fn is_cut(&self) -> bool { fn is_cut(&self) -> bool {

View file

@ -139,7 +139,7 @@ impl Panel for TerminalPanel {
matches!(event, Event::Close) matches!(event, Event::Close)
} }
fn label(&self, cx: &AppContext) -> Option<String> { fn icon_label(&self, cx: &AppContext) -> Option<String> {
let count = self.pane.read(cx).items_len(); let count = self.pane.read(cx).items_len();
if count == 0 { if count == 0 {
None None

View file

@ -8,20 +8,25 @@ use settings::Settings;
use std::rc::Rc; use std::rc::Rc;
pub trait Panel: View { pub trait Panel: View {
fn should_activate_on_event(&self, _: &Self::Event, _: &AppContext) -> bool { fn position(&self, cx: &WindowContext) -> DockPosition;
false fn position_is_valid(&self, position: DockPosition) -> bool;
} fn icon_path(&self) -> &'static str;
fn should_close_on_event(&self, _: &Self::Event, _: &AppContext) -> bool { fn icon_tooltip(&self) -> String;
false fn icon_label(&self, _: &AppContext) -> Option<String> {
}
fn label(&self, _: &AppContext) -> Option<String> {
None None
} }
fn should_change_position_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;
} }
pub trait PanelHandle { pub trait PanelHandle {
fn id(&self) -> usize; fn id(&self) -> usize;
fn label(&self, cx: &WindowContext) -> Option<String>; fn position(&self, cx: &WindowContext) -> DockPosition;
fn position_is_valid(&self, position: DockPosition, cx: &WindowContext) -> bool;
fn icon_path(&self, cx: &WindowContext) -> &'static str;
fn icon_tooltip(&self, cx: &WindowContext) -> String;
fn icon_label(&self, cx: &WindowContext) -> Option<String>;
fn is_focused(&self, cx: &WindowContext) -> bool; fn is_focused(&self, cx: &WindowContext) -> bool;
fn as_any(&self) -> &AnyViewHandle; fn as_any(&self) -> &AnyViewHandle;
} }
@ -34,8 +39,24 @@ where
self.id() self.id()
} }
fn label(&self, cx: &WindowContext) -> Option<String> { fn position(&self, cx: &WindowContext) -> DockPosition {
self.read(cx).label(cx) self.read(cx).position(cx)
}
fn position_is_valid(&self, position: DockPosition, cx: &WindowContext) -> bool {
self.read(cx).position_is_valid(position)
}
fn icon_path(&self, cx: &WindowContext) -> &'static str {
self.read(cx).icon_path()
}
fn icon_tooltip(&self, cx: &WindowContext) -> String {
self.read(cx).icon_tooltip()
}
fn icon_label(&self, cx: &WindowContext) -> Option<String> {
self.read(cx).icon_label(cx)
} }
fn is_focused(&self, cx: &WindowContext) -> bool { fn is_focused(&self, cx: &WindowContext) -> bool {
@ -82,8 +103,6 @@ impl DockPosition {
} }
struct Item { struct Item {
icon_path: &'static str,
tooltip: String,
view: Rc<dyn PanelHandle>, view: Rc<dyn PanelHandle>,
_subscriptions: [Subscription; 2], _subscriptions: [Subscription; 2],
} }
@ -132,13 +151,7 @@ impl Dock {
cx.notify(); cx.notify();
} }
pub fn add_item<T: Panel>( pub fn add_panel<T: Panel>(&mut self, view: ViewHandle<T>, cx: &mut ViewContext<Self>) {
&mut self,
icon_path: &'static str,
tooltip: String,
view: ViewHandle<T>,
cx: &mut ViewContext<Self>,
) {
let subscriptions = [ let subscriptions = [
cx.observe(&view, |_, _, cx| cx.notify()), cx.observe(&view, |_, _, cx| cx.notify()),
cx.subscribe(&view, |this, view, event, cx| { cx.subscribe(&view, |this, view, event, cx| {
@ -157,8 +170,6 @@ impl Dock {
]; ];
self.items.push(Item { self.items.push(Item {
icon_path,
tooltip,
view: Rc::new(view), view: Rc::new(view),
_subscriptions: subscriptions, _subscriptions: subscriptions,
}); });
@ -235,15 +246,15 @@ impl Entity for PanelButtons {
impl View for PanelButtons { impl View for PanelButtons {
fn ui_name() -> &'static str { fn ui_name() -> &'static str {
"DockToggleButton" "PanelButtons"
} }
fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> { fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
let theme = &cx.global::<Settings>().theme; let theme = &cx.global::<Settings>().theme;
let tooltip_style = theme.tooltip.clone(); let tooltip_style = theme.tooltip.clone();
let theme = &theme.workspace.status_bar.panel_buttons; let theme = &theme.workspace.status_bar.panel_buttons;
let dock = self.dock.read(cx);
let item_style = theme.button.clone(); let item_style = theme.button.clone();
let dock = self.dock.read(cx);
let active_ix = dock.active_item_ix; let active_ix = dock.active_item_ix;
let is_open = dock.is_open; let is_open = dock.is_open;
let dock_position = dock.position; let dock_position = dock.position;
@ -253,69 +264,65 @@ impl View for PanelButtons {
DockPosition::Right => theme.group_right, DockPosition::Right => theme.group_right,
}; };
#[allow(clippy::needless_collect)]
let items = dock let items = dock
.items .items
.iter() .iter()
.map(|item| (item.icon_path, item.tooltip.clone(), item.view.clone())) .map(|item| item.view.clone())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
Flex::row() Flex::row()
.with_children(items.into_iter().enumerate().map( .with_children(items.into_iter().enumerate().map(|(ix, view)| {
|(ix, (icon_path, tooltip, item_view))| { let action = TogglePanel {
let action = TogglePanel { dock_position,
dock_position, item_index: ix,
item_index: ix, };
}; MouseEventHandler::<Self, _>::new(ix, cx, |state, cx| {
MouseEventHandler::<Self, _>::new(ix, cx, |state, cx| { let is_active = is_open && ix == active_ix;
let is_active = is_open && ix == active_ix; let style = item_style.style_for(state, is_active);
let style = item_style.style_for(state, is_active); Flex::row()
Flex::row() .with_child(
.with_child( Svg::new(view.icon_path(cx))
Svg::new(icon_path) .with_color(style.icon_color)
.with_color(style.icon_color) .constrained()
.constrained() .with_width(style.icon_size)
.with_width(style.icon_size) .aligned(),
)
.with_children(if let Some(label) = view.icon_label(cx) {
Some(
Label::new(label, style.label.text.clone())
.contained()
.with_style(style.label.container)
.aligned(), .aligned(),
) )
.with_children(if let Some(label) = item_view.label(cx) { } else {
Some( None
Label::new(label, style.label.text.clone()) })
.contained() .constrained()
.with_style(style.label.container) .with_height(style.icon_size)
.aligned(), .contained()
) .with_style(style.container)
} else { })
None .with_cursor_style(CursorStyle::PointingHand)
}) .on_click(MouseButton::Left, {
.constrained() let action = action.clone();
.with_height(style.icon_size) move |_, this, cx| {
.contained() if let Some(workspace) = this.workspace.upgrade(cx) {
.with_style(style.container) let action = action.clone();
}) cx.window_context().defer(move |cx| {
.with_cursor_style(CursorStyle::PointingHand) workspace.update(cx, |workspace, cx| {
.on_click(MouseButton::Left, { workspace.toggle_panel(&action, cx)
let action = action.clone();
move |_, this, cx| {
if let Some(workspace) = this.workspace.upgrade(cx) {
let action = action.clone();
cx.window_context().defer(move |cx| {
workspace.update(cx, |workspace, cx| {
workspace.toggle_panel(&action, cx)
});
}); });
} });
} }
}) }
.with_tooltip::<Self>( })
ix, .with_tooltip::<Self>(
tooltip, ix,
Some(Box::new(action)), view.icon_tooltip(cx),
tooltip_style.clone(), Some(Box::new(action)),
cx, tooltip_style.clone(),
) cx,
}, )
)) }))
.contained() .contained()
.with_style(group_style) .with_style(group_style)
.into_any() .into_any()

View file

@ -1060,5 +1060,33 @@ pub(crate) mod test {
} }
} }
impl Panel for TestItem {} impl Panel for TestItem {
fn position(&self, cx: &gpui::WindowContext) -> crate::dock::DockPosition {
unimplemented!()
}
fn position_is_valid(&self, position: crate::dock::DockPosition) -> bool {
unimplemented!()
}
fn icon_path(&self) -> &'static str {
unimplemented!()
}
fn icon_tooltip(&self) -> String {
unimplemented!()
}
fn should_change_position_on_event(&self, _: &Self::Event, _: &AppContext) -> bool {
unimplemented!()
}
fn should_activate_on_event(&self, _: &Self::Event, _: &AppContext) -> bool {
unimplemented!()
}
fn should_close_on_event(&self, _: &Self::Event, _: &AppContext) -> bool {
unimplemented!()
}
}
} }

View file

@ -60,7 +60,7 @@ use crate::{
notifications::simple_message_notification::MessageNotification, notifications::simple_message_notification::MessageNotification,
persistence::model::{SerializedPane, SerializedPaneGroup, SerializedWorkspace}, persistence::model::{SerializedPane, SerializedPaneGroup, SerializedWorkspace},
}; };
use dock::{Dock, DockPosition, PanelButtons, TogglePanel}; use dock::{Dock, DockPosition, Panel, PanelButtons, PanelHandle, TogglePanel};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use notifications::{NotificationHandle, NotifyResultExt}; use notifications::{NotificationHandle, NotifyResultExt};
pub use pane::*; pub use pane::*;
@ -834,6 +834,15 @@ impl Workspace {
&self.right_dock &self.right_dock
} }
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,
};
dock.update(cx, |dock, cx| dock.add_panel(panel, cx));
}
pub fn status_bar(&self) -> &ViewHandle<StatusBar> { pub fn status_bar(&self) -> &ViewHandle<StatusBar> {
&self.status_bar &self.status_bar
} }

View file

@ -311,19 +311,12 @@ pub fn initialize_workspace(
cx.add_view(|cx| CollabTitlebarItem::new(workspace, &workspace_handle, cx)); cx.add_view(|cx| CollabTitlebarItem::new(workspace, &workspace_handle, cx));
workspace.set_titlebar_item(collab_titlebar_item.into_any(), cx); workspace.set_titlebar_item(collab_titlebar_item.into_any(), cx);
let project_panel = ProjectPanel::new(workspace, cx); let project_panel = cx.add_view(|cx| ProjectPanel::new(workspace, cx));
workspace.left_dock().update(cx, |dock, cx| { workspace.add_panel(panel, cx);
dock.add_item(
"icons/folder_tree_16.svg",
"Project Panel".to_string(),
project_panel,
cx,
);
});
let terminal_panel = cx.add_view(|cx| TerminalPanel::new(workspace, cx)); let terminal_panel = cx.add_view(|cx| TerminalPanel::new(workspace, cx));
workspace.bottom_dock().update(cx, |dock, cx| { workspace.bottom_dock().update(cx, |dock, cx| {
dock.add_item( dock.add_panel(
"icons/terminal_12.svg", "icons/terminal_12.svg",
"Terminals".to_string(), "Terminals".to_string(),
terminal_panel, terminal_panel,