waft-material 0.14.1

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
//! Flutter counterpart: material_ui's `material_state_mixin.dart`.
//!
//! [`MaterialStateMixin`] is a mixin with fields ([`MaterialStateMixinData`]) on a [`State`],
//! which holds it under the field `material_state`.

use std::rc::Rc;

use waft_foundation::{
    App, DiagnosticPropertiesBuilder, DiagnosticsProperty, Handle, ValueChanged,
};
use waft_widgets::{State, WidgetState, WidgetStates};

/// The fields of Dart's `MaterialStateMixin`, which a state holds under the field
/// `material_state`.
#[derive(Debug, Default)]
pub struct MaterialStateMixinData {
    /// Managed set of active [`WidgetState`] values; designed to be passed to
    /// `WidgetStateProperty::resolve` methods.
    ///
    /// To mutate and have `set_state` called automatically for you, use
    /// [`MaterialStateMixin::set_material_state`], [`MaterialStateMixin::add_material_state`],
    /// or [`MaterialStateMixin::remove_material_state`]. Directly mutating the set is possible,
    /// and may be necessary if you need to alter its list without calling `set_state` (and thus
    /// triggering a re-render).
    pub material_states: WidgetStates,
}

impl MaterialStateMixinData {
    /// The bag of a state with no active [`WidgetState`].
    pub fn new() -> MaterialStateMixinData {
        MaterialStateMixinData::default()
    }

    /// Dart's `MaterialStateMixin.debugFillProperties`, shared by every state with the mixin.
    pub(crate) fn debug_fill_properties(&self, properties: &mut DiagnosticPropertiesBuilder) {
        properties.add(
            DiagnosticsProperty::<String>::new(
                "materialStates",
                Some(waft_widgets::widget_states(&self.material_states)),
            )
            .default_value(Some("{}".to_string())),
        );
    }
}

/// Mixin for [`State`] classes that require knowledge of changing [`WidgetState`]
/// values for their child widgets.
///
/// This mixin does nothing by mere application to a [`State`] class, but is
/// helpful when writing `build` methods that include child `InkWell`,
/// `GestureDetector`, `MouseRegion`, or `Focus` widgets. Instead of manually
/// creating handlers for each type of user interaction, such [`State`] classes can
/// instead provide a `ValueChanged<bool>` function and allow [`MaterialStateMixin`]
/// to manage the set of active [`WidgetState`]s, and the calling of `set_state`
/// as necessary.
///
/// The state holds a [`MaterialStateMixinData`] under the field `material_state`.
///
/// ```text
/// impl State for MyWidgetState {
///     fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef {
///         let color = self.widget(app).color.clone();
///         InkWell::new()
///             .on_focus_change(self.update_material_state(WidgetState::Focused, None))
///             .child(ColoredBox::new(color.resolve(self.material_states(app))))
///             .into_widget()
///     }
/// }
/// ```
pub trait MaterialStateMixin: State {
    /// Mixin field access.
    fn material_state_data(self: Handle<Self>, app: &App) -> &MaterialStateMixinData;

    /// See [`material_state_data`](Self::material_state_data).
    fn material_state_data_mut(self: Handle<Self>, app: &mut App) -> &mut MaterialStateMixinData;

    /// Dart's `materialStates`: the set of active [`WidgetState`] values.
    fn material_states(self: Handle<Self>, app: &App) -> &WidgetStates {
        &self.material_state_data(app).material_states
    }

    /// Callback factory which accepts a [`WidgetState`] value and returns a
    /// closure to mutate [`material_states`](Self::material_states) and call `set_state`.
    ///
    /// Accepts an optional second argument, `on_changed`, which allows
    /// arbitrary functionality to be wired through the [`MaterialStateMixin`].
    /// If supplied, the `on_changed` function is only called when child widgets
    /// report events that make changes to the current set of [`WidgetState`]s.
    ///
    /// ```text
    /// InkWell::new().on_highlight_changed(self.update_material_state(
    ///     WidgetState::Pressed,
    ///     Some(Rc::new(move |app, value| {
    ///         if value {
    ///             self.widget(app).on_pressed();
    ///         }
    ///     })),
    /// ))
    /// ```
    fn update_material_state(
        self: Handle<Self>,
        key: WidgetState,
        on_changed: Option<ValueChanged<bool>>,
    ) -> ValueChanged<bool> {
        Rc::new(move |app: &mut App, value: bool| {
            if self.material_states(app).contains(&key) == value {
                return;
            }
            self.set_material_state(app, key, value);
            if let Some(on_changed) = &on_changed {
                on_changed(app, value);
            }
        })
    }

