waft-winui 0.18.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
//! The background, border and corner radius every template's root `ContentPresenter`, `Border` or `Grid` carries: XAML `Background`, `BorderBrush`, `BorderThickness`, `CornerRadius`, `BackgroundSizing` and `Padding`.

use crate::Brush;
use waft_embedder::{Canvas, Clip, Offset, RRect, Radius, Size, TextBaseline};
use waft_foundation::App;
use waft_painting::{BorderRadius, ClipContext, EdgeInsets, EdgeInsetsGeometry};
use waft_rendering::*;
use waft_widgets::*;

/// XAML `BackgroundSizing`: whether the background stops at the border's inner edge or extends under it.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum BackgroundSizing {
    /// Paints the background inside the border's inner edge.
    #[default]
    InnerBorderEdge,
    /// Paints the background beneath the border up to its outer edge.
    OuterBorderEdge,
}

/// A rounded rectangle with a background brush and a border brush, the child inside the border and padding.
#[derive(Clone, Debug)]
pub struct ControlBorder {
    /// The content placed inside the border and padding.
    pub child: Option<WidgetRef>,
    /// The brush filling the area selected by `background_sizing`.
    pub background: Option<Brush>,
    /// The brush filling the ring between the inner and outer border outlines.
    pub border_brush: Option<Brush>,
    /// Left, top, right, bottom.
    pub border_thickness: [f64; 4],
    /// Top-left, top-right, bottom-right, bottom-left.
    pub corner_radius: [f64; 4],
    /// Whether the background extends beneath the border.
    pub background_sizing: BackgroundSizing,
    /// Left, top, right, bottom, inside the border.
    pub padding: [f64; 4],
}

impl ControlBorder {
    /// Creates border chrome with the supplied brushes and a one-pixel border.
    pub fn new(background: Brush, border_brush: Brush) -> ControlBorder {
        Self::new_optional(Some(background), Some(border_brush))
    }

    /// Preserves XAML's distinction between an absent brush and a transparent brush.
    pub fn new_optional(background: Option<Brush>, border_brush: Option<Brush>) -> Self {
        ControlBorder {
            child: None,
            background,
            border_brush,
            border_thickness: [1.0; 4],
            corner_radius: [0.0; 4],
            background_sizing: BackgroundSizing::InnerBorderEdge,
            padding: [0.0; 4],
        }
    }

    /// Sets the content inside the border and padding.
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn child<K>(mut self, child: impl IntoWidget<K>) -> ControlBorder {
        self.child = Some(child.into_widget());
        self
    }

    /// Sets a uniform border width on all four sides.
    pub fn border_thickness(mut self, thickness: f64) -> ControlBorder {
        self.border_thickness = [thickness; 4];
        self
    }

    /// XAML `BorderThickness`, in left, top, right, bottom order.
    pub fn border_thickness_ltrb(mut self, thickness: [f64; 4]) -> ControlBorder {
        self.border_thickness = thickness;
        self
    }

    /// Sets a uniform radius on all four corners.
    pub fn corner_radius(mut self, radius: f64) -> ControlBorder {
        self.corner_radius = [radius; 4];
        self
    }

    /// XAML `CornerRadius`, in top-left, top-right, bottom-right, bottom-left order.
    pub fn corner_radius_corners(mut self, radii: [f64; 4]) -> ControlBorder {
        self.corner_radius = radii;
        self
    }

    /// Sets whether the background fills beneath the border.
    pub fn background_sizing(mut self, sizing: BackgroundSizing) -> ControlBorder {
        self.background_sizing = sizing;
        self
    }

    /// Sets additional insets inside the border in left, top, right, bottom order.
    pub fn padding(mut self, padding: [f64; 4]) -> ControlBorder {
        self.padding = padding;
        self
    }
}

impl StatelessWidget for ControlBorder {
    fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
        let [left, top, right, bottom] = self.padding;
        let thickness = self.border_thickness;
        let style = ControlBorderStyle {
            background: self.background,
            border_brush: self.border_brush,
            border_thickness: thickness,
            corner_radius: self.corner_radius,
            background_sizing: self.background_sizing,
        };
        let inner = Padding::new(EdgeInsetsGeometry::from_ltrb(
            left + thickness[0],
            top + thickness[1],
            right + thickness[2],
            bottom + thickness[3],
        ));
        let inner = match &self.child {
            Some(child) => inner.child(child.clone()),
            None => inner,
        };
        let [top_left, top_right, bottom_right, bottom_left] =
            self.corner_radius.map(Radius::circular);
        let rounded = self.corner_radius.iter().any(|radius| *radius > 0.0);
        ClipRRect::new()
            .border_radius(
                BorderRadius::only(top_left, top_right, bottom_left, bottom_right).into(),
            )
            .clip_behavior(if rounded { Clip::AntiAlias } else { Clip::None })
            .child(BorderPaint {
                style,
                child: inner.into_widget(),
            })
            .into_widget()
    }
}

