use std::borrow::Borrow; use crate::{impl_focusable, style::*}; #[derive(Clone, Debug, PartialEq)] pub struct Text { pub content: String, align: Alignment, colour: Option, size: Option, strength: Option, width: iced::Length, is_focused: bool, } #[derive(Clone, Debug, PartialEq)] enum Alignment { Left, Right, } #[derive(Clone, Debug, PartialEq)] enum Colour { Danger, Primary, Highlight, } #[derive(Clone, Debug, PartialEq)] enum Strength { Base, Weak, } #[derive(Clone, Debug, PartialEq)] enum Size { VSmall, Base, } impl Text { pub fn default(content: &str) -> Self { Self { align: Alignment::Left, content: String::from(content), colour: Some(Colour::Primary), size: Some(Size::Base), strength: Some(Strength::Base), is_focused: false, width: iced::Length::Shrink, } } pub fn new(content: &str) -> Self { Self { align: Alignment::Left, content: String::from(content), colour: None, size: None, strength: None, width: iced::Length::Shrink, is_focused: false, } } pub fn as_element<'a, Message>(self) -> iced::Element<'a, Message> { >>::into(self) } pub fn v_small(&self) -> Self { Self { size: Some(Size::VSmall), ..self.clone() } } pub fn danger(&self) -> Self { Self { colour: Some(Colour::Danger), ..self.clone() } } pub fn weak(&self) -> Self { Self { strength: Some(Strength::Weak), ..self.clone() } } pub fn highlight_maybe(&self, do_highlight: bool) -> Self { if do_highlight { Self { colour: Some(Colour::Highlight), ..self.clone() } } else { self.clone() } } pub fn align_right(&self) -> Self { Self { align: Alignment::Right, ..self.clone() } } pub fn width(&self, width: iced::Length) -> Self { Self { width, ..self.clone() } } } impl<'a, Message> From for iced::Element<'a, Message> { fn from(text: Text) -> Self { text_as_element(text) } } impl<'a, Message> From<&'a Text> for iced::Element<'a, Message> { fn from(text: &'a Text) -> Self { text_as_element(text) } } fn text_as_element<'a, T, Message>(text: T) -> iced::Element<'a, Message> where T: Borrow + 'a, { iced::widget::text(text.borrow().content.clone()) .size(match text.borrow().size { Some(Size::VSmall) => TEXT_SIZE_V_SM, Some(Size::Base) | None => TEXT_SIZE_BASE, }) .align_x(match &text.borrow().align { Alignment::Left => iced::alignment::Horizontal::Left, Alignment::Right => iced::alignment::Horizontal::Right, }) .width(text.borrow().width) .style(move |theme: &iced::Theme| iced::widget::text::Style { color: match ( &text.borrow().borrow().colour, &text.borrow().borrow().strength, ) { (Some(Colour::Danger), Some(Strength::Base) | None) => { Some(theme.extended_palette().danger.base.text) } (Some(Colour::Danger), Some(Strength::Weak)) => { Some(theme.extended_palette().danger.weak.text) } (Some(Colour::Primary), Some(Strength::Base) | None) => { Some(theme.extended_palette().primary.base.text) } (Some(Colour::Primary), Some(Strength::Weak)) => { Some(theme.extended_palette().primary.weak.text) } (Some(Colour::Highlight), Some(Strength::Base) | None) => { Some(theme.extended_palette().primary.base.color) } (Some(Colour::Highlight), Some(Strength::Weak)) => { Some(theme.extended_palette().primary.weak.color) } (None, _) => None, }, }) .into() } impl_focusable!(Text);