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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//! Flutter counterpart: `widgets/pages.dart`.

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

use waft_animation::AnimationRef;
use waft_foundation::{App, Handle, HandleId, Retained};
use waft_painting::Color;

use crate::framework::{BuildContext, WidgetRef};
use crate::widgets::navigator::{AnyRoute, Route, RouteData, RouteSettingsRef};
use crate::widgets::routes::{
    AnyModalRoute, AnyTransitionRoute, LocalHistoryRoute, LocalHistoryRouteData, ModalRoute,
    ModalRouteData, OverlayRoute, OverlayRouteData, PredictiveBackRoute, RoutePageBuilder,
    RouteTransitionsBuilder, TransitionRoute, TransitionRouteData,
};
use waft_foundation::Ref;

/// The fields Dart's `PageRoute` declares; every page route carries this bag under the field
/// `page_route`.
pub struct PageRouteData {
    /// Whether this page route is a full-screen dialog.
    ///
    /// In Material and Cupertino, being fullscreen has the effects of making the app bars have
    /// a close button instead of a back button. On iOS, dialogs transitions animate differently
    /// and are also not closeable with the back swipe gesture.
    pub fullscreen_dialog: bool,
    /// See [`TransitionRoute::allow_snapshotting`].
    pub allow_snapshotting: bool,
    /// See [`ModalRoute::barrier_dismissible`].
    pub barrier_dismissible: bool,
}

impl PageRouteData {
    /// The bag of a freshly created page route; Dart's constructor defaults.
    pub fn new() -> PageRouteData {
        PageRouteData {
            fullscreen_dialog: false,
            allow_snapshotting: true,
            barrier_dismissible: false,
        }
    }
}

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

/// The accessors [`PageRoute`] asks for, for a struct whose bag is the field `page_route`.
#[macro_export]
macro_rules! page_route_accessors {
    () => {
        fn page_route_data(
            self: ::waft_foundation::Handle<Self>,
            app: &::waft_foundation::App,
        ) -> &$crate::PageRouteData {
            &app.get(self).page_route
        }

        fn page_route_data_mut(
            self: ::waft_foundation::Handle<Self>,
            app: &mut ::waft_foundation::App,
        ) -> &mut $crate::PageRouteData {
            &mut app.get_mut(self).page_route
        }
    };
}

/// A modal route that replaces the entire screen.
///
/// [`PageRouteBuilder`] provides a way to create a [`PageRoute`] using callbacks rather than by
/// defining a new type.
///
/// If [`barrier_dismissible`](ModalRoute::barrier_dismissible) is true, then pressing the
/// escape key on the keyboard will cause the current route to be popped with no value.
///
/// See also:
///
///  * [`Route`], which documents the meaning of Dart's `T` generic type argument.
pub trait PageRoute: ModalRoute {
    /// Dart's `PageRoute` fields, held under the field `page_route`
    /// ([`page_route_accessors!`](crate::page_route_accessors)).
    fn page_route_data(self: Handle<Self>, app: &App) -> &PageRouteData;

    /// See [`page_route_data`](Self::page_route_data).
    fn page_route_data_mut(self: Handle<Self>, app: &mut App) -> &mut PageRouteData;

    /// This route as the erased [`AnyPageRoute`].
    fn as_page_route(self: Handle<Self>) -> AnyPageRoute {
        AnyPageRoute {
            id: self.id(),
            vtable: const { &PageRouteVTable::of::<Self>() },
        }
    }

    /// Dart's `PageRoute.fullscreenDialog`.
    fn fullscreen_dialog(self: Handle<Self>, app: &App) -> bool {
        self.page_route_data(app).fullscreen_dialog
    }

    /// Dart's `PageRoute.allowSnapshotting`.
    fn allow_snapshotting(self: Handle<Self>, app: &App) -> bool {
        self.page_route_data(app).allow_snapshotting
    }

    /// Dart's `PageRoute.opaque`.
    fn opaque(self: Handle<Self>, app: &App) -> bool {
        let _ = app;
        true
    }

    /// Dart's `PageRoute.barrierDismissible`.
    fn barrier_dismissible(self: Handle<Self>, app: &App) -> bool {
        self.page_route_data(app).barrier_dismissible
    }

    /// Dart's `PageRoute.canTransitionTo`.
    fn can_transition_to(self: Handle<Self>, app: &App, next_route: AnyTransitionRoute) -> bool {
        next_route.as_route().as_page_route(app).is_some()
    }

    /// Dart's `PageRoute.canTransitionFrom`.
    fn can_transition_from(
        self: Handle<Self>,
        app: &App,
        previous_route: AnyTransitionRoute,
    ) -> bool {
        previous_route.as_route().as_page_route(app).is_some()
    }

