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
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
//! TabViewItem's header template and pointer behavior from TabView.xaml and TabViewItem.cpp.

use crate::*;
use std::{any::Any, fmt, rc::Rc};
use waft_embedder::Canvas;
use waft_embedder::valo::{FillRule, Paint};
use waft_embedder::{Color, FontWeight, Path, PathBuilder, Size};
use waft_foundation::{App, Listener};
use waft_painting::{Alignment, EdgeInsetsGeometry};
use waft_rendering::{BoxConstraints, CustomPainter};
use waft_widgets::*;

/// How the tab strip allocates width to its headers.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TabViewWidthMode {
    /// Equal widths constrained by TabViewItemMinWidth and TabViewItemMaxWidth.
    #[default]
    Equal,

    /// Each header takes the width required by its content.
    SizeToContent,

    /// Unselected headers show their icon without their label.
    Compact,
}

/// When closable tabs expose their close button.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TabViewCloseButtonOverlayMode {
    /// The desktop default displays the close button.
    #[default]
    Auto,

    /// Displays the button for a selected or hovered tab.
    OnPointerOver,

    /// Displays the close button whenever the tab is closable.
    Always,
}

/// A stable tab identity, header, and content supplied to TabView.
///
/// The identity must remain unchanged when the tab moves within the collection.
#[derive(Clone)]
pub struct TabViewItem {
    /// Stable item identity, independent of its current collection index.
    pub id: String,

    /// Header content presented in the tab strip.
    pub header: WidgetRef,

    /// The original string header used for the default header ToolTip.
    pub header_text: Option<String>,

    /// The icon element presented beside the header.
    pub icon_source: Option<WidgetRef>,

    /// Content presented when this tab is selected.
    pub content: WidgetRef,

    /// Whether the close button and middle-click close request are available.
    pub is_closable: bool,

    /// Whether the tab accepts input and keyboard selection.
    pub is_enabled: bool,

    /// Whether the tab participates in layout and keyboard navigation.
    pub is_visible: bool,

    /// An explicit foreground used in the unselected resting state.
    pub foreground: Option<Color>,

    /// An explicit tooltip, overriding the nonempty string header fallback.
    pub tool_tip: Option<ToolTip>,

    /// Raised after the parent TabCloseRequested event; the owner decides whether to remove it.
    pub close_requested: Option<Listener>,
}

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

impl TabViewItem {
    /// Creates a tab with a stable identity, header widget and content widget.
    pub fn new<H, C>(
        id: impl Into<String>,
        header: impl IntoWidget<H>,
        content: impl IntoWidget<C>,
    ) -> Self {
        Self {
            id: id.into(),
            header: header.into_widget(),
            header_text: None,
            icon_source: None,
            content: content.into_widget(),
            is_closable: true,
            is_enabled: true,
            is_visible: true,
            foreground: None,
            tool_tip: None,
            close_requested: None,
        }
    }

    /// Creates a string header with the source's automatic header tooltip.
    pub fn text<C>(
        id: impl Into<String>,
        header: impl Into<String>,
        content: impl IntoWidget<C>,
    ) -> Self {
        let text = header.into();
        let mut item = Self::new(id, Text::new(text.clone()), content);
        item.header_text = Some(text);
        item
    }

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

    /// Sets whether closing can be requested for this tab.
    pub fn is_closable(mut self, value: bool) -> Self {
        self.is_closable = value;
        self
    }

    /// Sets whether the tab accepts pointer and keyboard input.
    pub fn is_enabled(mut self, value: bool) -> Self {
        self.is_enabled = value;
        self
    }

    /// Sets whether the tab participates in layout and navigation.
    pub fn is_visible(mut self, value: bool) -> Self {
        self.is_visible = value;
        self
    }

    /// Sets the unselected resting foreground.
    pub fn foreground(mut self, value: Color) -> Self {
        self.foreground = Some(value);
        self
    }

    /// Overrides the automatic string header tooltip.
    pub fn tool_tip(mut self, value: ToolTip) -> Self {
        self.tool_tip = Some(value);
        self
    }

    /// Receives close requests after the parent's event.
    pub fn close_requested(mut self, callback: Listener) -> Self {
        self.close_requested = Some(callback);
        self
    }
}

/// Source bottom border states for the item beside the selection.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum TabBottomBorderState {
    /// Draws the continuous resting border.
    Normal,

    /// Draws the arc joining the selected tab on the right.
    LeftOfSelectedTab,

    /// Draws the arc joining the selected tab on the left.
    RightOfSelectedTab,

    /// Hides the border while selected or dragging.
    NoBottomBorderLine,
}

