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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! WinUI TextBox template and behavior, using Waft’s native editing engine.
use super::text_control::{TextControl, TextControlState};
use crate::Brush;
use std::{fmt, rc::Rc};
use waft_embedder::{Color, Offset, Rect, TextAlign, TextSelection};
use waft_foundation::{App, Handle};
use waft_rendering::RenderBox;
use waft_services::{SelectionChangedCause, TextInputFormatterRef};
use waft_widgets::*;
/// XAML `TextWrapping`: whether long lines break at the box's width.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextWrapping {
/// Lines stay whole and scroll sideways, a single line and, with `AcceptsReturn`, each line
/// of a multiline box.
#[default]
NoWrap,
/// Long lines wrap within the editor width.
Wrap,
}
/// XAML `ScrollBarVisibility` for `ScrollViewer.HorizontalScrollBarVisibility`, as far as the kit's scroll bar can draw it.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ScrollBarVisibility {
/// The content does not scroll on this axis: it is laid out at the view's width and clipped.
Disabled,
/// A scroll bar shows when the content is wider than the view.
Auto,
/// The content scrolls, with no scroll bar; `TextBox`'s style default.
#[default]
Hidden,
}
/// Notification of a user edit after the native controller updates.
pub type TextChangedCallback = Rc<dyn Fn(&mut App, String)>;
/// Notification of a native selection change and its input cause.
pub type TextSelectionChangedCallback =
Rc<dyn Fn(&mut App, TextSelection, Option<SelectionChangedCause>)>;
/// The WinUI TextBox control with its default Fluent template.
#[derive(Clone)]
pub struct TextBox {
/// Owns the text, selection and composing range; the caller retains and disposes it.
pub controller: TextEditingControllerRef,
/// An optional caller-owned focus node; otherwise the control creates one.
pub focus_node: Option<FocusNodeRef>,
/// Content above the editor, with the source header margin.
pub header: Option<WidgetRef>,
/// Supporting content below the editor.
pub description: Option<WidgetRef>,
/// Text displayed while the controller is empty.
pub placeholder_text: String,
/// Whether the editor accepts pointer and keyboard input.
pub is_enabled: bool,
/// The editor font size and basis of the helper-button width threshold; the theme's `ControlContentThemeFontSize` when `None`.
pub font_size: Option<f64>,
/// Editor padding in left, top, right, bottom order; the theme's `TextControlThemePadding` when `None`.
pub padding: Option<[f64; 4]>,
/// The template border and helper-button corner radius.
pub corner_radius: Option<f64>,
/// Overrides the normal editor foreground; visual states use their source theme resources.
pub foreground: Option<Color>,
/// Overrides the normal editor background.
pub background: Option<Brush>,
/// Overrides the normal editor border brush.
pub border_brush: Option<Brush>,
/// Overrides the selection fill while the editor has focus.
pub selection_highlight_color: Option<Color>,
/// Limits user edits through the native length formatter; programmatic assignments remain unrestricted.
pub max_length: Option<i32>,
/// Native formatters applied to user edits, following Flutter composition and undo behavior.
pub input_formatters: Vec<TextInputFormatterRef>,
/// Requests initial keyboard focus through the native editor.
pub autofocus: bool,
/// Whether the caret is drawn while the box has the focus; Flutter's `TextField.showCursor`,
/// which XAML's `TextBox` has no property for. `None` draws it unless the box is read-only.
/// A box that hides it still takes what is typed.
pub show_cursor: Option<bool>,
/// Overrides the desktop text-command menu.
pub context_menu_builder: Option<EditableTextContextMenuBuilder>,
/// Allows selection and copying while preventing edits.
pub is_read_only: bool,
/// Accepts line breaks and enables native wrapped multiline editing.
///
/// A multiline box fills the height its parent gives it, as WinUI's default
/// `VerticalAlignment="Stretch"` does, and its editor scrolls once the text passes it; in an
/// unbounded parent it grows with its text.
pub accepts_return: bool,
/// Wraps text independently of accepting Enter; with `NoWrap`, a multiline box keeps each line whole and scrolls sideways.
pub text_wrapping: TextWrapping,
/// `ScrollViewer.HorizontalScrollBarVisibility`: how a multiline box that does not wrap scrolls its lines sideways.
pub horizontal_scroll_bar_visibility: ScrollBarVisibility,
/// Limits the visible height in lines for a multiline editor, which grows with its text from
/// one line up to it; past it, the content scrolls vertically. Setting it leaves the editor
/// at that height instead of filling the box; leave it unset for a box that fills its pane.
pub max_lines: Option<i32>,
/// Alignment of editor and placeholder text.
pub text_alignment: TextAlign,
/// Overrides the placeholder foreground in every visual state.
pub placeholder_foreground: Option<Color>,
/// Receives user text edits and clear-button actions; listen to the controller for all assignments.
pub text_changed: Option<TextChangedCallback>,
/// Receives native user selection changes after the controller updates.
pub selection_changed: Option<TextSelectionChangedCallback>,
/// Identity retained across parent rebuilds.
pub key: Option<KeyRef>,
}
impl TextBox {
/// Creates an editor using a caller-owned native editing controller.
pub fn new(controller: TextEditingControllerRef) -> Self {
Self {
controller,
focus_node: None,
header: None,
description: None,
placeholder_text: String::new(),
is_enabled: true,
font_size: None,
padding: None,
corner_radius: None,
foreground: None,
background: None,
border_brush: None,
selection_highlight_color: None,
max_length: None,
input_formatters: Vec::new(),
autofocus: false,
show_cursor: None,
context_menu_builder: None,
is_read_only: false,
accepts_return: false,
text_wrapping: TextWrapping::NoWrap,
horizontal_scroll_bar_visibility: ScrollBarVisibility::Hidden,
max_lines: None,
text_alignment: TextAlign::Start,
placeholder_foreground: None,
text_changed: None,
selection_changed: None,
key: None,
}
}
/// Sets FocusNode.
pub fn focus_node(mut self, value: FocusNodeRef) -> Self {
self.focus_node = Some(value);
self
}
/// Sets Header.
#[cfg_attr(debug_assertions, track_caller)]
pub fn header<K>(mut self, value: impl IntoWidget<K>) -> Self {
self.header = Some(value.into_widget());
self
}
/// Sets Description.
#[cfg_attr(debug_assertions, track_caller)]
pub fn description<K>(mut self, value: impl IntoWidget<K>) -> Self {
self.description = Some(value.into_widget());
self
}
/// Sets PlaceholderText.
pub fn placeholder_text(mut self, value: impl Into<String>) -> Self {
self.placeholder_text = value.into();
self
}
/// Sets IsEnabled.
pub fn is_enabled(mut self, value: bool) -> Self {
self.is_enabled = value;
self
}
/// Sets FontSize.
pub fn font_size(mut self, value: f64) -> Self {
self.font_size = Some(value);
self
}
/// Sets Padding.
pub fn padding(mut self, value: [f64; 4]) -> Self {
self.padding = Some(value);
self
}
/// Sets CornerRadius.
pub fn corner_radius(mut self, value: f64) -> Self {
self.corner_radius = Some(value);
self
}
/// Sets Foreground.
pub fn foreground(mut self, value: Color) -> Self {
self.foreground = Some(value);
self
}
/// Sets Background.
pub fn background(mut self, value: Brush) -> Self {
self.background = Some(value);
self
}
/// Sets BorderBrush.
pub fn border_brush(mut self, value: Brush) -> Self {
self.border_brush = Some(value);
self
}
/// Sets SelectionHighlightColor.
pub fn selection_highlight_color(mut self, value: Color) -> Self {
self.selection_highlight_color = Some(value);
self
}
/// Sets MaxLength.
pub fn max_length(mut self, value: i32) -> Self {
assert!(value >= 0, "MaxLength must be non-negative");
self.max_length = Some(value);
self
}
/// Sets InputFormatters.
pub fn input_formatters(mut self, value: Vec<TextInputFormatterRef>) -> Self {
self.input_formatters = value;
self
}
/// Sets Autofocus.
pub fn autofocus(mut self, value: bool) -> Self {
self.autofocus = value;
self
}
/// Sets ContextMenuBuilder.
pub fn context_menu_builder(
mut self,
value: impl Fn(&mut App, BuildContext, Handle<EditableTextState>) -> WidgetRef + 'static,
) -> Self {
self.context_menu_builder = Some(Rc::new(value));
self
}
/// Sets IsReadOnly.
pub fn is_read_only(mut self, value: bool) -> Self {
self.is_read_only = value;
self
}
/// Sets AcceptsReturn.
pub fn accepts_return(mut self, value: bool) -> Self {
self.accepts_return = value;
self
}
/// Sets TextWrapping.
pub fn text_wrapping(mut self, value: TextWrapping) -> Self {
self.text_wrapping = value;
self
}
/// Sets `ScrollViewer.HorizontalScrollBarVisibility`.
pub fn horizontal_scroll_bar_visibility(mut self, value: ScrollBarVisibility) -> Self {
self.horizontal_scroll_bar_visibility = value;
self
}
/// Sets MaxLines.
pub fn max_lines(mut self, value: i32) -> Self {
self.max_lines = Some(value);
self
}
/// Sets TextAlignment.
pub fn text_alignment(mut self, value: TextAlign) -> Self {
self.text_alignment = value;
self
}
/// Whether the caret is drawn while the box has the focus: Flutter's `TextField.showCursor`.
pub fn show_cursor(mut self, value: bool) -> Self {
self.show_cursor = Some(value);
self
}
/// Sets PlaceholderForeground.
pub fn placeholder_foreground(mut self, value: Color) -> Self {
self.placeholder_foreground = Some(value);
self
}
/// Sets TextChanged.
pub fn text_changed(mut self, value: impl Fn(&mut App, String) + 'static) -> Self {
self.text_changed = Some(Rc::new(value));
self
}
/// Sets SelectionChanged.
pub fn selection_changed(
mut self,
value: impl Fn(&mut App, TextSelection, Option<SelectionChangedCause>) + 'static,
) -> Self {
self.selection_changed = Some(Rc::new(value));
self
}
/// Sets widget identity.
pub fn key(mut self, key: KeyRef) -> Self {
self.key = Some(key);
self
}
}
impl fmt::Debug for TextBox {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TextBox")
.field("is_enabled", &self.is_enabled)
.finish_non_exhaustive()
}
}
impl StatefulWidget for TextBox {
type State = TextBoxState;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_state(&self) -> TextBoxState {
TextBoxState {
state: StateData::new(),
}
}
}
/// A `TextBox` in the tree, which a `GlobalKey` on the box reaches.
pub struct TextBoxState {
state: StateData<TextBox>,
}
impl TextBoxState {
/// `TextBox::GetRectFromCharacterIndex` (`TextBox.cpp`): the leading or, with
/// `trailing_edge`, the trailing edge of the character at `char_index`, a UTF-16 index into
/// the text, as a rectangle of no width and the line's height, in the box's own coordinates
/// and where the character shows now, scrolled on either axis. `None` for an index outside
/// the text, the end of the text and every index of an empty box among them, where WinUI
/// throws `E_INVALIDARG`, and before the box is laid out.
pub fn get_rect_from_character_index(
self: Handle<Self>,
app: &mut App,
char_index: i32,
trailing_edge: bool,
) -> Option<Rect> {
let length = self
.widget(app)
.controller
.text_value(app)
.encode_utf16()
.count();
if char_index < 0 || char_index as usize >= length {
return None;
}
let editable = self.text_control(app)?.render_editable(app)?;
let character = TextSelection::new(char_index, char_index + 1);
let boxes = editable.get_boxes_for_selection(app, character);
let glyph = boxes.first()?;
// RichEdit's `TA_LEFT` and `TA_RIGHT`: the character's left and right edges.
let x = if trailing_edge {
glyph.right
} else {
glyph.left
};
let top_left = editable
.as_box()
.local_to_global(app, Offset::new(x, glyph.top), None);
let own = self.context(app).find_render_object(app)?.as_box()?;
let local = own.global_to_local(app, top_left, None);
Some(Rect::from_ltwh(
local.dx(),
local.dy(),
0.0,
glyph.bottom - glyph.top,
))
}
/// The shared editor this box builds.
fn text_control(self: Handle<Self>, app: &App) -> Option<Handle<TextControlState>> {
let mut found = None;
self.context(app).visit_child_elements(app, &mut |child| {
found = child.state_handle::<TextControlState>(app);
});
found
}
}
impl State for TextBoxState {
type Widget = TextBox;
waft_widgets::state_accessors!();
fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
TextControl::new(self.widget(app).clone(), None).into_widget()
}
}