waft_rendering/
semantics_annotations.rs1use std::rc::Rc;
11
12use waft_embedder::{Locale, TextDirection};
13use waft_foundation::App;
14
15use crate::object::{RenderHandle, RenderObject};
16use crate::semantics::{AttributedString, SemanticsProperties};
17
18#[derive(Debug, Default)]
20pub struct SemanticsAnnotationsData {
21 properties: Option<Rc<SemanticsProperties>>,
22 container: bool,
23 explicit_child_nodes: bool,
24 exclude_semantics: bool,
25 block_user_actions: bool,
26 locale_for_subtree: Option<Locale>,
27 attributed_label: Option<AttributedString>,
28 attributed_value: Option<AttributedString>,
29 attributed_increased_value: Option<AttributedString>,
30 attributed_decreased_value: Option<AttributedString>,
31 attributed_hint: Option<AttributedString>,
32 text_direction: Option<TextDirection>,
33}
34
35impl SemanticsAnnotationsData {
36 pub fn new() -> SemanticsAnnotationsData {
40 SemanticsAnnotationsData::default()
41 }
42}
43
44pub trait SemanticsAnnotationsMixin: RenderObject {
47 fn semantics_annotations_data(self: RenderHandle<Self>, app: &App)
49 -> &SemanticsAnnotationsData;
50
51 fn semantics_annotations_data_mut(
53 self: RenderHandle<Self>,
54 app: &mut App,
55 ) -> &mut SemanticsAnnotationsData;
56
57 #[allow(clippy::too_many_arguments)]
59 fn init_semantics_annotations(
60 self: RenderHandle<Self>,
61 app: &mut App,
62 properties: Rc<SemanticsProperties>,
63 container: bool,
64 explicit_child_nodes: bool,
65 exclude_semantics: bool,
66 block_user_actions: bool,
67 locale_for_subtree: Option<Locale>,
68 text_direction: Option<TextDirection>,
69 ) {
70 let data = self.semantics_annotations_data_mut(app);
71 data.properties = Some(properties.clone());
72 data.container = container;
73 data.explicit_child_nodes = explicit_child_nodes;
74 data.exclude_semantics = exclude_semantics;
75 data.block_user_actions = block_user_actions;
76 data.locale_for_subtree = locale_for_subtree;
77 data.text_direction = text_direction;
78 self.update_attributed_fields(app, &properties);
79 }
80
81 fn properties(self: RenderHandle<Self>, app: &App) -> Rc<SemanticsProperties> {
83 self.semantics_annotations_data(app)
84 .properties
85 .clone()
86 .expect("set by init_semantics_annotations")
87 }
88
89 fn set_properties(self: RenderHandle<Self>, app: &mut App, value: Rc<SemanticsProperties>) {
91 if Rc::ptr_eq(&self.properties(app), &value) {
92 return;
93 }
94 self.semantics_annotations_data_mut(app).properties = Some(value.clone());
95 self.update_attributed_fields(app, &value);
96 }
97
98 fn container(self: RenderHandle<Self>, app: &App) -> bool {
106 self.semantics_annotations_data(app).container
107 }
108
109 fn set_container(self: RenderHandle<Self>, app: &mut App, value: bool) {
111 if self.container(app) == value {
112 return;
113 }
114 self.semantics_annotations_data_mut(app).container = value;
115 }
116
117 fn explicit_child_nodes(self: RenderHandle<Self>, app: &App) -> bool {
131 self.semantics_annotations_data(app).explicit_child_nodes
132 }
133
134 fn set_explicit_child_nodes(self: RenderHandle<Self>, app: &mut App, value: bool) {
136 if self.semantics_annotations_data(app).explicit_child_nodes == value {
137 return;
138 }
139 self.semantics_annotations_data_mut(app)
140 .explicit_child_nodes = value;
141 }
142
143 fn exclude_semantics(self: RenderHandle<Self>, app: &App) -> bool {
150 self.semantics_annotations_data(app).exclude_semantics
151 }
152
153 fn set_exclude_semantics(self: RenderHandle<Self>, app: &mut App, value: bool) {
155 if self.semantics_annotations_data(app).exclude_semantics == value {
156 return;
157 }
158 self.semantics_annotations_data_mut(app).exclude_semantics = value;
159 }
160
161 fn block_user_actions(self: RenderHandle<Self>, app: &App) -> bool {
167 self.semantics_annotations_data(app).block_user_actions
168 }
169
170 fn set_block_user_actions(self: RenderHandle<Self>, app: &mut App, value: bool) {
172 if self.semantics_annotations_data(app).block_user_actions == value {
173 return;
174 }
175 self.semantics_annotations_data_mut(app).block_user_actions = value;
176 }
177
178 fn locale_for_subtree(self: RenderHandle<Self>, app: &App) -> Option<Locale> {
182 self.semantics_annotations_data(app)
183 .locale_for_subtree
184 .clone()
185 }
186
187 fn set_locale_for_subtree(self: RenderHandle<Self>, app: &mut App, value: Option<Locale>) {
189 if self.semantics_annotations_data(app).locale_for_subtree == value {
190 return;
191 }
192 self.semantics_annotations_data_mut(app).locale_for_subtree = value;
193 }
194
195 fn update_attributed_fields(
197 self: RenderHandle<Self>,
198 app: &mut App,
199 value: &SemanticsProperties,
200 ) {
201 let attributed_label = effective_attributed_label(value);
202 let attributed_value = effective_attributed_value(value);
203 let attributed_increased_value = effective_attributed_increased_value(value);
204 let attributed_decreased_value =
205 effective_attributed_decreased_value(&self.properties(app), value);
206 let attributed_hint = effective_attributed_hint(value);
207 let data = self.semantics_annotations_data_mut(app);
208 data.attributed_label = attributed_label;
209 data.attributed_value = attributed_value;
210 data.attributed_increased_value = attributed_increased_value;
211 data.attributed_decreased_value = attributed_decreased_value;
212 data.attributed_hint = attributed_hint;
213 }
214
215 fn attributed_label(self: RenderHandle<Self>, app: &App) -> Option<AttributedString> {
217 self.semantics_annotations_data(app)
218 .attributed_label
219 .clone()
220 }
221
222 fn attributed_value(self: RenderHandle<Self>, app: &App) -> Option<AttributedString> {
224 self.semantics_annotations_data(app)
225 .attributed_value
226 .clone()
227 }
228
229 fn attributed_increased_value(self: RenderHandle<Self>, app: &App) -> Option<AttributedString> {
231 self.semantics_annotations_data(app)
232 .attributed_increased_value
233 .clone()
234 }
235
236 fn attributed_decreased_value(self: RenderHandle<Self>, app: &App) -> Option<AttributedString> {
238 self.semantics_annotations_data(app)
239 .attributed_decreased_value
240 .clone()
241 }
242
243 fn attributed_hint(self: RenderHandle<Self>, app: &App) -> Option<AttributedString> {
245 self.semantics_annotations_data(app).attributed_hint.clone()
246 }
247
248 fn text_direction(self: RenderHandle<Self>, app: &App) -> Option<TextDirection> {
257 self.semantics_annotations_data(app).text_direction
258 }
259
260 fn set_text_direction(self: RenderHandle<Self>, app: &mut App, value: Option<TextDirection>) {
262 if self.text_direction(app) == value {
263 return;
264 }
265 self.semantics_annotations_data_mut(app).text_direction = value;
266 }
267}
268
269fn effective_attributed_label(value: &SemanticsProperties) -> Option<AttributedString> {
270 value
271 .attributed_label
272 .clone()
273 .or_else(|| value.label.clone().map(AttributedString::new))
274}
275
276fn effective_attributed_value(value: &SemanticsProperties) -> Option<AttributedString> {
277 value
278 .attributed_value
279 .clone()
280 .or_else(|| value.value.clone().map(AttributedString::new))
281}
282
283fn effective_attributed_increased_value(value: &SemanticsProperties) -> Option<AttributedString> {
284 value
285 .attributed_increased_value
286 .clone()
287 .or_else(|| value.increased_value.clone().map(AttributedString::new))
288}
289
290fn effective_attributed_decreased_value(
293 properties: &SemanticsProperties,
294 value: &SemanticsProperties,
295) -> Option<AttributedString> {
296 properties
297 .attributed_decreased_value
298 .clone()
299 .or_else(|| value.decreased_value.clone().map(AttributedString::new))
300}
301
302fn effective_attributed_hint(value: &SemanticsProperties) -> Option<AttributedString> {
303 value
304 .attributed_hint
305 .clone()
306 .or_else(|| value.hint.clone().map(AttributedString::new))
307}