waft-material 0.8.0

Material Design 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! Flutter counterpart: material_ui's `spell_check_suggestions_toolbar.dart`.

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

use waft_cupertino::CupertinoTextSelectionToolbar;
use waft_embedder::{Offset, TextRange};
use waft_foundation::{App, Handle, Listener};
use waft_painting::{
    AlignmentGeometry, Border, BorderSide, BorderStyle, BoxDecoration, EdgeInsets,
    EdgeInsetsGeometry, TextStyle,
};
use waft_scheduler::{FrameCallback, SchedulerBinding};
use waft_services::{SelectionChangedCause, TextInputClient, TextSelectionDelegate};
use waft_widgets::{
    AnimatedSize, BuildContext, Column, ContextMenuButtonItem, ContextMenuButtonType,
    CrossAxisAlignment, CustomSingleChildLayout, DecoratedBox, EditableTextState, IntoWidget,
    KeyRef, MainAxisSize, MediaQuery, Padding, SizedBox, State, StatelessWidget, Text,
    TextSelectionToolbarAnchors, WidgetRef,
};

use crate::adaptive_text_selection_toolbar::AdaptiveTextSelectionToolbar;
use crate::colors::Colors;
use crate::material::{Material, MaterialType};
use crate::spell_check_suggestions_toolbar_layout_delegate::SpellCheckSuggestionsToolbarLayoutDelegate;
use crate::text_selection_toolbar_text_button::TextSelectionToolbarTextButton;

// The default height of the SpellCheckSuggestionsToolbar, which
// assumes there are the maximum number of spell check suggestions available, 3.
// Size eyeballed on Pixel 4 emulator running Android API 31.
const K_DEFAULT_TOOLBAR_HEIGHT: f64 = 193.0;

/// The maximum number of suggestions in the toolbar is 3, plus a delete button.
const K_MAX_SUGGESTIONS: usize = 3;

/// The default spell check suggestions toolbar for Android.
///
/// Tries to position itself below the [`anchor`](Self::anchor), but if it doesn't fit, then it
/// readjusts to fit above bottom view insets.
///
/// See also:
///
///  * `CupertinoSpellCheckSuggestionsToolbar`, which is similar but builds an
///    iOS-style spell check toolbar.
pub struct SpellCheckSuggestionsToolbar {
    pub key: Option<KeyRef>,
    /// The focal point below which the toolbar attempts to position itself.
    pub anchor: Offset,
    /// The [`ContextMenuButtonItem`]s that will be turned into the correct button
    /// widgets and displayed in the spell check suggestions toolbar.
    ///
    /// Must not contain more than four items, typically three suggestions and a
    /// delete button.
    ///
    /// See also:
    ///
    ///  * [`AdaptiveTextSelectionToolbar::button_items`], the list of
    ///    [`ContextMenuButtonItem`]s that are used to build the buttons of the
    ///    text selection toolbar.
    ///  * `CupertinoSpellCheckSuggestionsToolbar::button_items`, the list of
    ///    [`ContextMenuButtonItem`]s used to build the Cupertino style spell check
    ///    suggestions toolbar.
    pub button_items: Vec<ContextMenuButtonItem>,
}

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

impl SpellCheckSuggestionsToolbar {
    /// Constructs a [`SpellCheckSuggestionsToolbar`].
    ///
    /// `button_items` must not contain more than four items, generally three
    /// suggestions and one delete button.
    pub fn new(
        anchor: Offset,
        button_items: Vec<ContextMenuButtonItem>,
    ) -> SpellCheckSuggestionsToolbar {
        debug_assert!(button_items.len() <= K_MAX_SUGGESTIONS + 1);
        SpellCheckSuggestionsToolbar {
            key: None,
            anchor,
            button_items,
        }
    }

    /// Dart `SpellCheckSuggestionsToolbar(key:)`.
    pub fn key(mut self, key: KeyRef) -> SpellCheckSuggestionsToolbar {
        self.key = Some(key);
        self
    }

    /// Constructs a [`SpellCheckSuggestionsToolbar`] with the default children for
    /// an `EditableText`.
    ///
    /// See also:
    ///  * `CupertinoSpellCheckSuggestionsToolbar::editable_text`, which is similar
    ///    but builds an iOS-style toolbar.
    pub fn editable_text(
        app: &mut App,
        editable_text_state: Handle<EditableTextState>,
    ) -> SpellCheckSuggestionsToolbar {
        SpellCheckSuggestionsToolbar {
            key: None,
            button_items: Self::build_button_items(app, editable_text_state).unwrap_or_default(),
            anchor: Self::get_toolbar_anchor(editable_text_state.context_menu_anchors(app)),
        }
    }

