waft-winui 0.4.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
//! XAML `InfoBadge` (`controls/dev/InfoBadge/InfoBadge_themeresources.xaml` and
//! `InfoBadge.cpp`): a small status badge showing a number, an icon, or a bare dot, filled with
//! its severity colour and rounded to a stadium by half its own height.

use crate::{Brush, FontIcon, Margin, ThemeResources, control_text_style};
use waft_embedder::{Canvas, Color, FontWeight, Matrix4, Offset, RRect, Radius, Size};
use waft_foundation::App;
use waft_painting::EdgeInsets;
use waft_rendering::*;
use waft_widgets::*;

/// The named `InfoBadge` styles of `InfoBadge_themeresources.xaml`, which differ in the
/// background their severity calls for.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum InfoBadgeStyle {
    /// `DefaultInfoBadgeStyle`: `InfoBadgeBackground`, the accent fill.
    #[default]
    Default,
    /// `AttentionDotInfoBadgeStyle` and its value and icon variants.
    Attention,
    /// `InformationalDotInfoBadgeStyle` and its value and icon variants.
    Informational,
    /// `SuccessDotInfoBadgeStyle` and its value and icon variants.
    Success,
    /// `CautionDotInfoBadgeStyle` and its value and icon variants.
    Caution,
    /// `CriticalDotInfoBadgeStyle` and its value and icon variants.
    Critical,
}

impl InfoBadgeStyle {
    /// The `Background` setter of the style, resolved in this theme.
    pub fn background(self, resources: &ThemeResources) -> Color {
        let common = &resources;
        match self {
            Self::Default => resources.info_badge_background,
            Self::Attention => common.system_fill_color_attention_brush,
            Self::Informational => common.system_fill_color_solid_neutral_brush,
            Self::Success => common.system_fill_color_success_brush,
            Self::Caution => common.system_fill_color_caution_brush,
            Self::Critical => common.system_fill_color_critical_brush,
        }
    }
}

/// The `DisplayKindStates` group of the template, as `OnDisplayKindPropertiesChanged` chooses it.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum InfoBadgeDisplayKind {
    /// `Dot`: neither a value nor an icon, leaving the filled shape alone.
    Dot,
    /// `Value`: the number, which takes precedence over an icon.
    Value,
    /// `Icon`: an icon that is not a glyph, inset on all four sides.
    Icon,
    /// `FontIcon`: a glyph, which needs no inset above it.
    FontIcon,
}

impl InfoBadgeDisplayKind {
    /// The margin the state's setter gives the visible element.
    fn margin(self, r: &ThemeResources) -> [f64; 4] {
        match self {
            Self::Dot => [0.0; 4],
            Self::Value => r.value_info_badge_text_margin,
            Self::Icon => r.icon_info_badge_icon_margin,
            Self::FontIcon => r.icon_info_badge_font_icon_margin,
        }
    }
}

/// XAML `InfoBadge`: a badge showing a count, an icon or a dot.
#[derive(Clone, Debug)]
pub struct InfoBadge {
    /// `Value`: the number shown, or -1 for none. Values below -1 are out of bounds.
    pub value: i32,

    /// `IconSource`: the icon shown when no value is set.
    pub icon_source: Option<WidgetRef>,

    /// The named style whose severity supplies the background.
    pub style: InfoBadgeStyle,

    /// `Background`, in place of the style's.
    pub background: Option<Brush>,

    /// Inherited number and icon color, in place of `InfoBadgeForeground`.
    pub foreground: Option<Color>,

    /// `CornerRadius` when the application sets one; otherwise half the arranged height, as
    /// `OnSizeChanged` computes it.
    pub corner_radius: Option<[f64; 4]>,

    /// `Padding` inside the filled shape; the theme's `InfoBadgePadding` when `None`.
    pub padding: Option<[f64; 4]>,

    /// Identity retained across parent rebuilds.
    pub key: Option<KeyRef>,
}

impl Default for InfoBadge {
    fn default() -> Self {
        Self {
            // `MUX_DEFAULT_VALUE("-1")` of InfoBadge.idl.
            value: -1,
            icon_source: None,
            style: InfoBadgeStyle::Default,
            background: None,
            foreground: None,
            corner_radius: None,
            padding: None,
            key: None,
        }
    }
}

impl InfoBadge {
    /// Creates a badge showing neither a value nor an icon: the source's dot.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the XAML `Value`; `OnPropertyChanged` rejects anything below -1.
    pub fn value(mut self, value: i32) -> Self {
        assert!(value >= -1, "Value must be equal to or greater than -1");
        self.value = value;
        self
    }

