waft-winui 0.8.2

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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
//! Expander.xaml, Expander header styles and Expander.cpp state transitions.

use crate::*;
use std::{fmt, rc::Rc, time::Duration};
use waft_animation::{Cubic, Curve};
use waft_embedder::{FontWeight, Offset};
use waft_foundation::{App, Handle, Listener, OptionRetained, Ref};
use waft_painting::{Alignment, EdgeInsetsGeometry};
use waft_rendering::BoxConstraints;
use waft_scheduler::{FrameCallback, SchedulerBinding, Ticker};
use waft_widgets::*;

/// Direction in which an Expander reveals its content.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ExpandDirection {
    /// The header is above its content.
    #[default]
    Down,

    /// The header is below its content.
    Up,
}

/// Requests an owner update to IsExpanded.
pub type ExpanderExpandedChangedHandler = Rc<dyn Fn(&mut App, bool)>;

/// A header toggle that reveals or hides retained content using the Fluent template.
#[derive(Clone)]
pub struct Expander {
    /// Whether the content is expanded.
    pub is_expanded: bool,

    /// Requests a new expanded value from the owner.
    pub is_expanded_changed: ExpanderExpandedChangedHandler,

    /// Content displayed by the header toggle.
    pub header: Option<WidgetRef>,

    /// Content retained while the Expander is collapsed.
    pub content: Option<WidgetRef>,

    /// Whether content is above or below the header.
    pub expand_direction: ExpandDirection,

    /// Whether the header and content accept input.
    pub is_enabled: bool,

    /// Optional content background.
    pub background: Option<Brush>,

    /// Optional content border.
    pub border_brush: Option<Brush>,

    /// Content padding in left, top, right, bottom order; the theme's `ExpanderContentPadding` when `None`.
    pub padding: Option<[f64; 4]>,

    /// Corner radius shared by the header and content; the theme's `ControlCornerRadius` when `None`.
    pub corner_radius: Option<f64>,

    /// Minimum width of the control; the theme's `FlyoutThemeMinWidth` when `None`.
    pub min_width: Option<f64>,

    /// Minimum height of both header and content borders; the theme's `ExpanderMinHeight` when `None`.
    pub min_height: Option<f64>,

    /// Called when IsExpanded changes to true, before the opening animation completes.
    pub expanding: Option<Listener>,

    /// Called when IsExpanded changes to false, rather than after the collapse animation.
    pub collapsed: Option<Listener>,

    /// Stable identity across rebuilds.
    pub key: Option<KeyRef>,
}

impl Expander {
    /// Creates an Expander whose expanded value is owned by its caller.
    pub fn new(is_expanded: bool, changed: impl Fn(&mut App, bool) + 'static) -> Self {
        Self {
            is_expanded,
            is_expanded_changed: Rc::new(changed),
            header: None,
            content: None,
            expand_direction: ExpandDirection::Down,
            is_enabled: true,
            background: None,
            border_brush: None,
            padding: None,
            corner_radius: None,
            min_width: None,
            min_height: None,
            expanding: None,
            collapsed: None,
            key: None,
        }
    }

    /// Sets Header.
    pub fn header<K>(mut self, value: impl IntoWidget<K>) -> Self {
        self.header = Some(value.into_widget());
        self
    }

    /// Sets Content.
    pub fn content<K>(mut self, value: impl IntoWidget<K>) -> Self {
        self.content = Some(value.into_widget());
        self
    }

    /// Sets ExpandDirection.
    pub fn expand_direction(mut self, value: ExpandDirection) -> Self {
        self.expand_direction = value;
        self
    }

    /// Sets IsEnabled.
    pub fn is_enabled(mut self, value: bool) -> Self {
        self.is_enabled = value;
        self
    }

    /// Sets Background.
    pub fn background(mut self, value: Brush) -> Self {
        self.background = Some(value);
        self
    }

    /// Sets BorderBrush.
    pub fn border_brush(mut self, value: Brush) -> Self {
        self.border_brush = Some(value);
        self
    }

    /// Sets Padding.
    pub fn padding(mut self, value: [f64; 4]) -> Self {
        self.padding = Some(value);
        self
    }

    /// Sets CornerRadius.
    pub fn corner_radius(mut self, value: f64) -> Self {
        self.corner_radius = Some(value);
        self
    }

    /// Sets MinWidth.
    pub fn min_width(mut self, value: f64) -> Self {
        self.min_width = Some(value);
        self
    }

    /// Sets MinHeight.
    pub fn min_height(mut self, value: f64) -> Self {
        self.min_height = Some(value);
        self
    }

    /// Sets Expanding.
    pub fn expanding(mut self, value: Listener) -> Self {
        self.expanding = Some(value);
        self
    }

    /// Sets Collapsed.
    pub fn collapsed(mut self, value: Listener) -> Self {
        self.collapsed = Some(value);
        self
    }

    /// Sets Key.
    pub fn key(mut self, value: KeyRef) -> Self {
        self.key = Some(value);
        self
    }
}

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

/// Expander's animation and retained content lifecycle.
pub struct ExpanderState {
    /// Framework state identity.
    state: StateData<Expander>,

    /// Native TickerMode subscription.
    single_ticker_provider: SingleTickerProviderStateMixinData,

    /// Clock for the explicit ExpandStates storyboard.
    ticker: Option<Ref<Ticker>>,

    /// Elapsed time since the last IsExpanded change.
    elapsed: Duration,

    /// Whether the collapsed visibility key has executed.
    content_visible: bool,

    /// Direction captured by ExpandStates, independent of ExpandDirectionStates.
    motion_direction: ExpandDirection,
}

impl StatefulWidget for Expander {
    type State = ExpanderState;

    /// Returns the identity used to retain this control across owner rebuilds.
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    /// Initializes template state from the first widget description.
    fn create_state(&self) -> ExpanderState {
        ExpanderState {
            state: StateData::new(),
            single_ticker_provider: Default::default(),
            ticker: None,
            elapsed: Duration::ZERO,
            content_visible: self.is_expanded,
            motion_direction: self.expand_direction,
        }
    }
}

impl SingleTickerProviderStateMixin for ExpanderState {
    fn single_ticker_provider_data(
        self: Handle<Self>,
        app: &App,
    ) -> &SingleTickerProviderStateMixinData {
        &app.get(self).single_ticker_provider
    }

    fn single_ticker_provider_data_mut(
        self: Handle<Self>,
        app: &mut App,
    ) -> &mut SingleTickerProviderStateMixinData {
        &mut app.get_mut(self).single_ticker_provider
    }
}

impl ExpanderState {
    /// Duration of the last storyboard key, including CollapseDown's delayed visibility key.
    fn duration(self: Handle<Self>, app: &App) -> Duration {
        Duration::from_millis(if self.widget(app).is_expanded {
            333
        } else if app.get(self).motion_direction == ExpandDirection::Up {
            200
        } else {
            167
        })
    }

    /// Applies the source translate keys and collapses only at the visibility key.
    fn tick(self: Handle<Self>, app: &mut App, elapsed: Duration) {
        let finished = elapsed >= self.duration(app);
        let expanded = self.widget(app).is_expanded;
        self.set_state(app, |state| {
            state.elapsed = elapsed;
            if finished && !expanded {
                state.content_visible = false;
            }
        });

        if finished {
            app.get(self).ticker.handle().unwrap().stop(app, false);
        }
    }

    /// Translation as a fraction of the actual content height, avoiding a delayed size sample.
    fn translation(self: Handle<Self>, app: &App) -> f64 {
        let time = app.get(self).elapsed.as_secs_f64();
        let sign = if app.get(self).motion_direction == ExpandDirection::Down {
            -1.0
        } else {
            1.0
        };
        if self.widget(app).is_expanded {
            sign * (1.0 - Cubic::new(0.0, 0.0, 0.0, 1.0).transform((time / 0.333).min(1.0)))
        } else {
            sign * Cubic::new(1.0, 1.0, 0.0, 1.0).transform((time / 0.167).min(1.0))
        }
    }
}

impl State for ExpanderState {
    type Widget = Expander;
    waft_widgets::state_accessors!();

    /// Creates the native clock for the source storyboards.
    fn init_state(self: Handle<Self>, app: &mut App) {
        let ticker = SingleTickerProviderStateMixin::create_ticker(
            self,
            app,
            FrameCallback::new(move |app, elapsed| self.tick(app, elapsed)),
        );
        app.get_mut(self).ticker = Some(ticker.clone());
        if self.widget(app).is_expanded {
            ticker.start(app);
        }
    }

