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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
use std::borrow::Borrow;
use crate::{impl_focusable, style::*};
#[derive(Clone, Debug, PartialEq)]
pub struct Text {
pub content: String,
align: Alignment,
colour: Option<Colour>,
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,
Highlight,
}
#[derive(Clone, Debug, PartialEq)]
enum Strength {
Base,
Weak,
}
#[derive(Clone, Debug, PartialEq)]
enum Size {
VSmall,
Small,
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> {
<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),
..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<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::VSmall) => TEXT_SIZE_V_SM,
Some(Size::Small) => TEXT_SIZE_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);
|