    /// Mutator to mark a [`WidgetState`] value as either active or inactive.
    fn set_material_state(self: Handle<Self>, app: &mut App, state: WidgetState, is_set: bool) {
        if is_set {
            self.add_material_state(app, state)
        } else {
            self.remove_material_state(app, state)
        }
    }

    /// Mutator to mark a [`WidgetState`] value as active.
    fn add_material_state(self: Handle<Self>, app: &mut App, state: WidgetState) {
        if self
            .material_state_data_mut(app)
            .material_states
            .insert(state)
        {
            self.set_state(app, |_| {});
        }
    }

    /// Mutator to mark a [`WidgetState`] value as inactive.
    fn remove_material_state(self: Handle<Self>, app: &mut App, state: WidgetState) {
        if self
            .material_state_data_mut(app)
            .material_states
            .remove(&state)
        {
            self.set_state(app, |_| {});
        }
    }

    /// Getter for whether this class considers [`WidgetState::Disabled`] to be active.
    fn is_disabled(self: Handle<Self>, app: &App) -> bool {
        self.material_states(app).contains(&WidgetState::Disabled)
    }

    /// Getter for whether this class considers [`WidgetState::Dragged`] to be active.
    fn is_dragged(self: Handle<Self>, app: &App) -> bool {
        self.material_states(app).contains(&WidgetState::Dragged)
    }

    /// Getter for whether this class considers [`WidgetState::Error`] to be active.
    fn is_errored(self: Handle<Self>, app: &App) -> bool {
        self.material_states(app).contains(&WidgetState::Error)
    }

    /// Getter for whether this class considers [`WidgetState::Focused`] to be active.
    fn is_focused(self: Handle<Self>, app: &App) -> bool {
        self.material_states(app).contains(&WidgetState::Focused)
    }

    /// Getter for whether this class considers [`WidgetState::Hovered`] to be active.
    fn is_hovered(self: Handle<Self>, app: &App) -> bool {
        self.material_states(app).contains(&WidgetState::Hovered)
    }

    /// Getter for whether this class considers [`WidgetState::Pressed`] to be active.
    fn is_pressed(self: Handle<Self>, app: &App) -> bool {
        self.material_states(app).contains(&WidgetState::Pressed)
    }

    /// Getter for whether this class considers [`WidgetState::ScrolledUnder`] to be active.
    fn is_scrolled_under(self: Handle<Self>, app: &App) -> bool {
        self.material_states(app)
            .contains(&WidgetState::ScrolledUnder)
    }

    /// Getter for whether this class considers [`WidgetState::Selected`] to be active.
    fn is_selected(self: Handle<Self>, app: &App) -> bool {
        self.material_states(app).contains(&WidgetState::Selected)
    }
}

#[cfg(test)]
mod tests {
    use std::cell::RefCell;
    use std::fmt::{self, Debug};

    use waft_embedder::Color;
    use waft_widgets::{
        BuildContext, ColoredBox, Container, GlobalKey, IntoWidget, StateData, StatefulWidget,
        WidgetRef, downcast_widget,
    };

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

    use super::*;

    const TRUE_COLOR: Color = Color::new(0xFFF44336);
    const FALSE_COLOR: Color = Color::new(0xFF4CAF50);

    /// What Dart's `StreamController<bool>` carries: the callback the inner widget got.
    type Controller = Rc<RefCell<Option<ValueChanged<bool>>>>;

    /// Mock widget which plays the role of a button: it hands its `on_value_changed` to the
    /// controller, through which a test emits notifications that [`WidgetState`] values are
    /// now in or out of play (Dart's `_InnerWidget`, whose `initState` listens to the stream).
    struct InnerWidget {
        on_value_changed: ValueChanged<bool>,
        controller: Controller,
    }

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

    struct InnerWidgetState {
        state: StateData<InnerWidget>,
    }

    impl StatefulWidget for InnerWidget {
        type State = InnerWidgetState;

        fn create_state(&self) -> InnerWidgetState {
            InnerWidgetState {
                state: StateData::new(),
            }
        }
    }

    impl State for InnerWidgetState {
        type Widget = InnerWidget;
        waft_widgets::state_accessors!();

        fn init_state(self: Handle<Self>, app: &mut App) {
            let widget = self.widget(app);
            *widget.controller.borrow_mut() = Some(Rc::clone(&widget.on_value_changed));
        }