/// Source brushes and geometry used for both painting and hit testing.
#[derive(Clone, Copy, Debug, PartialEq)]
struct ControlBorderStyle {
    /// An absent background leaves the interior out of this element's hit area.
    background: Option<Brush>,

    /// An absent border leaves the stroke out of this element's hit area.
    border_brush: Option<Brush>,

    /// Width of the four border edges.
    border_thickness: [f64; 4],

    /// Physical corner radii in clockwise order.
    corner_radius: [f64; 4],

    /// Chooses whether the fill extends under the border.
    background_sizing: BackgroundSizing,
}

impl ControlBorderStyle {
    /// Uses the same rounded outlines for drawing and pointer hit testing.
    fn outlines(self, size: Size) -> (RRect, RRect) {
        let bounds = Offset::ZERO & size;
        let [top_left, top_right, bottom_right, bottom_left] =
            self.corner_radius.map(Radius::circular);
        let outer =
            RRect::from_rect_and_corners(bounds, top_left, top_right, bottom_right, bottom_left)
                .scale_radii();
        let [left, top, right, bottom] = self.border_thickness;
        let inner = EdgeInsets::from_ltrb(left, top, right, bottom).deflate_rrect(outer);
        (outer, inner)
    }

    /// CBorder::HitTestLocalInternalImpl for the brush shapes supported by this kit.
    fn hit_test(self, size: Size, point: Offset) -> bool {
        if size.width() <= 0.0 || size.height() <= 0.0 {
            return false;
        }
        let (outer, inner) = self.outlines(size);
        let in_outer = outer.contains(point);
        let in_inner = inner.contains(point);
        let background_hit = self.background.is_some()
            && match self.background_sizing {
                BackgroundSizing::InnerBorderEdge => in_inner,
                BackgroundSizing::OuterBorderEdge => in_outer,
            };
        background_hit || (self.border_brush.is_some() && in_outer && !in_inner)
    }

    /// Paints the fill followed by the stroke, before the child.
    fn paint(self, canvas: &mut Canvas, size: Size) {
        let bounds = Offset::ZERO & size;
        let (outer, inner) = self.outlines(size);
        let background_rect = match self.background_sizing {
            BackgroundSizing::InnerBorderEdge => inner,
            BackgroundSizing::OuterBorderEdge => outer,
        };
        if let Some(background) = self.background
            && (matches!(background, Brush::Acrylic(_))
                || background.representative_color().a > 0.0)
        {
            background.paint_rrect(canvas, background_rect, bounds);
        }
        if let Some(border) = self.border_brush
            && self
                .border_thickness
                .iter()
                .any(|thickness| *thickness > 0.0)
            && (matches!(border, Brush::Acrylic(_)) || border.representative_color().a > 0.0)
        {
            border.paint_drrect(canvas, outer, inner, bounds);
        }
    }
}

/// A render-object widget keeps hit testing tied to layout size rather than the last paint.
#[derive(Debug)]
struct BorderPaint {
    /// Current source geometry and brushes.
    style: ControlBorderStyle,

    /// Child with border thickness and padding already applied.
    child: WidgetRef,
}

impl RenderObjectWidget for BorderPaint {
    type RenderObject = RenderBorderPaint;

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

    fn update_render_object(
        &self,
        app: &mut App,
        _: BuildContext,
        render: RenderHandle<RenderBorderPaint>,
    ) {
        if render.get(app).style != self.style {
            render.get_mut(app).style = self.style;
            render.as_object().mark_needs_paint(app);
        }
    }
}

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

/// Native layout proxy with WinUI's optional-brush hit-test rules.
struct RenderBorderPaint {
    /// Render identity and invalidation.
    render_object: RenderObjectData,

    /// Layout geometry used before any paint has occurred.
    render_box: RenderBoxData,

    /// The padded child.
    child: RenderObjectWithChildData<AnyRenderBox>,

    /// Source appearance and hit area.
    style: ControlBorderStyle,
}

impl RenderObjectWithChildMixin for RenderBorderPaint {
    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 RenderBorderPaint {}

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

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

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

    fn paint(
        self: RenderHandle<Self>,
        app: &mut App,
        context: &mut PaintingContext,
        offset: Offset,
    ) {
        let style = self.get(app).style;
        let size = self.size(app);
        let canvas = context.canvas();
        canvas.save();
        canvas.translate(offset.dx() as f32, offset.dy() as f32);
        style.paint(canvas, size);
        canvas.restore();
        RenderProxyBoxMixin::paint(self, app, context, offset);
    }
}

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

    fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
        RenderProxyBoxMixin::compute_min_intrinsic_width(self, app, height)
    }

    fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
        RenderProxyBoxMixin::compute_max_intrinsic_width(self, app, height)
    }

    fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
        RenderProxyBoxMixin::compute_min_intrinsic_height(self, app, width)
    }

    fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
        RenderProxyBoxMixin::compute_max_intrinsic_height(self, app, width)
    }

    fn compute_dry_layout(
        self: RenderHandle<Self>,
        app: &mut App,
        constraints: BoxConstraints,
    ) -> Size {
        RenderProxyBoxMixin::compute_dry_layout(self, app, constraints)
    }

    fn compute_distance_to_actual_baseline(
        self: RenderHandle<Self>,
        app: &mut App,
        baseline: TextBaseline,
    ) -> Option<f64> {
        RenderProxyBoxMixin::compute_distance_to_actual_baseline(self, app, baseline)
    }

    fn compute_dry_baseline(
        self: RenderHandle<Self>,
        app: &mut App,
        constraints: BoxConstraints,
        baseline: TextBaseline,
    ) -> Option<f64> {
        RenderProxyBoxMixin::compute_dry_baseline(self, app, constraints, baseline)
    }

    fn hit_test_self(self: RenderHandle<Self>, app: &App, position: Offset) -> bool {
        self.get(app).style.hit_test(self.size(app), position)
    }

    fn hit_test_children(
        self: RenderHandle<Self>,
        app: &mut App,
        result: &mut BoxHitTestResult<'_>,
        position: Offset,
    ) -> bool {
        // UIElement::HitTestRoundedCornerClip also clips descendants to the outer outline.
        let outer = self.get(app).style.outlines(self.size(app)).0;
        outer.contains(position)
            && RenderProxyBoxMixin::hit_test_children(self, app, result, position)
    }
}

#[cfg(test)]
mod tests {
    use waft_embedder::{Color, TextDirection};
    use waft_widgets::testing::WidgetTester;
    use waft_widgets::{
        CustomScrollView, Directionality, GlobalKey, IntoWidget, LayoutBuilder, SizedBox,
        SliverList, SliverToBoxAdapter,
    };

    use super::*;
    use crate::Margin;

    /// Every widget names the line of the app's code that made it: a child passed to a setter,
    /// a child passed to a constructor, a sliver list built from its builder, and a widget that
    /// is its own tree node.
    #[test]
    fn each_widget_names_the_line_that_made_it() {
        let mut tester = WidgetTester::new();
        let setter_key = GlobalKey::new();
        let constructor_key = GlobalKey::new();
        let builder_key = GlobalKey::new();
        let tree_node_key = GlobalKey::new();

        let border = ControlBorder::new(
            Brush::Solid(Color::new(0xFF000000)),
            Brush::Solid(Color::new(0xFFFFFFFF)),
        );
        let setter_line = line!() + 1;
        let setter = border.child(SizedBox::new().key(setter_key.clone().into()));
        let setter = setter.into_widget();

        let constructor_child = SizedBox::new().key(constructor_key.clone().into());
        let constructor_line = line!() + 1;
        let constructor = Margin::new([1.0; 4], constructor_child).into_widget();

        let builder = SliverList::builder(|_app, _context, index| {
            (index < 1).then(|| SizedBox::new().height(10.0).into_widget())
        })
        .item_count(1)
        .key(builder_key.clone().into());
        let builder_line = line!() + 1;
        let builder = builder.into_widget();

        let tree_node =
            LayoutBuilder::new(|_app, _context, _constraints| SizedBox::new().into_widget())
                .key(tree_node_key.clone().into());
        let tree_node_line = line!() + 1;
        let tree_node = tree_node.into_widget();

        tester.pump_widget(Directionality::new(
            TextDirection::Ltr,
            CustomScrollView::new().slivers([
                SliverToBoxAdapter::new().child(setter).into_widget(),
                SliverToBoxAdapter::new().child(constructor).into_widget(),
                builder,
                SliverToBoxAdapter::new().child(tree_node).into_widget(),
            ]),
        ));

        for (key, line) in [
            (setter_key, setter_line),
            (constructor_key, constructor_line),
            (builder_key, builder_line),
            (tree_node_key, tree_node_line),
        ] {
            let app = tester.cell.borrow();
            let context = key
                .current_context(&app)
                .expect("the widget is in the tree");
            let location = context
                .widget(&app)
                .creation_location()
                .expect("a creation location");
            assert_eq!(location.file(), file!());
            assert_eq!(location.line(), line);
        }
    }
}