/// Reports pointer entry and exit to the owning strip.
type TabViewItemHoverChanged = Rc<dyn Fn(&mut App, bool)>;

/// The realized header container; selection and item lifetime belong to its parent TabView.
#[derive(Clone)]
pub(crate) struct TabViewItemHeader {
    /// Owner-supplied identity and visual content.
    pub item: TabViewItem,

    /// Whether this header represents the selected page.
    pub selected: bool,

    /// Controls compact icon-only presentation.
    pub width_mode: TabViewWidthMode,

    /// Controls close-button visibility.
    pub close_overlay_mode: TabViewCloseButtonOverlayMode,

    /// Position of this item relative to the selected tab.
    pub bottom_border: TabBottomBorderState,

    /// Whether the trailing separator is present in the resting state.
    pub separator_visible: bool,

    /// Header focus node retained by the owner strip.
    pub focus_node: AnyFocusNode,

    /// Close-button focus node retained by the owner strip.
    pub close_focus_node: AnyFocusNode,

    /// Requests selecting this item.
    pub select: Listener,

    /// Requests closing this item.
    pub close: Listener,

    /// Updates the strip’s hovered identity and width-deferral state.
    pub hover_changed: TabViewItemHoverChanged,
}

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

impl StatelessWidget for TabViewItemHeader {
    fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
        let header = self.clone();
        let content = CommonStates::new(self.select.clone(), move |app, context, states| {
            header.template(app, context, states, true)
        })
        .is_enabled(self.item.is_enabled)
        .is_tab_stop(self.selected)
        .focus_node(self.focus_node);
        let enter = self.hover_changed.clone();
        let leave = self.hover_changed.clone();
        let close = self.close.clone();
        let mut content = GestureDetector::new().child(content);
        if self.item.is_enabled && self.item.is_closable {
            content = content.on_tertiary_tap_up(Rc::new(move |app, _| close.call(app)));
        }
        let content = MouseRegion::new()
            .on_enter(Rc::new(move |app, _| enter(app, true)))
            .on_exit(Rc::new(move |app, _| leave(app, false)))
            .child(content)
            .into_widget();
        let tooltip = self.item.tool_tip.clone().or_else(|| {
            self.item
                .header_text
                .as_ref()
                .filter(|text| !text.is_empty())
                .map(|text| ToolTip::text(text).placement(PlacementMode::Mouse))
        });
        if let Some(tooltip) = tooltip {
            ToolTipService::new(content, tooltip)
                .is_enabled(self.item.is_enabled)
                .into_widget()
        } else {
            content
        }
    }
}

impl TabViewItemHeader {
    /// UpdateCloseButton: OnPointerOver still shows the close button on a selected tab.
    fn close_visible(&self, states: ControlStates) -> bool {
        self.item.is_closable
            && (self.close_overlay_mode != TabViewCloseButtonOverlayMode::OnPointerOver
                || self.selected
                || matches!(
                    states.common,
                    CommonState::PointerOver | CommonState::Pressed
                ))
    }

    /// Builds LayoutRoot's named parts with the source visual-state resource substitutions.
    pub(crate) fn feedback(&self, app: &mut App, context: BuildContext) -> WidgetRef {
        let mut visual = self.clone();
        visual.item.header = self.item.header_text.as_ref().map_or_else(
            || SizedBox::new().into_widget(),
            |text| Text::new(text.clone()).into_widget(),
        );
        visual.item.icon_source = self.item.icon_source.as_ref().and_then(|icon| {
            downcast_widget::<FontIcon>(icon.as_ref()).map(|icon| {
                let mut icon = icon.clone();
                icon.key = None;
                icon.into_widget()
            })
        });
        visual.template(
            app,
            context,
            ControlStates {
                common: CommonState::Normal,
                focused: false,
            },
            false,
        )
    }

