2022-04-26 20:10:40 +00:00
|
|
|
use gpui::{
|
|
|
|
elements::*, impl_actions, platform::CursorStyle, AnyViewHandle, Entity, RenderContext, View,
|
|
|
|
ViewContext, ViewHandle,
|
|
|
|
};
|
2022-04-13 18:08:21 +00:00
|
|
|
use serde::Deserialize;
|
2022-04-26 20:10:40 +00:00
|
|
|
use settings::Settings;
|
2021-08-27 12:30:35 +00:00
|
|
|
use std::{cell::RefCell, rc::Rc};
|
2022-03-11 22:59:05 +00:00
|
|
|
use theme::Theme;
|
2021-08-20 20:51:52 +00:00
|
|
|
|
2022-04-26 20:10:40 +00:00
|
|
|
use crate::StatusItemView;
|
|
|
|
|
2021-08-20 20:51:52 +00:00
|
|
|
pub struct Sidebar {
|
|
|
|
side: Side,
|
|
|
|
items: Vec<Item>,
|
|
|
|
active_item_ix: Option<usize>,
|
2022-04-22 09:45:18 +00:00
|
|
|
actual_width: Rc<RefCell<f32>>,
|
|
|
|
custom_width: Rc<RefCell<f32>>,
|
2021-08-20 20:51:52 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 20:10:40 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Deserialize)]
|
2021-08-20 20:51:52 +00:00
|
|
|
pub enum Side {
|
|
|
|
Left,
|
|
|
|
Right,
|
|
|
|
}
|
|
|
|
|
2022-04-26 20:10:40 +00:00
|
|
|
#[derive(Clone)]
|
2021-08-20 20:51:52 +00:00
|
|
|
struct Item {
|
|
|
|
icon_path: &'static str,
|
|
|
|
view: AnyViewHandle,
|
|
|
|
}
|
|
|
|
|
2022-04-26 20:10:40 +00:00
|
|
|
pub struct SidebarButtons {
|
|
|
|
sidebar: ViewHandle<Sidebar>,
|
|
|
|
}
|
2022-04-07 23:20:49 +00:00
|
|
|
|
2022-04-26 20:10:40 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
pub struct ToggleSidebarItem {
|
|
|
|
pub side: Side,
|
|
|
|
pub item_index: usize,
|
|
|
|
}
|
2021-08-23 03:02:48 +00:00
|
|
|
|
2022-04-26 20:10:40 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
pub struct ToggleSidebarItemFocus {
|
2021-08-23 15:29:46 +00:00
|
|
|
pub side: Side,
|
|
|
|
pub item_index: usize,
|
2021-08-23 03:02:48 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 20:10:40 +00:00
|
|
|
impl_actions!(workspace, [ToggleSidebarItem, ToggleSidebarItemFocus]);
|
|
|
|
|
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,
|
2022-04-22 09:45:18 +00:00
|
|
|
actual_width: Rc::new(RefCell::new(260.)),
|
|
|
|
custom_width: Rc::new(RefCell::new(260.)),
|
2021-08-20 20:51:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 20:10:40 +00:00
|
|
|
pub fn add_item(
|
|
|
|
&mut self,
|
|
|
|
icon_path: &'static str,
|
|
|
|
view: AnyViewHandle,
|
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) {
|
2021-08-20 20:51:52 +00:00
|
|
|
self.items.push(Item { icon_path, view });
|
2022-04-26 20:10:40 +00:00
|
|
|
cx.notify()
|
2021-08-20 20:51:52 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 20:10:40 +00:00
|
|
|
pub fn activate_item(&mut self, item_ix: usize, cx: &mut ViewContext<Self>) {
|
2021-09-30 22:20:03 +00:00
|
|
|
self.active_item_ix = Some(item_ix);
|
2022-04-26 20:10:40 +00:00
|
|
|
cx.notify();
|
2021-09-30 22:20:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 20:10:40 +00:00
|
|
|
pub fn toggle_item(&mut self, item_ix: usize, cx: &mut ViewContext<Self>) {
|
2021-08-20 20:51:52 +00:00
|
|
|
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
|
|
|
}
|
2022-04-26 20:10:40 +00:00
|
|
|
cx.notify();
|
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)
|
|
|
|
}
|
|
|
|
|
2022-04-26 20:10:40 +00:00
|
|
|
fn render_resize_handle(&self, theme: &Theme, cx: &mut RenderContext<Self>) -> ElementBox {
|
|
|
|
let actual_width = self.actual_width.clone();
|
|
|
|
let custom_width = self.custom_width.clone();
|
2021-08-20 20:51:52 +00:00
|
|
|
let side = self.side;
|
2022-04-26 20:10:40 +00:00
|
|
|
MouseEventHandler::new::<Self, _, _>(side as usize, cx, |_, _| {
|
|
|
|
Empty::new()
|
|
|
|
.contained()
|
|
|
|
.with_style(theme.workspace.sidebar_resize_handle)
|
|
|
|
.boxed()
|
|
|
|
})
|
|
|
|
.with_padding(Padding {
|
|
|
|
left: 4.,
|
|
|
|
right: 4.,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.with_cursor_style(CursorStyle::ResizeLeftRight)
|
|
|
|
.on_drag(move |delta, cx| {
|
|
|
|
let prev_width = *actual_width.borrow();
|
|
|
|
match side {
|
|
|
|
Side::Left => *custom_width.borrow_mut() = 0f32.max(prev_width + delta.x()),
|
|
|
|
Side::Right => *custom_width.borrow_mut() = 0f32.max(prev_width - delta.x()),
|
|
|
|
}
|
|
|
|
|
|
|
|
cx.notify();
|
|
|
|
})
|
2021-08-20 20:51:52 +00:00
|
|
|
.boxed()
|
|
|
|
}
|
2022-04-26 20:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Entity for Sidebar {
|
|
|
|
type Event = ();
|
|
|
|
}
|
2021-08-27 12:30:35 +00:00
|
|
|
|
2022-04-26 20:10:40 +00:00
|
|
|
impl View for Sidebar {
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
"Sidebar"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
|
|
|
|
let theme = cx.global::<Settings>().theme.clone();
|
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) {
|
2022-04-26 20:10:40 +00:00
|
|
|
container.add_child(self.render_resize_handle(&theme, 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-04-26 20:10:40 +00:00
|
|
|
ChildView::new(active_item)
|
|
|
|
.constrained()
|
2022-04-22 09:45:18 +00:00
|
|
|
.with_max_width(*self.custom_width.borrow())
|
2022-01-07 01:29:34 +00:00
|
|
|
.boxed(),
|
2021-09-03 09:40:18 +00:00
|
|
|
)
|
2022-01-07 01:29:34 +00:00
|
|
|
.on_after_layout({
|
2022-04-22 09:45:18 +00:00
|
|
|
let actual_width = self.actual_width.clone();
|
|
|
|
move |size, _| *actual_width.borrow_mut() = size.x()
|
2022-01-07 01:29:34 +00:00
|
|
|
})
|
2022-03-30 14:38:00 +00:00
|
|
|
.flex(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) {
|
2022-04-26 20:10:40 +00:00
|
|
|
container.add_child(self.render_resize_handle(&theme, cx));
|
2021-08-27 12:30:35 +00:00
|
|
|
}
|
2022-04-26 20:10:40 +00:00
|
|
|
container.boxed()
|
2021-08-27 12:30:35 +00:00
|
|
|
} else {
|
2022-04-26 20:10:40 +00:00
|
|
|
Empty::new().boxed()
|
2021-08-27 12:30:35 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-26 20:10:40 +00:00
|
|
|
}
|
2021-08-27 12:30:35 +00:00
|
|
|
|
2022-04-26 20:10:40 +00:00
|
|
|
impl SidebarButtons {
|
|
|
|
pub fn new(sidebar: ViewHandle<Sidebar>, cx: &mut ViewContext<Self>) -> Self {
|
|
|
|
cx.observe(&sidebar, |_, _, cx| cx.notify()).detach();
|
|
|
|
Self { sidebar }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Entity for SidebarButtons {
|
|
|
|
type Event = ();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl View for SidebarButtons {
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
"SidebarToggleButton"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
|
|
|
|
let theme = &cx.global::<Settings>().theme.workspace.status_bar;
|
|
|
|
let sidebar = self.sidebar.read(cx);
|
2022-04-27 16:38:31 +00:00
|
|
|
let item_style = theme.sidebar_item;
|
|
|
|
let hover_item_style = theme.sidebar_item_hover;
|
|
|
|
let active_item_style = theme.sidebar_item_active;
|
2022-04-26 20:10:40 +00:00
|
|
|
let active_ix = sidebar.active_item_ix;
|
|
|
|
let side = sidebar.side;
|
2022-04-27 16:38:31 +00:00
|
|
|
let group_style = match side {
|
|
|
|
Side::Left => theme.sidebar_items_left,
|
|
|
|
Side::Right => theme.sidebar_items_right,
|
|
|
|
};
|
2022-04-26 20:10:40 +00:00
|
|
|
let items = sidebar.items.clone();
|
|
|
|
Flex::row()
|
|
|
|
.with_children(items.iter().enumerate().map(|(ix, item)| {
|
|
|
|
MouseEventHandler::new::<Self, _, _>(ix, cx, move |state, _| {
|
|
|
|
let style = if Some(ix) == active_ix {
|
2022-04-27 16:38:31 +00:00
|
|
|
active_item_style
|
2022-04-26 20:10:40 +00:00
|
|
|
} else if state.hovered {
|
2022-04-27 16:38:31 +00:00
|
|
|
hover_item_style
|
2022-04-26 20:10:40 +00:00
|
|
|
} else {
|
2022-04-27 16:38:31 +00:00
|
|
|
item_style
|
2022-04-26 20:10:40 +00:00
|
|
|
};
|
|
|
|
Svg::new(item.icon_path)
|
|
|
|
.with_color(style.icon_color)
|
|
|
|
.constrained()
|
|
|
|
.with_height(style.icon_size)
|
|
|
|
.contained()
|
|
|
|
.with_style(style.container)
|
|
|
|
.boxed()
|
|
|
|
})
|
|
|
|
.with_cursor_style(CursorStyle::PointingHand)
|
|
|
|
.on_click(move |cx| {
|
|
|
|
cx.dispatch_action(ToggleSidebarItem {
|
|
|
|
side,
|
|
|
|
item_index: ix,
|
|
|
|
})
|
|
|
|
})
|
2021-08-27 12:30:35 +00:00
|
|
|
.boxed()
|
2022-04-26 20:10:40 +00:00
|
|
|
}))
|
2022-04-27 16:38:31 +00:00
|
|
|
.contained()
|
|
|
|
.with_style(group_style)
|
2022-04-26 20:10:40 +00:00
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
}
|
2021-08-27 12:30:35 +00:00
|
|
|
|
2022-04-26 20:10:40 +00:00
|
|
|
impl StatusItemView for SidebarButtons {
|
|
|
|
fn set_active_pane_item(
|
|
|
|
&mut self,
|
|
|
|
_: Option<&dyn crate::ItemHandle>,
|
|
|
|
_: &mut ViewContext<Self>,
|
|
|
|
) {
|
2021-08-27 12:30:35 +00:00
|
|
|
}
|
|
|
|
}
|