    /// Applies source property-change behavior when the owner supplies new values.
    fn did_update_widget(self: Handle<Self>, app: &mut App, old: &Expander) {
        let widget = self.widget(app).clone();
        if old.is_expanded == widget.is_expanded {
            return;
        }

        let ticker = app.get(self).ticker.clone().unwrap();
        ticker.stop(app, false);
        let state = app.get_mut(self);
        state.elapsed = Duration::ZERO;
        state.content_visible = true;
        state.motion_direction = widget.expand_direction;
        ticker.start(app);

        let event = if widget.is_expanded {
            widget.expanding
        } else {
            widget.collapsed
        };
        if let Some(event) = event {
            SchedulerBinding::add_post_frame_callback(
                app,
                FrameCallback::new(move |app, _| {
                    if app.contains(self) && self.mounted(app) {
                        event.call(app);
                    }
                }),
                View::maybe_id_of(app, self.context(app)),
            );
        }
    }

    fn activate(self: Handle<Self>, app: &mut App) {
        SingleTickerProviderStateMixin::activate(self, app);
    }

    /// Releases the storyboard clock before disposing its ticker provider.
    fn dispose(self: Handle<Self>, app: &mut App) {
        app.get(self).ticker.handle().unwrap().dispose(app);
        SingleTickerProviderStateMixin::dispose(self, app);
    }

    /// Builds the named template parts from the current visual state.
    fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef {
        let widget = self.widget(app).clone();
        let resources = ThemeResources::of(app, context);
        let header_widget = widget.clone();
        let header_down = app.get(self).motion_direction == ExpandDirection::Down;
        let changed = widget.is_expanded_changed.clone();
        let header = ToggleButton::new(Some(widget.is_expanded), move |app, value| {
            changed(app, value == Some(true))
        })
        .content(
            widget
                .header
                .clone()
                .unwrap_or_else(|| SizedBox::shrink().into_widget()),
        )
        .is_enabled(widget.is_enabled)
        .template(move |app, context, _, content, states| {
            expander_header(
                app,
                context,
                &header_widget,
                header_down,
                content.unwrap_or_else(|| SizedBox::shrink().into_widget()),
                states,
            )
        });

        let down = widget.expand_direction == ExpandDirection::Down;
        let content = ControlBorder::new(
            widget
                .background
                .unwrap_or(Brush::Solid(resources.expander_content_background)),
            widget
                .border_brush
                .unwrap_or(Brush::Solid(resources.expander_content_border_brush)),
        )
        .border_thickness_ltrb(if down {
            resources.expander_content_down_border_thickness
        } else {
            resources.expander_content_up_border_thickness
        })
        .corner_radius_corners(if down {
            [
                0.0,
                0.0,
                widget
                    .corner_radius
                    .unwrap_or(resources.control_corner_radius[0]),
                widget
                    .corner_radius
                    .unwrap_or(resources.control_corner_radius[0]),
            ]
        } else {
            [
                widget
                    .corner_radius
                    .unwrap_or(resources.control_corner_radius[0]),
                widget
                    .corner_radius
                    .unwrap_or(resources.control_corner_radius[0]),
                0.0,
                0.0,
            ]
        })
        .padding(widget.padding.unwrap_or(resources.expander_content_padding))
        .child(
            widget
                .content
                .clone()
                .unwrap_or_else(|| SizedBox::shrink().into_widget()),
        );
        let content = ConstrainedBox::new(
            BoxConstraints::new()
                .min_height(widget.min_height.unwrap_or(resources.expander_min_height)),
        )
        .child(content);
        let visible = app.get(self).content_visible;

        // ExpanderContentClip contains the translated ExpanderContent; height is reserved immediately.
        let content = Offstage::new().offstage(!visible).child(TickerMode::new(
            visible,
            ExcludeFocus::new(ClipRect::new().child(
                FractionalTranslation::new(Offset::new(0.0, self.translation(app))).child(content),
            ))
            .excluding(!visible),
        ));
        let grid = Grid::new()
            .row_definitions([
                RowDefinition::new(if down {
                    GridLength::AUTO
                } else {
                    GridLength::STAR
                }),
                RowDefinition::new(if down {
                    GridLength::STAR
                } else {
                    GridLength::AUTO
                }),
            ])
            .children([
                GridCell::new(header)
                    .row(if down { 0 } else { 1 })
                    .into_widget(),
                GridCell::new(content)
                    .row(if down { 1 } else { 0 })
                    .into_widget(),
            ]);

        ExcludeFocus::new(
            IgnorePointer::new().ignoring(!widget.is_enabled).child(
                ConstrainedBox::new(
                    BoxConstraints::new()
                        .min_width(widget.min_width.unwrap_or(resources.flyout_theme_min_width)),
                )
                .child(grid),
            ),
        )
        .excluding(!widget.is_enabled)
        .into_widget()
    }
}

