Add sidebars

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld
2021-08-20 13:51:52 -07:00
co-authored by Nathan Sobo
parent a7ac37a318
commit b88b3e7657
10 changed files with 243 additions and 35 deletions
+93
View File
@@ -0,0 +1,93 @@
use crate::Settings;
use gpui::{
elements::{
Align, ConstrainedBox, Container, Flex, MouseEventHandler, ParentElement as _, Svg,
},
AnyViewHandle, AppContext, Element as _, ElementBox,
};
pub struct Sidebar {
side: Side,
items: Vec<Item>,
active_item_ix: Option<usize>,
}
#[derive(Clone, Copy)]
pub enum Side {
Left,
Right,
}
struct Item {
icon_path: &'static str,
view: AnyViewHandle,
}
impl Sidebar {
pub fn new(side: Side) -> Self {
Self {
side,
items: Default::default(),
active_item_ix: None,
}
}
pub fn add_item(&mut self, icon_path: &'static str, view: AnyViewHandle) {
self.items.push(Item { icon_path, view });
}
pub fn toggle_item(&mut self, item_ix: usize) {
if self.active_item_ix == Some(item_ix) {
self.active_item_ix = None;
} else {
self.active_item_ix = Some(item_ix)
}
}
pub fn active_item(&self) -> Option<&AnyViewHandle> {
self.active_item_ix
.and_then(|ix| self.items.get(ix))
.map(|item| &item.view)
}
pub fn render(&self, settings: &Settings, cx: &AppContext) -> ElementBox {
let side = self.side;
let line_height = cx.font_cache().line_height(
cx.font_cache().default_font(settings.ui_font_family),
settings.ui_font_size,
);
Container::new(
Flex::column()
.with_children(self.items.iter().enumerate().map(|(item_ix, item)| {
let theme = if Some(item_ix) == self.active_item_ix {
&settings.theme.active_sidebar_icon
} else {
&settings.theme.sidebar_icon
};
enum SidebarButton {}
MouseEventHandler::new::<SidebarButton, _>(item.view.id(), cx, |_| {
ConstrainedBox::new(
Align::new(
ConstrainedBox::new(
Svg::new(item.icon_path).with_color(theme.color).boxed(),
)
.with_height(line_height)
.boxed(),
)
.boxed(),
)
.with_height(line_height + 16.0)
.boxed()
})
.on_click(move |cx| {
cx.dispatch_action("workspace:toggle_sidebar_item", (side, item_ix))
})
.boxed()
}))
.boxed(),
)
.with_style(&settings.theme.sidebar)
.boxed()
}
}