Skip to main content

swiftui_native/views/
enums.rs

1//! Modifiers taking a plain SwiftUI enum.
2
3use super::*;
4use crate::abi;
5use crate::resolve;
6//
7// `Visibility`, `Prominence` and friends have cases rather than static
8// properties, so there is no getter to call. The case is selected through the
9// enum's own `destructiveInjectEnumTag` witness — writing the index by hand
10// happens to work for some and produces a case that does not exist for others.
11//
12// How it is then *passed* depends on the enum, and there is no way to ask:
13// `Visibility` is frozen and travels in a register, while `ColorScheme`,
14// `DynamicTypeSize`, `Prominence` and `TextSelectionAffinity` are not and
15// travel by pointer. All four are one byte, so size does not tell them apart —
16// getting it wrong crashes one way and silently yields `Visibility.()` the
17// other, which is why each is spelled out here.
18
19/// `SwiftUI.DynamicTypeSize`, in declaration order.
20///
21/// An enum with no associated values is laid out as its case index, so the
22/// discriminant here *is* the representation.
23#[derive(Clone, Copy, Debug, PartialEq, Eq)]
24pub enum DynamicTypeSize {
25    XSmall = 0,
26    Small = 1,
27    Medium = 2,
28    Large = 3,
29    XLarge = 4,
30    XxLarge = 5,
31    XxxLarge = 6,
32    Accessibility1 = 7,
33    Accessibility2 = 8,
34    Accessibility3 = 9,
35    Accessibility4 = 10,
36    Accessibility5 = 11,
37}
38
39/// `SwiftUI.LayoutDirection`, in declaration order.
40///
41/// An enum with no associated values is laid out as its case index, so the
42/// discriminant here *is* the representation.
43#[derive(Clone, Copy, Debug, PartialEq, Eq)]
44pub enum LayoutDirection {
45    LeftToRight = 0,
46    RightToLeft = 1,
47}
48
49/// `SwiftUI.Prominence`, in declaration order.
50///
51/// An enum with no associated values is laid out as its case index, so the
52/// discriminant here *is* the representation.
53#[derive(Clone, Copy, Debug, PartialEq, Eq)]
54pub enum Prominence {
55    Standard = 0,
56    Increased = 1,
57}
58
59/// `SwiftUI.TextSelectionAffinity`, in declaration order.
60///
61/// An enum with no associated values is laid out as its case index, so the
62/// discriminant here *is* the representation.
63#[derive(Clone, Copy, Debug, PartialEq, Eq)]
64pub enum TextSelectionAffinity {
65    Automatic = 0,
66    Upstream = 1,
67    Downstream = 2,
68}
69
70/// `SwiftUI.TouchBarItemPresence`, in declaration order.
71///
72/// Every case carries the item's identifier, so all three are payload cases —
73/// and payload cases are numbered before empty ones, which here means the tags
74/// are still the declaration order. That puts `required` first, not `default`.
75#[derive(Clone, Copy, Debug, PartialEq, Eq)]
76pub enum TouchBarItemPresence {
77    Required = 0,
78    Default = 1,
79    Optional = 2,
80}
81
82/// `SwiftUI.Visibility`, in declaration order.
83///
84/// An enum with no associated values is laid out as its case index, so the
85/// discriminant here *is* the representation.
86#[derive(Clone, Copy, Debug, PartialEq, Eq)]
87pub enum Visibility {
88    Automatic = 0,
89    Visible = 1,
90    Hidden = 2,
91}
92
93/// Apply `.colorScheme(_:)`.
94pub fn color_scheme(view: &ViewHandle, value: ColorScheme) -> ViewHandle {
95    unsafe {
96        let meta = abi::metadata0(resolve::color_scheme_ma());
97        let mut arg = vec![0u8; abi::type_bytes(meta)];
98        abi::inject_enum_tag(&mut arg, meta, value as u32);
99        opaque_modifier(
100            resolve::color_scheme_mod_fn(),
101            resolve::color_scheme_oret(),
102            view,
103            &[arg.as_mut_ptr() as u64],
104            [0.0; 4],
105        )
106    }
107}
108
109/// Apply `.dynamicTypeSize(_:)`.
110pub fn dynamic_type_size(view: &ViewHandle, value: DynamicTypeSize) -> ViewHandle {
111    unsafe {
112        let meta = abi::metadata0(resolve::dynamic_type_size_ma());
113        let mut arg = vec![0u8; abi::type_bytes(meta)];
114        abi::inject_enum_tag(&mut arg, meta, value as u32);
115        opaque_modifier(
116            resolve::dynamic_type_size_fn(),
117            resolve::dynamic_type_size_oret(),
118            view,
119            &[arg.as_mut_ptr() as u64],
120            [0.0; 4],
121        )
122    }
123}
124
125/// Apply `.headerProminence(_:)`.
126pub fn header_prominence(view: &ViewHandle, value: Prominence) -> ViewHandle {
127    unsafe {
128        let meta = abi::metadata0(resolve::prominence_ma());
129        let mut arg = vec![0u8; abi::type_bytes(meta)];
130        abi::inject_enum_tag(&mut arg, meta, value as u32);
131        opaque_modifier(
132            resolve::header_prominence_fn(),
133            resolve::header_prominence_oret(),
134            view,
135            &[arg.as_mut_ptr() as u64],
136            [0.0; 4],
137        )
138    }
139}
140
141/// Apply `.labelsVisibility(_:)`.
142pub fn labels_visibility(view: &ViewHandle, value: Visibility) -> ViewHandle {
143    unsafe {
144        let meta = abi::metadata0(resolve::visibility_ma());
145        let arg = abi::enum_argument(meta, value as u32);
146        opaque_modifier(
147            resolve::labels_visibility_fn(),
148            resolve::labels_visibility_oret(),
149            view,
150            &[arg],
151            [0.0; 4],
152        )
153    }
154}
155
156/// Apply `.menuIndicator(_:)`.
157pub fn menu_indicator(view: &ViewHandle, value: Visibility) -> ViewHandle {
158    unsafe {
159        let meta = abi::metadata0(resolve::visibility_ma());
160        let arg = abi::enum_argument(meta, value as u32);
161        opaque_modifier(
162            resolve::menu_indicator_fn(),
163            resolve::menu_indicator_oret(),
164            view,
165            &[arg],
166            [0.0; 4],
167        )
168    }
169}
170
171/// Apply `.persistentSystemOverlays(_:)`.
172pub fn persistent_system_overlays(view: &ViewHandle, value: Visibility) -> ViewHandle {
173    unsafe {
174        let meta = abi::metadata0(resolve::visibility_ma());
175        let arg = abi::enum_argument(meta, value as u32);
176        opaque_modifier(
177            resolve::persistent_system_overlays_fn(),
178            resolve::persistent_system_overlays_oret(),
179            view,
180            &[arg],
181            [0.0; 4],
182        )
183    }
184}
185
186/// Apply `.pointerVisibility(_:)`.
187pub fn pointer_visibility(view: &ViewHandle, value: Visibility) -> ViewHandle {
188    unsafe {
189        let meta = abi::metadata0(resolve::visibility_ma());
190        let arg = abi::enum_argument(meta, value as u32);
191        opaque_modifier(
192            resolve::pointer_visibility_fn(),
193            resolve::pointer_visibility_oret(),
194            view,
195            &[arg],
196            [0.0; 4],
197        )
198    }
199}
200
201/// Apply `.presentationDragIndicator(_:)`.
202pub fn presentation_drag_indicator(view: &ViewHandle, value: Visibility) -> ViewHandle {
203    unsafe {
204        let meta = abi::metadata0(resolve::visibility_ma());
205        let arg = abi::enum_argument(meta, value as u32);
206        opaque_modifier(
207            resolve::presentation_drag_indicator_fn(),
208            resolve::presentation_drag_indicator_oret(),
209            view,
210            &[arg],
211            [0.0; 4],
212        )
213    }
214}
215
216/// Apply `.scrollContentBackground(_:)`.
217pub fn scroll_content_background(view: &ViewHandle, value: Visibility) -> ViewHandle {
218    unsafe {
219        let meta = abi::metadata0(resolve::visibility_ma());
220        let arg = abi::enum_argument(meta, value as u32);
221        opaque_modifier(
222            resolve::scroll_content_background_fn(),
223            resolve::scroll_content_background_oret(),
224            view,
225            &[arg],
226            [0.0; 4],
227        )
228    }
229}
230
231/// Apply `.sliderThumbVisibility(_:)`.
232pub fn slider_thumb_visibility(view: &ViewHandle, value: Visibility) -> ViewHandle {
233    unsafe {
234        let meta = abi::metadata0(resolve::visibility_ma());
235        let arg = abi::enum_argument(meta, value as u32);
236        opaque_modifier(
237            resolve::slider_thumb_visibility_fn(),
238            resolve::slider_thumb_visibility_oret(),
239            view,
240            &[arg],
241            [0.0; 4],
242        )
243    }
244}
245
246/// Apply `.tableColumnHeaders(_:)`.
247pub fn table_column_headers(view: &ViewHandle, value: Visibility) -> ViewHandle {
248    unsafe {
249        let meta = abi::metadata0(resolve::visibility_ma());
250        let arg = abi::enum_argument(meta, value as u32);
251        opaque_modifier(
252            resolve::table_column_headers_fn(),
253            resolve::table_column_headers_oret(),
254            view,
255            &[arg],
256            [0.0; 4],
257        )
258    }
259}
260
261/// Apply `.textSelectionAffinity(_:)`.
262pub fn text_selection_affinity(view: &ViewHandle, value: TextSelectionAffinity) -> ViewHandle {
263    unsafe {
264        let meta = abi::metadata0(resolve::text_selection_affinity_ma());
265        let mut arg = vec![0u8; abi::type_bytes(meta)];
266        abi::inject_enum_tag(&mut arg, meta, value as u32);
267        opaque_modifier(
268            resolve::text_selection_affinity_fn(),
269            resolve::text_selection_affinity_oret(),
270            view,
271            &[arg.as_mut_ptr() as u64],
272            [0.0; 4],
273        )
274    }
275}
276
277
278/// Apply `.writingToolsAffordanceVisibility(_:)`.
279pub fn writing_tools_affordance_visibility(view: &ViewHandle, value: Visibility) -> ViewHandle {
280    unsafe {
281        let meta = abi::metadata0(resolve::visibility_ma());
282        let arg = abi::enum_argument(meta, value as u32);
283        opaque_modifier(
284            resolve::writing_tools_affordance_visibility_fn(),
285            resolve::writing_tools_affordance_visibility_oret(),
286            view,
287            &[arg],
288            [0.0; 4],
289        )
290    }
291}