use std::borrow::Cow;
use crate::{
theme,
widget::{column, container, flex_row, horizontal_space, row, text, FlexRow, Row},
Element,
};
use derive_setters::Setters;
use iced_core::{text::Wrapping, Length};
use taffy::AlignContent;
#[must_use]
#[allow(clippy::module_name_repetitions)]
pub fn item<'a, Message: 'static>(
title: impl Into<Cow<'a, str>> + 'a,
widget: impl Into<Element<'a, Message>> + 'a,
) -> Row<'a, Message> {
item_row(vec![
text(title).wrapping(Wrapping::Word).into(),
horizontal_space().width(iced::Length::Fill).into(),
widget.into(),
])
}
#[must_use]
#[allow(clippy::module_name_repetitions)]
pub fn item_row<Message>(children: Vec<Element<Message>>) -> Row<Message> {
let cosmic_theme::Spacing {
space_s, space_xs, ..
} = theme::THEME.lock().unwrap().cosmic().spacing;
row::with_children(children)
.spacing(space_xs)
.align_y(iced::Alignment::Center)
.padding([0, space_s])
}
#[allow(clippy::module_name_repetitions)]
pub fn flex_item<'a, Message: 'static>(
title: impl Into<Cow<'a, str>> + 'a,
widget: impl Into<Element<'a, Message>> + 'a,
) -> FlexRow<'a, Message> {
flex_item_row(vec![
text(title)
.wrapping(Wrapping::Word)
.width(Length::Fill)
.into(),
container(widget).into(),
])
}
#[allow(clippy::module_name_repetitions)]
pub fn flex_item_row<Message>(children: Vec<Element<Message>>) -> FlexRow<Message> {
let cosmic_theme::Spacing {
space_s, space_xs, ..
} = theme::THEME.lock().unwrap().cosmic().spacing;
flex_row(children)
.padding([0, space_s])
.spacing(space_xs)
.min_item_width(200.0)
.justify_items(iced::Alignment::Center)
.justify_content(AlignContent::SpaceBetween)
.width(Length::Fill)
}
pub fn builder<'a, Message: 'static>(title: impl Into<Cow<'a, str>>) -> Item<'a, Message> {
Item {
title: title.into(),
description: None,
icon: None,
}
}
#[derive(Setters)]
pub struct Item<'a, Message> {
title: Cow<'a, str>,
#[setters(strip_option, into)]
description: Option<Cow<'a, str>>,
#[setters(strip_option, into)]
icon: Option<Element<'a, Message>>,
}
impl<'a, Message: 'static> Item<'a, Message> {
pub fn control(self, widget: impl Into<Element<'a, Message>>) -> Row<'a, Message> {
item_row(self.control_(widget))
}
pub fn flex_control(self, widget: impl Into<Element<'a, Message>>) -> FlexRow<'a, Message> {
flex_item_row(self.control_(widget))
}
fn control_(self, widget: impl Into<Element<'a, Message>>) -> Vec<Element<'a, Message>> {
let mut contents = Vec::with_capacity(4);
if let Some(icon) = self.icon {
contents.push(icon);
}
if let Some(description) = self.description {
let column = column::with_capacity(2)
.spacing(2)
.push(text::body(self.title).wrapping(Wrapping::Word))
.push(text::caption(description).wrapping(Wrapping::Word))
.width(Length::Fill);
contents.push(column.into());
} else {
contents.push(text(self.title).width(Length::Fill).into());
}
contents.push(widget.into());
contents
}
pub fn toggler(
self,
is_checked: bool,
message: impl Fn(bool) -> Message + 'static,
) -> Row<'a, Message> {
self.control(crate::widget::toggler(is_checked).on_toggle(message))
}
}