breadcrumb: Rename item to child and add children method. (#1519)

This commit is contained in:
Jason Lee 2025-11-05 15:13:29 +08:00 committed by GitHub
parent b08f5be97c
commit d3ec1f29f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 47 additions and 12 deletions

View file

@ -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()))
}),
),
)

View file

@ -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<BreadcrumbItem>,
}
/// Item for the [`Breadcrumb`].
#[derive(IntoElement)]
pub struct BreadcrumbItem {
id: ElementId,
style: StyleRefinement,
text: SharedString,
label: SharedString,
on_click: Option<Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>>,
disabled: bool,
is_last: bool,
}
impl BreadcrumbItem {
pub fn new(id: impl Into<ElementId>, text: impl Into<SharedString>) -> Self {
/// Create a new BreadcrumbItem with the given id and label.
pub fn new(label: impl Into<SharedString>) -> 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<ElementId>) -> 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<String> for BreadcrumbItem {
fn from(value: String) -> Self {
Self::new(value)
}
}
impl From<SharedString> 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<BreadcrumbItem>) -> Self {
self.items.push(item.into());
self
}
/// Add multiple [`BreadcrumbItem`] items to the breadcrumb.
pub fn children(mut self, items: impl IntoIterator<Item = impl Into<BreadcrumbItem>>) -> 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());