summaryrefslogtreecommitdiff
path: root/schist_desktop_gui/src/gui/components/text.rs
blob: d171043dbc3ff2037e9621a897faa4bdfbe6fc7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use std::borrow::Borrow;

use crate::{impl_focusable, style::*};

#[derive(Clone, Debug, PartialEq)]
pub struct Text {
    pub content: String,
    colour: Option<Colour>,
    size: Option<Size>,
    strength: Option<Strength>,
    is_focused: bool,
}

#[derive(Clone, Debug, PartialEq)]
enum Colour {
    Danger,
    Primary,
    Highlight,
}

#[derive(Clone, Debug, PartialEq)]
enum Strength {
    Base,
    Weak,
}

#[derive(Clone, Debug, PartialEq)]
enum Size {
    Small,
    Base,
}

impl Text {
    pub fn default(content: &str) -> Self {
        Self {
            content: String::from(content),
            colour: Some(Colour::Primary),
            size: Some(Size::Base),
            strength: Some(Strength::Base),
            is_focused: false,
        }
    }

    pub fn new(content: &str) -> Self {
        Self {
            content: String::from(content),
            colour: None,
            size: None,
            strength: None,
            is_focused: false,
        }
    }

    pub fn as_element<'a, Message>(self) -> iced::Element<'a, Message> {
        <Self as Into<iced::Element<'a, Message>>>::into(self)
    }

    pub fn small(&self) -> Self {
        Self {
            size: Some(Size::Small),
            ..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()
        }
    }
}

impl<'a, Message> From<Text> 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<Text> + 'a,
{
    iced::widget::text(text.borrow().content.clone())
        .size(match text.borrow().size {
            Some(Size::Small) => TEXT_SIZE_SM,
            Some(Size::Base) | None => TEXT_SIZE_BASE,
        })
        .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);