use std::rc::Rc;
use arc_motion::{arc_transition, is_reduced_motion};
use arc_tokens::{ActiveArcTheme as _, Typography};
use gpui_kit::{
App, ElementId, IntoElement, ParentElement, RenderOnce, SharedString, Styled, Window,
assets::IconName,
base::{Button as BaseButton, StyledExt as _},
component::Icon,
div,
prelude::*,
px,
};
type NavigateHandler = Rc<dyn Fn(usize, &mut Window, &mut App)>;
#[derive(Clone)]
pub struct BreadcrumbItem {
pub label: SharedString,
}
impl BreadcrumbItem {
pub fn new(label: impl Into<SharedString>) -> Self {
Self {
label: label.into(),
}
}
}
struct BreadcrumbInternal {
known: Vec<String>,
animated: Vec<String>,
first_render: bool,
}
#[derive(IntoElement)]
pub struct Breadcrumb {
id: ElementId,
items: Vec<BreadcrumbItem>,
on_navigate: Option<NavigateHandler>,
}
impl Breadcrumb {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
items: Vec::new(),
on_navigate: None,
}
}
pub fn items(mut self, items: impl IntoIterator<Item = BreadcrumbItem>) -> Self {
self.items.extend(items);
self
}
pub fn on_navigate(mut self, handler: impl Fn(usize, &mut Window, &mut App) + 'static) -> Self {
self.on_navigate = Some(Rc::new(handler));
self
}
}
impl RenderOnce for Breadcrumb {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.arc_theme().clone();
let colors = theme.colors;
let motion = theme.motion;
let reduced = is_reduced_motion(cx);
let state_id = ElementId::NamedChild(self.id.clone().into(), "__arc-breadcrumb".into());
let internal = window.use_keyed_state(state_id, cx, |_, _| BreadcrumbInternal {
known: Vec::new(),
animated: Vec::new(),
first_render: true,
});
let keys: Vec<String> = self
.items
.iter()
.enumerate()
.map(|(i, it)| format!("{}-{i}", it.label))
.collect();
internal.update(cx, |s, _| {
for key in &keys {
if !s.known.contains(key) {
s.known.push(key.clone());
if !s.first_render {
s.animated.push(key.clone());
}
}
}
s.first_render = false;
});
let animated = internal.read(cx).animated.clone();
let last = self.items.len().saturating_sub(1);
let mut row = div()
.id(self.id.clone())
.flex()
.flex_row()
.flex_wrap()
.items_center()
.gap_x(px(6.0))
.gap_y(px(2.0));
for (index, item) in self.items.into_iter().enumerate() {
let key = keys[index].clone();
let progress = if animated.contains(&key) && !reduced {
arc_transition(
(self.id.clone(), format!("crumb-{key}")),
1.0,
motion.duration.standard,
motion.ease.enter,
window,
cx,
)
} else {
1.0
};
let slide = (1.0 - progress) * -8.0;
let current = index == last;
let navigable = !current && self.on_navigate.is_some();
let label = item.label.clone();
let text: gpui_kit::AnyElement = if navigable {
let handler = self.on_navigate.clone();
let hover_color = colors.accent_strong;
BaseButton::new(ElementId::NamedChild(
self.id.clone().into(),
SharedString::from(format!("crumb-btn-{index}")),
))
.accessibility_label(label.clone())
.on_click(move |_, window, cx| {
if let Some(h) = &handler {
h(index, window, cx);
}
})
.px(px(2.0))
.py(px(4.0))
.rounded(px(7.0))
.text_size(Typography::TEXT_SM)
.text_color(colors.text_secondary)
.cursor_pointer()
.hover(move |s| s.text_color(hover_color))
.child(label)
.into_any_element()
} else if current {
div()
.px(px(2.0))
.py(px(4.0))
.text_size(Typography::TEXT_SM)
.font_medium()
.text_color(colors.accent_strong)
.child(label)
.into_any_element()
} else {
div()
.px(px(2.0))
.py(px(4.0))
.text_size(Typography::TEXT_SM)
.text_color(colors.text_muted)
.child(label)
.into_any_element()
};
row = row.child(
div()
.flex()
.items_center()
.gap(px(6.0))
.ml(px(slide))
.opacity(progress.clamp(0.0, 1.0))
.when(index > 0, |this| {
this.child(
Icon::new(IconName::ChevronRight)
.text_size(px(16.0))
.text_color(colors.border_strong),
)
})
.child(text),
);
}
row
}
}
#[cfg(test)]
mod tests {
use std::{cell::Cell, rc::Rc};
use arc_tokens::{Accent, Spacing, ThemeMode, install_arc_theme};
use gpui_kit::{
Context, MouseButton, Render, Role, TestAppContext, div,
test::{TestEventExt as _, TestQueryExt as _, TestWindowExt as _},
};
use super::*;
struct CrumbHost {
depth: usize,
picked: Rc<Cell<Option<usize>>>,
}
impl Render for CrumbHost {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
let picked = self.picked.clone();
let all = ["Home", "Projects", "Arc", "Settings"];
div().w(px(480.0)).p(Spacing::S4).child(
Breadcrumb::new("crumbs")
.items(all.iter().take(self.depth).map(|l| BreadcrumbItem::new(*l)))
.on_navigate(move |i, _, _| picked.set(Some(i))),
)
}
}
#[gpui_kit::test]
fn breadcrumb_navigates_to_earlier_crumb_and_grows(cx: &mut TestAppContext) {
cx.update(|cx| {
gpui_kit::init(cx);
install_arc_theme(ThemeMode::Light, Accent::Violet, cx);
});
let picked = Rc::new(Cell::new(None));
let (view, cx) = cx.add_window_view(|_, _| CrumbHost {
depth: 3,
picked: picked.clone(),
});
cx.update(|window, cx| {
window.render_frame(cx);
let crumb = window.find_by_label("Projects");
assert_eq!(crumb.role(), Some(Role::Button));
let pos = crumb.bounds().center();
window.pointer_down(pos, MouseButton::Left, cx);
window.pointer_up(pos, MouseButton::Left, cx);
view.update(cx, |h, cx| {
h.depth = 4;
cx.notify();
});
window.render_frame(cx);
});
assert_eq!(picked.get(), Some(1));
}
}