    /// Sets the XAML `IconSource`.
    pub fn icon_source<K>(mut self, icon: impl IntoWidget<K>) -> Self {
        self.icon_source = Some(icon.into_widget());
        self
    }

    /// Sets which named style supplies the background.
    pub fn style(mut self, style: InfoBadgeStyle) -> Self {
        self.style = style;
        self
    }

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

    /// Sets the XAML `Foreground`.
    pub fn foreground(mut self, foreground: Color) -> Self {
        self.foreground = Some(foreground);
        self
    }

    /// Sets the XAML `CornerRadius`, which the source keeps instead of halving the height.
    pub fn corner_radius(mut self, radii: [f64; 4]) -> Self {
        self.corner_radius = Some(radii);
        self
    }

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

    /// Sets the identity retained across rebuilds.
    pub fn key(mut self, key: KeyRef) -> Self {
        self.key = Some(key);
        self
    }

    /// `InfoBadge::OnDisplayKindPropertiesChanged`: the value wins over an icon, and a glyph
    /// icon takes the tighter margin.
    pub fn display_kind(&self) -> InfoBadgeDisplayKind {
        if self.value >= 0 {
            InfoBadgeDisplayKind::Value
        } else if let Some(icon) = &self.icon_source {
            if downcast_widget::<FontIcon>(icon.as_ref()).is_some() {
                InfoBadgeDisplayKind::FontIcon
            } else {
                InfoBadgeDisplayKind::Icon
            }
        } else {
            InfoBadgeDisplayKind::Dot
        }
    }
}

impl StatelessWidget for InfoBadge {
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
        let resources = ThemeResources::of(app, context);
        let foreground = self
            .foreground
            .unwrap_or_else(|| resources.info_badge_foreground);
        let kind = self.display_kind();
        // ValueTextBlock and IconPresenter, of which one state at a time is visible.
        let content = match kind {
            InfoBadgeDisplayKind::Dot => SizedBox::new().into_widget(),
            InfoBadgeDisplayKind::Value => Margin::new(
                kind.margin(&resources),
                // HorizontalAlignment and VerticalAlignment are Center, so the text keeps its
                // own size until the badge is squared up around it.
                Center::new().width_factor(1.0).height_factor(1.0).child(
                    Text::new(self.value.to_string()).style(control_text_style(
                        &resources,
                        resources.info_badge_value_font_size,
                        FontWeight::NORMAL,
                        foreground,
                    )),
                ),
            )
            .into_widget(),
            InfoBadgeDisplayKind::Icon | InfoBadgeDisplayKind::FontIcon => Margin::new(
                kind.margin(&resources),
                // IconPresenter is a Viewbox: it keeps the icon's own size where there is room
                // and scales it down to whatever height the badge is left with.
                Center::new()
                    .width_factor(1.0)
                    .height_factor(1.0)
                    .child(FittedBox::new().child(self.icon_source.clone().unwrap())),
            )
            .into_widget(),
        };
        let content = DefaultTextStyle::new(
            DefaultTextStyle::of(app, context)
                .style
                .clone()
                .color(foreground),
            content,
        );

        // RootGrid: the filled, rounded background around the padded content.
        let [left, top, right, bottom] = self.padding.unwrap_or(resources.info_badge_padding);
        let root = CustomPaint::new()
            .painter(BadgeFill {
                background: self
                    .background
                    .unwrap_or_else(|| Brush::Solid(self.style.background(&resources))),
                corner_radius: self.corner_radius,
            })
            .child(
                Padding::new(EdgeInsets::from_ltrb(left, top, right, bottom).into()).child(content),
            );
        ConstrainedBox::new(BoxConstraints {
            min_width: resources.info_badge_min_width,
            max_width: f64::INFINITY,
            min_height: resources.info_badge_min_height,
            max_height: resources.info_badge_max_height,
        })
        .child(SquareWhenTallerThanWide::new(root))
        .into_widget()
    }
}

/// RootGrid's fill: `Background` inside `TemplateSettings.InfoBadgeCornerRadius`.
#[derive(Clone, Copy, Debug, PartialEq)]
struct BadgeFill {
    background: Brush,
    corner_radius: Option<[f64; 4]>,
}

impl CustomPainter for BadgeFill {
    fn paint(&self, _app: &mut App, canvas: &mut Canvas, size: Size) {
        // InfoBadge::OnSizeChanged halves the arranged height unless CornerRadius is set.
        let radii = self.corner_radius.unwrap_or([size.height() / 2.0; 4]);
        let [top_left, top_right, bottom_right, bottom_left] = radii.map(Radius::circular);
        let bounds = Offset::ZERO & size;
        let shape =
            RRect::from_rect_and_corners(bounds, top_left, top_right, bottom_right, bottom_left)
                .scale_radii();
        self.background.paint_rrect(canvas, shape, bounds);
    }

