Skip to main content

waft_winui/community_toolkit/
settings_card.rs

1//! The Toolkit's `SettingsCard` with `DefaultSettingsCardStyle` of `SettingsCard/SettingsCard.xaml`: one setting as a card, its name and description at the start and its control at the end, or a card to click through.
2//!
3//! Template, element by element: `PART_RootGrid` (rows `*` and Auto; columns Auto, `*`, Auto, Auto; the card's background, border, corner radius and padding, with an 83 ms `BrushTransition` on the background) holds `PART_HeaderIconPresenterHolder` (a `Viewbox` of at most 20 × 20 in column 0), `HeaderPanel` (column 1, a vertical `StackPanel` of `PART_HeaderPresenter` and `PART_DescriptionPresenter`, centred down the row), `PART_ContentPresenter` (column 2, at the right and centred) and `PART_ActionIconPresenterHolder` (a `Viewbox` of at most 13 × 13 in column 3, over both rows).
4//!
5//! Visual state groups: `CommonStates` swaps the background, border and foregrounds, and leaves `Normal` unless the card can be clicked; `ContentAlignmentStates` moves the content under the header ([`ContentAlignmentState`]); `ContentSpacingStates` puts `SettingsCardVerticalHeaderContentSpacing` between the rows once the content is under a header or description; `ContentVisibilityStates` drops the content presenter of a card without content.
6//!
7//! Behaviour is `SettingsCard.cs`: a `ButtonBase` that takes pointer and keyboard input only while `IsClickEnabled`, shows its action icon only then, and collapses the presenters of a header, description and header icon that are not set.
8
9use crate::{
10    BackgroundSizing, Brush, ColorTransition, ColumnDefinition, CommonState, CommonStates,
11    ControlStates, FLUENT_ICON_FONT_FAMILY, FluentSymbol, FocusVisual, FontIcon, Grid, GridCell,
12    GridLength, HorizontalAlignment, Margin, RowDefinition, ThemeResources, ToolTip,
13    ToolTipService, control_text_style,
14};
15use std::fmt;
16use waft_embedder::{Color, FontWeight};
17use waft_foundation::{App, Listener};
18use waft_painting::{AlignmentGeometry, TextStyle};
19use waft_rendering::BoxConstraints;
20use waft_widgets::*;
21
22/// `SettingsCardBorderThickness`: left, top, right, bottom.
23pub const SETTINGS_CARD_BORDER_THICKNESS: [f64; 4] = [1.0; 4];
24/// `SettingsCardPadding`: left, top, right, bottom.
25pub const SETTINGS_CARD_PADDING: [f64; 4] = [16.0; 4];
26/// `SettingsCardMinWidth`.
27pub const SETTINGS_CARD_MIN_WIDTH: f64 = 148.0;
28/// `SettingsCardMinHeight`.
29pub const SETTINGS_CARD_MIN_HEIGHT: f64 = 68.0;
30/// `SettingsCardDescriptionFontSize`.
31pub const SETTINGS_CARD_DESCRIPTION_FONT_SIZE: f64 = 12.0;
32/// `SettingsCardHeaderIconMaxSize`.
33pub const SETTINGS_CARD_HEADER_ICON_MAX_SIZE: f64 = 20.0;
34/// `SettingsCardLeftIndention`: the least width of the header icon's column.
35pub const SETTINGS_CARD_LEFT_INDENTION: f64 = 0.0;
36/// `SettingsCardContentMinWidth`: the `MinWidth` the card's style gives a `Slider`, `ComboBox` or `TextBox` in it.
37pub const SETTINGS_CARD_CONTENT_MIN_WIDTH: f64 = 120.0;
38/// `SettingsCardHeaderIconMargin`: left, top, right, bottom.
39pub const SETTINGS_CARD_HEADER_ICON_MARGIN: [f64; 4] = [2.0, 0.0, 20.0, 0.0];
40/// `SettingsCardActionIconMargin`: left, top, right, bottom.
41pub const SETTINGS_CARD_ACTION_ICON_MARGIN: [f64; 4] = [14.0, 0.0, 0.0, 0.0];
42/// `SettingsCardActionIconMaxSize`.
43pub const SETTINGS_CARD_ACTION_ICON_MAX_SIZE: f64 = 13.0;
44/// `SettingsCardVerticalHeaderContentSpacing`.
45pub const SETTINGS_CARD_VERTICAL_HEADER_CONTENT_SPACING: f64 = 8.0;
46/// `SettingsCardWrapThreshold`: a card narrower than this shows its content under its header.
47pub const SETTINGS_CARD_WRAP_THRESHOLD: f64 = 476.0;
48/// `SettingsCardWrapNoIconThreshold`: a card narrower than this also hides its header icon.
49pub const SETTINGS_CARD_WRAP_NO_ICON_THRESHOLD: f64 = 286.0;
50/// `HeaderPanel`'s `Margin` while the content is beside it: left, top, right, bottom.
51const HEADER_PANEL_MARGIN: [f64; 4] = [0.0, 0.0, 24.0, 0.0];
52/// `FocusVisualMargin` of the style.
53const FOCUS_VISUAL_MARGIN: [f64; 4] = [-3.0; 4];
54
55/// The Toolkit's `ContentAlignment`: where a [`SettingsCard`] puts its content.
56#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
57pub enum ContentAlignment {
58    /// The content is at the right of the header.
59    #[default]
60    Right,
61    /// The content is at the left, and the header, description and header icon are not shown; for a check box, radio buttons or a layout of the app's own.
62    Left,
63    /// The content is under the header and description.
64    Vertical,
65}
66
67/// The `ContentAlignmentStates` group, picked from [`ContentAlignment`] and the card's width.
68#[derive(Clone, Copy, Debug, PartialEq, Eq)]
69pub enum ContentAlignmentState {
70    Right,
71    /// The card is narrower than `SettingsCardWrapThreshold`: the content is under the header, at the left.
72    RightWrapped,
73    /// The card is narrower than `SettingsCardWrapNoIconThreshold`: wrapped, and the header icon is not shown.
74    RightWrappedNoIcon,
75    Left,
76    Vertical,
77}
78
79impl ContentAlignmentState {
80    /// The state for a card `width` wide. Its triggers are all custom ones, so where two are active the one the XAML declares first wins: a narrow card wraps whatever its `ContentAlignment`.
81    pub fn of(content_alignment: ContentAlignment, width: f64) -> ContentAlignmentState {
82        if width < SETTINGS_CARD_WRAP_NO_ICON_THRESHOLD {
83            ContentAlignmentState::RightWrappedNoIcon
84        } else if width < SETTINGS_CARD_WRAP_THRESHOLD {
85            ContentAlignmentState::RightWrapped
86        } else {
87            match content_alignment {
88                ContentAlignment::Right => ContentAlignmentState::Right,
89                ContentAlignment::Left => ContentAlignmentState::Left,
90                ContentAlignment::Vertical => ContentAlignmentState::Vertical,
91            }
92        }
93    }
94
95    /// Whether the state hides `PART_HeaderIconPresenterHolder`.
96    fn collapses_header_icon(self) -> bool {
97        matches!(
98            self,
99            ContentAlignmentState::RightWrappedNoIcon | ContentAlignmentState::Left
100        )
101    }
102
103    /// Whether the state hides `PART_HeaderPresenter` and `PART_DescriptionPresenter`.
104    fn collapses_header(self) -> bool {
105        self == ContentAlignmentState::Left
106    }
107}
108
109/// The Toolkit's `SettingsCard`: `Header`, `Description` and `HeaderIcon` at the start, `Content` at the end, and with `IsClickEnabled` a `Click` on the whole card, marked by `ActionIcon`.
110#[derive(Clone)]
111pub struct SettingsCard {
112    /// `Header`: the setting's name; nothing is shown for it when unset.
113    pub header: Option<WidgetRef>,
114    /// `Description`: the text under the header, which wraps; nothing is shown for it when unset.
115    pub description: Option<WidgetRef>,
116    /// `HeaderIcon`: the icon before the header, scaled into 20 × 20.
117    pub header_icon: Option<WidgetRef>,
118    /// `ActionIcon`: the icon at the end of a card that can be clicked, scaled into 13 × 13; a chevron pointing on by default.
119    pub action_icon: WidgetRef,
120    /// `ActionIconToolTip`: the tooltip of the action icon.
121    pub action_icon_tool_tip: Option<String>,
122    /// `IsClickEnabled`: whether the card itself is a button.
123    pub is_click_enabled: bool,
124    /// `ContentAlignment`.
125    pub content_alignment: ContentAlignment,
126    /// `IsActionIconVisible`: whether a card that can be clicked shows its action icon.
127    pub is_action_icon_visible: bool,
128    /// `Content`: the setting's control.
129    pub content: Option<WidgetRef>,
130    /// `Click` of `ButtonBase`, raised only while `is_click_enabled`.
131    pub click: Option<Listener>,
132    /// `IsEnabled`.
133    pub is_enabled: bool,
134    /// `HorizontalContentAlignment`, which places the content across the card in the [`ContentAlignment::Vertical`] layout; `Right` in the style.
135    pub horizontal_content_alignment: HorizontalAlignment,
136    /// `Background`; `SettingsCardBackground` when `None`.
137    pub background: Option<Color>,
138    /// `BorderThickness`: left, top, right, bottom; `SettingsCardBorderThickness` when `None`.
139    pub border_thickness: Option<[f64; 4]>,
140    /// `CornerRadius` of every corner; the theme's `ControlCornerRadius` when `None`.
141    pub corner_radius: Option<f64>,
142    /// `MinHeight`; `SettingsCardMinHeight` when `None`.
143    pub min_height: Option<f64>,
144    /// `Padding`: left, top, right, bottom; `SettingsCardPadding` when `None`.
145    pub padding: Option<[f64; 4]>,
146    pub key: Option<KeyRef>,
147}
148
149impl Default for SettingsCard {
150    fn default() -> SettingsCard {
151        SettingsCard {
152            header: None,
153            description: None,
154            header_icon: None,
155            // The constructor's `FontIcon`, glyph U+E974 mirrored in right-to-left layout.
156            action_icon: FontIcon::symbol(FluentSymbol::ChevronRight)
157                .mirrored_when_right_to_left(true)
158                .into_widget(),
159            action_icon_tool_tip: None,
160            is_click_enabled: false,
161            content_alignment: ContentAlignment::Right,
162            is_action_icon_visible: true,
163            content: None,
164            click: None,
165            is_enabled: true,
166            horizontal_content_alignment: HorizontalAlignment::Right,
167            background: None,
168            border_thickness: None,
169            corner_radius: None,
170            min_height: None,
171            padding: None,
172            key: None,
173        }
174    }
175}
176
177impl SettingsCard {
178    pub fn new() -> SettingsCard {
179        SettingsCard::default()
180    }
181
182    /// XAML `Header`.
183    #[cfg_attr(debug_assertions, track_caller)]
184    pub fn header<K>(mut self, header: impl IntoWidget<K>) -> SettingsCard {
185        self.header = Some(header.into_widget());
186        self
187    }
188
189    /// XAML `Description`.
190    #[cfg_attr(debug_assertions, track_caller)]
191    pub fn description<K>(mut self, description: impl IntoWidget<K>) -> SettingsCard {
192        self.description = Some(description.into_widget());
193        self
194    }
195
196    /// XAML `HeaderIcon`.
197    #[cfg_attr(debug_assertions, track_caller)]
198    pub fn header_icon<K>(mut self, header_icon: impl IntoWidget<K>) -> SettingsCard {
199        self.header_icon = Some(header_icon.into_widget());
200        self
201    }
202
203    /// XAML `ActionIcon`.
204    #[cfg_attr(debug_assertions, track_caller)]
205    pub fn action_icon<K>(mut self, action_icon: impl IntoWidget<K>) -> SettingsCard {
206        self.action_icon = action_icon.into_widget();
207        self
208    }
209
210    /// XAML `ActionIconToolTip`.
211    pub fn action_icon_tool_tip(mut self, tool_tip: impl Into<String>) -> SettingsCard {
212        self.action_icon_tool_tip = Some(tool_tip.into());
213        self
214    }
215
216    /// XAML `IsClickEnabled`.
217    pub fn is_click_enabled(mut self, is_click_enabled: bool) -> SettingsCard {
218        self.is_click_enabled = is_click_enabled;
219        self
220    }
221
222    /// XAML `ContentAlignment`.
223    pub fn content_alignment(mut self, content_alignment: ContentAlignment) -> SettingsCard {
224        self.content_alignment = content_alignment;
225        self
226    }
227
228    /// XAML `IsActionIconVisible`.
229    pub fn is_action_icon_visible(mut self, is_action_icon_visible: bool) -> SettingsCard {
230        self.is_action_icon_visible = is_action_icon_visible;
231        self
232    }
233
234    /// XAML `Content`.
235    #[cfg_attr(debug_assertions, track_caller)]
236    pub fn content<K>(mut self, content: impl IntoWidget<K>) -> SettingsCard {
237        self.content = Some(content.into_widget());
238        self
239    }
240
241    /// XAML `Click`.
242    pub fn click(mut self, click: Listener) -> SettingsCard {
243        self.click = Some(click);
244        self
245    }
246
247    /// XAML `IsEnabled`.
248    pub fn is_enabled(mut self, is_enabled: bool) -> SettingsCard {
249        self.is_enabled = is_enabled;
250        self
251    }
252
253    /// XAML `HorizontalContentAlignment`.
254    pub fn horizontal_content_alignment(mut self, alignment: HorizontalAlignment) -> SettingsCard {
255        self.horizontal_content_alignment = alignment;
256        self
257    }
258
259    /// XAML `Background`.
260    pub fn background(mut self, background: Color) -> SettingsCard {
261        self.background = Some(background);
262        self
263    }
264
265    /// XAML `BorderThickness`.
266    pub fn border_thickness(mut self, border_thickness: [f64; 4]) -> SettingsCard {
267        self.border_thickness = Some(border_thickness);
268        self
269    }
270
271    /// XAML `CornerRadius`.
272    pub fn corner_radius(mut self, corner_radius: f64) -> SettingsCard {
273        self.corner_radius = Some(corner_radius);
274        self
275    }
276
277    /// XAML `MinHeight`.
278    pub fn min_height(mut self, min_height: f64) -> SettingsCard {
279        self.min_height = Some(min_height);
280        self
281    }
282
283    /// XAML `Padding`.
284    pub fn padding(mut self, padding: [f64; 4]) -> SettingsCard {
285        self.padding = Some(padding);
286        self
287    }
288
289    pub fn key(mut self, key: KeyRef) -> SettingsCard {
290        self.key = Some(key);
291        self
292    }
293}
294
295impl fmt::Debug for SettingsCard {
296    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
297        f.debug_struct("SettingsCard")
298            .field("is_click_enabled", &self.is_click_enabled)
299            .field("content_alignment", &self.content_alignment)
300            .field("is_enabled", &self.is_enabled)
301            .finish_non_exhaustive()
302    }
303}
304
305impl StatelessWidget for SettingsCard {
306    fn key(&self) -> Option<&KeyRef> {
307        self.key.as_ref()
308    }
309
310    fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
311        let card = self.clone();
312        // The `ControlSizeTrigger`s watch `PART_RootGrid`'s width, which the style's `HorizontalAlignment="Stretch"` makes the width the card is given.
313        LayoutBuilder::new(move |app, context, constraints| {
314            let width = constraints.max_width;
315            if !card.is_click_enabled {
316                // `DisableButtonInteraction`: no pointer or key handler is attached, so `CommonStates` leaves `Normal` only for `Disabled`.
317                let states = ControlStates {
318                    common: if card.is_enabled {
319                        CommonState::Normal
320                    } else {
321                        CommonState::Disabled
322                    },
323                    focused: false,
324                };
325                return while_enabled(
326                    card.is_enabled,
327                    template(app, context, &card, states, width),
328                );
329            }
330            let click = card.click.clone().unwrap_or_else(|| Listener::new(|_| {}));
331            let templated = card.clone();
332            let button = CommonStates::new(click, move |app, context, states| {
333                template(app, context, &templated, states, width)
334            })
335            .is_enabled(card.is_enabled);
336            while_enabled(card.is_enabled, button)
337        })
338        .into_widget()
339    }
340}
341
342/// `IsEnabled` reaches everything in a control: a disabled card's content takes no pointer and no focus.
343#[cfg_attr(debug_assertions, track_caller)]
344fn while_enabled<K>(is_enabled: bool, child: impl IntoWidget<K>) -> WidgetRef {
345    ExcludeFocus::new(IgnorePointer::new().ignoring(!is_enabled).child(child))
346        .excluding(!is_enabled)
347        .into_widget()
348}
349
350/// The brushes of one `CommonStates` state: the `{ThemeResource SettingsCard…}` values its storyboard names, each the WinUI brush the Toolkit's theme dictionaries alias.
351struct CardBrushes {
352    /// `PART_RootGrid.Background`; `None` leaves the card's `Background`.
353    background: Option<Color>,
354    /// `PART_RootGrid.BorderBrush`.
355    border_brush: Brush,
356    /// `PART_HeaderPresenter.Foreground` and `PART_HeaderIconPresenter.Foreground`.
357    header: Color,
358    /// `PART_DescriptionPresenter.Foreground`.
359    description: Color,
360    /// `PART_ActionIconPresenter.Foreground`.
361    action_icon: Color,
362}
363
364impl CardBrushes {
365    fn of(r: &ThemeResources, state: CommonState) -> CardBrushes {
366        // `SettingsCardForeground`, the style's `Foreground`, and the description presenter's own.
367        let foreground = r.text_fill_color_primary_brush;
368        let description = r.text_fill_color_secondary_brush;
369        // `SettingsCardBorderBrush`, the style's `BorderBrush`.
370        let border_brush = Brush::Solid(r.card_stroke_color_default_brush);
371        match state {
372            CommonState::Normal => CardBrushes {
373                background: None,
374                border_brush,
375                header: foreground,
376                description,
377                action_icon: foreground,
378            },
379            CommonState::PointerOver => CardBrushes {
380                // `SettingsCardBackgroundPointerOver`.
381                background: Some(r.control_fill_color_secondary_brush),
382                // `SettingsCardBorderBrushPointerOver`.
383                border_brush: Brush::ControlElevation(r.control_elevation_border_brush),
384                // `SettingsCardForegroundPointerOver`.
385                header: r.text_fill_color_primary_brush,
386                description,
387                action_icon: foreground,
388            },
389            CommonState::Pressed => CardBrushes {
390                // `SettingsCardBackgroundPressed`.
391                background: Some(r.control_fill_color_tertiary_brush),
392                // `SettingsCardBorderBrushPressed`.
393                border_brush: Brush::Solid(r.control_stroke_color_default_brush),
394                // `SettingsCardForegroundPressed`.
395                header: r.text_fill_color_secondary_brush,
396                description: r.text_fill_color_secondary_brush,
397                action_icon: foreground,
398            },
399            // `SettingsCardForegroundDisabled`; the state names no background or border.
400            CommonState::Disabled => CardBrushes {
401                background: None,
402                border_brush,
403                header: r.text_fill_color_disabled_brush,
404                description: r.text_fill_color_disabled_brush,
405                action_icon: r.text_fill_color_disabled_brush,
406            },
407        }
408    }
409}
410
411/// `DefaultSettingsCardStyle`'s `ControlTemplate` for a card `width` wide in `states`.
412fn template(
413    app: &mut App,
414    context: BuildContext,
415    card: &SettingsCard,
416    states: ControlStates,
417    width: f64,
418) -> WidgetRef {
419    let resources = ThemeResources::of(app, context);
420    let r = &*resources;
421    let brushes = CardBrushes::of(r, states.common);
422    let alignment = ContentAlignmentState::of(card.content_alignment, width);
423    let header = card
424        .header
425        .clone()
426        .filter(|_| !alignment.collapses_header());
427    let description = card
428        .description
429        .clone()
430        .filter(|_| !alignment.collapses_header());
431    // `CheckVerticalSpacingState`, which reads the properties and not what the state shows.
432    let content_spacing = alignment != ContentAlignmentState::Right
433        && alignment != ContentAlignmentState::Left
434        && card.content.is_some()
435        && (card.header.is_some() || card.description.is_some());
436
437    let mut children = Vec::new();
438    if let Some(icon) = card.header_icon.clone()
439        && !alignment.collapses_header_icon()
440    {
441        children.push(header_icon_presenter_holder(r, icon, brushes.header));
442    }
443    children.push(header_panel(r, &brushes, alignment, header, description));
444    if let Some(content) = card.content.clone() {
445        children.push(content_presenter(r, card, alignment, content));
446    }
447    if card.is_click_enabled && card.is_action_icon_visible {
448        children.push(action_icon_presenter_holder(r, card, brushes.action_icon));
449    }
450
451    let corner_radius = card.corner_radius.unwrap_or(r.control_corner_radius[0]);
452    let border_thickness = card
453        .border_thickness
454        .unwrap_or(SETTINGS_CARD_BORDER_THICKNESS);
455    let padding = card.padding.unwrap_or(SETTINGS_CARD_PADDING);
456    let border_brush = brushes.border_brush;
457    // `SettingsCardBackground`, the style's `Background`.
458    let background = brushes
459        .background
460        .or(card.background)
461        .unwrap_or(r.card_background_fill_color_default_brush);
462    // `PART_RootGrid`, whose `BackgroundTransition` is a `BrushTransition` of 83 ms.
463    let root_grid = ColorTransition::new(
464        background,
465        r.control_faster_animation_duration,
466        move |_app, background| {
467            Grid::new()
468                .row_definitions([
469                    RowDefinition::new(GridLength::STAR),
470                    RowDefinition::new(GridLength::AUTO),
471                ])
472                .column_definitions([
473                    ColumnDefinition::new(GridLength::AUTO).min_width(SETTINGS_CARD_LEFT_INDENTION),
474                    ColumnDefinition::new(GridLength::STAR),
475                    ColumnDefinition::new(GridLength::AUTO),
476                    ColumnDefinition::new(GridLength::AUTO),
477                ])
478                .row_spacing(if content_spacing {
479                    SETTINGS_CARD_VERTICAL_HEADER_CONTENT_SPACING
480                } else {
481                    0.0
482                })
483                .background(Brush::Solid(background))
484                .background_sizing(BackgroundSizing::InnerBorderEdge)
485                .border_brush(border_brush)
486                .border_thickness_ltrb(border_thickness)
487                .corner_radius(corner_radius)
488                .padding(padding)
489                .children(children.clone())
490                .into_widget()
491        },
492    );
493    // The style's `MinWidth` and `MinHeight`, and its `HorizontalAlignment="Stretch"` across a bounded width.
494    let min_width = if width.is_finite() {
495        width.max(SETTINGS_CARD_MIN_WIDTH)
496    } else {
497        SETTINGS_CARD_MIN_WIDTH
498    };
499    let sized = ConstrainedBox::new(
500        BoxConstraints::new()
501            .min_width(min_width)
502            .min_height(card.min_height.unwrap_or(SETTINGS_CARD_MIN_HEIGHT)),
503    )
504    .child(root_grid);
505    FocusVisual::new(sized, r.theme)
506        .visible(states.focused)
507        .margin(FOCUS_VISUAL_MARGIN)
508        .corner_radius(corner_radius)
509        .into_widget()
510}
511
512/// A `Viewbox` with `MaxWidth` and `MaxHeight` `max_size`: `icon` scaled to fit that square, with the presenter's `text` style for what it does not set itself.
513fn icon_viewbox(icon: WidgetRef, max_size: f64, text: TextStyle) -> WidgetRef {
514    SizedBox::new()
515        .width(max_size)
516        .height(max_size)
517        .child(FittedBox::new().child(DefaultTextStyle::new(text, icon)))
518        .into_widget()
519}
520
521/// `PART_HeaderIconPresenterHolder`: the header icon in column 0 of the first row, centred down it.
522fn header_icon_presenter_holder(
523    r: &ThemeResources,
524    icon: WidgetRef,
525    foreground: Color,
526) -> WidgetRef {
527    let text = control_text_style(
528        r,
529        r.control_content_theme_font_size,
530        FontWeight::W400,
531        foreground,
532    );
533    GridCell::new(Margin::new(
534        SETTINGS_CARD_HEADER_ICON_MARGIN,
535        Center::new()
536            .width_factor(1.0)
537            .height_factor(1.0)
538            .child(icon_viewbox(icon, SETTINGS_CARD_HEADER_ICON_MAX_SIZE, text)),
539    ))
540    .into_widget()
541}
542
543/// `HeaderPanel`: `PART_HeaderPresenter` over `PART_DescriptionPresenter` in column 1, centred down the row; both wrap. Its right margin goes once the content is under it.
544fn header_panel(
545    r: &ThemeResources,
546    brushes: &CardBrushes,
547    alignment: ContentAlignmentState,
548    header: Option<WidgetRef>,
549    description: Option<WidgetRef>,
550) -> WidgetRef {
551    let header = header.map(|header| {
552        DefaultTextStyle::new(
553            control_text_style(
554                r,
555                r.control_content_theme_font_size,
556                FontWeight::W400,
557                brushes.header,
558            ),
559            header,
560        )
561        .into_widget()
562    });
563    let description = description.map(|description| {
564        DefaultTextStyle::new(
565            control_text_style(
566                r,
567                SETTINGS_CARD_DESCRIPTION_FONT_SIZE,
568                FontWeight::W400,
569                brushes.description,
570            ),
571            description,
572        )
573        .into_widget()
574    });
575    let wrapped = matches!(
576        alignment,
577        ContentAlignmentState::RightWrapped | ContentAlignmentState::RightWrappedNoIcon
578    );
579    let panel = Column::new()
580        .main_axis_size(MainAxisSize::Min)
581        .cross_axis_alignment(CrossAxisAlignment::Start)
582        .children(header.into_iter().chain(description).collect::<Vec<_>>());
583    GridCell::new(Margin::new(
584        if wrapped {
585            [0.0; 4]
586        } else {
587            HEADER_PANEL_MARGIN
588        },
589        Align::new()
590            .alignment(AlignmentGeometry::CENTER_LEFT)
591            .width_factor(1.0)
592            .height_factor(1.0)
593            .child(panel),
594    ))
595    .column(1)
596    .into_widget()
597}
598
599/// `PART_ContentPresenter`: at the right of the first row, centred down it; or, in every other state, in the second row under the header, where the state says how it sits across the card.
600fn content_presenter(
601    r: &ThemeResources,
602    card: &SettingsCard,
603    alignment: ContentAlignmentState,
604    content: WidgetRef,
605) -> WidgetRef {
606    // The style's `Foreground`, `FontFamily`, `FontWeight` and `FontSize`, which content inherits.
607    let content = DefaultTextStyle::new(
608        control_text_style(
609            r,
610            r.control_content_theme_font_size,
611            FontWeight::W400,
612            r.text_fill_color_primary_brush,
613        ),
614        content,
615    );
616    let aligned = |alignment: AlignmentGeometry| {
617        Align::new()
618            .alignment(alignment)
619            .width_factor(1.0)
620            .height_factor(1.0)
621            .child(content.clone())
622            .into_widget()
623    };
624    let across = match alignment {
625        ContentAlignmentState::Right => {
626            return GridCell::new(aligned(AlignmentGeometry::CENTER_RIGHT))
627                .column(2)
628                .into_widget();
629        }
630        // `HorizontalAlignment="Stretch"` with `HorizontalContentAlignment="Left"`.
631        ContentAlignmentState::RightWrapped | ContentAlignmentState::RightWrappedNoIcon => {
632            HorizontalAlignment::Left
633        }
634        ContentAlignmentState::Left => HorizontalAlignment::Left,
635        // `HorizontalAlignment="Stretch"` with the card's `HorizontalContentAlignment`.
636        ContentAlignmentState::Vertical => card.horizontal_content_alignment,
637    };
638    let presenter = match across {
639        HorizontalAlignment::Left => aligned(AlignmentGeometry::CENTER_LEFT),
640        HorizontalAlignment::Center => aligned(AlignmentGeometry::CENTER),
641        HorizontalAlignment::Right => aligned(AlignmentGeometry::CENTER_RIGHT),
642        HorizontalAlignment::Stretch => content.into_widget(),
643    };
644    GridCell::new(presenter).row(1).column(1).into_widget()
645}
646
647/// `PART_ActionIconPresenterHolder`: the action icon in column 3, centred down both rows, with `ActionIconToolTip`.
648fn action_icon_presenter_holder(
649    r: &ThemeResources,
650    card: &SettingsCard,
651    foreground: Color,
652) -> WidgetRef {
653    // `FontFamily="{ThemeResource SymbolThemeFontFamily}"` at the style's `FontSize`.
654    let text = TextStyle::new()
655        .font_family(FLUENT_ICON_FONT_FAMILY)
656        .font_size(r.control_content_theme_font_size)
657        .color(foreground);
658    let icon = icon_viewbox(
659        card.action_icon.clone(),
660        SETTINGS_CARD_ACTION_ICON_MAX_SIZE,
661        text,
662    );
663    let icon = match card.action_icon_tool_tip.clone() {
664        Some(tool_tip) => ToolTipService::new(icon, ToolTip::text(tool_tip)).into_widget(),
665        None => icon,
666    };
667    GridCell::new(Margin::new(
668        SETTINGS_CARD_ACTION_ICON_MARGIN,
669        Center::new()
670            .width_factor(1.0)
671            .height_factor(1.0)
672            .child(icon),
673    ))
674    .column(3)
675    .row_span(2)
676    .into_widget()
677}