| 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 struct _FlViewAccessible {
12 GtkContainerAccessible parent_instance;
13
14 FlEngine* engine;
15
16 // Semantics nodes keyed by ID
17 GHashTable* semantics_nodes_by_id;
18 };
19
20 enum { kProp0, kPropEngine, kPropLast };
21
22 G_DEFINE_TYPE(FlViewAccessible,
23 fl_view_accessible,
24 GTK_TYPE_CONTAINER_ACCESSIBLE)
25
26 static void init_engine(FlViewAccessible* self, FlEngine* engine) {
27 g_assert(self->engine == nullptr);
28 self->engine = engine;
29 g_object_add_weak_pointer(G_OBJECT(self),
30 reinterpret_cast<gpointer*>(&self->engine));
31 }
32
33 static FlEngine* get_engine(FlViewAccessible* self) {
34 if (self->engine == nullptr) {
35 FlView* view = FL_VIEW(gtk_accessible_get_widget(GTK_ACCESSIBLE(self)));
36 init_engine(self, fl_view_get_engine(view));
37 }
38 return self->engine;
39 }
40
41 static FlAccessibleNode* create_node(FlViewAccessible* self,
42 FlutterSemanticsNode2* semantics) {
43 FlEngine* engine = get_engine(self);
44
45 if (semantics->flags & kFlutterSemanticsFlagIsTextField) {
46 return fl_accessible_text_field_new(engine, semantics->id);
47 }
48
49 return fl_accessible_node_new(engine, semantics->id);
50 }
51
52 static FlAccessibleNode* lookup_node(FlViewAccessible* self, int32_t id) {
53 return FL_ACCESSIBLE_NODE(
54 g_hash_table_lookup(self->semantics_nodes_by_id, GINT_TO_POINTER(id)));
55 }
56
57 // Gets the ATK node for the given id.
58 // If the node doesn't exist it will be created.
59 static FlAccessibleNode* get_node(FlViewAccessible* self,
60 FlutterSemanticsNode2* semantics) {
61 FlAccessibleNode* node = lookup_node(self, semantics->id);
62 if (node != nullptr) {
63 return node;
64 }
65
66 node = create_node(self, semantics);
67 if (semantics->id == 0) {
68 fl_accessible_node_set_parent(node, ATK_OBJECT(self), 0);
69 g_signal_emit_by_name(self, "children-changed::add", 0, node, nullptr);
70 }
71 g_hash_table_insert(self->semantics_nodes_by_id,
72 GINT_TO_POINTER(semantics->id),
73 reinterpret_cast<gpointer>(node));
74
75 return node;
76 }
77
78 // Implements AtkObject::get_n_children
79 static gint fl_view_accessible_get_n_children(AtkObject* accessible) {
80 FlViewAccessible* self = FL_VIEW_ACCESSIBLE(accessible);
81 FlAccessibleNode* node = lookup_node(self, 0);
82
83 if (node == nullptr) {
84 return 0;
85 }
86
87 return 1;
88 }
89
90 // Implements AtkObject::ref_child
91 static AtkObject* fl_view_accessible_ref_child(AtkObject* accessible, gint i) {
92 FlViewAccessible* self = FL_VIEW_ACCESSIBLE(accessible);
93 FlAccessibleNode* node = lookup_node(self, 0);
94
95 if (i != 0 || node == nullptr) {
96 return nullptr;
97 }
98
99 return ATK_OBJECT(g_object_ref(node));
100 }
101
102 // Implements AtkObject::get_role
103 static AtkRole fl_view_accessible_get_role(AtkObject* accessible) {
104 return ATK_ROLE_PANEL;
105 }
106
107 // Implements GObject::set_property
108 static void fl_view_accessible_set_property(GObject* object,
109 guint prop_id,
110 const GValue* value,
111 GParamSpec* pspec) {
112 FlViewAccessible* self = FL_VIEW_ACCESSIBLE(object);
113 switch (prop_id) {
114 case kPropEngine:
115 init_engine(self, FL_ENGINE(g_value_get_object(value)));
116 break;
117 default:
118 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
119 break;
120 }
121 }
122
123 static void fl_view_accessible_dispose(GObject* object) {
124 FlViewAccessible* self = FL_VIEW_ACCESSIBLE(object);
125
126 g_clear_pointer(&self->semantics_nodes_by_id, g_hash_table_unref);
127
128 if (self->engine != nullptr) {
129 g_object_remove_weak_pointer(object,
130 reinterpret_cast<gpointer*>(&self->engine));
131 self->engine = nullptr;
132 }
133
134 G_OBJECT_CLASS(fl_view_accessible_parent_class)->dispose(object);
135 }
136
137 static void fl_view_accessible_class_init(FlViewAccessibleClass* klass) {
138 ATK_OBJECT_CLASS(klass)->get_n_children = fl_view_accessible_get_n_children;
139 ATK_OBJECT_CLASS(klass)->ref_child = fl_view_accessible_ref_child;
140 ATK_OBJECT_CLASS(klass)->get_role = fl_view_accessible_get_role;
141
142 G_OBJECT_CLASS(klass)->dispose = fl_view_accessible_dispose;
143 G_OBJECT_CLASS(klass)->set_property = fl_view_accessible_set_property;
144
145 g_object_class_install_property(
146 G_OBJECT_CLASS(klass), kPropEngine,
147 g_param_spec_object(
148 "engine", "engine", "Flutter engine", fl_engine_get_type(),
149 static_cast<GParamFlags>(G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
150 G_PARAM_STATIC_STRINGS)));
151 }
152
153 static void fl_view_accessible_init(FlViewAccessible* self) {
154 self->semantics_nodes_by_id = g_hash_table_new_full(
155 g_direct_hash, g_direct_equal, nullptr, g_object_unref);
156 }
157
158 void fl_view_accessible_handle_update_semantics(
159 FlViewAccessible* self,
160 const FlutterSemanticsUpdate2* update) {
161 g_autoptr(GHashTable) pending_children =
162 g_hash_table_new_full(g_direct_hash, g_direct_equal, nullptr,
163 reinterpret_cast<GDestroyNotify>(fl_value_unref));
164
165 for (size_t i = 0; i < update->node_count; i++) {
166 FlutterSemanticsNode2* node = update->nodes[i];
167 FlAccessibleNode* atk_node = get_node(self, node);
168
169 fl_accessible_node_set_flags(atk_node, node->flags);
170 fl_accessible_node_set_actions(atk_node, node->actions);
171 fl_accessible_node_set_name(atk_node, node->label);
172 fl_accessible_node_set_extents(
173 atk_node, node->rect.left + node->transform.transX,
174 node->rect.top + node->transform.transY,
175 node->rect.right - node->rect.left, node->rect.bottom - node->rect.top);
176 fl_accessible_node_set_value(atk_node, node->value);
177 fl_accessible_node_set_text_selection(atk_node, node->text_selection_base,
178 node->text_selection_extent);
179 fl_accessible_node_set_text_direction(atk_node, node->text_direction);
180
181 FlValue* children = fl_value_new_int32_list(
182 node->children_in_traversal_order, node->child_count);
183 g_hash_table_insert(pending_children, atk_node, children);
184 }
185
186 g_hash_table_foreach_remove(
187 pending_children,
188 [](gpointer key, gpointer value, gpointer user_data) -> gboolean {
189 FlViewAccessible* self = FL_VIEW_ACCESSIBLE(user_data);
190
191 FlAccessibleNode* parent = FL_ACCESSIBLE_NODE(key);
192
193 size_t child_count = fl_value_get_length(static_cast<FlValue*>(value));
194 const int32_t* children_in_traversal_order =
195 fl_value_get_int32_list(static_cast<FlValue*>(value));
196
197 g_autoptr(GPtrArray) children = g_ptr_array_new();
198 for (size_t i = 0; i < child_count; i++) {
199 FlAccessibleNode* child =
200 lookup_node(self, children_in_traversal_order[i]);
201 g_assert(child != nullptr);
202 fl_accessible_node_set_parent(child, ATK_OBJECT(parent), i);
203 g_ptr_array_add(children, child);
204 }
205 fl_accessible_node_set_children(parent, children);
206
207 return true;
208 },
209 self);
210 }
prop_id
guint prop_id
Definition: fl_standard_method_codec.cc:31
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:59
kPropLast
@ kPropLast
Definition: fl_view_accessible.cc:20
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:158
fl_accessible_node_set_text_selection
void fl_accessible_node_set_text_selection(FlAccessibleNode *self, gint base, gint extent)
Definition: fl_accessible_node.cc:562
fl_view_accessible_ref_child
static AtkObject * fl_view_accessible_ref_child(AtkObject *accessible, gint i)
Definition: fl_view_accessible.cc:91
fl_accessible_node_set_flags
void fl_accessible_node_set_flags(FlAccessibleNode *self, FlutterSemanticsFlag flags)
Definition: fl_accessible_node.cc:542
fl_accessible_node_set_parent
void fl_accessible_node_set_parent(FlAccessibleNode *self, AtkObject *parent, gint index)
Definition: fl_accessible_node.cc:487
fl_accessible_text_field_new
FlAccessibleNode * fl_accessible_text_field_new(FlEngine *engine, int32_t id)
Definition: fl_accessible_text_field.cc:618
_FlViewAccessible::engine
FlEngine * engine
Definition: fl_view_accessible.cc:14
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
user_data
FlKeyEvent uint64_t FlKeyResponderAsyncCallback gpointer user_data
Definition: fl_key_channel_responder.cc:121
_FlViewAccessible::semantics_nodes_by_id
GHashTable * semantics_nodes_by_id
Definition: fl_view_accessible.cc:17
pspec
guint const GValue GParamSpec * pspec
Definition: fl_standard_method_codec.cc:33
fl_accessible_node_set_children
void fl_accessible_node_set_children(FlAccessibleNode *self, GPtrArray *children)
Definition: fl_accessible_node.cc:498
fl_view_accessible_init
static void fl_view_accessible_init(FlViewAccessible *self)
Definition: fl_view_accessible.cc:153
update
G_BEGIN_DECLS const FlutterSemanticsUpdate2 * update
Definition: fl_view_accessible.h:40
kProp0
@ kProp0
Definition: fl_view_accessible.cc:20
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:41
fl_view_accessible_dispose
static void fl_view_accessible_dispose(GObject *object)
Definition: fl_view_accessible.cc:123
fl_view_accessible_set_property
static void fl_view_accessible_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
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:531
fl_accessible_node_set_text_direction
void fl_accessible_node_set_text_direction(FlAccessibleNode *self, FlutterTextDirection direction)
Definition: fl_accessible_node.cc:571
kPropEngine
@ kPropEngine
Definition: fl_view_accessible.cc:20
fl_view_get_engine
G_MODULE_EXPORT FlEngine * fl_view_get_engine(FlView *self)
Definition: fl_view.cc:800
fl_accessible_node_set_value
void fl_accessible_node_set_value(FlAccessibleNode *self, const gchar *value)
Definition: fl_accessible_node.cc:556
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
g_object_add_weak_pointer
g_object_add_weak_pointer(G_OBJECT(self), reinterpret_cast< gpointer * >(&self->engine))
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:481
fl_view.h
G_DEFINE_TYPE
G_DEFINE_TYPE(FlViewAccessible, fl_view_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE) static void init_engine(FlViewAccessible *self
get_engine
static FlEngine * get_engine(FlViewAccessible *self)
Definition: fl_view_accessible.cc:33
_FlViewAccessible
Definition: fl_view_accessible.cc:11
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:79
fl_value.h
_FlViewAccessible::parent_instance
GtkContainerAccessible parent_instance
Definition: fl_view_accessible.cc:12
engine
FlEngine * engine
Definition: fl_view_accessible.cc:26
fl_accessible_node_set_name
void fl_accessible_node_set_name(FlAccessibleNode *self, const gchar *name)
Definition: fl_accessible_node.cc:525
fl_accessible_node_set_actions
void fl_accessible_node_set_actions(FlAccessibleNode *self, FlutterSemanticsAction actions)
Definition: fl_accessible_node.cc:549
value
uint8_t value
Definition: fl_standard_message_codec.cc:36
lookup_node
static FlAccessibleNode * lookup_node(FlViewAccessible *self, int32_t id)
Definition: fl_view_accessible.cc:52
fl_view_accessible_get_role
static AtkRole fl_view_accessible_get_role(AtkObject *accessible)
Definition: fl_view_accessible.cc:103
fl_view_accessible_class_init
static void fl_view_accessible_class_init(FlViewAccessibleClass *klass)
Definition: fl_view_accessible.cc:137