use crate::ext::CollectionWidget;
use crate::widget::{column, text, ListColumn};
use crate::Element;
use std::borrow::Cow;
#[deprecated(note = "use `settings::section().title()` instead")]
pub fn view_section<'a, Message: 'static>(title: impl Into<Cow<'a, str>>) -> Section<'a, Message> {
Section {
title: title.into(),
children: ListColumn::default(),
}
}
pub fn section<'a, Message: 'static>() -> Section<'a, Message> {
with_column(ListColumn::default())
}
pub fn with_column<'a, Message: 'static>(
children: ListColumn<'a, Message>,
) -> Section<'a, Message> {
Section {
title: Cow::Borrowed(""),
children,
}
}
#[must_use]
pub struct Section<'a, Message> {
title: Cow<'a, str>,
children: ListColumn<'a, Message>,
}
impl<'a, Message: 'static> Section<'a, Message> {
pub fn title(mut self, title: impl Into<Cow<'a, str>>) -> Self {
self.title = title.into();
self
}
#[allow(clippy::should_implement_trait)]
pub fn add(mut self, item: impl Into<Element<'a, Message>>) -> Self {
self.children = self.children.add(item.into());
self
}
pub fn add_maybe(self, item: Option<impl Into<Element<'a, Message>>>) -> Self {
if let Some(item) = item {
self.add(item)
} else {
self
}
}
pub fn extend(
self,
children: impl IntoIterator<Item = impl Into<Element<'a, Message>>>,
) -> Self {
children.into_iter().fold(self, Self::add)
}
}
impl<'a, Message: 'static> From<Section<'a, Message>> for Element<'a, Message> {
fn from(data: Section<'a, Message>) -> Self {
column::with_capacity(2)
.spacing(8)
.push_maybe(if data.title.is_empty() {
None
} else {
Some(text::heading(data.title))
})
.push(data.children)
.into()
}
}