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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//! Alinhamento e âncoras, com o mesmo comportamento do FMX.
use std::ops::BitOr;
use crate::prop::PropEnum;
/// Alinhamento de um controle dentro do pai (como `TAlignLayout`).
///
/// Os alinhamentos de encaixe (`MostTop`…`Right`) consomem espaço da área livre do pai,
/// nesta ordem: `MostTop`, `MostBottom`, `MostLeft`, `MostRight`, `Top`, `Bottom`,
/// `Left`, `Right`. Controles com o mesmo alinhamento seguem a ordem dos filhos.
/// Os demais usam a área que sobrou.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Align {
/// Posição e tamanho livres; obedece às [`Anchors`].
#[default]
None,
Top,
Bottom,
Left,
Right,
MostTop,
MostBottom,
MostLeft,
MostRight,
/// Ocupa toda a área que sobrou.
Client,
/// Ocupa o pai inteiro, ignorando o padding e os outros controles.
Contents,
/// Mantém o tamanho e centraliza na área que sobrou.
Center,
/// Centraliza na vertical e estica na horizontal.
VertCenter,
/// Centraliza na horizontal e estica na vertical.
HorzCenter,
/// Estica na horizontal; mantém `top` e `height`.
Horizontal,
/// Estica na vertical; mantém `left` e `width`.
Vertical,
/// Escala mantendo a proporção até caber, centralizado.
Fit,
}
impl Align {
pub const ALL: [Align; 17] = [
Align::None,
Align::Top,
Align::Bottom,
Align::Left,
Align::Right,
Align::MostTop,
Align::MostBottom,
Align::MostLeft,
Align::MostRight,
Align::Client,
Align::Contents,
Align::Center,
Align::VertCenter,
Align::HorzCenter,
Align::Horizontal,
Align::Vertical,
Align::Fit,
];
/// Verdadeiro para os alinhamentos que consomem espaço da área livre.
pub fn is_dock(self) -> bool {
matches!(
self,
Align::Top
| Align::Bottom
| Align::Left
| Align::Right
| Align::MostTop
| Align::MostBottom
| Align::MostLeft
| Align::MostRight
)
}
}
impl PropEnum for Align {
const NAMES: &'static [&'static str] = &[
"none",
"top",
"bottom",
"left",
"right",
"most_top",
"most_bottom",
"most_left",
"most_right",
"client",
"contents",
"center",
"vert_center",
"horz_center",
"horizontal",
"vertical",
"fit",
];
fn from_index(index: usize) -> Option<Self> {
Align::ALL.get(index).copied()
}
fn index(self) -> usize {
self as usize
}
}
/// Onde um controle encaixado fica no sentido em que o encaixe estica (como o `align-self`
/// do CSS): em cima ou embaixo, na horizontal; à esquerda ou à direita, na vertical.
///
/// `Stretch` (o padrão) ocupa a faixa inteira (com a `max_width`, no meio). Os outros mantêm
/// a própria `width` (ou `height`) e encostam no começo, no meio ou no fim: um botão ou um
/// fundo do tamanho do conteúdo numa pilha de campos, sem conta de margem pela largura do pai.
/// Com a `max_width` (ou `max_height`), ocupam a faixa até a máxima e encostam do mesmo
/// jeito: uma coluna de leitura à esquerda numa tela larga.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum SelfAlign {
#[default]
Stretch,
Start,
Center,
End,
}
impl SelfAlign {
pub const ALL: [SelfAlign; 4] = [SelfAlign::Stretch, SelfAlign::Start, SelfAlign::Center, SelfAlign::End];
}
impl PropEnum for SelfAlign {
const NAMES: &'static [&'static str] = &["stretch", "start", "center", "end"];
fn from_index(index: usize) -> Option<Self> {
SelfAlign::ALL.get(index).copied()
}
fn index(self) -> usize {
self as usize
}
}
/// Lados do pai aos quais um controle com `Align::None` fica preso (como `TAnchors`).
///
/// - esquerda + direita: a largura acompanha o pai;
/// - só direita: o controle se move junto com a borda direita;
/// - nenhum dos dois: o controle se move metade do quanto o pai cresceu.
///
/// O mesmo vale para cima/embaixo. O padrão é esquerda + cima.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Anchors(u8);
impl Anchors {
pub const NONE: Anchors = Anchors(0);
pub const LEFT: Anchors = Anchors(1);
pub const TOP: Anchors = Anchors(1 << 1);
pub const RIGHT: Anchors = Anchors(1 << 2);
pub const BOTTOM: Anchors = Anchors(1 << 3);
pub const ALL: Anchors = Anchors(0b1111);
/// Nomes na mesma ordem dos bits.
pub const NAMES: [&'static str; 4] = ["left", "top", "right", "bottom"];
pub const fn contains(self, other: Anchors) -> bool {
self.0 & other.0 == other.0
}
pub const fn union(self, other: Anchors) -> Anchors {
Anchors(self.0 | other.0)
}
pub fn names(self) -> Vec<String> {
Self::NAMES
.iter()
.enumerate()
.filter(|(i, _)| self.0 & (1 << i) != 0)
.map(|(_, n)| n.to_string())
.collect()
}
/// Monta a partir de nomes (`"left"`, `"right"`...). `None` se algum nome for inválido.
pub fn from_names<I, S>(names: I) -> Option<Anchors>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let mut bits = 0u8;
for name in names {
let i = Self::NAMES.iter().position(|n| n.eq_ignore_ascii_case(name.as_ref()))?;
bits |= 1 << i;
}
Some(Anchors(bits))
}
}
impl Default for Anchors {
fn default() -> Self {
Anchors::LEFT | Anchors::TOP
}
}
impl BitOr for Anchors {
type Output = Anchors;
fn bitor(self, other: Anchors) -> Anchors {
self.union(other)
}
}