use crate::components::{
Align, Button, ButtonKind, ButtonSize, ButtonVariant, DropdownMenu, DropdownMenuSize, MenuItem,
Select, button::Geometry,
};
use crate::{
density::{self, Density},
icons::CarbonIcon,
theme::color,
tokens::{self, ColorRole},
};
use gpui_kit::{
AnyElement, App, Bounds, ElementId, FocusHandle, InteractiveElement as _, IntoElement,
KeyDownEvent, ParentElement, Pixels, RenderOnce, SharedString, Styled, Window, canvas, div,
prelude::FluentBuilder as _, px, rems,
};
use std::rc::Rc;
pub const BAR_PADDING: tokens::TokenLength = tokens::SPACING_02;
pub const SEPARATOR_SIZE: (f32, f32) = (1., 20.);
pub const SEPARATOR_MARGIN: tokens::TokenLength = tokens::SPACING_02;
type Handler = Rc<dyn Fn(&mut Window, &mut App)>;
#[derive(Clone)]
pub struct ToolbarAction {
id: SharedString,
label: SharedString,
icon: CarbonIcon,
selected: bool,
disabled: bool,
on_click: Option<Handler>,
}
impl ToolbarAction {
pub fn new(
id: impl Into<SharedString>,
label: impl Into<SharedString>,
icon: CarbonIcon,
) -> Self {
Self {
id: id.into(),
label: label.into(),
icon,
selected: false,
disabled: false,
on_click: None,
}
}
pub fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn on_click(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_click = Some(Rc::new(f));
self
}
}
pub enum ToolbarItem {
Action(ToolbarAction),
Separator,
Control {
width: f32,
element: AnyElement,
},
Spacer,
Dropdown {
width: f32,
select: Box<Select>,
},
}
impl ToolbarItem {
pub fn action(action: ToolbarAction) -> Self {
Self::Action(action)
}
pub fn control(width: f32, element: impl IntoElement) -> Self {
Self::Control {
width,
element: element.into_any_element(),
}
}
pub fn dropdown(width: f32, select: Select) -> Self {
Self::Dropdown {
width,
select: Box::new(select),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Slot {
Fixed(f32),
Rule(f32),
Spacer,
}
pub fn button_width(density: Density) -> f32 {
let g = Geometry::of(ButtonSize::Md, density, ButtonKind::Ghost, true);
g.width.unwrap_or(g.height)
}
pub fn separator_width() -> f32 {
SEPARATOR_SIZE.0 + 2. * SEPARATOR_MARGIN.px_value()
}
pub fn fit(slots: &[Slot], available: f32, more: f32, always_more: bool) -> (Vec<usize>, bool) {
let width = |s: &Slot| match s {
Slot::Fixed(w) | Slot::Rule(w) => *w,
Slot::Spacer => 0.,
};
let total: f32 = slots.iter().map(width).sum();
let budget = if always_more {
available - more
} else {
available
};
if total <= budget + 0.5 {
return ((0..slots.len()).collect(), always_more);
}
let budget = available - more;
let mut used = 0.;
let mut kept = Vec::new();
for (i, slot) in slots.iter().enumerate() {
used += width(slot);
if used > budget + 0.5 {
break;
}
kept.push(i);
}
while kept
.last()
.is_some_and(|&i| !matches!(slots[i], Slot::Fixed(_)))
{
kept.pop();
}
(kept, true)
}
pub fn step(key: &str, len: usize, current: Option<usize>) -> Option<usize> {
if len == 0 {
return None;
}
let n = len as isize;
let i = current.map(|c| c as isize);
match key {
"right" => Some(i.map_or(0, |i| (i + 1).rem_euclid(n)) as usize),
"left" => Some(i.map_or(n - 1, |i| (i - 1).rem_euclid(n)) as usize),
"home" => Some(0),
"end" => Some(len - 1),
_ => None,
}
}
#[derive(IntoElement)]
pub struct TextToolbar {
id: ElementId,
items: Vec<ToolbarItem>,
overflow_label: SharedString,
overflow_items: Vec<MenuItem>,
density: Option<Density>,
}
impl TextToolbar {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
items: Vec::new(),
overflow_label: SharedString::default(),
overflow_items: Vec::new(),
density: None,
}
}
pub fn item(mut self, item: ToolbarItem) -> Self {
self.items.push(item);
self
}
pub fn items(mut self, items: impl IntoIterator<Item = ToolbarItem>) -> Self {
self.items.extend(items);
self
}
pub fn overflow_label(mut self, label: impl Into<SharedString>) -> Self {
self.overflow_label = label.into();
self
}
pub fn overflow_items(mut self, items: impl IntoIterator<Item = MenuItem>) -> Self {
self.overflow_items.extend(items);
self
}
pub fn density(mut self, density: Density) -> Self {
self.density = Some(density);
self
}
}
fn rem_px(v: f32) -> gpui_kit::Rems {
rems(v / tokens::REM_PX)
}
impl RenderOnce for TextToolbar {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let density = self.density.unwrap_or_else(density::current);
let button_w = button_width(density);
let bounds = window.use_keyed_state((id.clone(), "bounds"), cx, |_, _| {
Bounds::<Pixels>::default()
});
let scale = f32::from(window.rem_size()) / tokens::REM_PX;
let measured = f32::from(bounds.read(cx).size.width) / scale;
let available = if measured > 0. {
measured - 2. * BAR_PADDING.px_value()
} else {
f32::MAX
};
let slots: Vec<Slot> = self
.items
.iter()
.map(|item| match item {
ToolbarItem::Action(_) => Slot::Fixed(button_w),
ToolbarItem::Separator => Slot::Rule(separator_width()),
ToolbarItem::Control { width, .. } | ToolbarItem::Dropdown { width, .. } => {
Slot::Fixed(*width)
}
ToolbarItem::Spacer => Slot::Spacer,
})
.collect();
let always_more = !self.overflow_items.is_empty();
let (kept, more) = fit(&slots, available, button_w, always_more);
let action_count = kept
.iter()
.filter(|&&i| matches!(self.items[i], ToolbarItem::Action(_)))
.count();
let handles =
window.use_keyed_state((id.clone(), "focus"), cx, |_, _| Vec::<FocusHandle>::new());
handles.update(cx, |h, cx| {
while h.len() < action_count {
h.push(cx.focus_handle());
}
});
let focus: Vec<FocusHandle> = handles.read(cx)[..action_count].to_vec();
let dropdown_count = self
.items
.iter()
.filter(|item| matches!(item, ToolbarItem::Dropdown { .. }))
.count();
let dropdown_handles =
window.use_keyed_state((id.clone(), "dropdown-focus"), cx, |_, _| {
Vec::<FocusHandle>::new()
});
dropdown_handles.update(cx, |h, cx| {
while h.len() < dropdown_count {
h.push(cx.focus_handle());
}
});
let dropdown_focus: Vec<FocusHandle> = dropdown_handles.read(cx)[..dropdown_count].to_vec();
let mut dropdown_slot = 0;
let more_focus = window
.use_keyed_state((id.clone(), "more-focus"), cx, |_, cx| cx.focus_handle())
.read(cx)
.clone();
let mut stops: Vec<FocusHandle> = Vec::new();
let mut row: Vec<AnyElement> = Vec::new();
let mut menu: Vec<MenuItem> = Vec::new();
let mut slot = 0;
for (i, item) in self.items.into_iter().enumerate() {
let keep = kept.contains(&i);
match item {
ToolbarItem::Action(action) => {
if keep {
if !action.disabled {
stops.push(focus[slot].clone());
}
let handler = action.on_click.clone();
row.push(
Button::new((id.clone(), action.id.clone()), action.label.clone())
.variant(ButtonVariant::Ghost)
.size(ButtonSize::Md)
.density(density)
.icon(action.icon)
.icon_only(true)
.selected(action.selected)
.disabled(action.disabled)
.tooltip_align(Align::Bottom)
.track_focus(&focus[slot])
.when_some(handler, |b, h| b.on_click(move |_, w, cx| h(w, cx)))
.into_any_element(),
);
slot += 1;
} else {
let mut entry = MenuItem::item(action.label)
.icon(action.icon)
.disabled(action.disabled);
if let Some(h) = action.on_click {
entry = entry.on_select(move |w, cx| h(w, cx));
}
menu.push(entry);
}
}
ToolbarItem::Separator if keep => row.push(
div()
.flex_none()
.w(px(SEPARATOR_SIZE.0))
.h(rem_px(SEPARATOR_SIZE.1))
.mx(SEPARATOR_MARGIN)
.bg(color(ColorRole::BorderSubtle01))
.into_any_element(),
),
ToolbarItem::Control { width, element } if keep => row.push(
div()
.flex_none()
.w(rem_px(width))
.child(element)
.into_any_element(),
),
ToolbarItem::Dropdown { width, select } if keep => {
let own = &dropdown_focus[dropdown_slot];
dropdown_slot += 1;
let select = if select.has_focus_handle() {
*select
} else {
select.track_focus(own)
};
stops.push(select.focus_handle(window, cx));
row.push(
div()
.flex_none()
.w(rem_px(width))
.child(select)
.into_any_element(),
)
}
ToolbarItem::Dropdown { .. } => dropdown_slot += 1,
ToolbarItem::Spacer if keep => {
row.push(div().flex_1().min_w_0().into_any_element())
}
_ => {}
}
}
if more {
stops.push(more_focus.clone());
menu.extend(self.overflow_items);
if !kept.iter().any(|&i| matches!(slots[i], Slot::Spacer)) {
row.push(div().flex_1().min_w_0().into_any_element());
}
row.push(
div()
.flex_none()
.child(
DropdownMenu::new((id.clone(), "more"))
.overflow_label(self.overflow_label.clone())
.overflow_icon(CarbonIcon::OverflowMenuHorizontal)
.size(DropdownMenuSize::Md)
.track_focus(&more_focus)
.items(menu),
)
.into_any_element(),
);
}
let measure = canvas(
move |b, window, cx| {
if *bounds.read(cx) != b {
bounds.update(cx, |s, _| *s = b);
window.on_next_frame(|window, _| window.refresh());
}
},
|_, _, _, _| {},
)
.absolute()
.top_0()
.left_0()
.size_full();
let buttons = stops;
div()
.id(id)
.relative()
.flex()
.flex_row()
.items_center()
.w_full()
.min_w_0()
.overflow_hidden()
.px(BAR_PADDING)
.bg(color(ColorRole::Layer01))
.border_b_1()
.border_color(color(ColorRole::BorderSubtle01))
.on_key_down(move |event: &KeyDownEvent, window, cx| {
let current = buttons.iter().position(|h| h.is_focused(window));
if current.is_none() {
return;
}
if let Some(next) = step(event.keystroke.key.as_str(), buttons.len(), current) {
cx.stop_propagation();
buttons[next].focus(window, cx);
}
})
.children(row)
.child(measure)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn geometry_follows_patterns_css() {
assert_eq!(separator_width(), 9.);
assert_eq!(SEPARATOR_SIZE.1, 20.);
assert_eq!(BAR_PADDING.px_value(), 4.);
assert_eq!(button_width(Density::Comfortable), 40.);
assert_eq!(button_width(Density::Compact), 32.);
}
#[test]
fn everything_fits_without_overflow() {
let slots = [Slot::Fixed(40.), Slot::Rule(9.), Slot::Fixed(40.)];
assert_eq!(fit(&slots, 100., 40., false), (vec![0, 1, 2], false));
assert_eq!(fit(&slots, 89., 40., false), (vec![0, 1, 2], false));
assert_eq!(fit(&slots, 200., 40., true), (vec![0, 1, 2], true));
}
#[test]
fn items_leave_from_the_right_and_rules_go_with_them() {
let slots = [
Slot::Fixed(40.),
Slot::Fixed(40.),
Slot::Rule(9.),
Slot::Fixed(40.),
Slot::Spacer,
];
assert_eq!(fit(&slots, 128., 40., false), (vec![0, 1], true));
assert_eq!(fit(&slots, 60., 40., false), (vec![], true));
}
#[test]
fn arrow_keys_wrap_like_a_toolbar() {
assert_eq!(step("right", 3, Some(2)), Some(0));
assert_eq!(step("left", 3, Some(0)), Some(2));
assert_eq!(step("home", 3, Some(1)), Some(0));
assert_eq!(step("end", 3, Some(1)), Some(2));
assert_eq!(step("up", 3, Some(1)), None);
assert_eq!(step("right", 0, None), None);
}
}