    /// Builds the source template for the current layout and visual state.
    fn template(
        &self,
        app: &mut App,
        context: BuildContext,
        states: ControlStates,
        interactive: bool,
    ) -> WidgetRef {
        let theme = ThemeResources::of(app, context);
        let r = &theme;
        let selected = self.selected;
        let compact = self.width_mode == TabViewWidthMode::Compact && !selected;
        let close_visible = self.close_visible(states);
        let (background, foreground, icon_foreground, close_background, close_foreground) =
            if !self.item.is_enabled {
                (
                    r.tab_view_item_header_background_disabled,
                    r.tab_view_item_header_foreground_disabled,
                    r.tab_view_button_foreground_disabled,
                    r.tab_view_item_header_disabled_close_button_background,
                    r.tab_view_item_header_disabled_close_button_foreground,
                )
            } else if selected {
                (
                    r.tab_view_item_header_background,
                    r.tab_view_item_header_foreground_selected,
                    r.tab_view_item_icon_foreground_selected,
                    r.tab_view_item_header_selected_close_button_background,
                    r.tab_view_item_header_selected_close_button_foreground,
                )
            } else {
                match states.common {
                    CommonState::PointerOver => (
                        r.tab_view_item_header_background_pointer_over,
                        r.tab_view_item_header_foreground_pointer_over,
                        r.tab_view_item_icon_foreground_pointer_over,
                        r.tab_view_item_header_pointer_over_close_button_background,
                        r.tab_view_item_header_pointer_over_close_button_foreground,
                    ),
                    CommonState::Pressed => (
                        r.tab_view_item_header_background_pressed,
                        r.tab_view_item_header_foreground_pressed,
                        r.tab_view_item_icon_foreground_pressed,
                        r.tab_view_item_header_pressed_close_button_background,
                        r.tab_view_item_header_pressed_close_button_foreground,
                    ),
                    _ => (
                        r.tab_view_item_header_background,
                        self.item
                            .foreground
                            .unwrap_or(r.tab_view_item_header_foreground),
                        self.item
                            .foreground
                            .unwrap_or(r.tab_view_item_icon_foreground),
                        r.tab_view_item_header_close_button_background,
                        r.tab_view_item_header_close_button_foreground,
                    ),
                }
            };
        let mut children = Vec::new();
        if let Some(icon) = &self.item.icon_source {
            // IconBox / IconControl, independently coloured from ContentPresenter.
            let icon = SizedBox::new()
                .width(r.tab_view_item_header_icon_size)
                .height(r.tab_view_item_header_icon_size)
                .child(FittedBox::new().child(DefaultTextStyle::new(
                    control_text_style(
                        &theme,
                        r.tab_view_item_header_icon_size,
                        FontWeight::NORMAL,
                        icon_foreground,
                    ),
                    icon.clone(),
                )));
            children.push(
                GridCell::new(
                    Center::new().child(
                        Padding::new(EdgeInsetsGeometry::only(
                            0.0,
                            0.0,
                            if compact {
                                0.0
                            } else {
                                r.tab_view_item_header_icon_margin[2]
                            },
                            0.0,
                        ))
                        .child(icon),
                    ),
                )
                .column(0)
                .into_widget(),
            );
        }
        if !compact {
            // ContentPresenter never implicitly binds to the tab's page content.
            children.push(
                GridCell::new(
                    Align::new().alignment(Alignment::CENTER_LEFT.into()).child(
                        DefaultTextStyle::new(
                            control_text_style(
                                &theme,
                                r.tab_view_item_header_font_size,
                                if selected {
                                    FontWeight::W600
                                } else {
                                    FontWeight::NORMAL
                                },
                                foreground,
                            ),
                            self.item.header.clone(),
                        )
                        .max_lines(1)
                        .overflow(waft_painting::TextOverflow::Ellipsis),
                    ),
                )
                .column(1)
                .into_widget(),
            );
        }
        if close_visible {
            let r2 = r.clone();
            let theme_kind = theme.theme;
            let close = if interactive {
                Button::new(
                    FontIcon::symbol(FluentSymbol::Dismiss)
                        .font_size(r.tab_view_item_header_close_font_size),
                    self.close.clone(),
                )
                .is_enabled(self.item.is_enabled)
                .is_tab_stop(false)
                .focus_node(self.close_focus_node)
                .template(move |_, _, states, content| {
                    close_button_template(
                        &r2,
                        theme_kind,
                        states,
                        content,
                        close_background,
                        close_foreground,
                    )
                })
                .into_widget()
            } else {
                close_button_template(
                    r,
                    theme_kind,
                    ControlStates {
                        common: CommonState::Normal,
                        focused: false,
                    },
                    FontIcon::symbol(FluentSymbol::Dismiss)
                        .font_size(r.tab_view_item_header_close_font_size)
                        .into_widget(),
                    close_background,
                    close_foreground,
                )
            };
            children.push(
                GridCell::new(
                    Padding::new(EdgeInsetsGeometry::only(
                        r.tab_view_item_header_close_margin[0],
                        0.0,
                        0.0,
                        0.0,
                    ))
                    .child(if interactive {
                        ToolTipService::new(close, ToolTip::text("Close tab (Ctrl+F4)"))
                            .into_widget()
                    } else {
                        close
                    }),
                )
                .column(2)
                .into_widget(),
            );
        }
        let grid = Grid::new()
            .column_definitions([
                ColumnDefinition::new(if compact {
                    GridLength::pixel(r.tab_view_item_header_icon_size)
                } else {
                    GridLength::AUTO
                }),
                ColumnDefinition::new(GridLength::star(1.0)),
                ColumnDefinition::new(GridLength::AUTO),
            ])
            .children(children);
        let border = if selected {
            Brush::VerticalElevation {
                stops: r.tab_view_selected_item_border_brush,
                length: 4.0,
            }
        } else {
            Brush::Solid(r.tab_view_item_border_brush)
        };
        // TabContainer: top corners only; the selected background supplies the lower outward curves.
        let content = ControlBorder::new(Brush::Solid(background), border)
            .border_thickness_ltrb(if selected {
                r.tab_view_selected_item_border_thickness
            } else {
                r.tab_view_item_border_thickness
            })
            .corner_radius_corners([
                r.overlay_corner_radius[0],
                r.overlay_corner_radius[1],
                0.0,
                0.0,
            ])
            .padding(if close_visible {
                r.tab_view_item_header_padding_with_close_button
            } else {
                r.tab_view_item_header_padding_without_close_button
            })
            .child(grid);
        let content = Margin::new(
            if selected {
                r.tab_view_selected_item_header_margin
            } else {
                [0.0; 4]
            },
            content,
        );
        let content =
            ConstrainedBox::new(BoxConstraints::new().min_height(r.tab_view_item_min_height))
                .child(content);
        let content = CustomPaint::new()
            .painter(TabChromePainter {
                selected,
                border_state: self.bottom_border,
                separator: self.separator_visible
                    && !selected
                    && !matches!(
                        states.common,
                        CommonState::PointerOver | CommonState::Pressed
                    ),
                selected_fill: r.tab_view_item_header_background_selected,
                border: r.tab_view_border_brush,
                separator_color: r.tab_view_item_separator,
                separator_margin: r.tab_view_item_separator_margin,
                selected_corner_radius: r.overlay_corner_radius,
            })
            .child(content);
        FocusVisual::new(content, theme.theme)
            .visible(states.focused)
            .margin([-3.0; 4])
            .corner_radius(r.overlay_corner_radius[0])
            .into_widget()
    }
}

/// TabViewCloseButtonStyle receives the parent item's rest brushes through template bindings.
/// Builds the source close-button chrome independently of its interaction wrapper.
fn close_button_template(
    r: &ThemeResources,
    theme: Theme,
    states: ControlStates,
    content: WidgetRef,
    background: Color,
    foreground: Color,
) -> WidgetRef {
    let (background, foreground, border) = match states.common {
        CommonState::PointerOver => (
            r.tab_view_item_header_close_button_background_pointer_over,
            r.tab_view_item_header_close_button_foreground_pointer_over,
            r.tab_view_item_header_close_button_border_brush_pointer_over,
        ),
        CommonState::Pressed => (
            r.tab_view_item_header_close_button_background_pressed,
            r.tab_view_item_header_close_button_foreground_pressed,
            r.tab_view_item_header_close_button_border_brush_pressed,
        ),
        CommonState::Disabled => (
            background,
            foreground,
            r.tab_view_item_header_close_button_border_brush_disabled,
        ),
        CommonState::Normal => (
            background,
            foreground,
            r.tab_view_item_header_close_button_border_brush,
        ),
    };
    let content = SizedBox::new()
        .width(r.tab_view_item_header_close_button_width)
        .height(r.tab_view_item_header_close_button_height)
        .child(
            ControlBorder::new(Brush::Solid(background), Brush::Solid(border))
                .border_thickness_ltrb(r.tab_view_item_header_close_button_border_thickness)
                .corner_radius(r.control_corner_radius[0])
                .child(Center::new().child(DefaultTextStyle::new(
                    control_text_style(
                        r,
                        r.tab_view_item_header_close_font_size,
                        FontWeight::NORMAL,
                        foreground,
                    ),
                    content,
                ))),
        );
    FocusVisual::new(content, theme)
        .visible(states.focused)
        .margin([-3.0; 4])
        .corner_radius(r.control_corner_radius[0])
        .into_widget()
}