/// ExpanderHeaderDownStyle / ExpanderHeaderUpStyle common and checked visual states.
fn expander_header(
    app: &mut App,
    context: BuildContext,
    widget: &Expander,
    header_down: bool,
    content: WidgetRef,
    states: ControlStates,
) -> WidgetRef {
    let theme = ThemeResources::of(app, context);
    let r = &theme;
    let (foreground, border, chevron_fill, chevron_border, chevron_foreground) = match states.common
    {
        CommonState::PointerOver => (
            r.expander_header_foreground_pointer_over,
            r.expander_header_border_pointer_over_brush,
            r.expander_chevron_pointer_over_background,
            r.expander_chevron_border_pointer_over_brush,
            r.expander_chevron_pointer_over_foreground,
        ),
        CommonState::Pressed => (
            r.expander_header_foreground_pressed,
            r.expander_header_border_pressed_brush,
            r.expander_chevron_pressed_background,
            r.expander_chevron_border_pressed_brush,
            r.expander_chevron_pressed_foreground,
        ),
        CommonState::Disabled => (
            r.expander_header_disabled_foreground,
            r.expander_header_disabled_border_brush,
            r.expander_chevron_background,
            r.expander_header_disabled_border_brush,
            r.expander_header_disabled_foreground,
        ),
        _ => (
            r.expander_header_foreground,
            r.expander_header_border_brush,
            r.expander_chevron_background,
            r.expander_chevron_border_brush,
            r.expander_chevron_foreground,
        ),
    };
    let radii = if widget.is_expanded {
        if header_down {
            [
                widget.corner_radius.unwrap_or(r.control_corner_radius[0]),
                widget.corner_radius.unwrap_or(r.control_corner_radius[0]),
                0.0,
                0.0,
            ]
        } else {
            [
                0.0,
                0.0,
                widget.corner_radius.unwrap_or(r.control_corner_radius[0]),
                widget.corner_radius.unwrap_or(r.control_corner_radius[0]),
            ]
        }
    } else {
        [widget.corner_radius.unwrap_or(r.control_corner_radius[0]); 4]
    };
    let points_up = widget.is_expanded == (widget.expand_direction == ExpandDirection::Down);

    // Native rotation is the established AnimatedIcon substitute used by NavigationView.
    let chevron = AnimatedRotation::new(
        if points_up { 0.5 } else { 0.0 },
        r.control_normal_animation_duration,
    )
    .child(
        FontIcon::symbol(FluentSymbol::ChevronDown)
            .font_size(r.expander_chevron_glyph_size)
            .foreground(chevron_foreground),
    );
    let chevron = Padding::new(EdgeInsetsGeometry::from_ltrb(
        r.expander_chevron_margin[0],
        r.expander_chevron_margin[1],
        r.expander_chevron_margin[2],
        r.expander_chevron_margin[3],
    ))
    .child(
        SizedBox::new()
            .width(r.expander_chevron_button_size)
            .height(r.expander_chevron_button_size)
            .child(
                ControlBorder::new(Brush::Solid(chevron_fill), Brush::Solid(chevron_border))
                    .border_thickness_ltrb(r.expander_chevron_border_thickness)
                    .corner_radius(r.control_corner_radius[0])
                    .child(Center::new().child(chevron)),
            ),
    );
    let layout = Grid::new()
        .column_definitions([
            ColumnDefinition::new(GridLength::STAR),
            ColumnDefinition::new(GridLength::AUTO),
        ])
        .children([
            GridCell::new(
                Align::new()
                    .alignment(Alignment::CENTER_LEFT.into())
                    .width_factor(1.0)
                    .height_factor(1.0)
                    .child(DefaultTextStyle::new(
                        control_text_style(
                            &theme,
                            r.control_content_theme_font_size,
                            FontWeight::NORMAL,
                            foreground,
                        ),
                        content,
                    )),
            )
            .into_widget(),
            GridCell::new(Center::new().child(chevron))
                .column(1)
                .into_widget(),
        ]);
    FocusVisual::new(
        ConstrainedBox::new(
            BoxConstraints::new().min_height(widget.min_height.unwrap_or(r.expander_min_height)),
        )
        .child(
            ControlBorder::new(
                Brush::Solid(r.expander_header_background),
                Brush::Solid(border),
            )
            .border_thickness_ltrb(r.expander_header_border_thickness)
            .corner_radius_corners(radii)
            .padding(r.expander_header_padding)
            .child(layout),
        ),
        theme.theme,
    )
    .visible(states.focused)
    .into_widget()
}