    fn should_repaint(&self, _app: &App, old_delegate: &dyn CustomPainter) -> bool {
        old_delegate.as_any().downcast_ref::<Self>() != Some(self)
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

/// `InfoBadge::MeasureOverride`: a badge never becomes taller than it is wide.
#[derive(Debug)]
struct SquareWhenTallerThanWide {
    key: Option<KeyRef>,
    child: WidgetRef,
}

impl SquareWhenTallerThanWide {
    fn new<K>(child: impl IntoWidget<K>) -> Self {
        Self {
            key: None,
            child: child.into_widget(),
        }
    }
}

impl RenderObjectWidget for SquareWhenTallerThanWide {
    type RenderObject = RenderSquareWhenTallerThanWide;

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

    fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
        RenderHandle::new_box(
            app,
            RenderSquareWhenTallerThanWide {
                render_object: RenderObjectData::new(),
                render_box: RenderBoxData::new(),
                child: RenderObjectWithChildData::new(),
            },
        )
        .as_object()
    }

    fn update_render_object(
        &self,
        _app: &mut App,
        _context: BuildContext,
        _render: RenderHandle<RenderSquareWhenTallerThanWide>,
    ) {
    }
}

impl SingleChildRenderObjectWidget for SquareWhenTallerThanWide {
    fn child(&self) -> Option<&WidgetRef> {
        Some(&self.child)
    }
}

/// Measures the template once, squares the result up, then arranges it at that size.
struct RenderSquareWhenTallerThanWide {
    render_object: RenderObjectData,
    render_box: RenderBoxData,
    child: RenderObjectWithChildData<AnyRenderBox>,
}

/// `MeasureOverride`'s rule: a badge narrower than it is tall becomes square.
fn squared(desired: Size) -> Size {
    if desired.width() < desired.height() {
        Size::new(desired.height(), desired.height())
    } else {
        desired
    }
}

impl RenderObjectWithChildMixin for RenderSquareWhenTallerThanWide {
    type ChildType = AnyRenderBox;

    fn child_data(self: RenderHandle<Self>, app: &App) -> &RenderObjectWithChildData<AnyRenderBox> {
        &self.get(app).child
    }

    fn child_data_mut(
        self: RenderHandle<Self>,
        app: &mut App,
    ) -> &mut RenderObjectWithChildData<AnyRenderBox> {
        &mut self.get_mut(app).child
    }
}

impl RenderProxyBoxMixin for RenderSquareWhenTallerThanWide {}

impl RenderObject for RenderSquareWhenTallerThanWide {
    waft_rendering::render_object_accessors!();

    fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
        let constraints = self.constraints(app);
        let Some(child) = self.child(app) else {
            self.set_size(app, constraints.constrain(Size::ZERO));
            return;
        };
        child.layout(app, constraints, true);
        let size = constraints.constrain(squared(child.size(app)));
        if size != child.size(app) {
            child.layout(app, BoxConstraints::tight(size), true);
        }
        self.set_size(app, size);
    }

    fn paint(
        self: RenderHandle<Self>,
        app: &mut App,
        context: &mut PaintingContext,
        offset: Offset,
    ) {
        RenderProxyBoxMixin::paint(self, app, context, offset);
    }

    fn visit_children(
        self: RenderHandle<Self>,
        app: &App,
        visitor: &mut dyn FnMut(AnyRenderObject),
    ) {
        if let Some(child) = self.child(app) {
            visitor(child.as_object());
        }
    }
}

impl RenderBox for RenderSquareWhenTallerThanWide {
    waft_rendering::render_box_accessors!();

    fn setup_parent_data(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
        RenderProxyBoxMixin::setup_parent_data(self, app, child);
    }

    fn apply_paint_transform(
        self: RenderHandle<Self>,
        app: &App,
        child: AnyRenderObject,
        transform: &mut Matrix4,
    ) {
        RenderProxyBoxMixin::apply_paint_transform(self, app, child, transform);
    }

    fn hit_test_children(
        self: RenderHandle<Self>,
        app: &mut App,
        result: &mut BoxHitTestResult<'_>,
        position: Offset,
    ) -> bool {
        RenderProxyBoxMixin::hit_test_children(self, app, result, position)
    }

    fn compute_dry_layout(
        self: RenderHandle<Self>,
        app: &mut App,
        constraints: BoxConstraints,
    ) -> Size {
        match self.child(app) {
            Some(child) => constraints.constrain(squared(child.get_dry_layout(app, constraints))),
            None => constraints.constrain(Size::ZERO),
        }
    }
}