breadcrumb: Rename item to child and add children method. (#1519)
This commit is contained in:
parent
b08f5be97c
commit
d3ec1f29f8
2 changed files with 47 additions and 12 deletions
|
|
@ -392,21 +392,22 @@ impl Render for SidebarStory {
|
||||||
.child(Divider::vertical().h_4())
|
.child(Divider::vertical().h_4())
|
||||||
.child(
|
.child(
|
||||||
Breadcrumb::new()
|
Breadcrumb::new()
|
||||||
.item(BreadcrumbItem::new("0", "Home").on_click(cx.listener(
|
.child("Breadcrumb")
|
||||||
|
.child(BreadcrumbItem::new("Home").on_click(cx.listener(
|
||||||
|this, _, _, cx| {
|
|this, _, _, cx| {
|
||||||
this.last_active_item = Item::Playground;
|
this.last_active_item = Item::Playground;
|
||||||
cx.notify();
|
cx.notify();
|
||||||
},
|
},
|
||||||
)))
|
)))
|
||||||
.item(
|
.child(
|
||||||
BreadcrumbItem::new("1", self.last_active_item.label())
|
BreadcrumbItem::new(self.last_active_item.label())
|
||||||
.on_click(cx.listener(|this, _, _, cx| {
|
.on_click(cx.listener(|this, _, _, cx| {
|
||||||
this.active_subitem = None;
|
this.active_subitem = None;
|
||||||
cx.notify();
|
cx.notify();
|
||||||
})),
|
})),
|
||||||
)
|
)
|
||||||
.when_some(self.active_subitem, |this, subitem| {
|
.when_some(self.active_subitem, |this, subitem| {
|
||||||
this.item(BreadcrumbItem::new("2", subitem.label()))
|
this.child(BreadcrumbItem::new(subitem.label()))
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -8,28 +8,31 @@ use gpui::{
|
||||||
|
|
||||||
use crate::{h_flex, ActiveTheme, Icon, IconName, StyledExt};
|
use crate::{h_flex, ActiveTheme, Icon, IconName, StyledExt};
|
||||||
|
|
||||||
|
/// A breadcrumb navigation element.
|
||||||
#[derive(IntoElement)]
|
#[derive(IntoElement)]
|
||||||
pub struct Breadcrumb {
|
pub struct Breadcrumb {
|
||||||
style: StyleRefinement,
|
style: StyleRefinement,
|
||||||
items: Vec<BreadcrumbItem>,
|
items: Vec<BreadcrumbItem>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Item for the [`Breadcrumb`].
|
||||||
#[derive(IntoElement)]
|
#[derive(IntoElement)]
|
||||||
pub struct BreadcrumbItem {
|
pub struct BreadcrumbItem {
|
||||||
id: ElementId,
|
id: ElementId,
|
||||||
style: StyleRefinement,
|
style: StyleRefinement,
|
||||||
text: SharedString,
|
label: SharedString,
|
||||||
on_click: Option<Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>>,
|
on_click: Option<Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>>,
|
||||||
disabled: bool,
|
disabled: bool,
|
||||||
is_last: bool,
|
is_last: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BreadcrumbItem {
|
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 {
|
Self {
|
||||||
id: id.into(),
|
id: ElementId::Integer(0),
|
||||||
style: StyleRefinement::default(),
|
style: StyleRefinement::default(),
|
||||||
text: text.into(),
|
label: label.into(),
|
||||||
on_click: None,
|
on_click: None,
|
||||||
disabled: false,
|
disabled: false,
|
||||||
is_last: false,
|
is_last: false,
|
||||||
|
|
@ -49,6 +52,11 @@ impl BreadcrumbItem {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn id(mut self, id: impl Into<ElementId>) -> Self {
|
||||||
|
self.id = id.into();
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// For internal use only.
|
/// For internal use only.
|
||||||
fn is_last(mut self, is_last: bool) -> Self {
|
fn is_last(mut self, is_last: bool) -> Self {
|
||||||
self.is_last = is_last;
|
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 {
|
impl RenderOnce for BreadcrumbItem {
|
||||||
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
|
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
|
||||||
div()
|
div()
|
||||||
.id(self.id)
|
.id(self.id)
|
||||||
.child(self.text)
|
.child(self.label)
|
||||||
.text_color(cx.theme().muted_foreground)
|
.text_color(cx.theme().muted_foreground)
|
||||||
.when(self.is_last, |this| this.text_color(cx.theme().foreground))
|
.when(self.is_last, |this| this.text_color(cx.theme().foreground))
|
||||||
.when(self.disabled, |this| {
|
.when(self.disabled, |this| {
|
||||||
|
|
@ -84,6 +110,7 @@ impl RenderOnce for BreadcrumbItem {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Breadcrumb {
|
impl Breadcrumb {
|
||||||
|
/// Create a new breadcrumb.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
items: Vec::new(),
|
items: Vec::new(),
|
||||||
|
|
@ -91,9 +118,15 @@ impl Breadcrumb {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add an item to the breadcrumb.
|
/// Add an [`BreadcrumbItem`] to the breadcrumb.
|
||||||
pub fn item(mut self, item: BreadcrumbItem) -> Self {
|
pub fn child(mut self, item: impl Into<BreadcrumbItem>) -> Self {
|
||||||
self.items.push(item);
|
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
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -123,6 +156,7 @@ impl RenderOnce for Breadcrumb {
|
||||||
for (ix, item) in self.items.into_iter().enumerate() {
|
for (ix, item) in self.items.into_iter().enumerate() {
|
||||||
let is_last = ix == items_count - 1;
|
let is_last = ix == items_count - 1;
|
||||||
|
|
||||||
|
let item = item.id(ix);
|
||||||
children.push(item.is_last(is_last).into_any_element());
|
children.push(item.is_last(is_last).into_any_element());
|
||||||
if !is_last {
|
if !is_last {
|
||||||
children.push(BreadcrumbSeparator.into_any_element());
|
children.push(BreadcrumbSeparator.into_any_element());
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue