waft-services 0.17.0

Keyboard, text input, and system services 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
//! Flutter `services/system_chrome.dart`: the application switcher description, the system
//! overlay style and what a lifecycle change does to it, and the system UI mode. The rest of
//! `SystemChrome` waits for the host capabilities it would talk to.
//!
//! The overlay style and the mode are a view's, sent to the bars over the view
//! ([`ViewHost::system_bars`](waft_embedder::ViewHost::system_bars)), since each window carries
//! its own bars, where Flutter's are the app's. A view with no bars over it, a desktop's window,
//! takes neither, as Flutter's calls do nothing on a desktop. The invariant a later editor will break: the pending and the
//! latest style are kept per view, so one view's style never stands for, or hides, another's.

use std::collections::BTreeMap;

use waft_embedder::{
    AppLifecycleState, ApplicationSwitcherDescription, SystemBarsBehavior, SystemUiOverlayStyle,
    ViewId,
};
use waft_foundation::{App, Handle, Listener};

/// Specifies a set of mutually exclusive system UI overlays.
///
/// Used by [`SystemChrome`] to specify the set of overlays that should be visible.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SystemUiOverlay {
    /// The status bar provided by the embedder on the top of the application
    /// surface, if any.
    Top,

    /// The status bar provided by the embedder on the bottom of the application
    /// surface, if any.
    Bottom,
}

/// Describes different display configurations for both Android and iOS.
///
/// These modes mimic Android-specific display setups. Used by
/// [`SystemChrome::set_enabled_system_ui_mode`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SystemUiMode {
    /// Fullscreen display with status and navigation bars presentable by tapping anywhere on the
    /// display. Android no longer has a tap that brings them back: here a swipe from an edge
    /// does, as in [`SystemUiMode::Immersive`].
    LeanBack,
    /// Fullscreen display with status and navigation bars presentable through a swipe gesture at
    /// the edges of the display.
    Immersive,
    /// Fullscreen display with status and navigation elements rendered over the application:
    /// a swipe at an edge shows them for a moment, and the application keeps that touch.
    ImmersiveSticky,
    /// Fullscreen display with status and navigation elements rendered over the application.
    EdgeToEdge,
    /// Declares manually configured [`SystemUiOverlay`]s.
    Manual,
}

/// Dart's `SystemChrome._pendingStyle` and `SystemChrome._latestStyle`, which are static
/// fields of a Dart class and one arena object per [`App`] here, each by view.
#[derive(Default)]
struct SystemChromeStyles {
    pending_style: BTreeMap<ViewId, SystemUiOverlayStyle>,
    latest_style: BTreeMap<ViewId, SystemUiOverlayStyle>,
}

/// Controls specific aspects of the operating system's graphical interface and
/// how it interacts with the application.
pub struct SystemChrome;

impl SystemChrome {
    /// Specifies the description of the current state of the application as it
    /// pertains to the application switcher (also known as "recent tasks").
    ///
    /// Any part of the description that is unsupported on the current platform
    /// will be ignored.
    pub fn set_application_switcher_description(
        app: &App,
        description: &ApplicationSwitcherDescription,
    ) {
        if let Some(chrome) = app.platform().system_chrome() {
            chrome.set_application_switcher_description(description);
        }
    }

    /// Specifies the style to use for the system overlays (e.g. the status bar on
    /// Android or iOS, the system navigation bar on Android) that are visible (if any) over
    /// `view`.
    ///
    /// This method will schedule the embedder update to be run in a microtask.
    /// Any subsequent calls to this method during the current event loop will
    /// overwrite the pending value, such that only the last specified value takes
    /// effect.
    ///
    /// Call this API in code whose lifecycle matches that of the desired
    /// system UI styles. For instance, to change the system UI style on a new
    /// page, consider calling when pushing/popping a new `PageRoute`.
    ///
    /// If a particular style is not supported on the platform, selecting it will
    /// have no effect.
    ///
    /// For more complex control of the system overlay styles, consider using
    /// an `AnnotatedRegion` widget instead of calling this method directly. That widget
    /// places a value directly into the layer tree where it can be hit-tested by the
    /// framework.
    pub fn set_system_ui_overlay_style(app: &mut App, view: ViewId, style: &SystemUiOverlayStyle) {
        let styles = app.singleton::<SystemChromeStyles>();
        if !app.get(styles).pending_style.is_empty() {
            // The microtask has already been queued; just update the pending value.
            app.get_mut(styles).pending_style.insert(view, *style);
            return;
        }
        if Some(style) == app.get(styles).latest_style.get(&view) {
            // Trivial success: no microtask has been queued and the given style is
            // already in effect, so no need to queue a microtask.
            return;
        }
        app.get_mut(styles).pending_style.insert(view, *style);
        app.schedule_microtask(Listener::new(move |app| {
            Self::send_pending_styles(app, styles)
        }));
    }