    /// Dart's `PageRoute.popGestureEnabled`.
    fn pop_gesture_enabled(self: Handle<Self>, app: &mut App) -> bool {
        // Fullscreen dialogs aren't dismissible by back swipe.
        !PageRoute::fullscreen_dialog(self, app) && ModalRoute::pop_gesture_enabled(self, app)
    }
}

/// The vtable of an erased [`AnyPageRoute`].
struct PageRouteVTable {
    type_name: fn() -> &'static str,
    route: fn(HandleId) -> AnyRoute,
    modal_route: fn(HandleId) -> AnyModalRoute,
}

impl PageRouteVTable {
    /// The table for one page route type.
    const fn of<R: PageRoute>() -> PageRouteVTable {
        PageRouteVTable {
            type_name: std::any::type_name::<R>,
            route: |id| Route::as_route(resolve::<R>(id)),
            modal_route: |id| ModalRoute::as_modal_route(resolve::<R>(id)),
        }
    }
}

/// Erased [`PageRoute`]: what Dart's `route is PageRoute` produces.
#[derive(Clone, Copy)]
pub struct AnyPageRoute {
    id: HandleId,
    vtable: &'static PageRouteVTable,
}

/// A kept page route as a [`RouteRef`] or a [`ModalRouteRef`], through
/// [`Retained::erase`]: Dart's upcast.
impl From<AnyPageRoute> for AnyRoute {
    fn from(route: AnyPageRoute) -> AnyRoute {
        route.as_route()
    }
}

impl From<AnyPageRoute> for AnyModalRoute {
    fn from(route: AnyPageRoute) -> AnyModalRoute {
        route.as_modal_route()
    }
}

/// A field Dart types as the erased object, kept: AnyPageRoute with its count.
pub type PageRouteRef = Retained<AnyPageRoute>;

waft_foundation::erased_handle!(AnyPageRoute: PageRoute => PageRoute::as_page_route);

impl Debug for AnyPageRoute {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}({:?})", (self.vtable.type_name)(), self.id)
    }
}

impl AnyPageRoute {
    /// This page route as the erased [`AnyRoute`].
    pub fn as_route(self) -> AnyRoute {
        (self.vtable.route)(self.id)
    }

    /// This page route as the erased [`AnyModalRoute`].
    pub fn as_modal_route(self) -> AnyModalRoute {
        (self.vtable.modal_route)(self.id)
    }
}

/// The typed handle for an erased id. Free: nothing is looked up; `get` checks the slot.
fn resolve<L: 'static>(id: HandleId) -> Handle<L> {
    Handle::from_id(id)
}

/// The `PredictiveBackRoute` implementation a [`PageRoute`] leaf inherits. Write it inside
/// `impl PredictiveBackRoute for MyRoute { .. }`.
#[macro_export]
macro_rules! page_predictive_back_route_overrides {
    () => {
        $crate::modal_predictive_back_route_overrides!($crate::PageRoute::pop_gesture_enabled);
    };
}

/// The `TransitionRoute` overrides a [`PageRoute`] leaf inherits, plus
/// [`modal_transition_route_overrides!`](crate::modal_transition_route_overrides). Write it
/// inside `impl TransitionRoute for MyRoute { .. }`, next to the leaf's own
/// `transition_duration` and `opaque` (which a leaf that does not override it writes as
/// `fn opaque(self: Handle<Self>, app: &App) -> bool { PageRoute::opaque(self, app) }`).
#[macro_export]
macro_rules! page_transition_route_overrides {
    () => {
        $crate::modal_transition_route_overrides!();

        fn allow_snapshotting(
            self: ::waft_foundation::Handle<Self>,
            app: &::waft_foundation::App,
        ) -> bool {
            $crate::PageRoute::allow_snapshotting(self, app)
        }

        fn can_transition_to(
            self: ::waft_foundation::Handle<Self>,
            app: &::waft_foundation::App,
            next_route: $crate::AnyTransitionRoute,
        ) -> bool {
            $crate::PageRoute::can_transition_to(self, app, next_route)
        }

        fn can_transition_from(
            self: ::waft_foundation::Handle<Self>,
            app: &::waft_foundation::App,
            previous_route: $crate::AnyTransitionRoute,
        ) -> bool {
            $crate::PageRoute::can_transition_from(self, app, previous_route)
        }
    };
}

/// The `Route` overrides a [`PageRoute`] leaf inherits, plus
/// [`modal_route_overrides!`](crate::modal_route_overrides). Write it inside
/// `impl Route for MyRoute { .. }`.
#[macro_export]
macro_rules! page_route_overrides {
    () => {
        $crate::modal_route_overrides!();

        fn as_page_route(
            self: ::waft_foundation::Handle<Self>,
        ) -> ::std::option::Option<$crate::AnyPageRoute> {
            ::std::option::Option::Some($crate::PageRoute::as_page_route(self))
        }
    };
}

