waft-widgets 0.11.0

Widget framework and widgets for Waft
Documentation
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
//! Flutter's `TestTextField` (`packages/flutter/test/widgets/editable_text_tester.dart`): an
//! [`EditableText`] a case types into without a Material or Cupertino text field.
//!
//! The field owns a controller and a focus node unless it is given them, so a case can read
//! its text from the field alone. Its selection handles take no room and it has no context
//! menu of its own.

use std::fmt::{self, Debug};
use std::rc::Rc;

use waft_embedder::{Color, Offset, Size};
use waft_foundation::{App, Handle, Ref};
use waft_rendering::HitTestBehavior;
use waft_services::AnyTextSelectionDelegate;

use crate::{
    BuildContext, EditableText, FocusNode, FocusNodeLeaf, FocusNodeRef, GlobalKey, IntoWidget,
    KeyRef, Semantics, SizedBox, State, StateData, StatefulWidget, TextEditingController,
    TextEditingControllerRef, TextSelectionControls, TextSelectionGestureDetectorBuilder,
    TextSelectionGestureDetectorBuilderData, TextSelectionGestureDetectorBuilderDelegate,
    TextSelectionHandleControls, TextSelectionHandleType, WidgetRef,
};

/// A minimal wrapper around [`EditableText`] for use in widget tests.
///
/// It provides text selection gestures through [`TextSelectionGestureDetector`], and fills the
/// members [`EditableText`] requires: a controller, a focus node, a style and two cursor
/// colors. Selection handles are not shown unless [`show_selection_handles`](Self::show_selection_handles)
/// is set.
///
/// [`TextSelectionGestureDetector`]: crate::TextSelectionGestureDetector
pub struct TestTextField {
    /// See [`Widget::key`](crate::Widget::key).
    pub key: Option<KeyRef>,

    /// The controller the field edits, or one the field owns.
    pub controller: Option<TextEditingControllerRef>,

    /// The focus node the field uses, or one the field owns.
    pub focus_node: Option<FocusNodeRef>,

    /// Called when the platform submits the field's text.
    pub on_submitted: Option<SubmittedCallback>,

    /// Whether the field takes the focus when it is first built.
    pub autofocus: bool,

    /// The number of lines the field shows, or `None` for no limit.
    pub max_lines: Option<i32>,

    /// Whether the field only shows its text.
    pub read_only: bool,

    /// The text style of the field's text.
    pub style: Option<waft_painting::TextStyle>,

    /// The color of the cursor while the field is focused, or red.
    pub cursor_color: Option<Color>,

    /// Whether the cursor fades in and out rather than blinking.
    pub cursor_opacity_animates: bool,

    /// Whether the field shows its cursor, or the platform's default.
    pub show_cursor: Option<bool>,

    /// Whether the field shows its selection handles.
    pub show_selection_handles: bool,

    /// The selection controls the field uses, or test handles that take no room.
    pub selection_controls: Option<Rc<dyn TextSelectionControls>>,

    /// Whether all text is selected when the field receives focus, or the platform's default.
    pub select_all_on_focus: Option<bool>,
}

/// `TestTextField.on_submitted`'s type.
pub type SubmittedCallback = Rc<dyn Fn(&mut App, String)>;

impl TestTextField {
    /// Creates a minimal [`EditableText`] wrapper for use in widget tests.
    pub fn new() -> TestTextField {
        TestTextField {
            key: None,
            controller: None,
            focus_node: None,
            on_submitted: None,
            autofocus: false,
            max_lines: Some(1),
            read_only: false,
            style: None,
            cursor_color: None,
            cursor_opacity_animates: false,
            show_cursor: None,
            show_selection_handles: false,
            selection_controls: None,
            select_all_on_focus: None,
        }
    }

    /// Dart `TestTextField(key:)`.
    pub fn key(mut self, key: &GlobalKey) -> TestTextField {
        self.key = Some(key.clone().into());
        self
    }

    /// Dart `TestTextField(controller:)`.
    pub fn controller(mut self, controller: TextEditingControllerRef) -> TestTextField {
        self.controller = Some(controller);
        self
    }

    /// Dart `TestTextField(focusNode:)`.
    pub fn focus_node(mut self, focus_node: FocusNodeRef) -> TestTextField {
        self.focus_node = Some(focus_node);
        self
    }

    /// Dart `TestTextField(onSubmitted:)`.
    pub fn on_submitted(
        mut self,
        on_submitted: impl Fn(&mut App, String) + 'static,
    ) -> TestTextField {
        self.on_submitted = Some(Rc::new(on_submitted));
        self
    }