    /// The body of the microtask Dart's `setSystemUIOverlayStyle` queues, for each view a style
    /// is pending for.
    fn send_pending_styles(app: &mut App, styles: Handle<SystemChromeStyles>) {
        let pending = std::mem::take(&mut app.get_mut(styles).pending_style);
        debug_assert!(!pending.is_empty());
        for (view, style) in pending {
            if app.get(styles).latest_style.get(&view) == Some(&style) {
                continue;
            }
            if let Some(view) = app.platform().view(view)
                && let Some(bars) = view.host().system_bars()
            {
                bars.set_overlay_style(&style);
            }
            app.get_mut(styles).latest_style.insert(view, style);
        }
    }

    /// Specifies the [`SystemUiMode`] to have visible over `view` when the application is
    /// running (Dart's `SystemChrome.setEnabledSystemUIMode`), carried out at once by the bars
    /// over the view, as Dart's sends its message without waiting for a microtask.
    ///
    /// The `overlays` are read for [`SystemUiMode::Manual`] alone: both shown is
    /// [`SystemUiMode::EdgeToEdge`], none shown is [`SystemUiMode::Immersive`]; one alone is not
    /// supported, the bars showing or hiding together, and leaves them as they are.
    ///
    /// A view not in the app, gone, or with no bars over it is not told, as Flutter's call does
    /// nothing on a desktop.
    pub fn set_enabled_system_ui_mode(
        app: &App,
        view: ViewId,
        mode: SystemUiMode,
        overlays: &[SystemUiOverlay],
    ) {
        let Some(view) = app.platform().view(view) else {
            return;
        };
        let Some(bars) = view.host().system_bars() else {
            return;
        };
        match asked_of_the_bars(mode, overlays) {
            BarsAsked::Show => bars.show(),
            BarsAsked::Hide(behavior) => bars.hide(behavior),
            BarsAsked::Nothing => {}
        }
    }

    /// Called by the binding during a transition to a new app lifecycle state.
    pub fn handle_app_lifecycle_state_changed(app: &mut App, state: AppLifecycleState) {
        // When the app is detached, clear the record of the style sent to the host
        // so that it will be sent again when the app is reattached.
        if state == AppLifecycleState::Detached {
            let styles = app.singleton::<SystemChromeStyles>();
            app.schedule_microtask(Listener::new(move |app| {
                app.get_mut(styles).latest_style.clear();
            }));
        }
    }

    /// The last style that was set over `view` using
    /// [`set_system_ui_overlay_style`](SystemChrome::set_system_ui_overlay_style).
    pub fn latest_style(app: &mut App, view: ViewId) -> Option<SystemUiOverlayStyle> {
        let styles = app.singleton::<SystemChromeStyles>();
        app.get(styles).latest_style.get(&view).copied()
    }
}

/// What a mode asks of the bars over a view.
enum BarsAsked {
    Show,
    Hide(SystemBarsBehavior),
    /// What they cannot do: a manual mode with one overlay alone, the bars showing or hiding
    /// together.
    Nothing,
}