/// Dart's `_defaultTransitionsBuilder`: a jump cut.
fn default_transitions_builder(
    _app: &mut App,
    _context: BuildContext,
    _animation: AnimationRef<f64>,
    _secondary_animation: AnimationRef<f64>,
    child: WidgetRef,
) -> WidgetRef {
    child
}

/// A utility type for defining one-off page routes in terms of callbacks.
///
/// Callers must define the [`page_builder`](Self::page_builder) function which creates the
/// route's primary contents. To add transitions define the
/// [`transitions_builder`](Self::transitions_builder) function.
///
/// See also:
///
///  * [`Route`], which documents the meaning of Dart's `T` generic type argument.
pub struct PageRouteBuilder {
    route: RouteData,
    overlay_route: OverlayRouteData,
    transition_route: TransitionRouteData,
    local_history: LocalHistoryRouteData,
    modal_route: ModalRouteData,
    page_route: PageRouteData,
    /// Used to build the route's primary contents.
    ///
    /// See [`ModalRoute::build_page`] for the complete definition of the parameters.
    pub page_builder: RoutePageBuilder,
    /// Used to build the route's transitions.
    ///
    /// The `animation` argument drives this route's own entrance and exit transition. The
    /// `secondary_animation` argument drives transitions for this route when another route is
    /// pushed on top of it or popped from above it, if both routes allow transition
    /// coordination. See [`TransitionRoute::can_transition_to`] and
    /// [`TransitionRoute::can_transition_from`].
    ///
    /// The default transition is a jump cut (i.e. no animation).
    pub transitions_builder: RouteTransitionsBuilder,
    transition_duration: Duration,
    reverse_transition_duration: Duration,
    opaque: bool,
    barrier_dismissible: bool,
    barrier_color: Option<Color>,
    barrier_label: Option<String>,
    maintain_state: bool,
}

impl PageRouteBuilder {
    /// Creates a route that delegates to builder callbacks; Dart's optional arguments are the
    /// setters.
    ///
    /// Dart's defaults: `transitions_builder` a jump cut, `transition_duration` and
    /// `reverse_transition_duration` 300 ms, `opaque` true, `barrier_dismissible` false,
    /// `maintain_state` true, `fullscreen_dialog` false, `allow_snapshotting` true.
    pub fn new(app: &mut App, page_builder: RoutePageBuilder) -> Ref<PageRouteBuilder> {
        let route = RouteData::new(app, None, None);
        let transition_route = TransitionRouteData::new(app);
        let modal_route = ModalRouteData::new(app);
        app.create(PageRouteBuilder {
            route,
            overlay_route: OverlayRouteData::new(),
            transition_route,
            local_history: LocalHistoryRouteData::new(),
            modal_route,
            page_route: PageRouteData::new(),
            page_builder,
            transitions_builder: Rc::new(default_transitions_builder),
            transition_duration: Duration::from_millis(300),
            reverse_transition_duration: Duration::from_millis(300),
            opaque: true,
            barrier_dismissible: false,
            barrier_color: None,
            barrier_label: None,
            maintain_state: true,
        })
    }

    /// Dart `PageRouteBuilder(settings:)`.
    pub fn settings(self: Ref<Self>, app: &mut App, settings: RouteSettingsRef) -> Ref<Self> {
        self.route_data_mut(app).set_settings(settings);
        self
    }

    /// Dart `PageRouteBuilder(requestFocus:)`.
    pub fn request_focus(self: Ref<Self>, app: &mut App, request_focus: bool) -> Ref<Self> {
        self.route_data_mut(app)
            .set_request_focus(Some(request_focus));
        self
    }

    /// Dart `PageRouteBuilder(transitionsBuilder:)`.
    pub fn transitions_builder(
        self: Ref<Self>,
        app: &mut App,
        transitions_builder: RouteTransitionsBuilder,
    ) -> Ref<Self> {
        app.get_mut(&self).transitions_builder = transitions_builder;
        self
    }

    /// Dart `PageRouteBuilder(transitionDuration:)`.
    pub fn transition_duration(
        self: Ref<Self>,
        app: &mut App,
        transition_duration: Duration,
    ) -> Ref<Self> {
        app.get_mut(&self).transition_duration = transition_duration;
        self
    }

