waft-winui 0.3.0

WinUI 3 controls and Fluent styling for the Waft UI framework
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//! Flyout content lifetime adapted to Flutter's retained OverlayEntry mechanism.

use crate::ThemeResources;
use crate::ThemeScope;
use std::{fmt, rc::Rc};
use waft_embedder::{Offset, Rect, TextDirection, ViewId};
use waft_foundation::{App, Handle, Listener};
use waft_painting::transform_rect;
use waft_scheduler::{FrameCallback, SchedulerBinding};
use waft_widgets::*;

/// Owns flyout content independently of any widget that opens it.
///
/// One root-overlay entry remains mounted between openings. Its child State is
/// disposed only when this host is disposed or the root overlay is removed.
pub struct RetainedFlyoutHost {
    /// Creates the content within the opening target's theme and direction.
    builder: WidgetBuilder,

    /// Root entry, created on the first opening.
    entry: Option<Handle<OverlayEntry>>,

    /// Overlay in which the retained entry lives.
    overlay: Option<Handle<OverlayState>>,

    /// Mounted state that owns the content focus scope.
    state: Option<Handle<RetainedFlyoutState>>,

    /// Current opening target, cleared when hidden or when that target unmounts.
    target: Option<Handle<FlyoutTargetState>>,

    /// Theme last supplied by the current target.
    resources: Option<Rc<ThemeResources>>,

    /// Text direction last supplied by the current target.
    direction: TextDirection,

    /// Whether the retained content is visible.
    open: bool,

    /// Whether opening requests keyboard focus for the content.
    take_focus: bool,

    /// Focus to restore when the flyout closes.
    previous_focus: Option<AnyFocusNode>,

    /// An explicitly focused menu header takes precedence over earlier queued focus.
    pub(crate) return_focus: Option<AnyFocusNode>,

    /// Prevents queued work from using a disposed host.
    disposed: bool,

    /// Coalesces content updates requested while another widget is building.
    rebuild_pending: bool,

    /// Invalidates focus requests queued before a newer open or close.
    epoch: u64,

    /// Presenter-loaded callback after the open frame and focus request.
    pub(crate) opened: Option<Listener>,

    /// Presenter-hidden callback after the close frame.
    pub(crate) closed: Option<Listener>,

    /// Notifies the owner when root removal disposes retained content.
    pub(crate) on_dispose: Option<Listener>,

    /// Optional owner close request when the active target is removed.
    pub(crate) target_removed: Option<Listener>,

    /// Popup key handling also applies when the scope itself has focus.
    pub(crate) on_key_event: Option<FocusOnKeyEventCallback>,
}

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

impl RetainedFlyoutHost {
    /// Creates a host; content is built lazily when first shown.
    pub fn new(app: &mut App, builder: WidgetBuilder) -> Handle<Self> {
        app.create(Self {
            builder,
            entry: None,
            overlay: None,
            state: None,
            target: None,
            resources: None,
            direction: TextDirection::Ltr,
            open: false,
            take_focus: true,
            previous_focus: None,
            return_focus: None,
            disposed: false,
            epoch: 0,
            rebuild_pending: false,
            opened: None,
            closed: None,
            target_removed: None,
            on_dispose: None,
            on_key_event: None,
        })
    }

    /// Whether the retained content is currently shown.
    pub fn is_open(self: Handle<Self>, app: &App) -> bool {
        app.get(self).open
    }

    /// Bounds of the current target in the root overlay's coordinate system.
    pub fn target_bounds(self: Handle<Self>, app: &App) -> Option<Rect> {
        let target = app.get(self).target?;
        if !app.contains(target) || !target.mounted(app) {
            return None;
        }
        let target_box = target.context(app).find_render_object(app)?.as_box()?;
        let overlay = app.get(self).overlay?;
        let root = overlay.context(app).find_render_object(app)?;
        let transform = target_box.as_object().get_transform_to(app, Some(root));
        Some(transform_rect(
            &transform,
            Offset::ZERO & target_box.size(app),
        ))
    }

    /// The view the content is shown in: its overlay's; none before the first opening.
    fn view(self: Handle<Self>, app: &App) -> Option<ViewId> {
        let overlay = app.get(self).overlay?;
        View::maybe_id_of(app, overlay.context(app))
    }

