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, GridLength, GridUnitType, TextLayoutRoundingPlacement, TickList,
7    WindowTransparencyLevel, WindowTransparencyLevelCollection,
8};
9use ferroui_base::data::core::ValueTypes;
10use ferroui_base::ferro_markup_type;
11use ferroui_base::media::MediaCollection;
12use ferroui_base::metadata::{MarkupType, MarkupTyped};
13
14ferro_markup_type!(struct GridLength {
15    namespace: "FerroUI.Controls",
16    handles: [GridLength],
17    parse: GridLength::parse,
18    constructors: [
19        () => GridLength::default,
20        (f64) => GridLength::from_pixels,
21        (f64, GridUnitType) => GridLength::new,
22    ],
23    static_properties: [
24        Auto: GridLength { get: GridLength::auto },
25        Star: GridLength { get: GridLength::star },
26    ],
27    properties: [
28        Value: f64 { get: |g: &GridLength| g.value() },
29        GridUnitType: GridUnitType { get: |g: &GridLength| g.grid_unit_type() },
30        IsAbsolute: bool { get: |g: &GridLength| g.is_absolute() },
31        IsAuto: bool { get: |g: &GridLength| g.is_auto() },
32        IsStar: bool { get: |g: &GridLength| g.is_star() },
33    ],
34});
35
36/// A transparency level from its name. (The managed original has no `Parse`:
37/// its markup compiler looks the name up among the static values of the type.)
38fn parse_transparency_level(s: &str) -> Result<WindowTransparencyLevel, String> {
39    match s.trim() {
40        "None" => Ok(WindowTransparencyLevel::none()),
41        "Transparent" => Ok(WindowTransparencyLevel::transparent()),
42        "Blur" => Ok(WindowTransparencyLevel::blur()),
43        "AcrylicBlur" => Ok(WindowTransparencyLevel::acrylic_blur()),
44        "Mica" => Ok(WindowTransparencyLevel::mica()),
45        other => Err(format!("Unable to parse \"{other}\" as a window transparency level.")),
46    }
47}
48
49/// A comma-separated list of transparency levels.
50fn parse_transparency_levels(s: &str) -> Result<WindowTransparencyLevelCollection, String> {
51    let levels: Result<Vec<_>, _> = s.split(',').filter(|part| !part.trim().is_empty()).map(parse_transparency_level).collect();
52    levels.map(WindowTransparencyLevelCollection::new)
53}
54
55ferro_markup_type!(class WindowTransparencyLevelCollection {
56    namespace: "FerroUI.Controls",
57    handles: [WindowTransparencyLevelCollection, Option<WindowTransparencyLevelCollection>],
58    parse: parse_transparency_levels,
59});
60
61ferro_markup_type!(struct WindowTransparencyLevel {
62    namespace: "FerroUI.Controls",
63    handles: [WindowTransparencyLevel],
64    parse: parse_transparency_level,
65    static_properties: [
66        None: WindowTransparencyLevel { get: WindowTransparencyLevel::none },
67        Transparent: WindowTransparencyLevel { get: WindowTransparencyLevel::transparent },
68        Blur: WindowTransparencyLevel { get: WindowTransparencyLevel::blur },
69        AcrylicBlur: WindowTransparencyLevel { get: WindowTransparencyLevel::acrylic_blur },
70        Mica: WindowTransparencyLevel { get: WindowTransparencyLevel::mica },
71    ],
72    constructors: [() => WindowTransparencyLevel::none],
73});
74
75ferro_markup_type!(struct AcrylicPlatformCompensationLevels {
76    namespace: "FerroUI.Controls",
77    handles: [AcrylicPlatformCompensationLevels],
78    constructors: [() => AcrylicPlatformCompensationLevels::default, (f64, f64, f64) => AcrylicPlatformCompensationLevels::new],
79});
80
81// The list of numbers of the `Ticks` properties (the notifying list of `double` in the
82// managed original): it derives from the list of numbers, whose members fill it, so that
83// the text of a `Ticks` attribute (`"0,20,25"`) is the list of its numbers.
84ferro_markup_type!(class TickList {
85    namespace: "FerroUI.Controls",
86    handles: [TickList, Option<TickList>],
87    base: MediaCollection<f64>,
88    constructors: [() => TickList::new],
89});
90
91/// The types declared in this file.
92pub(super) const TYPES: &[&MarkupType] = &[
93    <GridLength as MarkupTyped>::MARKUP,
94    <WindowTransparencyLevel as MarkupTyped>::MARKUP,
95    <WindowTransparencyLevelCollection as MarkupTyped>::MARKUP,
96    <AcrylicPlatformCompensationLevels as MarkupTyped>::MARKUP,
97    <TickList as MarkupTyped>::MARKUP,
98    // Declared next to the type (a FerroUI addition, not in the managed original).
99    <TextLayoutRoundingPlacement as MarkupTyped>::MARKUP,
100];
101
102/// Registers the nullable forms of the value types with the untyped value
103/// conversions of the current thread.
104pub(super) fn register_value_types() {
105    ValueTypes::register_nullable::<GridLength>();
106    ValueTypes::register_nullable::<WindowTransparencyLevel>();
107    ValueTypes::register_nullable::<WindowTransparencyLevelCollection>();
108    ValueTypes::register_nullable::<AcrylicPlatformCompensationLevels>();
109    ValueTypes::register_nullable::<TickList>();
110    ValueTypes::register_nullable::<TextLayoutRoundingPlacement>();
111    // A tick list is the list of numbers it derives from: the same list, so that the
112    // members of the list (`Capacity`, `Add`) are reached through the tick list.
113    ValueTypes::register_cast::<TickList, MediaCollection<f64>>(|ticks| MediaCollection::from_list(ticks.list().clone()));
114    // The text of a value (its `ToString()` in the managed original).
115    ValueTypes::register_display::<GridLength>();
116    ValueTypes::register_display::<WindowTransparencyLevel>();
117}