waft-widgets 0.17.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
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
//! Flutter counterpart: `widgets/navigation_toolbar.dart`.

use std::any::Any;
use std::rc::Rc;

use waft_embedder::{Offset, Size, TextDirection};
use waft_foundation::{App, ValueKey};
use waft_rendering::{
    BoxConstraints, ChildLayoutId, MultiChildLayoutChildren, MultiChildLayoutDelegate,
};

use crate::framework::{BuildContext, IntoWidget, KeyRef, StatelessWidget, WidgetRef};
use crate::widgets::basic::{CustomMultiChildLayout, Directionality, LayoutId};

/// [`NavigationToolbar`] is a layout helper to position 3 widgets or groups of
/// widgets along a horizontal axis that's sensible for an application's
/// navigation bar such as in Material Design and in iOS.
///
/// The [`leading`](Self::leading) and [`trailing`](Self::trailing) widgets occupy the edges of
/// the widget with reasonable size constraints while the [`middle`](Self::middle) widget
/// occupies the remaining space in either a center aligned or start aligned fashion.
///
/// Either directly use the themed app bars such as the iOS `CupertinoNavigationBar` or wrap
/// this widget with more theming specifications for your own custom app bar.
#[derive(Debug)]
pub struct NavigationToolbar {
    pub key: Option<KeyRef>,
    /// Widget to place at the start of the horizontal toolbar.
    pub leading: Option<WidgetRef>,
    /// Widget to place in the middle of the horizontal toolbar, occupying
    /// as much remaining space as possible.
    pub middle: Option<WidgetRef>,
    /// Widget to place at the end of the horizontal toolbar.
    pub trailing: Option<WidgetRef>,
    /// Whether to align the [`middle`](Self::middle) widget to the center of this widget or
    /// next to the [`leading`](Self::leading) widget when false.
    pub center_middle: bool,
    /// The spacing around the [`middle`](Self::middle) widget on horizontal axis.
    ///
    /// Defaults to [`K_MIDDLE_SPACING`](Self::K_MIDDLE_SPACING).
    pub middle_spacing: f64,
}

impl NavigationToolbar {
    /// The default spacing around the [`middle`](Self::middle) widget in dp.
    pub const K_MIDDLE_SPACING: f64 = 16.0;

    /// Creates a widget that lays out its children in a manner suitable for a
    /// toolbar; Dart's named arguments are the setters.
    pub fn new() -> NavigationToolbar {
        NavigationToolbar {
            key: None,
            leading: None,
            middle: None,
            trailing: None,
            center_middle: true,
            middle_spacing: NavigationToolbar::K_MIDDLE_SPACING,
        }
    }

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

    /// Dart `NavigationToolbar(leading:)`.
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn leading<K>(mut self, leading: impl IntoWidget<K>) -> NavigationToolbar {
        self.leading = Some(leading.into_widget());
        self
    }

    /// Dart `NavigationToolbar(middle:)`.
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn middle<K>(mut self, middle: impl IntoWidget<K>) -> NavigationToolbar {
        self.middle = Some(middle.into_widget());
        self
    }

    /// Dart `NavigationToolbar(trailing:)`.
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn trailing<K>(mut self, trailing: impl IntoWidget<K>) -> NavigationToolbar {
        self.trailing = Some(trailing.into_widget());
        self
    }

    /// Dart `NavigationToolbar(centerMiddle:)`.
    pub fn center_middle(mut self, center_middle: bool) -> NavigationToolbar {
        self.center_middle = center_middle;
        self
    }

    /// Dart `NavigationToolbar(middleSpacing:)`.
    pub fn middle_spacing(mut self, middle_spacing: f64) -> NavigationToolbar {
        self.middle_spacing = middle_spacing;
        self
    }
}

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

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

    fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
        // Dart's `debugCheckHasDirectionality` is `Directionality::of`'s own panic.
        let text_direction = Directionality::of(app, context);
        let mut children = Vec::new();
        if let Some(leading) = self.leading.clone() {
            children.push(LayoutId::new(ToolbarSlot::Leading.id(), leading).into_widget());
        }
        if let Some(middle) = self.middle.clone() {
            children.push(LayoutId::new(ToolbarSlot::Middle.id(), middle).into_widget());
        }
        if let Some(trailing) = self.trailing.clone() {
            children.push(LayoutId::new(ToolbarSlot::Trailing.id(), trailing).into_widget());
        }
        CustomMultiChildLayout::new(Rc::new(ToolbarLayout {
            center_middle: self.center_middle,
            middle_spacing: self.middle_spacing,
            text_direction,
        }))
        .children(children)
        .into_widget()
    }
}

/// Dart's `_ToolbarSlot`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum ToolbarSlot {
    Leading,
    Middle,
    Trailing,
}

impl ToolbarSlot {
    /// The [`ChildLayoutId`] this slot is known by, Dart's enum value as the `LayoutId.id`.
    fn id(self) -> ChildLayoutId {
        Rc::new(ValueKey::new(self))
    }
}

