diff options
| author | joeac <me@joeac.net> | 2026-02-28 08:50:46 +0000 |
|---|---|---|
| committer | joeac <me@joeac.net> | 2026-02-28 08:50:46 +0000 |
| commit | 342dadaf91093c1ce9c3ad248a2952ed3cde4de6 (patch) | |
| tree | 0546aa27e558c498cbc730a6f1854f11e9fa844b /schist_desktop_gui/src/gui/components/text.rs | |
| parent | 65596363d43995bf2002d4aa8405d84484f2542b (diff) | |
| parent | f3659bd8e9d5e20528aaa1e0e7c1b206fb0eb31c (diff) | |
Merge pull request 'task-085: view transactions in desktop GUI' (#5) from task-085 into main
Reviewed-on: https://git.joeac.net/joeac/schist/pulls/5
Diffstat (limited to 'schist_desktop_gui/src/gui/components/text.rs')
| -rw-r--r-- | schist_desktop_gui/src/gui/components/text.rs | 132 |
1 files changed, 120 insertions, 12 deletions
diff --git a/schist_desktop_gui/src/gui/components/text.rs b/schist_desktop_gui/src/gui/components/text.rs index d171043..068e89a 100644 --- a/schist_desktop_gui/src/gui/components/text.rs +++ b/schist_desktop_gui/src/gui/components/text.rs @@ -1,17 +1,29 @@ use std::borrow::Borrow; +use iced::{font, widget::container, Font}; + use crate::{impl_focusable, style::*}; #[derive(Clone, Debug, PartialEq)] pub struct Text { pub content: String, + align: Alignment, colour: Option<Colour>, + font_style: font::Style, + overflow_strategy: OverflowStrategy, size: Option<Size>, strength: Option<Strength>, + width: iced::Length, is_focused: bool, } #[derive(Clone, Debug, PartialEq)] +enum Alignment { + Left, + Right, +} + +#[derive(Clone, Debug, PartialEq)] enum Colour { Danger, Primary, @@ -19,6 +31,12 @@ enum Colour { } #[derive(Clone, Debug, PartialEq)] +enum OverflowStrategy { + Grow, + Clip, +} + +#[derive(Clone, Debug, PartialEq)] enum Strength { Base, Weak, @@ -26,6 +44,7 @@ enum Strength { #[derive(Clone, Debug, PartialEq)] enum Size { + VSmall, Small, Base, } @@ -33,28 +52,55 @@ enum Size { impl Text { pub fn default(content: &str) -> Self { Self { + align: Alignment::Left, content: String::from(content), colour: Some(Colour::Primary), + font_style: font::Style::Normal, + overflow_strategy: OverflowStrategy::Grow, 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, + font_style: font::Style::Normal, + overflow_strategy: OverflowStrategy::Grow, size: None, strength: None, + width: iced::Length::Shrink, is_focused: false, } } - pub fn as_element<'a, Message>(self) -> iced::Element<'a, Message> { + pub fn currency(amount: i32) -> Self { + let formatted_string = if amount < 0 { + format!("({} · {})", -amount / 100, -amount % 100) + } else { + format!(" {} · {} ", amount / 100, amount % 100) + }; + Self::default(&formatted_string) + } + + pub fn as_element<'a, Message>(self) -> iced::Element<'a, Message> + where + Message: 'a, + { <Self as Into<iced::Element<'a, Message>>>::into(self) } + pub fn v_small(&self) -> Self { + Self { + size: Some(Size::VSmall), + ..self.clone() + } + } + pub fn small(&self) -> Self { Self { size: Some(Size::Small), @@ -86,15 +132,56 @@ impl Text { 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() + } + } + + pub fn clip(&self) -> Self { + Self { + overflow_strategy: OverflowStrategy::Clip, + ..self.clone() + } + } + + pub fn italic(&self) -> Self { + Self { + font_style: font::Style::Italic, + ..self.clone() + } + } + + pub fn style(&self, font_style: font::Style) -> Self { + Self { + font_style, + ..self.clone() + } + } } -impl<'a, Message> From<Text> for iced::Element<'a, Message> { +impl<'a, Message> From<Text> for iced::Element<'a, Message> +where + Message: 'a, +{ fn from(text: Text) -> Self { text_as_element(text) } } -impl<'a, Message> From<&'a Text> for iced::Element<'a, Message> { +impl<'a, Message> From<&'a Text> for iced::Element<'a, Message> +where + Message: 'a, +{ fn from(text: &'a Text) -> Self { text_as_element(text) } @@ -103,17 +190,31 @@ impl<'a, Message> From<&'a Text> for iced::Element<'a, Message> { fn text_as_element<'a, T, Message>(text: T) -> iced::Element<'a, Message> where T: Borrow<Text> + 'a, + Message: 'a, { - iced::widget::text(text.borrow().content.clone()) + let overflow_strategy = &text.borrow().overflow_strategy.clone(); + + let text_elem: iced::widget::Text = iced::widget::text(text.borrow().content.clone()) + .font(Font { + style: text.borrow().font_style, + ..Default::default() + }) .size(match text.borrow().size { - Some(Size::Small) => TEXT_SIZE_SM, - Some(Size::Base) | None => TEXT_SIZE_BASE, + Some(Size::VSmall) => u32::from(TEXT_SIZE_V_SM), + Some(Size::Small) => u32::from(TEXT_SIZE_SM), + Some(Size::Base) | None => u32::from(TEXT_SIZE_BASE), + }) + .align_x(match &text.borrow().align { + Alignment::Left => iced::alignment::Horizontal::Left, + Alignment::Right => iced::alignment::Horizontal::Right, }) + .wrapping(match overflow_strategy { + OverflowStrategy::Grow => iced::widget::text::Wrapping::default(), + OverflowStrategy::Clip => iced::widget::text::Wrapping::None, + }) + .width(text.borrow().width) .style(move |theme: &iced::Theme| iced::widget::text::Style { - color: match ( - &text.borrow().borrow().colour, - &text.borrow().borrow().strength, - ) { + color: match (&text.borrow().colour, &text.borrow().strength) { (Some(Colour::Danger), Some(Strength::Base) | None) => { Some(theme.extended_palette().danger.base.text) } @@ -134,8 +235,15 @@ where } (None, _) => None, }, - }) - .into() + }); + + match overflow_strategy { + OverflowStrategy::Grow => text_elem.into(), + OverflowStrategy::Clip => container::<'a, Message, _, _>(text_elem) + .height(iced::Length::Shrink) + .clip(true) + .into(), + } } impl_focusable!(Text); |