    /// Dart `TestTextField(autofocus:)`.
    pub fn autofocus(mut self, autofocus: bool) -> TestTextField {
        self.autofocus = autofocus;
        self
    }

    /// Dart `TestTextField(maxLines:)`.
    pub fn max_lines(mut self, max_lines: i32) -> TestTextField {
        self.max_lines = Some(max_lines);
        self
    }

    /// Dart `TestTextField(readOnly:)`.
    pub fn read_only(mut self, read_only: bool) -> TestTextField {
        self.read_only = read_only;
        self
    }

    /// Dart `TestTextField(style:)`.
    pub fn style(mut self, style: waft_painting::TextStyle) -> TestTextField {
        self.style = Some(style);
        self
    }

    /// Dart `TestTextField(cursorColor:)`.
    pub fn cursor_color(mut self, cursor_color: Color) -> TestTextField {
        self.cursor_color = Some(cursor_color);
        self
    }

    /// Dart `TestTextField(cursorOpacityAnimates:)`.
    pub fn cursor_opacity_animates(mut self, cursor_opacity_animates: bool) -> TestTextField {
        self.cursor_opacity_animates = cursor_opacity_animates;
        self
    }

    /// Dart `TestTextField(showCursor:)`.
    pub fn show_cursor(mut self, show_cursor: bool) -> TestTextField {
        self.show_cursor = Some(show_cursor);
        self
    }

    /// Dart `TestTextField(showSelectionHandles:)`.
    pub fn show_selection_handles(mut self, show_selection_handles: bool) -> TestTextField {
        self.show_selection_handles = show_selection_handles;
        self
    }

    /// Dart `TestTextField(selectionControls:)`.
    pub fn selection_controls(
        mut self,
        selection_controls: Rc<dyn TextSelectionControls>,
    ) -> TestTextField {
        self.selection_controls = Some(selection_controls);
        self
    }

    /// Dart `TestTextField(selectAllOnFocus:)`.
    pub fn select_all_on_focus(mut self, select_all_on_focus: bool) -> TestTextField {
        self.select_all_on_focus = Some(select_all_on_focus);
        self
    }
}

impl Default for TestTextField {
    fn default() -> TestTextField {
        TestTextField::new()
    }
}

impl Debug for TestTextField {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("TestTextField").finish_non_exhaustive()
    }
}

impl StatefulWidget for TestTextField {
    type State = TestTextFieldState;

    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn create_state(&self) -> TestTextFieldState {
        TestTextFieldState {
            state: StateData::new(),
            controller: None,
            focus_node: None,
            editable_text_key: GlobalKey::new(),
            selection_gesture_detector_builder: None,
        }
    }
}

/// The state of a [`TestTextField`].
pub struct TestTextFieldState {
    state: StateData<TestTextField>,
    controller: Option<Ref<TextEditingController>>,
    focus_node: Option<Ref<FocusNode>>,
    editable_text_key: GlobalKey,
    selection_gesture_detector_builder: Option<Ref<TestTextFieldSelectionGestureDetectorBuilder>>,
}

impl TestTextFieldState {
    /// The controller the field edits: the one it was given, or one it made.
    fn effective_controller(self: Handle<Self>, app: &mut App) -> TextEditingControllerRef {
        if let Some(controller) = &self.widget(app).controller {
            return controller.clone();
        }
        if app.get(self).controller.is_none() {
            let controller = TextEditingController::new(app);
            app.get_mut(self).controller = Some(controller);
        }
        app.get(self)
            .controller
            .as_ref()
            .expect("made above")
            .erase()
    }

    /// The focus node the field uses: the one it was given, or one it made.
    fn effective_focus_node(self: Handle<Self>, app: &mut App) -> FocusNodeRef {
        if let Some(focus_node) = &self.widget(app).focus_node {
            return focus_node.clone();
        }
        if app.get(self).focus_node.is_none() {
            let focus_node = FocusNode::new(app);
            app.get_mut(self).focus_node = Some(focus_node);
        }
        app.get(self)
            .focus_node
            .as_ref()
            .expect("made above")
            .erase()
    }
}

impl TextSelectionGestureDetectorBuilderDelegate for TestTextFieldState {
    fn editable_text_key(self: Handle<Self>, app: &App) -> GlobalKey {
        app.get(self).editable_text_key.clone()
    }

    fn force_press_enabled(self: Handle<Self>, _app: &App) -> bool {
        false
    }

    fn selection_enabled(self: Handle<Self>, _app: &App) -> bool {
        true
    }
}

impl State for TestTextFieldState {
    type Widget = TestTextField;
    crate::state_accessors!();

