From d3ec1f29f851cc8546d7dc189ca8f79cf4034c35 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Wed, 5 Nov 2025 15:13:29 +0800 Subject: [PATCH] breadcrumb: Rename `item` to `child` and add `children` method. (#1519) --- crates/story/src/sidebar_story.rs | 9 +++--- crates/ui/src/breadcrumb.rs | 50 ++++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/crates/story/src/sidebar_story.rs b/crates/story/src/sidebar_story.rs index 15ef9d16..08af5594 100644 --- a/crates/story/src/sidebar_story.rs +++ b/crates/story/src/sidebar_story.rs @@ -392,21 +392,22 @@ impl Render for SidebarStory { .child(Divider::vertical().h_4()) .child( Breadcrumb::new() - .item(BreadcrumbItem::new("0", "Home").on_click(cx.listener( + .child("Breadcrumb") + .child(BreadcrumbItem::new("Home").on_click(cx.listener( |this, _, _, cx| { this.last_active_item = Item::Playground; cx.notify(); }, ))) - .item( - BreadcrumbItem::new("1", self.last_active_item.label()) + .child( + BreadcrumbItem::new(self.last_active_item.label()) .on_click(cx.listener(|this, _, _, cx| { this.active_subitem = None; cx.notify(); })), ) .when_some(self.active_subitem, |this, subitem| { - this.item(BreadcrumbItem::new("2", subitem.label())) + this.child(BreadcrumbItem::new(subitem.label())) }), ), ) diff --git a/crates/ui/src/breadcrumb.rs b/crates/ui/src/breadcrumb.rs index 06f67de4..328c110f 100644 --- a/crates/ui/src/breadcrumb.rs +++ b/crates/ui/src/breadcrumb.rs @@ -8,28 +8,31 @@ use gpui::{ use crate::{h_flex, ActiveTheme, Icon, IconName, StyledExt}; +/// A breadcrumb navigation element. #[derive(IntoElement)] pub struct Breadcrumb { style: StyleRefinement, items: Vec, } +/// Item for the [`Breadcrumb`]. #[derive(IntoElement)] pub struct BreadcrumbItem { id: ElementId, style: StyleRefinement, - text: SharedString, + label: SharedString, on_click: Option>, disabled: bool, is_last: bool, } impl BreadcrumbItem { - pub fn new(id: impl Into, text: impl Into) -> Self { + /// Create a new BreadcrumbItem with the given id and label. + pub fn new(label: impl Into) -> Self { Self { - id: id.into(), + id: ElementId::Integer(0), style: StyleRefinement::default(), - text: text.into(), + label: label.into(), on_click: None, disabled: false, is_last: false, @@ -49,6 +52,11 @@ impl BreadcrumbItem { self } + fn id(mut self, id: impl Into) -> Self { + self.id = id.into(); + self + } + /// For internal use only. fn is_last(mut self, is_last: bool) -> Self { self.is_last = is_last; @@ -62,11 +70,29 @@ impl Styled for BreadcrumbItem { } } +impl From<&'static str> for BreadcrumbItem { + fn from(value: &'static str) -> Self { + Self::new(value) + } +} + +impl From for BreadcrumbItem { + fn from(value: String) -> Self { + Self::new(value) + } +} + +impl From for BreadcrumbItem { + fn from(value: SharedString) -> Self { + Self::new(value) + } +} + impl RenderOnce for BreadcrumbItem { fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { div() .id(self.id) - .child(self.text) + .child(self.label) .text_color(cx.theme().muted_foreground) .when(self.is_last, |this| this.text_color(cx.theme().foreground)) .when(self.disabled, |this| { @@ -84,6 +110,7 @@ impl RenderOnce for BreadcrumbItem { } impl Breadcrumb { + /// Create a new breadcrumb. pub fn new() -> Self { Self { items: Vec::new(), @@ -91,9 +118,15 @@ impl Breadcrumb { } } - /// Add an item to the breadcrumb. - pub fn item(mut self, item: BreadcrumbItem) -> Self { - self.items.push(item); + /// Add an [`BreadcrumbItem`] to the breadcrumb. + pub fn child(mut self, item: impl Into) -> Self { + self.items.push(item.into()); + self + } + + /// Add multiple [`BreadcrumbItem`] items to the breadcrumb. + pub fn children(mut self, items: impl IntoIterator>) -> Self { + self.items.extend(items.into_iter().map(Into::into)); self } } @@ -123,6 +156,7 @@ impl RenderOnce for Breadcrumb { for (ix, item) in self.items.into_iter().enumerate() { let is_last = ix == items_count - 1; + let item = item.id(ix); children.push(item.is_last(is_last).into_any_element()); if !is_last { children.push(BreadcrumbSeparator.into_any_element());