    /// Builds the button items for the toolbar based on the available
    /// spell check suggestions.
    pub fn build_button_items(
        app: &mut App,
        editable_text_state: Handle<EditableTextState>,
    ) -> Option<Vec<ContextMenuButtonItem>> {
        // Determine if composing region is misspelled.
        let span_at_cursor_index = editable_text_state.find_suggestion_span_at_cursor_index(
            app,
            TextInputClient::current_text_editing_value(editable_text_state, app)
                .expect("an EditableTextState always has a value")
                .selection
                .base_offset,
        )?;

        let mut button_items = Vec::new();

        // Build suggestion buttons.
        let range = span_at_cursor_index.range;
        for suggestion in span_at_cursor_index
            .suggestions
            .into_iter()
            .take(K_MAX_SUGGESTIONS)
        {
            let label = suggestion.clone();
            button_items.push(
                ContextMenuButtonItem::new(Some(Listener::new(move |app| {
                    if !editable_text_state.mounted(app) {
                        return;
                    }
                    Self::replace_text(app, editable_text_state, &suggestion, range);
                })))
                .label(label),
            );
        }

        // Build delete button.
        let delete_button = ContextMenuButtonItem::new(Some(Listener::new(move |app| {
            if !editable_text_state.mounted(app) {
                return;
            }
            let composing = TextInputClient::current_text_editing_value(editable_text_state, app)
                .expect("an EditableTextState always has a value")
                .composing;
            Self::replace_text(app, editable_text_state, "", composing);
        })))
        .r#type(ContextMenuButtonType::Delete);
        button_items.push(delete_button);

        Some(button_items)
    }

    fn replace_text(
        app: &mut App,
        editable_text_state: Handle<EditableTextState>,
        text: &str,
        replacement_range: TextRange,
    ) {
        // Replacement cannot be performed if the text is read only or obscured.
        debug_assert!({
            let widget = editable_text_state.widget(app);
            !widget.read_only && !widget.obscure_text
        });

        let new_value = editable_text_state
            .text_editing_value(app)
            .replaced(replacement_range, text);
        editable_text_state.user_update_text_editing_value(
            app,
            new_value,
            SelectionChangedCause::Toolbar,
        );

        // Schedule a call to bringIntoView() after renderEditable updates.
        SchedulerBinding::add_post_frame_callback(
            app,
            FrameCallback::new(move |app, _duration| {
                if editable_text_state.mounted(app) {
                    let extent = editable_text_state
                        .text_editing_value(app)
                        .selection
                        .extent();
                    editable_text_state.bring_into_view(app, extent);
                }
            }),
            None,
        );
        editable_text_state.hide_toolbar(app, true);
    }

    /// Determines the Offset that the toolbar will be anchored to.
    pub fn get_toolbar_anchor(anchors: TextSelectionToolbarAnchors) -> Offset {
        // Since this will be positioned below the anchor point, use the secondary
        // anchor by default.
        match anchors.secondary_anchor {
            None => anchors.primary_anchor,
            Some(secondary_anchor) => secondary_anchor,
        }
    }

    /// Builds the toolbar buttons based on the [`button_items`](Self::button_items).
    fn build_toolbar_buttons(&self, app: &mut App, context: BuildContext) -> Vec<WidgetRef> {
        self.button_items
            .iter()
            .map(|button_item| {
                let mut label = Text::new(AdaptiveTextSelectionToolbar::get_button_label(
                    app,
                    context,
                    button_item,
                ));
                if button_item.r#type == ContextMenuButtonType::Delete {
                    label = label.style(TextStyle::new().color(Colors::BLUE.primary()));
                }
                let mut button = TextSelectionToolbarTextButton::new(
                    label,
                    EdgeInsets::from_ltrb(20.0, 0.0, 0.0, 0.0),
                )
                .alignment(AlignmentGeometry::CENTER_LEFT);
                if let Some(on_pressed) = button_item.on_pressed.clone() {
                    button = button.on_pressed(on_pressed);
                }

                if button_item.r#type != ContextMenuButtonType::Delete {
                    return button.into_widget();
                }
                DecoratedBox::new(BoxDecoration::new().border(Border {
                    top: BorderSide::new(
                        Colors::GREY.primary(),
                        1.0,
                        BorderStyle::Solid,
                        BorderSide::STROKE_ALIGN_INSIDE,
                    ),
                    ..Border::default()
                }))
                .child(button)
                .into_widget()
            })
            .collect()
    }
}