    fn init_state(self: Handle<Self>, app: &mut App) {
        let builder = app.create(TestTextFieldSelectionGestureDetectorBuilder {
            builder: TextSelectionGestureDetectorBuilderData::new(
                self.as_text_selection_gesture_detector_builder_delegate(),
            ),
        });
        app.get_mut(self).selection_gesture_detector_builder = Some(builder);
    }

    fn dispose(self: Handle<Self>, app: &mut App) {
        if let Some(focus_node) = app.get(self).focus_node.clone() {
            focus_node.dispose(app);
        }
        if let Some(controller) = app.get(self).controller.clone() {
            controller.dispose(app);
        }
    }

    fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
        const RED: Color = Color::new(0xFFF44336);
        let controller = self.effective_controller(app);
        let focus_node = self.effective_focus_node(app);
        let widget = self.widget(app);
        let mut field = EditableText::new(
            controller,
            focus_node.clone(),
            widget.style.clone().unwrap_or_default(),
            widget.cursor_color.unwrap_or(RED),
            RED,
        )
        .key(app.get(self).editable_text_key.clone().into())
        .renderer_ignores_pointer(true)
        .selection_controls(
            widget
                .selection_controls
                .clone()
                .unwrap_or_else(|| Rc::new(TestTextSelectionHandleControls)),
        )
        .autofocus(widget.autofocus)
        .read_only(widget.read_only)
        .show_selection_handles(widget.show_selection_handles)
        .cursor_opacity_animates(widget.cursor_opacity_animates);
        if let Some(max_lines) = widget.max_lines {
            field = field.max_lines(Some(max_lines));
        }
        if let Some(show_cursor) = widget.show_cursor {
            field = field.show_cursor(show_cursor);
        }
        if let Some(select_all_on_focus) = widget.select_all_on_focus {
            field = field.select_all_on_focus(select_all_on_focus);
        }
        if let Some(on_submitted) = widget.on_submitted.clone() {
            field = field.on_submitted(move |app, value| on_submitted(app, value));
        }
        let builder = app
            .get(self)
            .selection_gesture_detector_builder
            .as_ref()
            .expect("made in init_state")
            .handle();
        let gestured = TextSelectionGestureDetectorBuilder::build_gesture_detector(
            builder,
            app,
            None,
            Some(HitTestBehavior::Translucent),
            field.into_widget(),
        );
        Semantics::new()
            .enabled(true)
            .on_tap(Rc::new(move |app: &mut App| {
                focus_node.handle().request_focus(app, None)
            }))
            .child(gestured)
            .into_widget()
    }
}

/// Dart's `_TestTextFieldSelectionGestureDetectorBuilder`: the base builder, with no `onTap` of
/// its own.
struct TestTextFieldSelectionGestureDetectorBuilder {
    builder: TextSelectionGestureDetectorBuilderData,
}

impl TextSelectionGestureDetectorBuilder for TestTextFieldSelectionGestureDetectorBuilder {
    crate::text_selection_gesture_detector_builder_accessors!(builder);
}

/// Dart's `TestTextSelectionHandleControls`: handles that take no room.
pub struct TestTextSelectionHandleControls;

#[allow(deprecated)]
impl TextSelectionControls for TestTextSelectionHandleControls {
    fn build_handle(
        &self,
        _app: &mut App,
        _context: BuildContext,
        _handle_type: TextSelectionHandleType,
        _text_line_height: f64,
        _on_tap: Option<waft_foundation::Listener>,
    ) -> WidgetRef {
        SizedBox::shrink().into_widget()
    }

    fn get_handle_anchor(
        &self,
        _handle_type: TextSelectionHandleType,
        _text_line_height: f64,
    ) -> Offset {
        Offset::ZERO
    }

    fn build_toolbar(
        &self,
        _app: &mut App,
        _context: BuildContext,
        _global_editable_region: waft_embedder::Rect,
        _text_line_height: f64,
        _selection_midpoint: Offset,
        _endpoints: &[waft_rendering::TextSelectionPoint],
        _delegate: AnyTextSelectionDelegate,
        _clipboard_status: Option<Ref<crate::ClipboardStatusNotifier>>,
        _last_secondary_tap_down_position: Option<Offset>,
    ) -> WidgetRef {
        SizedBox::shrink().into_widget()
    }

    fn get_handle_size(&self, _text_line_height: f64) -> Size {
        Size::ZERO
    }

    fn as_text_selection_handle_controls(&self) -> Option<&dyn TextSelectionHandleControls> {
        Some(self)
    }
}

impl TextSelectionHandleControls for TestTextSelectionHandleControls {}

/// A [`TestTextField`]'s selection controls: test handles that take no room.
pub fn test_text_selection_handle_controls() -> Rc<dyn TextSelectionControls> {
    Rc::new(TestTextSelectionHandleControls)
}