    /// Rebuilds retained content after a controller property changes.
    pub fn rebuild(self: Handle<Self>, app: &mut App) {
        if app.get(self).disposed {
            return;
        }
        if SchedulerBinding::scheduler_phase(app)
            == waft_scheduler::SchedulerPhase::PersistentCallbacks
        {
            if app.get(self).rebuild_pending {
                return;
            }
            app.get_mut(self).rebuild_pending = true;
            SchedulerBinding::add_post_frame_callback(
                app,
                FrameCallback::new(move |app, _| {
                    if app.contains(self) {
                        app.get_mut(self).rebuild_pending = false;
                        self.rebuild(app);
                    }
                }),
                self.view(app),
            );
        } else if let Some(state) = app.get(self).state {
            state.set_state(app, |_| {});
        }
    }

    /// Shows the same content from a context beneath a FlyoutTarget.
    pub fn show_at(self: Handle<Self>, app: &mut App, context: BuildContext, take_focus: bool) {
        assert!(
            !app.get(self).disposed,
            "cannot show a disposed flyout host"
        );
        let target = context
            .find_ancestor_state_of_type::<FlyoutTargetState>(app)
            .expect("show_at requires a context beneath FlyoutTarget");
        let overlay = Overlay::of(app, context, true);
        if let Some(previous_overlay) = app.get(self).overlay {
            assert_eq!(
                previous_overlay, overlay,
                "a retained flyout belongs to one root overlay"
            );
        }

        let resources = app
            .get(target)
            .resources
            .clone()
            .expect("a built target has a theme");
        let direction = app.get(target).direction;
        if let Some(previous_target) = app.get(self).target
            && previous_target != target
            && app.contains(previous_target)
        {
            app.get_mut(previous_target)
                .hosts
                .retain(|host| *host != self);
        }
        let previous_focus = app.get(self).return_focus.or_else(|| primary_focus(app));
        let already_owns_focus = app
            .get(self)
            .state
            .is_some_and(|state| app.get(state).focus.unwrap().as_node().has_focus(app));
        let host = app.get_mut(self);
        host.target = Some(target);
        host.resources = Some(resources);
        host.direction = direction;
        host.open = true;
        host.take_focus = take_focus;
        host.epoch += 1;
        if !already_owns_focus {
            host.previous_focus = previous_focus;
        }
        if !app.get(target).hosts.contains(&self) {
            app.get_mut(target).hosts.push(self);
        }

        if let Some(state) = app.get(self).state {
            let entry = app.get(self).entry.unwrap();
            // Keep this entry's key while moving all other entries below it.
            overlay.rearrange(app, vec![entry], Some(entry), None);
            state.set_state(app, |_| {});
            state.focus_after_frame(app);
        } else if app.get(self).entry.is_none() {
            let entry = OverlayEntry::new(
                app,
                Rc::new(move |_, _| RetainedFlyoutRoot { host: self }.into_widget()),
                false,
                true,
                false,
            );
            app.get_mut(self).entry = Some(entry);
            app.get_mut(self).overlay = Some(overlay);
            overlay.insert(app, entry, None, None);
        }
    }

    /// Hides the content without removing its entry or disposing child State.
    pub fn hide(self: Handle<Self>, app: &mut App) {
        if app.get(self).disposed || !app.get(self).open {
            return;
        }
        if let Some(state) = app.get(self).state {
            state.restore_focus(app);
        }
        if let Some(target) = app.get(self).target
            && app.contains(target)
        {
            app.get_mut(target).hosts.retain(|host| *host != self);
        }
        let host = app.get_mut(self);
        host.open = false;
        host.target = None;
        host.previous_focus = None;
        host.epoch += 1;
        let epoch = host.epoch;
        self.rebuild(app);
        SchedulerBinding::add_post_frame_callback(
            app,
            FrameCallback::new(move |app, _| {
                if app.contains(self)
                    && !app.get(self).disposed
                    && !app.get(self).open
                    && app.get(self).epoch == epoch
                    && let Some(closed) = app.get(self).closed.clone()
                {
                    closed.call(app);
                }
            }),
            self.view(app),
        );
    }