/// LayoutRoot's bottom line, separator, selected background and outward corner arcs.
/// Draws the source selected geometry, adjacent arcs and trailing separator.
#[derive(Clone, Debug, PartialEq)]
struct TabChromePainter {
    /// Whether to draw the selected background geometry.
    selected: bool,

    /// Controls the source bottom-border and arc state.
    border_state: TabBottomBorderState,

    /// Whether to draw the trailing vertical separator.
    separator: bool,

    /// Theme brush for the selected background.
    selected_fill: Color,

    /// Theme brush for the lower border and radius arcs.
    border: Color,

    /// Theme brush for the trailing separator.
    separator_color: Color,

    /// `TabViewItemSeparatorMargin`: the separator's inset in left, top, right, bottom order.
    separator_margin: [f64; 4],

    /// `OverlayCornerRadius`: the selected background's top corners.
    selected_corner_radius: [f64; 4],
}

impl CustomPainter for TabChromePainter {
    fn paint(&self, _app: &mut App, canvas: &mut Canvas, size: Size) {
        let (w, h) = (size.width() as f32, size.height() as f32);
        if self.selected {
            canvas.draw_path(
                &selected_tab_geometry(self.selected_corner_radius, w, h),
                FillRule::NonZero,
                &Paint::from_color(self.selected_fill.into()),
            );
            // LeftRadiusRenderArc and RightRadiusRenderArc: literal source cubic paths.
            let mut left = PathBuilder::new();
            left.move_to((0.0, h - 4.0))
                .cubic_to(
                    (0.0, h - 2.80531),
                    (-0.52376, h - 1.73294),
                    (-1.35418, h - 1.0),
                )
                .line_to((-4.0, h - 1.0))
                .cubic_to((-2.34315, h - 1.0), (-1.0, h - 2.34315), (-1.0, h - 4.0))
                .line_to((0.0, h - 4.0))
                .close();
            canvas.draw_path(
                &left.build(),
                FillRule::NonZero,
                &Paint::from_color(self.border.into()),
            );
            let mut right = PathBuilder::new();
            right
                .move_to((w, h - 4.0))
                .cubic_to(
                    (w, h - 2.80531),
                    (w + 0.523755, h - 1.73294),
                    (w + 1.35418, h - 1.0),
                )
                .line_to((w + 4.0, h - 1.0))
                .cubic_to(
                    (w + 2.34315, h - 1.0),
                    (w + 1.0, h - 2.34315),
                    (w + 1.0, h - 4.0),
                )
                .line_to((w, h - 4.0))
                .close();
            canvas.draw_path(
                &right.build(),
                FillRule::NonZero,
                &Paint::from_color(self.border.into()),
            );
        } else if self.border_state != TabBottomBorderState::NoBottomBorderLine {
            let left = if self.border_state == TabBottomBorderState::RightOfSelectedTab {
                2.0
            } else {
                0.0
            };
            let right = if self.border_state == TabBottomBorderState::LeftOfSelectedTab {
                2.0
            } else {
                0.0
            };
            canvas.draw_rect(
                waft_embedder::valo::Rect::new(left, h - 1.0, w - left - right, 1.0),
                &Paint::from_color(self.border.into()),
            );
        }
        if self.separator {
            canvas.draw_rect(
                waft_embedder::valo::Rect::new(
                    w - 1.0,
                    self.separator_margin[1] as f32,
                    1.0,
                    (h - self.separator_margin[1] as f32 - self.separator_margin[3] as f32)
                        .max(0.0),
                ),
                &Paint::from_color(self.separator_color.into()),
            );
        }
    }

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

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

/// UpdateTabGeometry: four-pixel outward lower corners and the theme's upper radii.
fn selected_tab_geometry(corner_radius: [f64; 4], width: f32, height: f32) -> std::sync::Arc<Path> {
    use std::f32::consts::{FRAC_PI_2, PI};
    let left = corner_radius[0] as f32;
    let right = corner_radius[1] as f32;
    let mut path = PathBuilder::new();
    path.move_to((-4.0, height))
        .arc((-4.0, height - 4.0), 4.0, FRAC_PI_2, -FRAC_PI_2)
        .line_to((0.0, left))
        .arc((left, left), left, PI, FRAC_PI_2)
        .line_to((width - right, 0.0))
        .arc((width - right, right), right, -FRAC_PI_2, FRAC_PI_2)
        .line_to((width, height - 4.0))
        .arc((width + 4.0, height - 4.0), 4.0, PI, -FRAC_PI_2)
        .close();
    path.build()
}