2021-08-27 16:04:21 +00:00
|
|
|
use super::Workspace;
|
2021-10-05 09:14:30 +00:00
|
|
|
use crate::Settings;
|
2022-02-17 17:43:03 +00:00
|
|
|
use gpui::{action, elements::*, platform::CursorStyle, AnyViewHandle, RenderContext};
|
2021-08-27 12:30:35 +00:00
|
|
|
use std::{cell::RefCell, rc::Rc};
|
2021-08-20 20:51:52 +00:00
|
|
|
|
|
|
|
pub struct Sidebar {
|
|
|
|
side: Side,
|
|
|
|
items: Vec<Item>,
|
|
|
|
active_item_ix: Option<usize>,
|
2021-08-27 12:30:35 +00:00
|
|
|
width: Rc<RefCell<f32>>,
|
2021-08-20 20:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum Side {
|
|
|
|
Left,
|
|
|
|
Right,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Item {
|
|
|
|
icon_path: &'static str,
|
|
|
|
view: AnyViewHandle,
|
|
|
|
}
|
|
|
|
|
2021-09-30 22:20:03 +00:00
|
|
|
action!(ToggleSidebarItem, SidebarItemId);
|
|
|
|
action!(ToggleSidebarItemFocus, SidebarItemId);
|
2021-08-23 03:02:48 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
2021-09-30 22:20:03 +00:00
|
|
|
pub struct SidebarItemId {
|
2021-08-23 15:29:46 +00:00
|
|
|
pub side: Side,
|
|
|
|
pub item_index: usize,
|
2021-08-23 03:02:48 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 20:51:52 +00:00
|
|
|
impl Sidebar {
|
|
|
|
pub fn new(side: Side) -> Self {
|
|
|
|
Self {
|
|
|
|
side,
|
|
|
|
items: Default::default(),
|
|
|
|
active_item_ix: None,
|
2021-09-08 00:02:27 +00:00
|
|
|
width: Rc::new(RefCell::new(260.)),
|
2021-08-20 20:51:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_item(&mut self, icon_path: &'static str, view: AnyViewHandle) {
|
|
|
|
self.items.push(Item { icon_path, view });
|
|
|
|
}
|
|
|
|
|
2021-09-30 22:20:03 +00:00
|
|
|
pub fn activate_item(&mut self, item_ix: usize) {
|
|
|
|
self.active_item_ix = Some(item_ix);
|
|
|
|
}
|
|
|
|
|
2021-08-20 20:51:52 +00:00
|
|
|
pub fn toggle_item(&mut self, item_ix: usize) {
|
|
|
|
if self.active_item_ix == Some(item_ix) {
|
|
|
|
self.active_item_ix = None;
|
|
|
|
} else {
|
2021-08-27 08:01:44 +00:00
|
|
|
self.active_item_ix = Some(item_ix);
|
2021-08-20 20:51:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn active_item(&self) -> Option<&AnyViewHandle> {
|
|
|
|
self.active_item_ix
|
|
|
|
.and_then(|ix| self.items.get(ix))
|
|
|
|
.map(|item| &item.view)
|
|
|
|
}
|
|
|
|
|
2021-09-07 19:02:43 +00:00
|
|
|
fn theme<'a>(&self, settings: &'a Settings) -> &'a theme::Sidebar {
|
|
|
|
match self.side {
|
|
|
|
Side::Left => &settings.theme.workspace.left_sidebar,
|
|
|
|
Side::Right => &settings.theme.workspace.right_sidebar,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 16:04:21 +00:00
|
|
|
pub fn render(&self, settings: &Settings, cx: &mut RenderContext<Workspace>) -> ElementBox {
|
2021-08-20 20:51:52 +00:00
|
|
|
let side = self.side;
|
2021-09-07 19:02:43 +00:00
|
|
|
let theme = self.theme(settings);
|
2021-08-20 20:51:52 +00:00
|
|
|
|
2021-09-14 16:53:41 +00:00
|
|
|
ConstrainedBox::new(
|
|
|
|
Container::new(
|
|
|
|
Flex::column()
|
|
|
|
.with_children(self.items.iter().enumerate().map(|(item_index, item)| {
|
|
|
|
let theme = if Some(item_index) == self.active_item_ix {
|
2021-09-30 20:30:24 +00:00
|
|
|
&theme.active_item
|
2021-09-14 16:53:41 +00:00
|
|
|
} else {
|
2021-09-30 20:30:24 +00:00
|
|
|
&theme.item
|
2021-09-14 16:53:41 +00:00
|
|
|
};
|
|
|
|
enum SidebarButton {}
|
2022-02-17 21:43:58 +00:00
|
|
|
MouseEventHandler::new::<SidebarButton, _, _>(item.view.id(), cx, |_, _| {
|
|
|
|
ConstrainedBox::new(
|
|
|
|
Align::new(
|
|
|
|
ConstrainedBox::new(
|
|
|
|
Svg::new(item.icon_path)
|
|
|
|
.with_color(theme.icon_color)
|
|
|
|
.boxed(),
|
2021-09-14 16:53:41 +00:00
|
|
|
)
|
2022-02-17 21:43:58 +00:00
|
|
|
.with_height(theme.icon_size)
|
2021-09-14 16:53:41 +00:00
|
|
|
.boxed(),
|
2021-08-20 20:51:52 +00:00
|
|
|
)
|
2022-02-17 21:43:58 +00:00
|
|
|
.boxed(),
|
|
|
|
)
|
|
|
|
.with_height(theme.height)
|
|
|
|
.boxed()
|
|
|
|
})
|
2021-09-14 16:53:41 +00:00
|
|
|
.with_cursor_style(CursorStyle::PointingHand)
|
|
|
|
.on_mouse_down(move |cx| {
|
2021-09-30 22:20:03 +00:00
|
|
|
cx.dispatch_action(ToggleSidebarItem(SidebarItemId {
|
|
|
|
side,
|
|
|
|
item_index,
|
|
|
|
}))
|
2021-09-14 16:53:41 +00:00
|
|
|
})
|
2021-08-20 20:51:52 +00:00
|
|
|
.boxed()
|
2021-09-14 16:53:41 +00:00
|
|
|
}))
|
|
|
|
.boxed(),
|
|
|
|
)
|
2021-09-14 23:47:43 +00:00
|
|
|
.with_style(theme.container)
|
2021-09-14 16:53:41 +00:00
|
|
|
.boxed(),
|
2021-08-20 20:51:52 +00:00
|
|
|
)
|
2021-09-14 16:53:41 +00:00
|
|
|
.with_width(theme.width)
|
2021-08-20 20:51:52 +00:00
|
|
|
.boxed()
|
|
|
|
}
|
2021-08-27 12:30:35 +00:00
|
|
|
|
2021-08-27 16:04:21 +00:00
|
|
|
pub fn render_active_item(
|
|
|
|
&self,
|
|
|
|
settings: &Settings,
|
2022-02-17 17:43:03 +00:00
|
|
|
cx: &mut RenderContext<Workspace>,
|
2021-08-27 16:04:21 +00:00
|
|
|
) -> Option<ElementBox> {
|
2021-08-27 12:30:35 +00:00
|
|
|
if let Some(active_item) = self.active_item() {
|
|
|
|
let mut container = Flex::row();
|
|
|
|
if matches!(self.side, Side::Right) {
|
2021-08-27 12:57:47 +00:00
|
|
|
container.add_child(self.render_resize_handle(settings, cx));
|
2021-08-27 12:30:35 +00:00
|
|
|
}
|
2021-09-03 10:18:31 +00:00
|
|
|
|
2021-08-27 12:30:35 +00:00
|
|
|
container.add_child(
|
2022-01-07 01:29:34 +00:00
|
|
|
Hook::new(
|
2022-01-27 16:52:36 +00:00
|
|
|
ConstrainedBox::new(ChildView::new(active_item).boxed())
|
2022-01-07 01:29:34 +00:00
|
|
|
.with_max_width(*self.width.borrow())
|
|
|
|
.boxed(),
|
2021-09-03 09:40:18 +00:00
|
|
|
)
|
2022-01-07 01:29:34 +00:00
|
|
|
.on_after_layout({
|
|
|
|
let width = self.width.clone();
|
|
|
|
move |size, _| *width.borrow_mut() = size.x()
|
|
|
|
})
|
|
|
|
.flexible(1., false)
|
2021-09-03 09:40:18 +00:00
|
|
|
.boxed(),
|
2021-08-27 12:30:35 +00:00
|
|
|
);
|
|
|
|
if matches!(self.side, Side::Left) {
|
2021-08-27 12:57:47 +00:00
|
|
|
container.add_child(self.render_resize_handle(settings, cx));
|
2021-08-27 12:30:35 +00:00
|
|
|
}
|
|
|
|
Some(container.boxed())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 16:04:21 +00:00
|
|
|
fn render_resize_handle(
|
|
|
|
&self,
|
|
|
|
settings: &Settings,
|
2022-02-17 17:43:03 +00:00
|
|
|
cx: &mut RenderContext<Workspace>,
|
2021-08-27 16:04:21 +00:00
|
|
|
) -> ElementBox {
|
2021-08-27 12:30:35 +00:00
|
|
|
let width = self.width.clone();
|
|
|
|
let side = self.side;
|
2022-02-17 21:43:58 +00:00
|
|
|
MouseEventHandler::new::<Self, _, _>(side as usize, cx, |_, _| {
|
2021-08-27 12:30:35 +00:00
|
|
|
Container::new(Empty::new().boxed())
|
2021-09-14 23:47:43 +00:00
|
|
|
.with_style(self.theme(settings).resize_handle)
|
2021-08-27 12:30:35 +00:00
|
|
|
.boxed()
|
|
|
|
})
|
2021-09-07 19:22:21 +00:00
|
|
|
.with_padding(Padding {
|
|
|
|
left: 4.,
|
|
|
|
right: 4.,
|
|
|
|
..Default::default()
|
|
|
|
})
|
2021-08-27 17:01:49 +00:00
|
|
|
.with_cursor_style(CursorStyle::ResizeLeftRight)
|
2021-08-27 12:30:35 +00:00
|
|
|
.on_drag(move |delta, cx| {
|
2021-08-27 14:30:43 +00:00
|
|
|
let prev_width = *width.borrow();
|
2021-08-27 12:30:35 +00:00
|
|
|
match side {
|
2021-08-27 14:30:43 +00:00
|
|
|
Side::Left => *width.borrow_mut() = 0f32.max(prev_width + delta.x()),
|
|
|
|
Side::Right => *width.borrow_mut() = 0f32.max(prev_width - delta.x()),
|
2021-08-27 12:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cx.notify();
|
|
|
|
})
|
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
}
|