    /// Releases the overlay entry; its widget state disposes the focus scope on unmount.
    pub fn dispose(self: Handle<Self>, app: &mut App) {
        if app.get(self).disposed {
            return;
        }
        self.hide(app);
        app.get_mut(self).disposed = true;
        if let Some(entry) = app.get_mut(self).entry.take() {
            entry.remove(app);
            entry.dispose(app);
        }
        if let Some(disposed) = app.get(self).on_dispose.clone() {
            disposed.call(app);
        }
    }
}

/// Supplies an opening target's lifecycle, theme and direction to retained flyouts.
#[derive(Clone, Debug)]
pub struct FlyoutTarget {
    /// Target widget, including any Builder whose context is passed to show_at.
    pub child: WidgetRef,

    /// Identity of this target across parent rebuilds.
    pub key: Option<KeyRef>,
}

impl FlyoutTarget {
    /// Wraps an opening target without taking ownership of its flyout content.
    pub fn new<K>(child: impl IntoWidget<K>) -> Self {
        Self {
            child: child.into_widget(),
            key: None,
        }
    }

    /// Sets the target's widget identity.
    pub fn key(mut self, key: KeyRef) -> Self {
        self.key = Some(key);
        self
    }
}

/// Tracks only target lifetime and inherited settings, not flyout content State.
pub struct FlyoutTargetState {
    /// Native widget state.
    state: StateData<FlyoutTarget>,

    /// Hosts that have opened from this target.
    hosts: Vec<Handle<RetainedFlyoutHost>>,

    /// Theme obtained through this target's inherited dependencies.
    resources: Option<Rc<ThemeResources>>,

    /// Current inherited text direction.
    direction: TextDirection,
}

impl StatefulWidget for FlyoutTarget {
    type State = FlyoutTargetState;

    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn create_state(&self) -> Self::State {
        FlyoutTargetState {
            state: StateData::new(),
            hosts: Vec::new(),
            resources: None,
            direction: TextDirection::Ltr,
        }
    }
}

impl State for FlyoutTargetState {
    type Widget = FlyoutTarget;
    waft_widgets::state_accessors!();

    fn did_change_dependencies(self: Handle<Self>, app: &mut App) {
        let resources = ThemeResources::of(app, self.context(app));
        let direction = Directionality::of(app, self.context(app));
        app.get_mut(self).resources = Some(resources.clone());
        app.get_mut(self).direction = direction;
        let hosts = app.get(self).hosts.clone();
        for host in hosts {
            if app.contains(host) && !app.get(host).disposed && app.get(host).target == Some(self) {
                app.get_mut(host).resources = Some(resources.clone());
                app.get_mut(host).direction = direction;
                SchedulerBinding::add_post_frame_callback(
                    app,
                    FrameCallback::new(move |app, _| {
                        if app.contains(host)
                            && !app.get(host).disposed
                            && let Some(state) = app.get(host).state
                        {
                            state.set_state(app, |_| {});
                        }
                    }),
                    View::maybe_id_of(app, self.context(app)),
                );
            }
        }
    }

    fn dispose(self: Handle<Self>, app: &mut App) {
        let hosts = app.get(self).hosts.clone();
        for host in hosts {
            if app.contains(host) && !app.get(host).disposed && app.get(host).target == Some(self) {
                // Target removal may happen during build, so close after that pass finishes.
                SchedulerBinding::add_post_frame_callback(
                    app,
                    FrameCallback::new(move |app, _| {
                        if app.contains(host)
                            && !app.get(host).disposed
                            && app.get(host).target == Some(self)
                        {
                            if let Some(removed) = app.get(host).target_removed.clone() {
                                removed.call(app);
                            } else {
                                host.hide(app);
                            }
                        }
                    }),
                    // Disposed, the target is in no view any more.
                    None,
                );
            }
        }
    }

    fn build(self: Handle<Self>, app: &mut App, _: BuildContext) -> WidgetRef {
        self.widget(app).child.clone()
    }
}

