Skip to main content

ferroui_controls/markup_types/
values.rs

1//! Markup metadata of the value types of this crate and of the types that
2//! convert from text: their `Parse(string)`, their public constructors and
3//! their static values, as the managed original declares them.
4
5use crate::{
6    AcrylicPlatformCompensationLevels, AutoSuggestBoxQuerySubmittedEventArgs,
7    AutoSuggestBoxSuggestionChosenEventArgs, AutoSuggestBoxTextChangedEventArgs, AutoSuggestionBoxTextChangeReason,
8    GridLength, GridUnitType, InfoBarCloseReason, InfoBarClosedEventArgs, InfoBarClosingEventArgs,
9    InfoBarOpenedEventArgs, InfoBarSeverity, NumberBoxSpinButtonPlacementMode, NumberBoxValidationMode,
10    NumberBoxValueChangedEventArgs, Symbol, TextLayoutRoundingPlacement, TickList, WindowTransparencyLevel,
11    WindowTransparencyLevelCollection,
12};
13use ferroui_base::data::core::ValueTypes;
14use ferroui_base::ferro_markup_type;
15use ferroui_base::media::MediaCollection;
16use ferroui_base::metadata::{MarkupType, MarkupTyped};
17
18ferro_markup_type!(struct GridLength {
19    namespace: "FerroUI.Controls",
20    handles: [GridLength],
21    parse: GridLength::parse,
22    constructors: [
23        () => GridLength::default,
24        (f64) => GridLength::from_pixels,
25        (f64, GridUnitType) => GridLength::new,
26    ],
27    static_properties: [
28        Auto: GridLength { get: GridLength::auto },
29        Star: GridLength { get: GridLength::star },
30    ],
31    properties: [
32        Value: f64 { get: |g: &GridLength| g.value() },
33        GridUnitType: GridUnitType { get: |g: &GridLength| g.grid_unit_type() },
34        IsAbsolute: bool { get: |g: &GridLength| g.is_absolute() },
35        IsAuto: bool { get: |g: &GridLength| g.is_auto() },
36        IsStar: bool { get: |g: &GridLength| g.is_star() },
37    ],
38});
39
40/// A transparency level from its name. (The managed original has no `Parse`:
41/// its markup compiler looks the name up among the static values of the type.)
42fn parse_transparency_level(s: &str) -> Result<WindowTransparencyLevel, String> {
43    match s.trim() {
44        "None" => Ok(WindowTransparencyLevel::none()),
45        "Transparent" => Ok(WindowTransparencyLevel::transparent()),
46        "Blur" => Ok(WindowTransparencyLevel::blur()),
47        "AcrylicBlur" => Ok(WindowTransparencyLevel::acrylic_blur()),
48        "Mica" => Ok(WindowTransparencyLevel::mica()),
49        other => Err(format!("Unable to parse \"{other}\" as a window transparency level.")),
50    }
51}
52
53/// A comma-separated list of transparency levels.
54fn parse_transparency_levels(s: &str) -> Result<WindowTransparencyLevelCollection, String> {
55    let levels: Result<Vec<_>, _> = s.split(',').filter(|part| !part.trim().is_empty()).map(parse_transparency_level).collect();
56    levels.map(WindowTransparencyLevelCollection::new)
57}
58
59ferro_markup_type!(class WindowTransparencyLevelCollection {
60    namespace: "FerroUI.Controls",
61    handles: [WindowTransparencyLevelCollection, Option<WindowTransparencyLevelCollection>],
62    parse: parse_transparency_levels,
63});
64
65ferro_markup_type!(struct WindowTransparencyLevel {
66    namespace: "FerroUI.Controls",
67    handles: [WindowTransparencyLevel],
68    parse: parse_transparency_level,
69    static_properties: [
70        None: WindowTransparencyLevel { get: WindowTransparencyLevel::none },
71        Transparent: WindowTransparencyLevel { get: WindowTransparencyLevel::transparent },
72        Blur: WindowTransparencyLevel { get: WindowTransparencyLevel::blur },
73        AcrylicBlur: WindowTransparencyLevel { get: WindowTransparencyLevel::acrylic_blur },
74        Mica: WindowTransparencyLevel { get: WindowTransparencyLevel::mica },
75    ],
76    constructors: [() => WindowTransparencyLevel::none],
77});
78
79ferro_markup_type!(struct AcrylicPlatformCompensationLevels {
80    namespace: "FerroUI.Controls",
81    handles: [AcrylicPlatformCompensationLevels],
82    constructors: [() => AcrylicPlatformCompensationLevels::default, (f64, f64, f64) => AcrylicPlatformCompensationLevels::new],
83});
84
85// The list of numbers of the `Ticks` properties (the notifying list of `double` in the
86// managed original): it derives from the list of numbers, whose members fill it, so that
87// the text of a `Ticks` attribute (`"0,20,25"`) is the list of its numbers.
88ferro_markup_type!(class TickList {
89    namespace: "FerroUI.Controls",
90    handles: [TickList, Option<TickList>],
91    base: MediaCollection<f64>,
92    constructors: [() => TickList::new],
93});
94
95/// The types declared in this file.
96pub(super) const TYPES: &[&MarkupType] = &[
97    <GridLength as MarkupTyped>::MARKUP,
98    <WindowTransparencyLevel as MarkupTyped>::MARKUP,
99    <WindowTransparencyLevelCollection as MarkupTyped>::MARKUP,
100    <AcrylicPlatformCompensationLevels as MarkupTyped>::MARKUP,
101    <TickList as MarkupTyped>::MARKUP,
102    // Declared next to the type (a FerroUI addition, not in the managed original).
103    <TextLayoutRoundingPlacement as MarkupTyped>::MARKUP,
104    // WinUI's Symbol, declared next to the type (docs/porting/winui-controls.md).
105    <Symbol as MarkupTyped>::MARKUP,
106    // The enumerations and the event arguments of WinUI's InfoBar, declared next to the types.
107    <InfoBarSeverity as MarkupTyped>::MARKUP,
108    <InfoBarCloseReason as MarkupTyped>::MARKUP,
109    <InfoBarClosingEventArgs as MarkupTyped>::MARKUP,
110    <InfoBarClosedEventArgs as MarkupTyped>::MARKUP,
111    <InfoBarOpenedEventArgs as MarkupTyped>::MARKUP,
112    // WinUI's input controls, declared next to the types (docs/porting/winui-controls.md).
113    <NumberBoxSpinButtonPlacementMode as MarkupTyped>::MARKUP,
114    <NumberBoxValidationMode as MarkupTyped>::MARKUP,
115    <NumberBoxValueChangedEventArgs as MarkupTyped>::MARKUP,
116    <AutoSuggestionBoxTextChangeReason as MarkupTyped>::MARKUP,
117    <AutoSuggestBoxTextChangedEventArgs as MarkupTyped>::MARKUP,
118    <AutoSuggestBoxSuggestionChosenEventArgs as MarkupTyped>::MARKUP,
119    <AutoSuggestBoxQuerySubmittedEventArgs as MarkupTyped>::MARKUP,
120    // The WinUI materials (winui-materials.md), declared next to the types.
121    <crate::media::ApplicationTheme as MarkupTyped>::MARKUP,
122    <crate::media::RevealBrushState as MarkupTyped>::MARKUP,
123    <crate::media::FocusVisualKind as MarkupTyped>::MARKUP,
124    // WinUI's ZoomMode of the scroll viewer (docs/porting/winui-composition.md, phase 7).
125    <crate::ZoomMode as MarkupTyped>::MARKUP,
126];
127
128/// Registers the nullable forms of the value types with the untyped value
129/// conversions of the current thread.
130pub(super) fn register_value_types() {
131    ValueTypes::register_nullable::<GridLength>();
132    ValueTypes::register_nullable::<WindowTransparencyLevel>();
133    ValueTypes::register_nullable::<WindowTransparencyLevelCollection>();
134    ValueTypes::register_nullable::<AcrylicPlatformCompensationLevels>();
135    ValueTypes::register_nullable::<TickList>();
136    ValueTypes::register_nullable::<TextLayoutRoundingPlacement>();
137    ValueTypes::register_nullable::<Symbol>();
138    ValueTypes::register_nullable::<InfoBarSeverity>();
139    ValueTypes::register_nullable::<crate::ZoomMode>();
140    ValueTypes::register_nullable::<InfoBarCloseReason>();
141    ValueTypes::register_nullable::<NumberBoxSpinButtonPlacementMode>();
142    ValueTypes::register_nullable::<NumberBoxValidationMode>();
143    ValueTypes::register_nullable::<AutoSuggestionBoxTextChangeReason>();
144    crate::navigation::register_value_types();
145    crate::navigation_view_events::register_value_types();
146    ValueTypes::register_nullable::<crate::media::ApplicationTheme>();
147    ValueTypes::register_nullable::<crate::media::RevealBrushState>();
148    ValueTypes::register_nullable::<crate::media::FocusVisualKind>();
149    // A tick list is the list of numbers it derives from: the same list, so that the
150    // members of the list (`Capacity`, `Add`) are reached through the tick list.
151    ValueTypes::register_cast::<TickList, MediaCollection<f64>>(|ticks| MediaCollection::from_list(ticks.list().clone()));
152    // The text of a value (its `ToString()` in the managed original).
153    ValueTypes::register_display::<GridLength>();
154    ValueTypes::register_display::<WindowTransparencyLevel>();
155}