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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//! Decorations of known source ranges; the authoritative editor value remains a plain String.
use std::{ops::Range, sync::Arc};
use crate::{
app::theme::{IconGlyph, TypographyStyle},
layout::text_style::{FontStyle, FontWeight, TextStyle},
node::{TextColor, radius_value::RadiusValue, spacing_value::SpacingValue},
};
/// How a token is drawn: the color of its text and of its fill, the room on either side of its text, its
/// corners and its type; and, where it has them, its height, a line inside its fill, an icon at its end and
/// the looks it takes under the pointer and while the selection covers it. What the style does not set is
/// as an engine token always was: no line, no icon, as high as its text's line, padded alike at both ends.
#[derive(Clone, PartialEq)]
pub struct InputTokenStyle {
pub(crate) foreground: TextColor,
pub(crate) background: TextColor,
pub(crate) padding: SpacingValue,
pub(crate) padding_end: Option<SpacingValue>,
pub(crate) radius: RadiusValue,
pub(crate) typography: Option<TypographyStyle>,
pub(crate) text: Option<TextStyle>,
pub(crate) weight: Option<FontWeight>,
pub(crate) font_style: Option<FontStyle>,
pub(crate) height: Option<f32>,
pub(crate) border: Option<(f32, TextColor)>,
pub(crate) icon: Option<InputTokenIcon>,
pub(crate) hovered: Option<Arc<InputTokenStyle>>,
pub(crate) selected: Option<Arc<InputTokenStyle>>,
}
impl InputTokenStyle {
pub fn new(foreground: impl Into<TextColor>, background: impl Into<TextColor>) -> Self {
Self {
foreground: foreground.into(),
background: background.into(),
padding: 0.0.into(),
padding_end: None,
radius: 0.0.into(),
typography: None,
text: None,
weight: None,
font_style: None,
height: None,
border: None,
icon: None,
hovered: None,
selected: None,
}
}
/// The weight of the token's text, over its type: the input's own, or [`Self::text_style`]'s. A code field's
/// bold keyword is in the input's family and size.
pub fn weight(mut self, weight: FontWeight) -> Self {
self.weight = Some(weight);
self
}
/// Whether the token's text leans, over its type, as [`Self::weight`] is.
pub fn font_style(mut self, style: FontStyle) -> Self {
self.font_style = Some(style);
self
}
/// The room at both ends of the text, inside the fill. [`Self::padding_end`] sets the end's apart.
pub fn padding(mut self, horizontal: impl Into<SpacingValue>) -> Self {
self.padding = horizontal.into();
self
}
/// The room after the text (after the icon, where there is one), in place of [`Self::padding`]'s.
pub fn padding_end(mut self, end: impl Into<SpacingValue>) -> Self {
self.padding_end = Some(end.into());
self
}
/// The height of the fill, centred on the text's line, in place of the line's height. The line a token
/// stands in is as high as its type makes it: give the type a line height (see [`Self::text_style`]) that
/// leaves room around a fill taller than the text.
pub fn height(mut self, height: f32) -> Self {
self.height = Some(height);
self
}
/// A line of `width` inside the fill, in `color`.
pub fn border(mut self, width: f32, color: impl Into<TextColor>) -> Self {
self.border = Some((width, color.into()));
self
}
/// The token's type as it is, in place of [`Self::typography`]: its family, size, weight and line
/// height. Its color is the foreground's. The rows of a wrapping input are as high as the highest line in
/// them.
pub fn text_style(mut self, text: TextStyle) -> Self {
self.text = Some(text);
self
}
/// An icon at the end of the token, after its text, in the room the token keeps for it.
pub fn icon(mut self, icon: InputTokenIcon) -> Self {
self.icon = Some(icon);
self
}
/// How the token is drawn while the pointer is on it. Its geometry is the token's own: give it the same
/// padding, height, type and icon, and change colors.
pub fn hovered(mut self, hovered: InputTokenStyle) -> Self {
self.hovered = Some(Arc::new(hovered));
self
}
/// How the token is drawn while the selection covers all of it, over [`Self::hovered`]. Its geometry is
/// the token's own, as for the hovered look.
pub fn selected(mut self, selected: InputTokenStyle) -> Self {
self.selected = Some(Arc::new(selected));
self
}
pub fn radius(mut self, radius: impl Into<RadiusValue>) -> Self {
self.radius = radius.into();
self
}
pub fn typography(mut self, typography: TypographyStyle) -> Self {
self.typography = Some(typography);
self
}
}
/// An icon at the end of a token: a glyph of a font, as [`IconGlyph`] names it, as wide and high as its size.
#[derive(Clone, PartialEq)]
pub struct InputTokenIcon {
pub(crate) glyph: IconGlyph,
pub(crate) size: f32,
pub(crate) color: Option<TextColor>,
pub(crate) gap: SpacingValue,
pub(crate) removes: bool,
}
impl InputTokenIcon {
/// The glyph, 12 square, in the color of the token's text, right after it.
pub fn new(glyph: IconGlyph) -> Self {
Self {
glyph,
size: 12.0,
color: None,
gap: 0.0.into(),
removes: false,
}
}
/// The side of the icon in logical pixels.
pub fn size(mut self, size: f32) -> Self {
self.size = size;
self
}
/// The color of the icon, in place of the token's text's.
pub fn color(mut self, color: impl Into<TextColor>) -> Self {
self.color = Some(color.into());
self
}
/// The room between the token's text and the icon.
pub fn gap(mut self, gap: impl Into<SpacingValue>) -> Self {
self.gap = gap.into();
self
}
/// Whether a click on the icon takes the token away: its text is deleted as an edit is, through the
/// input's `on_input`, as one step of its undo.
pub fn removes(mut self, removes: bool) -> Self {
self.removes = removes;
self
}
}
/// A UTF-8 source range drawn as a token. By default it is editable text, not an embedded object: the
/// caret goes inside it and an edit can make it partial. An [`atomic`](Self::atomic) one is one thing to
/// the caret.
#[derive(Clone, PartialEq)]
pub struct InputToken {
pub(crate) range: Range<usize>,
pub(crate) style: InputTokenStyle,
pub(crate) atomic: bool,
}
impl InputToken {
pub fn new(range: Range<usize>, style: InputTokenStyle) -> Self {
Self {
range,
style,
atomic: false,
}
}
/// Whether the token is one thing to the caret, as a chip is: the caret never rests inside it, the
/// arrow keys go past it whole, a click on it selects all of it, a selection that takes part of it
/// takes all of it, and Backspace after it (Delete before it) selects it, so that the next deletes it.
/// Its text is still the input's value: copied, pasted, undone and read as text.
pub fn atomic(mut self, atomic: bool) -> Self {
self.atomic = atomic;
self
}
}
impl super::TextInput {
/// Has the layout put `anchor` where the caret before `byte` stands, as high as the text's line there: an
/// element of no size of its own, for an overlay to hang from a place in the text, as a list of suggestions
/// hangs under a trigger. It is set as the input is laid out, so an overlay placed in that pass stands at the
/// byte of the text laid out, after a paste or a value the app set as after a key. A byte past the text is
/// its end.
pub fn anchor_at(mut self, anchor: impl Into<crate::core::ElementRef>, byte: usize) -> Self {
let anchor = anchor.into();
self.update_node(|node| {
if let crate::node::node_kind::NodeKind::TextInput { state, .. } = node.node_kind() {
state.set_byte_anchor(Some((anchor, byte)));
}
});
self
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum InputTokenError {
InvalidRange,
Overlap,
MultilineToken,
}
impl std::fmt::Display for InputTokenError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::InvalidRange => "input token range must be a nonempty UTF-8 source range",
Self::Overlap => "input token ranges must not overlap",
Self::MultilineToken => "an inline input token cannot contain a line break",
})
}
}
impl std::error::Error for InputTokenError {}
/// Validated decorations of this EXACT source. An edit invalidates old ranges immediately;
/// pass freshly parsed ranges on the next render. Partial expressions stay ordinary editable text.
/// Debug output deliberately omits source and ranges, including when supplied to a masked input.
#[derive(Clone, PartialEq)]
pub struct InputTokens {
pub(crate) source: Arc<str>,
pub(crate) tokens: Arc<[InputToken]>,
}
impl std::fmt::Debug for InputTokens {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("InputTokens").finish_non_exhaustive()
}
}
impl InputTokens {
pub fn new(source: &str, mut tokens: Vec<InputToken>) -> Result<Self, InputTokenError> {
tokens.sort_by_key(|t| t.range.start);
let mut end = 0;
for token in &tokens {
let range = &token.range;
if range.start >= range.end || !source.is_char_boundary(range.start) || !source.is_char_boundary(range.end) {
return Err(InputTokenError::InvalidRange);
}
if range.start < end {
return Err(InputTokenError::Overlap);
}
if source[range.clone()].contains(['\r', '\n']) {
return Err(InputTokenError::MultilineToken);
}
end = range.end;
}
Ok(Self {
source: source.into(),
tokens: tokens.into(),
})
}
}