    /// Dart `PageRouteBuilder(reverseTransitionDuration:)`.
    pub fn reverse_transition_duration(
        self: Ref<Self>,
        app: &mut App,
        reverse_transition_duration: Duration,
    ) -> Ref<Self> {
        app.get_mut(&self).reverse_transition_duration = reverse_transition_duration;
        self
    }

    /// Dart `PageRouteBuilder(opaque:)`.
    pub fn opaque(self: Ref<Self>, app: &mut App, opaque: bool) -> Ref<Self> {
        app.get_mut(&self).opaque = opaque;
        self
    }

    /// Dart `PageRouteBuilder(barrierDismissible:)`.
    pub fn barrier_dismissible(
        self: Ref<Self>,
        app: &mut App,
        barrier_dismissible: bool,
    ) -> Ref<Self> {
        app.get_mut(&self).barrier_dismissible = barrier_dismissible;
        self
    }

    /// Dart `PageRouteBuilder(barrierColor:)`.
    pub fn barrier_color(self: Ref<Self>, app: &mut App, barrier_color: Color) -> Ref<Self> {
        app.get_mut(&self).barrier_color = Some(barrier_color);
        self
    }

    /// Dart `PageRouteBuilder(barrierLabel:)`.
    pub fn barrier_label(self: Ref<Self>, app: &mut App, barrier_label: String) -> Ref<Self> {
        app.get_mut(&self).barrier_label = Some(barrier_label);
        self
    }

    /// Dart `PageRouteBuilder(maintainState:)`.
    pub fn maintain_state(self: Ref<Self>, app: &mut App, maintain_state: bool) -> Ref<Self> {
        app.get_mut(&self).maintain_state = maintain_state;
        self
    }

    /// Dart `PageRouteBuilder(fullscreenDialog:)`.
    pub fn fullscreen_dialog(self: Ref<Self>, app: &mut App, fullscreen_dialog: bool) -> Ref<Self> {
        self.page_route_data_mut(app).fullscreen_dialog = fullscreen_dialog;
        self
    }

    /// Dart `PageRouteBuilder(allowSnapshotting:)`.
    pub fn allow_snapshotting(
        self: Ref<Self>,
        app: &mut App,
        allow_snapshotting: bool,
    ) -> Ref<Self> {
        self.page_route_data_mut(app).allow_snapshotting = allow_snapshotting;
        self
    }
}

impl Route for PageRouteBuilder {
    crate::page_route_overrides!();
}

impl OverlayRoute for PageRouteBuilder {
    crate::modal_overlay_route_overrides!();
}

impl PredictiveBackRoute for PageRouteBuilder {
    crate::page_predictive_back_route_overrides!();
}

impl TransitionRoute for PageRouteBuilder {
    crate::page_transition_route_overrides!();

    fn transition_duration(self: Handle<Self>, app: &mut App) -> Duration {
        app.get(self).transition_duration
    }

    fn reverse_transition_duration(self: Handle<Self>, app: &mut App) -> Duration {
        app.get(self).reverse_transition_duration
    }

    fn opaque(self: Handle<Self>, app: &App) -> bool {
        app.get(self).opaque
    }
}

impl LocalHistoryRoute for PageRouteBuilder {
    crate::local_history_route_accessors!();
}

impl ModalRoute for PageRouteBuilder {
    crate::modal_route_accessors!();

    fn barrier_dismissible(self: Handle<Self>, app: &App) -> bool {
        app.get(self).barrier_dismissible
    }

    fn barrier_color(self: Handle<Self>, app: &App) -> Option<Color> {
        app.get(self).barrier_color
    }

    fn barrier_label(self: Handle<Self>, app: &App) -> Option<String> {
        app.get(self).barrier_label.clone()
    }

    fn maintain_state(self: Handle<Self>, app: &App) -> bool {
        app.get(self).maintain_state
    }

    fn fullscreen_dialog(self: Handle<Self>, app: &App) -> bool {
        PageRoute::fullscreen_dialog(self, app)
    }

    fn build_page(
        self: Handle<Self>,
        app: &mut App,
        context: BuildContext,
        animation: AnimationRef<f64>,
        secondary_animation: AnimationRef<f64>,
    ) -> WidgetRef {
        let page_builder = Rc::clone(&app.get(self).page_builder);
        page_builder(app, context, animation, secondary_animation)
    }

    fn build_transitions(
        self: Handle<Self>,
        app: &mut App,
        context: BuildContext,
        animation: AnimationRef<f64>,
        secondary_animation: AnimationRef<f64>,
        child: WidgetRef,
    ) -> WidgetRef {
        let transitions_builder = Rc::clone(&app.get(self).transitions_builder);
        transitions_builder(app, context, animation, secondary_animation, child)
    }
}

impl PageRoute for PageRouteBuilder {
    crate::page_route_accessors!();
}