Skip to main content

waft_rendering/
semantics_annotations.rs

1//! Flutter counterpart: `rendering/object.dart` (`SemanticsAnnotationsMixin`).
2//!
3//! The mixin keeps what the semantics widgets give it. What reads it waits with the semantics
4//! tree: `describeSemanticsConfiguration`, which copies these values and registers the
5//! `_perform*` handlers, the `visitChildrenForSemantics` override that `excludeSemantics`
6//! controls, and the `markNeedsSemanticsUpdate` each setter ends with. The setters keep Dart's
7//! comparisons and early returns, so they change exactly when Dart's would mark the
8//! semantics dirty.
9
10use 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/// Flutter's `SemanticsAnnotationsMixin` fields.
19#[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    /// Nothing set yet: the host calls
37    /// [`init_semantics_annotations`](SemanticsAnnotationsMixin::init_semantics_annotations)
38    /// in its constructor.
39    pub fn new() -> SemanticsAnnotationsData {
40        SemanticsAnnotationsData::default()
41    }
42}
43
44/// A mixin for [`RenderObject`]s that want to annotate the `SemanticsNode`
45/// for their subtree.
46pub trait SemanticsAnnotationsMixin: RenderObject {
47    /// Mixin field access.
48    fn semantics_annotations_data(self: RenderHandle<Self>, app: &App)
49    -> &SemanticsAnnotationsData;
50
51    /// See [`semantics_annotations_data`](Self::semantics_annotations_data).
52    fn semantics_annotations_data_mut(
53        self: RenderHandle<Self>,
54        app: &mut App,
55    ) -> &mut SemanticsAnnotationsData;
56
57    /// Initializes the semantics annotations for this mixin.
58    #[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    /// All of the [`SemanticsProperties`] for this [`SemanticsAnnotationsMixin`].
82    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    /// Sets [`properties`](Self::properties).
90    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    /// If 'container' is true, this [`RenderObject`] will introduce a new
99    /// node in the semantics tree. Otherwise, the semantics will be
100    /// merged with the semantics of any ancestors.
101    ///
102    /// Whether descendants of this [`RenderObject`] can add their semantic information
103    /// to the `SemanticsNode` introduced by this configuration is controlled by
104    /// [`explicit_child_nodes`](Self::explicit_child_nodes).
105    fn container(self: RenderHandle<Self>, app: &App) -> bool {
106        self.semantics_annotations_data(app).container
107    }
108
109    /// Sets [`container`](Self::container).
110    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    /// Whether descendants of this [`RenderObject`] are allowed to add semantic
118    /// information to the `SemanticsNode` annotated by this widget.
119    ///
120    /// When set to false descendants are allowed to annotate `SemanticsNode`s of
121    /// their parent with the semantic information they want to contribute to the
122    /// semantic tree.
123    /// When set to true the only way for descendants to contribute semantic
124    /// information to the semantic tree is to introduce new explicit
125    /// `SemanticsNode`s to the tree.
126    ///
127    /// This setting is often used in combination with
128    /// `SemanticsConfiguration.isSemanticBoundary` to create semantic boundaries
129    /// that are either writable or not for children.
130    fn explicit_child_nodes(self: RenderHandle<Self>, app: &App) -> bool {
131        self.semantics_annotations_data(app).explicit_child_nodes
132    }
133
134    /// Sets [`explicit_child_nodes`](Self::explicit_child_nodes).
135    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    /// Whether descendants of this [`RenderObject`] should have their semantic
144    /// information ignored.
145    ///
146    /// When this flag is set to true, all child semantics nodes are ignored.
147    /// This can be used as a convenience for cases where a child is wrapped in
148    /// an `ExcludeSemantics` widget and then another `Semantics` widget.
149    fn exclude_semantics(self: RenderHandle<Self>, app: &App) -> bool {
150        self.semantics_annotations_data(app).exclude_semantics
151    }
152
153    /// Sets [`exclude_semantics`](Self::exclude_semantics).
154    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    /// Whether to block user interactions for the semantics subtree.
162    ///
163    /// Setting this true prevents user from activating pointer related
164    /// `SemanticsAction`s, such as `SemanticsAction::TAP` or
165    /// `SemanticsAction::LONG_PRESS`.
166    fn block_user_actions(self: RenderHandle<Self>, app: &App) -> bool {
167        self.semantics_annotations_data(app).block_user_actions
168    }
169
170    /// Sets [`block_user_actions`](Self::block_user_actions).
171    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    /// The [`Locale`] for the semantics subtree.
179    ///
180    /// Setting this to null will inherit locale from ancestor semantics node.
181    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    /// Sets [`locale_for_subtree`](Self::locale_for_subtree).
188    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    /// Flutter's `_updateAttributedFields`.
196    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    /// Dart's `_attributedLabel`: the label `describeSemanticsConfiguration` will report.
216    fn attributed_label(self: RenderHandle<Self>, app: &App) -> Option<AttributedString> {
217        self.semantics_annotations_data(app)
218            .attributed_label
219            .clone()
220    }
221
222    /// Dart's `_attributedValue`.
223    fn attributed_value(self: RenderHandle<Self>, app: &App) -> Option<AttributedString> {
224        self.semantics_annotations_data(app)
225            .attributed_value
226            .clone()
227    }
228
229    /// Dart's `_attributedIncreasedValue`.
230    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    /// Dart's `_attributedDecreasedValue`.
237    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    /// Dart's `_attributedHint`.
244    fn attributed_hint(self: RenderHandle<Self>, app: &App) -> Option<AttributedString> {
245        self.semantics_annotations_data(app).attributed_hint.clone()
246    }
247
248    /// If non-null, sets the `SemanticsNode.textDirection` semantic to the given
249    /// value.
250    ///
251    /// This must not be null if [`SemanticsProperties::attributed_label`],
252    /// [`SemanticsProperties::attributed_hint`],
253    /// [`SemanticsProperties::attributed_value`],
254    /// [`SemanticsProperties::attributed_increased_value`], or
255    /// [`SemanticsProperties::attributed_decreased_value`] are not null.
256    fn text_direction(self: RenderHandle<Self>, app: &App) -> Option<TextDirection> {
257        self.semantics_annotations_data(app).text_direction
258    }
259
260    /// Sets [`text_direction`](Self::text_direction).
261    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
290/// Dart reads the attributed value off the mixin's `properties` and the plain one off
291/// `value`; both are the same object wherever it is called.
292fn 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}