impl StatelessWidget for SpellCheckSuggestionsToolbar {
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
        if self.button_items.is_empty() {
            return SizedBox::shrink().into_widget();
        }

        // Adjust toolbar height if needed.
        let spell_check_suggestions_toolbar_height =
            K_DEFAULT_TOOLBAR_HEIGHT - (48.0 * (4.0 - self.button_items.len() as f64));
        // Incorporate the padding distance between the content and toolbar.
        let media_query_data = MediaQuery::of(app, context);
        let soft_keyboard_view_insets_bottom = media_query_data.view_insets.bottom;
        let padding_above =
            media_query_data.padding.top + CupertinoTextSelectionToolbar::K_TOOLBAR_SCREEN_PADDING;
        // Makes up for the Padding.
        let local_adjustment = Offset::new(
            CupertinoTextSelectionToolbar::K_TOOLBAR_SCREEN_PADDING,
            padding_above,
        );

        let children = self.build_toolbar_buttons(app, context);
        Padding::new(EdgeInsetsGeometry::from_ltrb(
            CupertinoTextSelectionToolbar::K_TOOLBAR_SCREEN_PADDING,
            padding_above,
            CupertinoTextSelectionToolbar::K_TOOLBAR_SCREEN_PADDING,
            CupertinoTextSelectionToolbar::K_TOOLBAR_SCREEN_PADDING
                + soft_keyboard_view_insets_bottom,
        ))
        .child(
            CustomSingleChildLayout::new(Rc::new(SpellCheckSuggestionsToolbarLayoutDelegate::new(
                self.anchor - local_adjustment,
            )))
            .child(
                // This duration was eyeballed on a Pixel 2 emulator running Android
                // API 28 for the Material TextSelectionToolbar.
                AnimatedSize::new(Duration::from_millis(140)).child(
                    SpellCheckSuggestionsToolbarContainer {
                        height: spell_check_suggestions_toolbar_height,
                        children,
                    },
                ),
            ),
        )
        .into_widget()
    }
}

/// The Material-styled toolbar outline for the spell check suggestions
/// toolbar.
#[derive(Debug)]
struct SpellCheckSuggestionsToolbarContainer {
    height: f64,
    children: Vec<WidgetRef>,
}

impl StatelessWidget for SpellCheckSuggestionsToolbarContainer {
    fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
        Material::new()
            // This elevation was eyeballed on a Pixel 4 emulator running Android
            // API 31 for the SpellCheckSuggestionsToolbar.
            .elevation(2.0)
            .r#type(MaterialType::Card)
            .child(
                SizedBox::new()
                    // This width was eyeballed on a Pixel 4 emulator running Android
                    // API 31 for the SpellCheckSuggestionsToolbar.
                    .width(165.0)
                    .height(self.height)
                    .child(
                        Column::new()
                            .main_axis_size(MainAxisSize::Min)
                            .cross_axis_alignment(CrossAxisAlignment::Stretch)
                            .children(self.children.clone()),
                    ),
            )
            .into_widget()
    }
}

#[cfg(test)]
mod tests {
    use waft_embedder::Size;
    use waft_widgets::Center;

    use waft_widgets::testing::{WidgetTester, find, test_widgets};

    use super::*;
    use crate::scaffold::Scaffold;
    use crate::test_support::material_app;
    use crate::theme_data::ThemeData;

    // Vertical position at which to anchor the toolbar for testing.
    const K_ANCHOR: f64 = 200.0;
    // Amount for toolbar to overlap bottom padding for testing.
    const K_TEST_TOOLBAR_OVERLAP: f64 = 10.0;

    /// Builds test button items for each of the suggestions provided.
    fn build_suggestion_buttons(suggestions: &[&str]) -> Vec<ContextMenuButtonItem> {
        let mut items: Vec<ContextMenuButtonItem> = suggestions
            .iter()
            .map(|suggestion| {
                ContextMenuButtonItem::new(Some(Listener::new(|_app| {}))).label(*suggestion)
            })
            .collect();
        items.push(
            ContextMenuButtonItem::new(Some(Listener::new(|_app| {})))
                .r#type(ContextMenuButtonType::Delete)
                .label("DELETE"),
        );
        items
    }

