Visual Studio Code, rebuilt in Rust
Rust
//! The side bar card (sidebarPart.ts) and, with `workbench.activityBar.location` `top` or `bottom`, its compact
//! activity bar (`.header-or-footer` of paneCompositePart.ts).
use gpui::{ClickEvent, Context, Div, InteractiveElement, ParentElement, Rgba, SharedString, StatefulInteractiveElement, Styled, Window, div, prelude::FluentBuilder, px, svg};
use super::Workbench;
use super::hover::{HoverOptions, HoverPosition};
use super::search_view::SideBarView;
use super::view_containers::ContainerIcon;
/// The view containers of the activity bar: icon in the activity bar, icon in the compact bar, title, command, view.
/// Icons are registered icon ids, resolved through the product icon theme and their defaults.
pub(super) const VIEW_CONTAINERS: [(&str, &str, &str, &str, Option<SideBarView>); 5] = [
("explorer-view-icon", "explorer-view-icon", "Explorer", "workbench.view.explorer", Some(SideBarView::Explorer)),
("search-view-icon", "search", "Search", "workbench.view.search", Some(SideBarView::Search)),
("source-control-view-icon", "source-control-view-icon", "Source Control", "workbench.view.scm", Some(SideBarView::Scm)),
("run-view-icon", "run-view-icon", "Run and Debug", "workbench.view.debug", Some(SideBarView::Debug)),
("extensions-view-icon", "extensions-view-icon", "Extensions", "workbench.view.extensions", Some(SideBarView::Extensions)),
];
/// The action bar of the compact composite bar in the side bar's header or footer.
pub(super) const COMPACT_ACTIVITY_BAR: &str = "compact-activity";
impl Workbench {
/// `workbench.activityBar.location` is `top` or `bottom`.
pub(super) fn compact_activity_bar_location(&self) -> Option<bool> {
match self.setting("workbench.activityBar.location").as_str() {
Some("top") => Some(false),
Some("bottom") => Some(true),
_ => None,
}
}
/// The side bar card: without the activity bar left of it, its own left border and corners.
pub(super) fn side_bar(&self, window: &Window, cx: &mut Context<Self>) -> Div {
let activity_bar_visible = self.is_part_visible(super::layout::Part::ActivityBar);
let left = self.arrangement().side_bar == super::layout::Position::Left;
let compact = self.compact_activity_bar_location();
let card = div().flex_1().min_w_0().overflow_hidden().flex().flex_col().bg(self.color("sideBar.background"));
let card = if self.compact_density() {
// floatingPanels.css: a right rail draws the seam it shares with the side bar.
let edges = [false, !(activity_bar_visible && !left), true, self.part_at_left_edge(super::layout::Part::SideBar)];
card.border_1().border_color(gpui::transparent_black()).child(self.compact_card_strokes("surface.border", edges, true))
} else {
card.border_t_1()
.border_b_1()
.border_color(self.color("surface.border"))
// The seam with the activity bar is the bar's border; the side bar keeps a transparent 1px edge there.
.when(activity_bar_visible && left, |card| card.border_r_1().pl(px(1.)).rounded_r(px(8.)))
.when(activity_bar_visible && !left, |card| card.border_l_1().pr(px(1.)).rounded_l(px(8.)))
.when(!activity_bar_visible, |card| card.border_l_1().border_r_1().rounded(px(8.)))
};
card
.when(compact == Some(false), |card| card.child(self.compact_activity_bar(false, cx)))
.child(self.side_bar_view(window, cx))
.when(compact == Some(true), |card| card.child(self.compact_activity_bar(true, cx)))
}
/// The composite bar in the side bar's header or footer: 36x32 tabs with 16px icons on the tab strip's
/// background, the checked one in the side bar's color and connected to its content.
fn compact_activity_bar(&self, footer: bool, cx: &mut Context<Self>) -> Div {
let hover = HoverOptions::composite_bar(if footer { HoverPosition::Above } else { HoverPosition::Below });
let unpinned = self.unpinned_containers(super::layout::Part::SideBar);
let containers = self.activity_bar_containers().into_iter().filter(|(command, _)| !unpinned.iter().any(|id| id == command));
let hover_group = SharedString::from(super::ACTIVITY_ITEM_GROUP);
let tabs: Vec<_> = containers.enumerate().map(|(index, (command, title))| {
let view = self.compact_activity_view(command);
let active = view == self.side_bar_view;
let color = if active { self.color("activityBarTop.foreground") } else { self.color("activityBarTop.inactiveForeground") };
let hover_color = if active { color } else { self.foreground_or("modernTab.hoverForeground") };
let tab = div()
.id(command)
.relative()
.h(px(32.))
.when(active, |tab| tab.child(self.connected_pane_tab_fill(index == 0, footer, "sideBar.background")))
.when(!active, |tab| tab.child(self.inactive_pane_tab_hover(&hover_group, true)))
.child(self.compact_activity_icon(command, color, hover_color).h(px(32.)));
let tab = self.action_bar_item(COMPACT_ACTIVITY_BAR, command, tab, cx);
self.activity_composite_item(tab, COMPACT_ACTIVITY_BAR, command, title, self.compact_activity_drag_icon(command), active, cx)
.debug_selector(move || command.to_owned())
.cursor_pointer()
.child(self.action_hover(title, command, hover))
.on_click(cx.listener(move |workbench, event: &ClickEvent, window, cx| match view {
SideBarView::Generated(id) => workbench.generated_activity_item_clicked(active, id, event, cx),
_ => workbench.activity_item_clicked(active, command, event, window, cx),
}))
}).collect();
// connectedPaneTabs.css: the strip's line under a header, over a footer; tabs.css `gap: 0` wins over padding.css's 2px.
let line = div().absolute().left_0().right_0().h(px(1.)).bg(self.connected_pane_tab_border());
let line = if footer { line.top_0() } else { line.bottom_0() };
let strip = div().relative().h(px(32.)).flex_none().flex().bg(self.tab_strip_background()).child(line);
let bar = self.action_bar(COMPACT_ACTIVITY_BAR, false, strip, cx).children(tabs);
self.with_bar_end_drop(bar, super::layout::Part::SideBar, cx)
}
/// `compact_activity_icon`'s glyph as a drag image.
pub(super) fn compact_activity_drag_icon(&self, container: &'static str) -> SharedString {
if let Some((_, icon, ..)) = VIEW_CONTAINERS.into_iter().find(|(_, _, _, command, _)| *command == container) {
return self.codicon_svg(icon);
}
match self.compact_activity_view(container) {
SideBarView::Testing => self.codicon_svg("beaker"),
SideBarView::Contributed(id) => match &self.tree_views.containers.iter().find(|contributed| contributed.id == id).expect("a contributed container").icon {
ContainerIcon::Codicon(name) => self.codicon_svg(name),
ContainerIcon::File(path) => path.to_string_lossy().into_owned().into(),
},
_ => self.codicon_svg(self.container_codicon(container)),
}
}
/// viewPane.ts `getIcon` of a panel container's or a generated container's first view.
fn container_codicon(&self, container: &str) -> &'static str {
match self.panel.views.iter().find(|view| view.id == container) {
Some(view) => self.view_container_icon(view.view_id),
None => self.generated_container_view(container).expect("a container with a view").icon,
}
}
/// The side bar view an `activity_bar_containers` entry opens.
pub(super) fn compact_activity_view(&self, container: &'static str) -> SideBarView {
if let Some((.., view)) = VIEW_CONTAINERS.into_iter().find(|(_, _, _, command, _)| *command == container) {
return view.expect("a built-in container opens a view");
}
match container {
"workbench.view.testing" => SideBarView::Testing,
id if self.tree_views.containers.iter().any(|contributed| contributed.id == id) => SideBarView::Contributed(id),
id => SideBarView::Generated(id),
}
}
/// An `activity_bar_containers` entry's 16px icon: the compact bar's codicon of a built-in container, else the
/// icon its activity bar item shows; `hover` while its item is hovered.
pub(super) fn compact_activity_icon(&self, container: &'static str, color: Rgba, hover: Rgba) -> Div {
let style = move |icon: gpui::Svg| icon.text_color(color).group_hover(super::ACTIVITY_ITEM_GROUP, move |icon| icon.text_color(hover));
if let Some((_, icon, ..)) = VIEW_CONTAINERS.into_iter().find(|(_, _, _, command, _)| *command == container) {
return self.styled_icon(icon, 36., 16., style);
}
match self.compact_activity_view(container) {
SideBarView::Testing => self.styled_icon("beaker", 36., 16., style),
SideBarView::Contributed(id) => match &self.tree_views.containers.iter().find(|contributed| contributed.id == id).expect("a contributed container").icon {
ContainerIcon::Codicon(name) => self.styled_icon(name, 36., 16., style),
ContainerIcon::File(path) => div().debug_selector(move || format!("uri-icon-{id}")).size(px(36.)).flex_none().flex().items_center().justify_center().child(style(svg().external_path(path.to_string_lossy().into_owned()).size(px(16.)))),
},
_ => self.styled_icon(self.container_codicon(container), 36., 16., style),
}
}
/// paneCompositeBar.ts `ViewContainerActivityAction.run`: a double click runs once, and the active view's icon
/// hides the side bar unless `workbench.activityBar.iconClickBehavior` is `focus`.
pub(super) fn activity_item_clicked(&mut self, active: bool, command: &'static str, event: &ClickEvent, window: &mut Window, cx: &mut Context<Self>) {
if event.click_count() > 1 {
return;
}
let toggles = active && self.setting("workbench.activityBar.iconClickBehavior") != "focus";
let command = if toggles { "workbench.action.closeSidebar" } else { command };
self.execute_command(command, Vec::new(), window, cx);
}
}