/// Dart's `_ToolbarLayout`.
#[derive(Debug)]
struct ToolbarLayout {
    /// If false the middle widget should be start-justified within the space between the
    /// leading and trailing widgets. If true the middle widget is centered within the toolbar
    /// (not within the horizontal space between the leading and trailing widgets).
    center_middle: bool,
    /// The spacing around middle widget on horizontal axis.
    middle_spacing: f64,
    text_direction: TextDirection,
}

impl MultiChildLayoutDelegate for ToolbarLayout {
    fn perform_layout(&self, app: &mut App, children: &mut MultiChildLayoutChildren, size: Size) {
        let mut leading_width = 0.0;
        let mut trailing_width = 0.0;

        if children.has_child(&ToolbarSlot::Leading.id()) {
            let constraints = BoxConstraints::new()
                .max_width(size.width())
                // The height should be exactly the height of the bar.
                .min_height(size.height())
                .max_height(size.height());
            leading_width = children
                .layout_child(app, &ToolbarSlot::Leading.id(), constraints)
                .width();
            let leading_x = match self.text_direction {
                TextDirection::Rtl => size.width() - leading_width,
                TextDirection::Ltr => 0.0,
            };
            children.position_child(app, &ToolbarSlot::Leading.id(), Offset::new(leading_x, 0.0));
        }

        if children.has_child(&ToolbarSlot::Trailing.id()) {
            let constraints = BoxConstraints::loose(size);
            let trailing_size =
                children.layout_child(app, &ToolbarSlot::Trailing.id(), constraints);
            let trailing_x = match self.text_direction {
                TextDirection::Rtl => 0.0,
                TextDirection::Ltr => size.width() - trailing_size.width(),
            };
            let trailing_y = (size.height() - trailing_size.height()) / 2.0;
            trailing_width = trailing_size.width();
            children.position_child(
                app,
                &ToolbarSlot::Trailing.id(),
                Offset::new(trailing_x, trailing_y),
            );
        }

        if children.has_child(&ToolbarSlot::Middle.id()) {
            let max_width =
                (size.width() - leading_width - trailing_width - self.middle_spacing * 2.0)
                    .max(0.0);
            let constraints = BoxConstraints::loose(size).max_width(max_width);
            let middle_size = children.layout_child(app, &ToolbarSlot::Middle.id(), constraints);

            let middle_start_margin = leading_width + self.middle_spacing;
            let mut middle_start = middle_start_margin;
            let middle_y = (size.height() - middle_size.height()) / 2.0;
            // If the centered middle will not fit between the leading and trailing
            // widgets, then align its left or right edge with the adjacent boundary.
            if self.center_middle {
                middle_start = (size.width() - middle_size.width()) / 2.0;
                if middle_start + middle_size.width() > size.width() - trailing_width {
                    middle_start =
                        size.width() - trailing_width - middle_size.width() - self.middle_spacing;
                } else if middle_start < middle_start_margin {
                    middle_start = middle_start_margin;
                }
            }

            let middle_x = match self.text_direction {
                TextDirection::Rtl => size.width() - middle_size.width() - middle_start,
                TextDirection::Ltr => middle_start,
            };

            children.position_child(
                app,
                &ToolbarSlot::Middle.id(),
                Offset::new(middle_x, middle_y),
            );
        }
    }