    /// `MaterialApp(home: Scaffold(body: SpellCheckSuggestionsToolbar(anchor:, buttonItems:)))`.
    fn pump_toolbar(tester: &mut WidgetTester, anchor: Offset, suggestions: &[&str]) {
        let root = {
            let app = tester.cell.borrow();
            material_app(
                ThemeData::new(&app),
                Scaffold::new()
                    .body(SpellCheckSuggestionsToolbar::new(
                        anchor,
                        build_suggestion_buttons(suggestions),
                    ))
                    .into_widget(),
            )
        };
        tester.pump_widget(root);
    }

    /// Dart's `tester.getTopLeft(findSpellCheckSuggestionsToolbar()).dy`.
    fn toolbar_y(tester: &WidgetTester) -> f64 {
        let containers = find::by_type::<SpellCheckSuggestionsToolbarContainer>().evaluate(tester);
        assert_eq!(containers.len(), 1);
        tester.get_rect(containers[0]).top
    }

    #[test]
    fn positions_toolbar_below_anchor_when_it_fits_above_bottom_view_padding() {
        test_widgets(|tester| {
            // We expect the toolbar to be positioned right below the anchor with padding
            // accounted for.
            pump_toolbar(
                tester,
                Offset::new(0.0, K_ANCHOR),
                &["hello", "yellow", "yell"],
            );
            assert_eq!(toolbar_y(tester), K_ANCHOR);
        });
    }

    #[test]
    fn re_positions_toolbar_higher_below_anchor_when_it_does_not_fit_above_bottom_view_padding() {
        test_widgets(|tester| {
            // We expect the toolbar to be positioned _kTestToolbarOverlap pixels above the
            // anchor.
            let expected_toolbar_y = K_ANCHOR - K_TEST_TOOLBAR_OVERLAP;
            pump_toolbar(
                tester,
                Offset::new(0.0, K_ANCHOR - K_TEST_TOOLBAR_OVERLAP),
                &["hello", "yellow", "yell"],
            );
            assert_eq!(toolbar_y(tester), expected_toolbar_y);
        });
    }

    /// Dart's `more than three suggestions throws an error`, its first half: three build.
    #[test]
    fn three_suggestions_build() {
        test_widgets(|tester| {
            pump_toolbar(
                tester,
                Offset::new(0.0, K_ANCHOR - K_TEST_TOOLBAR_OVERLAP),
                &["hello", "yellow", "yell"],
            );
        });
    }

    /// Dart's `more than three suggestions throws an error`, its second half.
    #[test]
    #[should_panic(expected = "button_items.len() <= K_MAX_SUGGESTIONS + 1")]
    fn more_than_three_suggestions_panic() {
        let _ = SpellCheckSuggestionsToolbar::new(
            Offset::new(0.0, K_ANCHOR - K_TEST_TOOLBAR_OVERLAP),
            build_suggestion_buttons(&["hello", "yellow", "yell", "yeller"]),
        );
    }

    #[test]
    fn spell_check_suggestions_toolbar_does_not_crash_at_zero_area() {
        test_widgets(|tester| {
            let root = {
                let app = tester.cell.borrow();
                material_app(
                    ThemeData::new(&app),
                    Center::new()
                        .child(SizedBox::shrink().child(SpellCheckSuggestionsToolbar::new(
                            Offset::new(1.0, 1.0),
                            build_suggestion_buttons(&["X", "Y"]),
                        )))
                        .into_widget(),
                )
            };
            tester.pump_widget(root);
            let toolbars = find::by_type::<SpellCheckSuggestionsToolbar>().evaluate(tester);
            let render = tester.render_box_of(toolbars[0]);
            assert_eq!(render.size(&tester.cell.borrow()), Size::ZERO);
        });
    }

    #[test]
    fn get_toolbar_anchor_prefers_the_secondary_anchor() {
        let primary = Offset::new(1.0, 2.0);
        let secondary = Offset::new(3.0, 4.0);
        assert_eq!(
            SpellCheckSuggestionsToolbar::get_toolbar_anchor(TextSelectionToolbarAnchors::new(
                primary
            )),
            primary
        );
        assert_eq!(
            SpellCheckSuggestionsToolbar::get_toolbar_anchor(
                TextSelectionToolbarAnchors::new(primary).secondary_anchor(secondary)
            ),
            secondary
        );
    }
}