Flutter Engine
The Flutter Engine
fl_view_accessible.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/linux/fl_view_accessible.h"
6#include "flutter/shell/platform/linux/fl_accessible_node.h"
7#include "flutter/shell/platform/linux/fl_accessible_text_field.h"
8#include "flutter/shell/platform/linux/public/flutter_linux/fl_value.h"
9#include "flutter/shell/platform/linux/public/flutter_linux/fl_view.h"
10
11static constexpr int32_t kRootSemanticsNodeId = 0;
12
15
16 FlEngine* engine;
17
18 // Semantics nodes keyed by ID
20
21 // Flag to track when root node is created.
23};
24
26
27G_DEFINE_TYPE(FlViewAccessible, fl_view_accessible, ATK_TYPE_PLUG)
28
29static FlAccessibleNode* create_node(FlViewAccessible* self,
30 FlutterSemanticsNode2* semantics) {
31 if (semantics->flags & kFlutterSemanticsFlagIsTextField) {
32 return fl_accessible_text_field_new(self->engine, semantics->id);
33 }
34
35 return fl_accessible_node_new(self->engine, semantics->id);
36}
37
38static FlAccessibleNode* lookup_node(FlViewAccessible* self, int32_t id) {
39 return FL_ACCESSIBLE_NODE(
40 g_hash_table_lookup(self->semantics_nodes_by_id, GINT_TO_POINTER(id)));
41}
42
43// Gets the ATK node for the given id.
44// If the node doesn't exist it will be created.
45static FlAccessibleNode* get_node(FlViewAccessible* self,
46 FlutterSemanticsNode2* semantics) {
47 FlAccessibleNode* node = lookup_node(self, semantics->id);
48 if (node != nullptr) {
49 return node;
50 }
51
52 node = create_node(self, semantics);
53 if (semantics->id == kRootSemanticsNodeId) {
54 fl_accessible_node_set_parent(node, ATK_OBJECT(self), 0);
55 }
56 g_hash_table_insert(self->semantics_nodes_by_id,
57 GINT_TO_POINTER(semantics->id),
58 reinterpret_cast<gpointer>(node));
59
60 // Update when root node is created.
61 if (!self->root_node_created && semantics->id == kRootSemanticsNodeId) {
62 g_signal_emit_by_name(self, "children-changed::add", 0, node, nullptr);
63 self->root_node_created = true;
64 }
65
66 return node;
67}
68
69// Implements AtkObject::get_n_children
70static gint fl_view_accessible_get_n_children(AtkObject* accessible) {
71 FlViewAccessible* self = FL_VIEW_ACCESSIBLE(accessible);
72 FlAccessibleNode* node = lookup_node(self, 0);
73
74 if (node == nullptr) {
75 return 0;
76 }
77
78 return 1;
79}
80
81// Implements AtkObject::ref_child
82static AtkObject* fl_view_accessible_ref_child(AtkObject* accessible, gint i) {
83 FlViewAccessible* self = FL_VIEW_ACCESSIBLE(accessible);
84 FlAccessibleNode* node = lookup_node(self, 0);
85
86 if (i != 0 || node == nullptr) {
87 return nullptr;
88 }
89
90 return ATK_OBJECT(g_object_ref(node));
91}
92
93// Implements AtkObject::get_role
94static AtkRole fl_view_accessible_get_role(AtkObject* accessible) {
95 return ATK_ROLE_PANEL;
96}
97
98// Implements AtkObject::ref_state_set
99static AtkStateSet* fl_view_accessible_ref_state_set(AtkObject* accessible) {
100 FlViewAccessible* self = FL_VIEW_ACCESSIBLE(accessible);
101 FlAccessibleNode* node = lookup_node(self, 0);
102 return node != nullptr ? atk_object_ref_state_set(ATK_OBJECT(node)) : nullptr;
103}
104
105static void fl_view_accessible_dispose(GObject* object) {
106 FlViewAccessible* self = FL_VIEW_ACCESSIBLE(object);
107
108 g_clear_pointer(&self->semantics_nodes_by_id, g_hash_table_unref);
109
110 if (self->engine != nullptr) {
111 g_object_remove_weak_pointer(object,
112 reinterpret_cast<gpointer*>(&self->engine));
113 self->engine = nullptr;
114 }
115
116 G_OBJECT_CLASS(fl_view_accessible_parent_class)->dispose(object);
117}
118
119static void fl_view_accessible_class_init(FlViewAccessibleClass* klass) {
120 ATK_OBJECT_CLASS(klass)->get_n_children = fl_view_accessible_get_n_children;
121 ATK_OBJECT_CLASS(klass)->ref_child = fl_view_accessible_ref_child;
122 ATK_OBJECT_CLASS(klass)->get_role = fl_view_accessible_get_role;
123 ATK_OBJECT_CLASS(klass)->ref_state_set = fl_view_accessible_ref_state_set;
124
125 G_OBJECT_CLASS(klass)->dispose = fl_view_accessible_dispose;
126}
127
128static void fl_view_accessible_init(FlViewAccessible* self) {
129 self->semantics_nodes_by_id = g_hash_table_new_full(
130 g_direct_hash, g_direct_equal, nullptr, g_object_unref);
131}
132
133FlViewAccessible* fl_view_accessible_new(FlEngine* engine) {
134 FlViewAccessible* self =
135 FL_VIEW_ACCESSIBLE(g_object_new(fl_view_accessible_get_type(), nullptr));
136 self->engine = engine;
137 g_object_add_weak_pointer(G_OBJECT(self),
138 reinterpret_cast<gpointer*>(&self->engine));
139 return self;
140}
141
143 FlViewAccessible* self,
145 g_autoptr(GHashTable) pending_children =
146 g_hash_table_new_full(g_direct_hash, g_direct_equal, nullptr,
147 reinterpret_cast<GDestroyNotify>(fl_value_unref));
148 for (size_t i = 0; i < update->node_count; i++) {
149 FlutterSemanticsNode2* node = update->nodes[i];
150 FlAccessibleNode* atk_node = get_node(self, node);
151
152 fl_accessible_node_set_flags(atk_node, node->flags);
154 fl_accessible_node_set_name(atk_node, node->label);
156 atk_node, node->rect.left + node->transform.transX,
157 node->rect.top + node->transform.transY,
158 node->rect.right - node->rect.left, node->rect.bottom - node->rect.top);
159 fl_accessible_node_set_value(atk_node, node->value);
163
166 g_hash_table_insert(pending_children, atk_node, children);
167 }
168
169 g_hash_table_foreach_remove(
170 pending_children,
171 [](gpointer key, gpointer value, gpointer user_data) -> gboolean {
172 FlViewAccessible* self = FL_VIEW_ACCESSIBLE(user_data);
173
174 FlAccessibleNode* parent = FL_ACCESSIBLE_NODE(key);
175
176 size_t child_count = fl_value_get_length(static_cast<FlValue*>(value));
177 const int32_t* children_in_traversal_order =
178 fl_value_get_int32_list(static_cast<FlValue*>(value));
179
180 g_autoptr(GPtrArray) children = g_ptr_array_new();
181 for (size_t i = 0; i < child_count; i++) {
182 FlAccessibleNode* child =
183 lookup_node(self, children_in_traversal_order[i]);
184 g_assert(child != nullptr);
185 fl_accessible_node_set_parent(child, ATK_OBJECT(parent), i);
186 g_ptr_array_add(children, child);
187 }
188 fl_accessible_node_set_children(parent, children);
189
190 return true;
191 },
192 self);
193}
@ kFlutterSemanticsFlagIsTextField
Whether the semantic node represents a text field.
Definition: embedder.h:183
FlutterEngine engine
Definition: main.cc:68
void fl_accessible_node_set_name(FlAccessibleNode *self, const gchar *name)
void fl_accessible_node_set_text_direction(FlAccessibleNode *self, FlutterTextDirection direction)
void fl_accessible_node_set_text_selection(FlAccessibleNode *self, gint base, gint extent)
void fl_accessible_node_set_actions(FlAccessibleNode *self, FlutterSemanticsAction actions)
void fl_accessible_node_set_children(FlAccessibleNode *self, GPtrArray *children)
void fl_accessible_node_set_extents(FlAccessibleNode *self, gint x, gint y, gint width, gint height)
void fl_accessible_node_set_value(FlAccessibleNode *self, const gchar *value)
void fl_accessible_node_set_flags(FlAccessibleNode *self, FlutterSemanticsFlag flags)
void fl_accessible_node_set_parent(FlAccessibleNode *self, AtkObject *parent, gint index)
FlAccessibleNode * fl_accessible_node_new(FlEngine *engine, int32_t id)
FlAccessibleNode * fl_accessible_text_field_new(FlEngine *engine, int32_t id)
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
uint8_t value
G_MODULE_EXPORT void fl_value_unref(FlValue *self)
Definition: fl_value.cc:400
G_MODULE_EXPORT FlValue * fl_value_new_int32_list(const int32_t *data, size_t data_length)
Definition: fl_value.cc:309
G_MODULE_EXPORT size_t fl_value_get_length(FlValue *self)
Definition: fl_value.cc:724
G_MODULE_EXPORT const int32_t * fl_value_get_int32_list(FlValue *self)
Definition: fl_value.cc:696
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
static void fl_view_accessible_dispose(GObject *object)
static AtkStateSet * fl_view_accessible_ref_state_set(AtkObject *accessible)
static FlAccessibleNode * get_node(FlViewAccessible *self, FlutterSemanticsNode2 *semantics)
static void fl_view_accessible_init(FlViewAccessible *self)
static AtkRole fl_view_accessible_get_role(AtkObject *accessible)
@ kPropEngine
@ kPropLast
@ kProp0
static FlAccessibleNode * create_node(FlViewAccessible *self, FlutterSemanticsNode2 *semantics)
FlViewAccessible * fl_view_accessible_new(FlEngine *engine)
static AtkObject * fl_view_accessible_ref_child(AtkObject *accessible, gint i)
static gint fl_view_accessible_get_n_children(AtkObject *accessible)
static void fl_view_accessible_class_init(FlViewAccessibleClass *klass)
static constexpr int32_t kRootSemanticsNodeId
void fl_view_accessible_handle_update_semantics(FlViewAccessible *self, const FlutterSemanticsUpdate2 *update)
static FlAccessibleNode * lookup_node(FlViewAccessible *self, int32_t id)
Definition: update.py:1
double bottom
Definition: embedder.h:441
double top
Definition: embedder.h:439
double left
Definition: embedder.h:438
double right
Definition: embedder.h:440
const int32_t * children_in_traversal_order
Array of child node IDs in traversal order. Has length child_count.
Definition: embedder.h:1385
int32_t text_selection_extent
The position at which the text selection terminates.
Definition: embedder.h:1345
FlutterTextDirection text_direction
Definition: embedder.h:1376
FlutterSemanticsAction actions
The set of semantics actions applicable to this node.
Definition: embedder.h:1341
int32_t id
The unique identifier for this node.
Definition: embedder.h:1337
FlutterRect rect
The bounding box for this node in its coordinate system.
Definition: embedder.h:1378
FlutterTransformation transform
Definition: embedder.h:1381
size_t child_count
The number of children this node has.
Definition: embedder.h:1383
const char * label
A textual description of the node.
Definition: embedder.h:1363
int32_t text_selection_base
The position at which the text selection originates.
Definition: embedder.h:1343
const char * value
A textual description of the current value of the node.
Definition: embedder.h:1367
FlutterSemanticsFlag flags
The set of semantics flags associated with this node.
Definition: embedder.h:1339
A batch of updates to semantics nodes and custom actions.
Definition: embedder.h:1504
double transY
vertical translation
Definition: embedder.h:289
double transX
horizontal translation
Definition: embedder.h:283
GHashTable * semantics_nodes_by_id
void * user_data