/// Root entry widget whose identity does not change when the opening target changes.
#[derive(Debug)]
struct RetainedFlyoutRoot {
    /// Application-owned host.
    host: Handle<RetainedFlyoutHost>,
}

/// Owns focus resources for the root entry, independently of target widget lifetime.
struct RetainedFlyoutState {
    /// Native widget state.
    state: StateData<RetainedFlyoutRoot>,

    /// Focus scope owned by this retained subtree.
    focus: Option<Handle<FocusScopeNode>>,
}

impl StatefulWidget for RetainedFlyoutRoot {
    type State = RetainedFlyoutState;

    fn create_state(&self) -> Self::State {
        RetainedFlyoutState {
            state: StateData::new(),
            focus: None,
        }
    }
}

impl RetainedFlyoutState {
    /// Requests content focus only if this opening is still current after layout.
    fn focus_after_frame(self: Handle<Self>, app: &mut App) {
        let host = self.widget(app).host;
        let epoch = app.get(host).epoch;
        SchedulerBinding::add_post_frame_callback(
            app,
            FrameCallback::new(move |app, _| {
                if !app.contains(self)
                    || !self.mounted(app)
                    || !app.contains(host)
                    || app.get(host).disposed
                    || !app.get(host).open
                    || app.get(host).epoch != epoch
                {
                    return;
                }
                if app.get(host).take_focus {
                    let scope = app.get(self).focus.unwrap().as_node();
                    let first = scope
                        .traversal_descendants(app)
                        .into_iter()
                        .find(|node| node.can_request_focus(app));
                    first.unwrap_or(scope).request_focus(app, None);
                }
                if let Some(opened) = app.get(host).opened.clone() {
                    opened.call(app);
                }
            }),
            View::maybe_id_of(app, self.context(app)),
        );
    }

    /// Returns focus only if this popup still owns it and the previous node survives.
    fn restore_focus(self: Handle<Self>, app: &mut App) {
        let host = self.widget(app).host;
        let previous = app.get(host).previous_focus;
        if app.get(self).focus.unwrap().as_node().has_focus(app)
            && let Some(previous) = previous.filter(|node| {
                app.contains(node.id())
                    && !app.is_disposed(node.id())
                    && node.context(app).is_some()
                    && node.can_request_focus(app)
            })
        {
            previous.request_focus(app, None);
        }
    }
}

impl State for RetainedFlyoutState {
    type Widget = RetainedFlyoutRoot;
    waft_widgets::state_accessors!();

    fn init_state(self: Handle<Self>, app: &mut App) {
        let host = self.widget(app).host;
        let focus = FocusScopeNode::new(app);
        focus.set_traversal_edge_behavior(app, TraversalEdgeBehavior::ClosedLoop);
        app.get_mut(self).focus = Some(focus);
        app.get_mut(host).state = Some(self);
        self.focus_after_frame(app);
    }

    fn dispose(self: Handle<Self>, app: &mut App) {
        let focus = app.get(self).focus.unwrap();
        focus.as_node().dispose(app);
        app.destroy(focus);
        let host = self.widget(app).host;
        if app.contains(host) {
            app.get_mut(host).state = None;
            // Root-overlay teardown releases the retained entry even when its application owner survives.
            host.dispose(app);
        }
    }

    fn build(self: Handle<Self>, app: &mut App, _: BuildContext) -> WidgetRef {
        let host = self.widget(app).host;
        let data = app.get(host);
        let open = data.open;
        let resources = data
            .resources
            .clone()
            .expect("a shown flyout has a target theme");
        let direction = data.direction;
        let builder = data.builder.clone();
        let on_key_event = data.on_key_event.clone();
        let themed_content = ThemeScope::with_resources(
            resources,
            Directionality::new(
                direction,
                Builder::new(move |app, context| builder(app, context)),
            ),
        );
        let mut scope = FocusScope::new(themed_content).node(app.get(self).focus.unwrap());
        if let Some(handler) = on_key_event {
            scope = scope.on_key_event(handler);
        }
        Offstage::new()
            .offstage(!open)
            .child(TickerMode::new(
                open,
                ExcludeFocus::new(scope).excluding(!open),
            ))
            .into_widget()
    }
}