        fn build(self: Handle<Self>, _app: &mut App, _context: BuildContext) -> WidgetRef {
            Container::new().into_widget()
        }
    }

    /// Dart's `_MyWidget`.
    struct MyWidget {
        /// Wrapper around `MaterialStateMixin.is_pressed/is_hovered/is_focused/etc`.
        evaluator: fn(&App, Handle<MyWidgetState>) -> bool,
        controller: Controller,
        /// The value we're watching in the given test.
        material_state: WidgetState,
        key: GlobalKey,
    }

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

    struct MyWidgetState {
        state: StateData<MyWidget>,
        material_state: MaterialStateMixinData,
    }

    impl StatefulWidget for MyWidget {
        type State = MyWidgetState;

        fn create_state(&self) -> MyWidgetState {
            MyWidgetState {
                state: StateData::new(),
                material_state: MaterialStateMixinData::new(),
            }
        }
    }

    impl MaterialStateMixin for MyWidgetState {
        fn material_state_data(self: Handle<Self>, app: &App) -> &MaterialStateMixinData {
            &app.get(self).material_state
        }

        fn material_state_data_mut(
            self: Handle<Self>,
            app: &mut App,
        ) -> &mut MaterialStateMixinData {
            &mut app.get_mut(self).material_state
        }
    }

    impl State for MyWidgetState {
        type Widget = MyWidget;
        waft_widgets::state_accessors!();

        fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
            let widget = self.widget(app);
            let color = if (widget.evaluator)(app, self) {
                TRUE_COLOR
            } else {
                FALSE_COLOR
            };
            let (controller, material_state) =
                (Rc::clone(&widget.controller), widget.material_state);
            ColoredBox::new(color)
                .key(widget.key.clone().into())
                .child(InnerWidget {
                    on_value_changed: self.update_material_state(material_state, None),
                    controller,
                })
                .into_widget()
        }
    }

    fn colored_box_color(tester: &WidgetTester, key: &GlobalKey) -> u32 {
        let widget = key
            .current_widget(&tester.cell.borrow_mut())
            .expect("mounted");
        downcast_widget::<ColoredBox>(&*widget)
            .expect("a ColoredBox")
            .color
            .to_argb32()
    }

    fn verify(evaluator: fn(&App, Handle<MyWidgetState>) -> bool, material_state: WidgetState) {
        test_widgets(|tester| {
            let controller = Controller::default();
            let key = GlobalKey::new();
            tester.pump_widget(
                MyWidget {
                    evaluator,
                    controller: Rc::clone(&controller),
                    material_state,
                    key: key.clone(),
                }
                .into_widget(),
            );
            let emit = |tester: &mut WidgetTester, value: bool| {
                let on_value_changed = controller.borrow().clone().expect("the inner widget ran");
                on_value_changed(&mut tester.cell.borrow_mut(), value);
                tester.pump_and_settle();
            };

            // Set the value to True
            emit(tester, true);
            assert_eq!(colored_box_color(tester, &key), TRUE_COLOR.to_argb32());

            // Set the value to False
            emit(tester, false);
            assert_eq!(colored_box_color(tester, &key), FALSE_COLOR.to_argb32());
        });
    }

    #[test]
    fn widget_state_pressed_is_tracked() {
        verify(|app, state| state.is_pressed(app), WidgetState::Pressed);
    }

    #[test]
    fn widget_state_focused_is_tracked() {
        verify(|app, state| state.is_focused(app), WidgetState::Focused);
    }

    #[test]
    fn widget_state_hovered_is_tracked() {
        verify(|app, state| state.is_hovered(app), WidgetState::Hovered);
    }

    #[test]
    fn widget_state_disabled_is_tracked() {
        verify(|app, state| state.is_disabled(app), WidgetState::Disabled);
    }

    #[test]
    fn widget_state_selected_is_tracked() {
        verify(|app, state| state.is_selected(app), WidgetState::Selected);
    }

    #[test]
    fn widget_state_scrolled_under_is_tracked() {
        verify(
            |app, state| state.is_scrolled_under(app),
            WidgetState::ScrolledUnder,
        );
    }

    #[test]
    fn widget_state_dragged_is_tracked() {
        verify(|app, state| state.is_dragged(app), WidgetState::Dragged);
    }

    #[test]
    fn widget_state_error_is_tracked() {
        verify(|app, state| state.is_errored(app), WidgetState::Error);
    }
}