fn asked_of_the_bars(mode: SystemUiMode, overlays: &[SystemUiOverlay]) -> BarsAsked {
    match mode {
        SystemUiMode::EdgeToEdge => BarsAsked::Show,
        SystemUiMode::LeanBack | SystemUiMode::Immersive => {
            BarsAsked::Hide(SystemBarsBehavior::ShowBarsBySwipe)
        }
        SystemUiMode::ImmersiveSticky => {
            BarsAsked::Hide(SystemBarsBehavior::ShowTransientBarsBySwipe)
        }
        SystemUiMode::Manual => {
            let top = overlays.contains(&SystemUiOverlay::Top);
            let bottom = overlays.contains(&SystemUiOverlay::Bottom);
            match (top, bottom) {
                (true, true) => BarsAsked::Show,
                (false, false) => BarsAsked::Hide(SystemBarsBehavior::ShowBarsBySwipe),
                _ => BarsAsked::Nothing,
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::rc::Rc;
    use waft_foundation::AppCell;

    use waft_embedder::{Brightness, Color, TargetPlatform};
    use waft_test::{BarsShown, IMPLICIT_VIEW_ID, TestPlatform, TestView};

    use super::*;

    const VIEW: ViewId = IMPLICIT_VIEW_ID;
    const OTHER_VIEW: ViewId = ViewId(1);

    /// An app with two views, whose hosts record the overlay styles they are handed.
    fn recording_app() -> (Rc<TestPlatform>, Rc<AppCell>) {
        let platform = Rc::new(
            TestPlatform::new()
                .on(TargetPlatform::IOS)
                .with_views(&[VIEW, OTHER_VIEW]),
        );
        (Rc::clone(&platform), AppCell::with_platform(platform))
    }

    /// The styles `view`'s host was handed, oldest first.
    fn styles_of(platform: &TestPlatform, view: ViewId) -> Vec<SystemUiOverlayStyle> {
        let view: Rc<TestView> = platform.test_view(view);
        view.test_host().overlay_styles()
    }

    #[test]
    fn light_and_dark_differ_only_in_the_two_status_bar_brightnesses() {
        assert_eq!(
            SystemUiOverlayStyle::LIGHT.status_bar_icon_brightness,
            Some(Brightness::Light)
        );
        assert_eq!(
            SystemUiOverlayStyle::LIGHT.status_bar_brightness,
            Some(Brightness::Dark)
        );
        assert_eq!(
            SystemUiOverlayStyle::DARK.status_bar_icon_brightness,
            Some(Brightness::Dark)
        );
        assert_eq!(
            SystemUiOverlayStyle::DARK.status_bar_brightness,
            Some(Brightness::Light)
        );
        assert_eq!(
            SystemUiOverlayStyle::LIGHT.system_navigation_bar_color,
            SystemUiOverlayStyle::DARK.system_navigation_bar_color
        );
        assert_ne!(SystemUiOverlayStyle::LIGHT, SystemUiOverlayStyle::DARK);
    }

    #[test]
    fn copy_with_replaces_only_what_it_is_given() {
        let style = SystemUiOverlayStyle::LIGHT
            .copy_with()
            .status_bar_color(Color::new(0xFF112233));
        assert_eq!(style.status_bar_color, Some(Color::new(0xFF112233)));
        assert_eq!(
            style.status_bar_icon_brightness,
            SystemUiOverlayStyle::LIGHT.status_bar_icon_brightness
        );
        assert_eq!(SystemUiOverlayStyle::LIGHT.status_bar_color, None);
    }

    /// How the bars over `view` were hidden or shown, oldest first.
    fn bars_of(platform: &TestPlatform, view: ViewId) -> Vec<BarsShown> {
        let view: Rc<TestView> = platform.test_view(view);
        view.test_host().bars_shown()
    }

    #[test]
    fn each_mode_hides_or_shows_the_bars_of_its_view_alone() {
        let (platform, cell) = recording_app();
        let app = cell.borrow();
        let modes = [
            (SystemUiMode::ImmersiveSticky, vec![]),
            (SystemUiMode::Immersive, vec![]),
            (SystemUiMode::LeanBack, vec![]),
            (SystemUiMode::EdgeToEdge, vec![]),
            (SystemUiMode::Manual, vec![]),
            (SystemUiMode::Manual, vec![SystemUiOverlay::Top]),
            (
                SystemUiMode::Manual,
                vec![SystemUiOverlay::Top, SystemUiOverlay::Bottom],
            ),
        ];
        for (mode, overlays) in modes {
            SystemChrome::set_enabled_system_ui_mode(&app, VIEW, mode, &overlays);
        }
        assert_eq!(
            bars_of(&platform, VIEW),
            [
                BarsShown::Hidden(SystemBarsBehavior::ShowTransientBarsBySwipe),
                BarsShown::Hidden(SystemBarsBehavior::ShowBarsBySwipe),
                BarsShown::Hidden(SystemBarsBehavior::ShowBarsBySwipe),
                BarsShown::Shown,
                BarsShown::Hidden(SystemBarsBehavior::ShowBarsBySwipe),
                BarsShown::Shown,
            ],
            "one overlay alone leaves the bars as they are"
        );
        assert!(
            bars_of(&platform, OTHER_VIEW).is_empty(),
            "the other view is not told"
        );
    }

    /// `system_chrome_test.dart`: `SystemChrome overlay style test`.
    #[test]
    fn only_the_last_style_of_a_turn_reaches_the_host() {
        let (platform, cell) = recording_app();
        let mut app = cell.borrow_mut();
        SystemChrome::set_system_ui_overlay_style(&mut app, VIEW, &SystemUiOverlayStyle::LIGHT);
        SystemChrome::set_system_ui_overlay_style(&mut app, VIEW, &SystemUiOverlayStyle::DARK);
        assert!(
            styles_of(&platform, VIEW).is_empty(),
            "nothing is sent before the microtask runs"
        );
        assert_eq!(SystemChrome::latest_style(&mut app, VIEW), None);

        app.drain_microtasks();
        assert_eq!(
            styles_of(&platform, VIEW).as_slice(),
            [SystemUiOverlayStyle::DARK]
        );
        assert_eq!(
            SystemChrome::latest_style(&mut app, VIEW),
            Some(SystemUiOverlayStyle::DARK)
        );
    }

    #[test]
    fn a_style_already_in_effect_queues_nothing() {
        let (platform, cell) = recording_app();
        let mut app = cell.borrow_mut();
        SystemChrome::set_system_ui_overlay_style(&mut app, VIEW, &SystemUiOverlayStyle::LIGHT);
        app.drain_microtasks();

        SystemChrome::set_system_ui_overlay_style(&mut app, VIEW, &SystemUiOverlayStyle::LIGHT);
        app.drain_microtasks();
        assert_eq!(
            styles_of(&platform, VIEW).as_slice(),
            [SystemUiOverlayStyle::LIGHT],
            "an unchanged style does not reach the host again"
        );

        SystemChrome::set_system_ui_overlay_style(&mut app, VIEW, &SystemUiOverlayStyle::DARK);
        app.drain_microtasks();
        assert_eq!(
            styles_of(&platform, VIEW).as_slice(),
            [SystemUiOverlayStyle::LIGHT, SystemUiOverlayStyle::DARK]
        );
    }

    /// `system_chrome_test.dart`: the end of `SystemChrome overlay style test`.
    #[test]
    fn the_style_is_sent_again_after_the_app_was_detached() {
        let (platform, cell) = recording_app();
        let mut app = cell.borrow_mut();
        SystemChrome::set_system_ui_overlay_style(&mut app, VIEW, &SystemUiOverlayStyle::LIGHT);
        app.drain_microtasks();
        assert_eq!(styles_of(&platform, VIEW).len(), 1);

        // The state should be sent again if the app was detached.
        SystemChrome::handle_app_lifecycle_state_changed(&mut app, AppLifecycleState::Detached);
        app.drain_microtasks();
        SystemChrome::handle_app_lifecycle_state_changed(&mut app, AppLifecycleState::Resumed);
        SystemChrome::set_system_ui_overlay_style(&mut app, VIEW, &SystemUiOverlayStyle::LIGHT);
        app.drain_microtasks();
        assert_eq!(styles_of(&platform, VIEW).len(), 2);
    }

    #[test]
    fn a_style_that_returns_to_the_latest_within_the_turn_sends_nothing() {
        let (platform, cell) = recording_app();
        let mut app = cell.borrow_mut();
        SystemChrome::set_system_ui_overlay_style(&mut app, VIEW, &SystemUiOverlayStyle::LIGHT);
        app.drain_microtasks();

        SystemChrome::set_system_ui_overlay_style(&mut app, VIEW, &SystemUiOverlayStyle::DARK);
        SystemChrome::set_system_ui_overlay_style(&mut app, VIEW, &SystemUiOverlayStyle::LIGHT);
        app.drain_microtasks();
        assert_eq!(
            styles_of(&platform, VIEW).as_slice(),
            [SystemUiOverlayStyle::LIGHT]
        );
        assert_eq!(
            SystemChrome::latest_style(&mut app, VIEW),
            Some(SystemUiOverlayStyle::LIGHT)
        );
    }

    #[test]
    fn each_view_keeps_its_own_style() {
        let (platform, cell) = recording_app();
        let mut app = cell.borrow_mut();
        SystemChrome::set_system_ui_overlay_style(&mut app, VIEW, &SystemUiOverlayStyle::LIGHT);
        SystemChrome::set_system_ui_overlay_style(
            &mut app,
            OTHER_VIEW,
            &SystemUiOverlayStyle::DARK,
        );
        app.drain_microtasks();
        assert_eq!(
            styles_of(&platform, VIEW).as_slice(),
            [SystemUiOverlayStyle::LIGHT]
        );
        assert_eq!(
            styles_of(&platform, OTHER_VIEW).as_slice(),
            [SystemUiOverlayStyle::DARK]
        );

        SystemChrome::set_system_ui_overlay_style(&mut app, VIEW, &SystemUiOverlayStyle::LIGHT);
        app.drain_microtasks();
        assert_eq!(
            styles_of(&platform, VIEW).len(),
            1,
            "a style in effect over its view is not sent again for another view's"
        );
    }
}