| Flutter Linux Embedder |

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
11 static constexpr int32_t kRootSemanticsNodeId = 0;
12
13 struct _FlViewAccessible {
14 AtkPlug parent_instance;
15
16 GWeakRef engine;
17
18 // Semantics nodes keyed by ID
19 GHashTable* semantics_nodes_by_id;
20
21 // Flag to track when root node is created.
22 gboolean root_node_created;
23 };
24
25 G_DEFINE_TYPE(FlViewAccessible, fl_view_accessible, ATK_TYPE_PLUG)
26
27 static FlAccessibleNode* create_node(FlViewAccessible* self,
28 FlutterSemanticsNode2* semantics) {
29 g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
30 if (engine == nullptr) {
31 return nullptr;
32 }
33
34 if (semantics->flags & kFlutterSemanticsFlagIsTextField) {
35 return fl_accessible_text_field_new(engine, semantics->id);
36 }
37
38 return fl_accessible_node_new(engine, semantics->id);
39 }
40
41 static FlAccessibleNode* lookup_node(FlViewAccessible* self, int32_t id) {
42 return FL_ACCESSIBLE_NODE(
43 g_hash_table_lookup(self->semantics_nodes_by_id, GINT_TO_POINTER(id)));
44 }
45
46 // Gets the ATK node for the given id.
47 // If the node doesn't exist it will be created.
48 static FlAccessibleNode* get_node(FlViewAccessible* self,
49 FlutterSemanticsNode2* semantics) {
50 FlAccessibleNode* node = lookup_node(self, semantics->id);
51 if (node != nullptr) {
52 return node;
53 }
54
55 node = create_node(self, semantics);
56 if (semantics->id == kRootSemanticsNodeId) {
57 fl_accessible_node_set_parent(node, ATK_OBJECT(self), 0);
58 }
59 g_hash_table_insert(self->semantics_nodes_by_id,
60 GINT_TO_POINTER(semantics->id),
61 reinterpret_cast<gpointer>(node));
62
63 // Update when root node is created.
64 if (!self->root_node_created && semantics->id == kRootSemanticsNodeId) {
65 g_signal_emit_by_name(self, "children-changed::add", 0, node, nullptr);
66 self->root_node_created = true;
67 }
68
69 return node;
70 }
71
72 // Implements AtkObject::get_n_children
73 static gint fl_view_accessible_get_n_children(AtkObject* accessible) {
74 FlViewAccessible* self = FL_VIEW_ACCESSIBLE(accessible);
75 FlAccessibleNode* node = lookup_node(self, 0);
76
77 if (node == nullptr) {
78 return 0;
79 }
80
81 return 1;
82 }
83
84 // Implements AtkObject::ref_child
85 static AtkObject* fl_view_accessible_ref_child(AtkObject* accessible, gint i) {
86 FlViewAccessible* self = FL_VIEW_ACCESSIBLE(accessible);
87 FlAccessibleNode* node = lookup_node(self, 0);
88
89 if (i != 0 || node == nullptr) {
90 return nullptr;
91 }
92
93 return ATK_OBJECT(g_object_ref(node));
94 }
95
96 // Implements AtkObject::get_role
97 static AtkRole fl_view_accessible_get_role(AtkObject* accessible) {
98 return ATK_ROLE_PANEL;
99 }
100
101 // Implements AtkObject::ref_state_set
102 static AtkStateSet* fl_view_accessible_ref_state_set(AtkObject* accessible) {
103 FlViewAccessible* self = FL_VIEW_ACCESSIBLE(accessible);
104 FlAccessibleNode* node = lookup_node(self, 0);
105 return node != nullptr ? atk_object_ref_state_set(ATK_OBJECT(node)) : nullptr;
106 }
107
108 static void fl_view_accessible_dispose(GObject* object) {
109 FlViewAccessible* self = FL_VIEW_ACCESSIBLE(object);
110
111 g_clear_pointer(&self->semantics_nodes_by_id, g_hash_table_unref);
112
113 g_weak_ref_clear(&self->engine);
114
115 G_OBJECT_CLASS(fl_view_accessible_parent_class)->dispose(object);
116 }
117
118 static void fl_view_accessible_class_init(FlViewAccessibleClass* klass) {
119 ATK_OBJECT_CLASS(klass)->get_n_children = fl_view_accessible_get_n_children;
120 ATK_OBJECT_CLASS(klass)->ref_child = fl_view_accessible_ref_child;
121 ATK_OBJECT_CLASS(klass)->get_role = fl_view_accessible_get_role;
122 ATK_OBJECT_CLASS(klass)->ref_state_set = fl_view_accessible_ref_state_set;
123
124 G_OBJECT_CLASS(klass)->dispose = fl_view_accessible_dispose;
125 }
126
127 static void fl_view_accessible_init(FlViewAccessible* self) {
128 self->semantics_nodes_by_id = g_hash_table_new_full(
129 g_direct_hash, g_direct_equal, nullptr, g_object_unref);
130 }
131
132 FlViewAccessible* fl_view_accessible_new(FlEngine* engine) {
133 FlViewAccessible* self =
134 FL_VIEW_ACCESSIBLE(g_object_new(fl_view_accessible_get_type(), nullptr));
135 g_weak_ref_init(&self->engine, engine);
136 return self;
137 }
138
139 void fl_view_accessible_handle_update_semantics(
140 FlViewAccessible* self,
141 const FlutterSemanticsUpdate2* update) {
142 g_autoptr(GHashTable) pending_children =
143 g_hash_table_new_full(g_direct_hash, g_direct_equal, nullptr,
144 reinterpret_cast<GDestroyNotify>(fl_value_unref));
145 for (size_t i = 0; i < update->node_count; i++) {
146 FlutterSemanticsNode2* node = update->nodes[i];
147 FlAccessibleNode* atk_node = get_node(self, node);
148
149 fl_accessible_node_set_flags(atk_node, node->flags);
150 fl_accessible_node_set_actions(atk_node, node->actions);
151 fl_accessible_node_set_name(atk_node, node->label);
152 fl_accessible_node_set_extents(
153 atk_node, node->rect.left + node->transform.transX,
154 node->rect.top + node->transform.transY,
155 node->rect.right - node->rect.left, node->rect.bottom - node->rect.top);
156 fl_accessible_node_set_value(atk_node, node->value);
157 fl_accessible_node_set_text_selection(atk_node, node->text_selection_base,
158 node->text_selection_extent);
159 fl_accessible_node_set_text_direction(atk_node, node->text_direction);
160
161 FlValue* children = fl_value_new_int32_list(
162 node->children_in_traversal_order, node->child_count);
163 g_hash_table_insert(pending_children, atk_node, children);
164 }
165
166 g_hash_table_foreach_remove(
167 pending_children,
168 [](gpointer key, gpointer value, gpointer user_data) -> gboolean {
169 FlViewAccessible* self = FL_VIEW_ACCESSIBLE(user_data);
170
171 FlAccessibleNode* parent = FL_ACCESSIBLE_NODE(key);
172
173 size_t child_count = fl_value_get_length(static_cast<FlValue*>(value));
174 const int32_t* children_in_traversal_order =
175 fl_value_get_int32_list(static_cast<FlValue*>(value));
176
177 g_autoptr(GPtrArray) children = g_ptr_array_new();
178 for (size_t i = 0; i < child_count; i++) {
179 FlAccessibleNode* child =
180 lookup_node(self, children_in_traversal_order[i]);
181 g_assert(child != nullptr);
182 fl_accessible_node_set_parent(child, ATK_OBJECT(parent), i);
183 g_ptr_array_add(children, child);
184 }
185 fl_accessible_node_set_children(parent, children);
186
187 return TRUE;
188 },
189 self);
190 }
fl_value_get_int32_list
const G_MODULE_EXPORT int32_t * fl_value_get_int32_list(FlValue *self)
Definition: fl_value.cc:696
get_node
static FlAccessibleNode * get_node(FlViewAccessible *self, FlutterSemanticsNode2 *semantics)
Definition: fl_view_accessible.cc:48
fl_view_accessible.h
fl_view_accessible_handle_update_semantics
void fl_view_accessible_handle_update_semantics(FlViewAccessible *self, const FlutterSemanticsUpdate2 *update)
Definition: fl_view_accessible.cc:139
fl_accessible_node_set_text_selection
void fl_accessible_node_set_text_selection(FlAccessibleNode *self, gint base, gint extent)
Definition: fl_accessible_node.cc:563
fl_view_accessible_ref_child
static AtkObject * fl_view_accessible_ref_child(AtkObject *accessible, gint i)
Definition: fl_view_accessible.cc:85
fl_accessible_node_set_flags
void fl_accessible_node_set_flags(FlAccessibleNode *self, FlutterSemanticsFlag flags)
Definition: fl_accessible_node.cc:543
fl_accessible_node_set_parent
void fl_accessible_node_set_parent(FlAccessibleNode *self, AtkObject *parent, gint index)
Definition: fl_accessible_node.cc:488
fl_accessible_text_field_new
FlAccessibleNode * fl_accessible_text_field_new(FlEngine *engine, int32_t id)
Definition: fl_accessible_text_field.cc:618
i
int i
Definition: fl_socket_accessible.cc:18
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
_FlViewAccessible::semantics_nodes_by_id
GHashTable * semantics_nodes_by_id
Definition: fl_view_accessible.cc:19
fl_accessible_node_set_children
void fl_accessible_node_set_children(FlAccessibleNode *self, GPtrArray *children)
Definition: fl_accessible_node.cc:499
fl_view_accessible_init
static void fl_view_accessible_init(FlViewAccessible *self)
Definition: fl_view_accessible.cc:127
user_data
G_BEGIN_DECLS G_MODULE_EXPORT FlValue gpointer user_data
Definition: fl_event_channel.h:90
fl_value_unref
G_MODULE_EXPORT void fl_value_unref(FlValue *self)
Definition: fl_value.cc:400
create_node
static FlAccessibleNode * create_node(FlViewAccessible *self, FlutterSemanticsNode2 *semantics)
Definition: fl_view_accessible.cc:27
fl_view_accessible_dispose
static void fl_view_accessible_dispose(GObject *object)
Definition: fl_view_accessible.cc:108
fl_accessible_node_set_extents
void fl_accessible_node_set_extents(FlAccessibleNode *self, gint x, gint y, gint width, gint height)
Definition: fl_accessible_node.cc:532
fl_accessible_node_set_text_direction
void fl_accessible_node_set_text_direction(FlAccessibleNode *self, FlutterTextDirection direction)
Definition: fl_accessible_node.cc:572
G_DEFINE_TYPE
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
Definition: fl_basic_message_channel.cc:37
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
fl_accessible_node_set_value
void fl_accessible_node_set_value(FlAccessibleNode *self, const gchar *value)
Definition: fl_accessible_node.cc:557
fl_value_new_int32_list
G_MODULE_EXPORT FlValue * fl_value_new_int32_list(const int32_t *data, size_t data_length)
Definition: fl_value.cc:309
fl_value_get_length
G_MODULE_EXPORT size_t fl_value_get_length(FlValue *self)
Definition: fl_value.cc:724
fl_accessible_node.h
fl_accessible_node_new
FlAccessibleNode * fl_accessible_node_new(FlEngine *engine, int32_t id)
Definition: fl_accessible_node.cc:482
fl_view.h
fl_view_accessible_ref_state_set
static AtkStateSet * fl_view_accessible_ref_state_set(AtkObject *accessible)
Definition: fl_view_accessible.cc:102
_FlViewAccessible::engine
GWeakRef engine
Definition: fl_view_accessible.cc:16
_FlViewAccessible
Definition: fl_view_accessible.cc:13
fl_accessible_text_field.h
fl_view_accessible_get_n_children
static gint fl_view_accessible_get_n_children(AtkObject *accessible)
Definition: fl_view_accessible.cc:73
fl_value.h
kRootSemanticsNodeId
static constexpr int32_t kRootSemanticsNodeId
Definition: fl_view_accessible.cc:11
fl_accessible_node_set_name
void fl_accessible_node_set_name(FlAccessibleNode *self, const gchar *name)
Definition: fl_accessible_node.cc:526
fl_accessible_node_set_actions
void fl_accessible_node_set_actions(FlAccessibleNode *self, FlutterSemanticsAction actions)
Definition: fl_accessible_node.cc:550
_FlViewAccessible::parent_instance
AtkPlug parent_instance
Definition: fl_view_accessible.cc:14
value
uint8_t value
Definition: fl_standard_message_codec.cc:36
fl_view_accessible_new
FlViewAccessible * fl_view_accessible_new(FlEngine *engine)
Definition: fl_view_accessible.cc:132
lookup_node
static FlAccessibleNode * lookup_node(FlViewAccessible *self, int32_t id)
Definition: fl_view_accessible.cc:41
fl_view_accessible_get_role
static AtkRole fl_view_accessible_get_role(AtkObject *accessible)
Definition: fl_view_accessible.cc:97
_FlViewAccessible::root_node_created
gboolean root_node_created
Definition: fl_view_accessible.cc:22
fl_view_accessible_class_init
static void fl_view_accessible_class_init(FlViewAccessibleClass *klass)
Definition: fl_view_accessible.cc:118