Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
embedder_semantics_update.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/shell/platform/embedder/embedder_semantics_update.h"
6
7namespace flutter {
8
10 const SemanticsNodeUpdates& nodes,
11 const CustomAccessibilityActionUpdates& actions) {
12 for (const auto& value : nodes) {
13 AddNode(value.second);
14 }
15
16 for (const auto& value : actions) {
17 AddAction(value.second);
18 }
19
20 update_ = {
22 .nodes_count = nodes_.size(),
23 .nodes = nodes_.data(),
24 .custom_actions_count = actions_.size(),
25 .custom_actions = actions_.data(),
26 };
27}
28
29void EmbedderSemanticsUpdate::AddNode(const SemanticsNode& node) {
31 FlutterTransformation flutter_transform{
37
38 // Do not add new members to FlutterSemanticsNode.
39 // This would break the forward compatibility of FlutterSemanticsUpdate.
40 // All new members must be added to FlutterSemanticsNode2 instead.
41 nodes_.push_back({
43 node.id,
44 static_cast<FlutterSemanticsFlag>(node.flags),
45 static_cast<FlutterSemanticsAction>(node.actions),
48 node.scrollChildren,
49 node.scrollIndex,
50 node.scrollPosition,
51 node.scrollExtentMax,
52 node.scrollExtentMin,
53 node.elevation,
54 node.thickness,
55 node.label.c_str(),
56 node.hint.c_str(),
57 node.value.c_str(),
58 node.increasedValue.c_str(),
59 node.decreasedValue.c_str(),
60 static_cast<FlutterTextDirection>(node.textDirection),
61 FlutterRect{node.rect.fLeft, node.rect.fTop, node.rect.fRight,
62 node.rect.fBottom},
63 flutter_transform,
64 node.childrenInTraversalOrder.size(),
65 node.childrenInTraversalOrder.data(),
66 node.childrenInHitTestOrder.data(),
69 node.platformViewId,
70 node.tooltip.c_str(),
71 });
72}
73
74void EmbedderSemanticsUpdate::AddAction(
75 const CustomAccessibilityAction& action) {
76 // Do not add new members to FlutterSemanticsCustomAction.
77 // This would break the forward compatibility of FlutterSemanticsUpdate.
78 // All new members must be added to FlutterSemanticsCustomAction2 instead.
79 actions_.push_back({
81 action.id,
82 static_cast<FlutterSemanticsAction>(action.overrideId),
83 action.label.c_str(),
84 action.hint.c_str(),
85 });
86}
87
89
91 const SemanticsNodeUpdates& nodes,
92 const CustomAccessibilityActionUpdates& actions) {
93 nodes_.reserve(nodes.size());
94 node_pointers_.reserve(nodes.size());
95 actions_.reserve(actions.size());
96 action_pointers_.reserve(actions.size());
97
98 for (const auto& value : nodes) {
99 AddNode(value.second);
100 }
101
102 for (const auto& value : actions) {
103 AddAction(value.second);
104 }
105
106 for (size_t i = 0; i < nodes_.size(); i++) {
107 node_pointers_.push_back(&nodes_[i]);
108 }
109
110 for (size_t i = 0; i < actions_.size(); i++) {
111 action_pointers_.push_back(&actions_[i]);
112 }
113
114 update_ = {
116 .node_count = node_pointers_.size(),
117 .nodes = node_pointers_.data(),
118 .custom_action_count = action_pointers_.size(),
119 .custom_actions = action_pointers_.data(),
120 };
121}
122
124
125void EmbedderSemanticsUpdate2::AddNode(const SemanticsNode& node) {
127 FlutterTransformation flutter_transform{
133
134 auto label_attributes = CreateStringAttributes(node.labelAttributes);
135 auto hint_attributes = CreateStringAttributes(node.hintAttributes);
136 auto value_attributes = CreateStringAttributes(node.valueAttributes);
137 auto increased_value_attributes =
138 CreateStringAttributes(node.increasedValueAttributes);
139 auto decreased_value_attributes =
140 CreateStringAttributes(node.decreasedValueAttributes);
141
142 nodes_.push_back({
143 sizeof(FlutterSemanticsNode2),
144 node.id,
145 static_cast<FlutterSemanticsFlag>(node.flags),
146 static_cast<FlutterSemanticsAction>(node.actions),
149 node.scrollChildren,
150 node.scrollIndex,
151 node.scrollPosition,
152 node.scrollExtentMax,
153 node.scrollExtentMin,
154 node.elevation,
155 node.thickness,
156 node.label.c_str(),
157 node.hint.c_str(),
158 node.value.c_str(),
159 node.increasedValue.c_str(),
160 node.decreasedValue.c_str(),
161 static_cast<FlutterTextDirection>(node.textDirection),
162 FlutterRect{node.rect.fLeft, node.rect.fTop, node.rect.fRight,
163 node.rect.fBottom},
164 flutter_transform,
165 node.childrenInTraversalOrder.size(),
166 node.childrenInTraversalOrder.data(),
167 node.childrenInHitTestOrder.data(),
168 node.customAccessibilityActions.size(),
169 node.customAccessibilityActions.data(),
170 node.platformViewId,
171 node.tooltip.c_str(),
172 label_attributes.count,
173 label_attributes.attributes,
174 hint_attributes.count,
175 hint_attributes.attributes,
176 value_attributes.count,
177 value_attributes.attributes,
178 increased_value_attributes.count,
179 increased_value_attributes.attributes,
180 decreased_value_attributes.count,
181 decreased_value_attributes.attributes,
182 });
183}
184
185void EmbedderSemanticsUpdate2::AddAction(
186 const CustomAccessibilityAction& action) {
187 actions_.push_back({
189 action.id,
190 static_cast<FlutterSemanticsAction>(action.overrideId),
191 action.label.c_str(),
192 action.hint.c_str(),
193 });
194}
195
196EmbedderSemanticsUpdate2::EmbedderStringAttributes
197EmbedderSemanticsUpdate2::CreateStringAttributes(
198 const StringAttributes& attributes) {
199 // Minimize allocations if attributes are empty.
200 if (attributes.empty()) {
201 return {.count = 0, .attributes = nullptr};
202 }
203
204 // Translate the engine attributes to embedder attributes.
205 // The result vector's data is returned by this method.
206 // The result vector will be owned by |node_string_attributes_|
207 // so that the embedder attributes are cleaned up at the end of the
208 // semantics update callback when when the |EmbedderSemanticsUpdate2|
209 // is destroyed.
210 auto result = std::make_unique<std::vector<const FlutterStringAttribute*>>();
211 result->reserve(attributes.size());
212
213 for (const auto& attribute : attributes) {
214 auto embedder_attribute = std::make_unique<FlutterStringAttribute>();
215 embedder_attribute->struct_size = sizeof(FlutterStringAttribute);
216 embedder_attribute->start = attribute->start;
217 embedder_attribute->end = attribute->end;
218
219 switch (attribute->type) {
221 std::shared_ptr<flutter::LocaleStringAttribute> locale_attribute =
222 std::static_pointer_cast<flutter::LocaleStringAttribute>(attribute);
223
224 auto embedder_locale = std::make_unique<FlutterLocaleStringAttribute>();
225 embedder_locale->struct_size = sizeof(FlutterLocaleStringAttribute);
226 embedder_locale->locale = locale_attribute->locale.c_str();
227 locale_attributes_.push_back(std::move(embedder_locale));
228
229 embedder_attribute->type = FlutterStringAttributeType::kLocale;
230 embedder_attribute->locale = locale_attributes_.back().get();
231 break;
232 }
234 // All spell out attributes are identical and share a lazily created
235 // instance.
236 if (!spell_out_attribute_) {
237 auto spell_out_attribute_ =
238 std::make_unique<FlutterSpellOutStringAttribute>();
239 spell_out_attribute_->struct_size =
241 }
242
243 embedder_attribute->type = FlutterStringAttributeType::kSpellOut;
244 embedder_attribute->spell_out = spell_out_attribute_.get();
245 break;
246 }
247 }
248
249 string_attributes_.push_back(std::move(embedder_attribute));
250 result->push_back(string_attributes_.back().get());
251 }
252
253 node_string_attributes_.push_back(std::move(result));
254
255 return {
256 .count = node_string_attributes_.back()->size(),
257 .attributes = node_string_attributes_.back()->data(),
258 };
259}
260
261} // namespace flutter
SkMatrix asM33() const
Definition SkM44.h:409
static constexpr int kMScaleX
horizontal scale factor
Definition SkMatrix.h:353
static constexpr int kMTransY
vertical translation
Definition SkMatrix.h:358
static constexpr int kMPersp1
input y perspective factor
Definition SkMatrix.h:360
static constexpr int kMPersp0
input x perspective factor
Definition SkMatrix.h:359
static constexpr int kMPersp2
perspective bias
Definition SkMatrix.h:361
static constexpr int kMTransX
horizontal translation
Definition SkMatrix.h:355
static constexpr int kMSkewY
vertical skew factor
Definition SkMatrix.h:356
static constexpr int kMScaleY
vertical scale factor
Definition SkMatrix.h:357
static constexpr int kMSkewX
horizontal skew factor
Definition SkMatrix.h:354
EmbedderSemanticsUpdate2(const SemanticsNodeUpdates &nodes, const CustomAccessibilityActionUpdates &actions)
EmbedderSemanticsUpdate(const SemanticsNodeUpdates &nodes, const CustomAccessibilityActionUpdates &actions)
FlutterSemanticsAction
Definition embedder.h:113
@ kSpellOut
Definition embedder.h:1200
@ kLocale
Definition embedder.h:1202
FlutterTextDirection
Definition embedder.h:246
FlutterSemanticsFlag
Definition embedder.h:170
uint8_t value
GAsyncResult * result
std::unordered_map< int32_t, SemanticsNode > SemanticsNodeUpdates
std::unordered_map< int32_t, CustomAccessibilityAction > CustomAccessibilityActionUpdates
std::vector< StringAttributePtr > StringAttributes
static SkColor4f transform(SkColor4f c, SkColorSpace *src, SkColorSpace *dst)
Definition p3.cpp:47
A structure to represent a rectangle.
Definition embedder.h:435
A batch of updates to semantics nodes and custom actions.
Definition embedder.h:1502
size_t struct_size
The size of the struct. Must be sizeof(FlutterSemanticsUpdate2).
Definition embedder.h:1504
size_t struct_size
The size of the struct. Must be sizeof(FlutterSemanticsUpdate).
Definition embedder.h:1490
SkScalar fBottom
larger y-axis bounds
Definition extension.cpp:17
SkScalar fLeft
smaller x-axis bounds
Definition extension.cpp:14
SkScalar fRight
larger x-axis bounds
Definition extension.cpp:16
SkScalar fTop
smaller y-axis bounds
Definition extension.cpp:15
StringAttributes decreasedValueAttributes
StringAttributes hintAttributes
StringAttributes increasedValueAttributes
StringAttributes valueAttributes
StringAttributes labelAttributes
std::vector< int32_t > childrenInHitTestOrder
std::vector< int32_t > customAccessibilityActions
std::vector< int32_t > childrenInTraversalOrder