    fn should_relayout(&self, old_delegate: &dyn MultiChildLayoutDelegate) -> bool {
        let old_delegate = old_delegate
            .as_any()
            .downcast_ref::<ToolbarLayout>()
            .expect("the delegate types matched");
        old_delegate.center_middle != self.center_middle
            || old_delegate.middle_spacing != self.middle_spacing
            || old_delegate.text_direction != self.text_direction
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

#[cfg(test)]
mod tests {
    use waft_foundation::AppCell;
    use waft_rendering::{
        AnyRenderBox, ContainerBoxParentData, ContainerRenderObjectMixin,
        MultiChildLayoutParentData, RenderCustomMultiChildLayoutBox,
    };

    use super::*;
    use crate::test_harness::Harness;
    use crate::widgets::basic::SizedBox;

    fn sized(width: f64, height: f64) -> WidgetRef {
        SizedBox {
            width: Some(width),
            height: Some(height),
            ..Default::default()
        }
        .into_widget()
    }

    /// The toolbar's children, with the size and offset the delegate gave each.
    fn placements(harness: &Harness, app: &App) -> Vec<(Size, Offset)> {
        let toolbar: waft_rendering::RenderHandle<RenderCustomMultiChildLayoutBox> = harness
            .render_root(app)
            .child(app)
            .expect("a child")
            .as_object()
            .downcast::<RenderCustomMultiChildLayoutBox>(app)
            .expect("a RenderCustomMultiChildLayoutBox");
        let mut placements = Vec::new();
        let mut child: Option<AnyRenderBox> = toolbar.first_child(app);
        while let Some(current) = child {
            let parent_data = current
                .as_object()
                .parent_data_of::<MultiChildLayoutParentData>(app);
            placements.push((current.size(app), parent_data.offset()));
            child = toolbar.child_after(app, current);
        }
        placements
    }

    /// A toolbar with fixed-size children under the given direction, mounted and laid out.
    fn toolbar(
        app: &mut App,
        text_direction: TextDirection,
        center_middle: bool,
        leading: Option<(f64, f64)>,
        middle: Option<(f64, f64)>,
        trailing: Option<(f64, f64)>,
    ) -> Harness {
        let mut bar = NavigationToolbar::new().center_middle(center_middle);
        if let Some((width, height)) = leading {
            bar = bar.leading(sized(width, height));
        }
        if let Some((width, height)) = middle {
            bar = bar.middle(sized(width, height));
        }
        if let Some((width, height)) = trailing {
            bar = bar.trailing(sized(width, height));
        }
        let harness = Harness::mount(app, Directionality::new(text_direction, bar).into_widget());
        harness.pump(app);
        harness
    }

    /// The view is 300 x 200, so `size.height` is 200 and the leading widget is stretched to it.
    #[test]
    fn a_centered_middle_sits_in_the_middle_of_the_bar_in_both_directions() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let harness = toolbar(
            &mut app,
            TextDirection::Ltr,
            true,
            Some((50.0, 20.0)),
            Some((60.0, 20.0)),
            Some((40.0, 20.0)),
        );
        assert_eq!(
            placements(&harness, &app),
            vec![
                (Size::new(50.0, 200.0), Offset::new(0.0, 0.0)),
                (Size::new(60.0, 20.0), Offset::new(120.0, 90.0)),
                (Size::new(40.0, 20.0), Offset::new(260.0, 90.0)),
            ]
        );

        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let harness = toolbar(
            &mut app,
            TextDirection::Rtl,
            true,
            Some((50.0, 20.0)),
            Some((60.0, 20.0)),
            Some((40.0, 20.0)),
        );
        assert_eq!(
            placements(&harness, &app),
            vec![
                (Size::new(50.0, 200.0), Offset::new(250.0, 0.0)),
                (Size::new(60.0, 20.0), Offset::new(120.0, 90.0)),
                (Size::new(40.0, 20.0), Offset::new(0.0, 90.0)),
            ]
        );
    }

    #[test]
    fn an_uncentered_middle_starts_next_to_the_leading_widget_in_both_directions() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let harness = toolbar(
            &mut app,
            TextDirection::Ltr,
            false,
            Some((50.0, 20.0)),
            Some((60.0, 20.0)),
            Some((40.0, 20.0)),
        );
        assert_eq!(
            placements(&harness, &app)[1],
            (Size::new(60.0, 20.0), Offset::new(66.0, 90.0)),
            "leading width plus the middle spacing"
        );

        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let harness = toolbar(
            &mut app,
            TextDirection::Rtl,
            false,
            Some((50.0, 20.0)),
            Some((60.0, 20.0)),
            Some((40.0, 20.0)),
        );
        assert_eq!(
            placements(&harness, &app)[1],
            (Size::new(60.0, 20.0), Offset::new(174.0, 90.0)),
            "the same margin measured from the right edge"
        );
    }

    #[test]
    fn a_centered_middle_that_would_overlap_the_leading_widget_is_pushed_off_it() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let harness = toolbar(
            &mut app,
            TextDirection::Ltr,
            true,
            Some((200.0, 20.0)),
            Some((100.0, 20.0)),
            Some((40.0, 20.0)),
        );
        assert_eq!(
            placements(&harness, &app)[1],
            (Size::new(28.0, 20.0), Offset::new(216.0, 90.0)),
            "the middle is capped at the free width and aligned with the leading edge"
        );
    }

    #[test]
    fn a_centered_middle_that_would_overlap_the_trailing_widget_is_pushed_off_it() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let harness = toolbar(
            &mut app,
            TextDirection::Ltr,
            true,
            None,
            Some((100.0, 20.0)),
            Some((200.0, 20.0)),
        );
        assert_eq!(
            placements(&harness, &app),
            vec![
                (Size::new(68.0, 20.0), Offset::new(16.0, 90.0)),
                (Size::new(200.0, 20.0), Offset::new(100.0, 90.0)),
            ],
            "with no leading widget the middle is the first child"
        );
    }

    #[test]
    fn a_toolbar_lays_out_only_the_children_it_was_given() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let harness = toolbar(
            &mut app,
            TextDirection::Ltr,
            true,
            None,
            Some((60.0, 20.0)),
            None,
        );
        assert_eq!(
            placements(&harness, &app),
            vec![(Size::new(60.0, 20.0), Offset::new(120.0, 90.0))]
        );
    }
}