|
| 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_accessibility_semantics_store.h" |
| 6 | + |
| 7 | +#include <cstring> |
| 8 | + |
| 9 | +#include "flutter/shell/platform/linux/fl_glib_compat.h" |
| 10 | + |
| 11 | +struct _FlAccessibilitySemanticsStore { |
| 12 | + GObject parent_instance; |
| 13 | + |
| 14 | + FlutterViewId view_id; |
| 15 | + GHashTable* nodes_by_id; |
| 16 | + gboolean root_node_present; |
| 17 | +}; |
| 18 | + |
| 19 | +G_DEFINE_TYPE(FlAccessibilitySemanticsStore, |
| 20 | + fl_accessibility_semantics_store, |
| 21 | + G_TYPE_OBJECT) |
| 22 | + |
| 23 | +static void fl_accessibility_semantics_node_free(gpointer data) { |
| 24 | + FlAccessibilitySemanticsNode* node = |
| 25 | + static_cast<FlAccessibilitySemanticsNode*>(data); |
| 26 | + g_free(node->label); |
| 27 | + g_free(node->value); |
| 28 | + g_free(node->children_in_traversal_order); |
| 29 | + g_free(node); |
| 30 | +} |
| 31 | + |
| 32 | +static FlAccessibilitySemanticsNode* fl_accessibility_semantics_node_new( |
| 33 | + FlutterSemanticsNode2* semantics) { |
| 34 | + FlAccessibilitySemanticsNode* node = g_new0(FlAccessibilitySemanticsNode, 1); |
| 35 | + node->id = semantics->id; |
| 36 | + node->label = g_strdup(semantics->label); |
| 37 | + node->value = g_strdup(semantics->value); |
| 38 | + if (semantics->flags2 != nullptr) { |
| 39 | + node->flags = *semantics->flags2; |
| 40 | + } else { |
| 41 | + memset(&node->flags, 0, sizeof(node->flags)); |
| 42 | + } |
| 43 | + node->actions = semantics->actions; |
| 44 | + node->text_selection_base = semantics->text_selection_base; |
| 45 | + node->text_selection_extent = semantics->text_selection_extent; |
| 46 | + node->text_direction = semantics->text_direction; |
| 47 | + node->rect = semantics->rect; |
| 48 | + node->transform = semantics->transform; |
| 49 | + node->child_count = semantics->child_count; |
| 50 | + if (semantics->child_count > 0 && |
| 51 | + semantics->children_in_traversal_order != nullptr) { |
| 52 | + node->children_in_traversal_order = static_cast<int32_t*>( |
| 53 | + g_memdup2(semantics->children_in_traversal_order, |
| 54 | + sizeof(int32_t) * semantics->child_count)); |
| 55 | + } |
| 56 | + |
| 57 | + return node; |
| 58 | +} |
| 59 | + |
| 60 | +static void fl_accessibility_semantics_store_dispose(GObject* object) { |
| 61 | + FlAccessibilitySemanticsStore* self = |
| 62 | + FL_ACCESSIBILITY_SEMANTICS_STORE(object); |
| 63 | + |
| 64 | + g_clear_pointer(&self->nodes_by_id, g_hash_table_unref); |
| 65 | + |
| 66 | + G_OBJECT_CLASS(fl_accessibility_semantics_store_parent_class) |
| 67 | + ->dispose(object); |
| 68 | +} |
| 69 | + |
| 70 | +static void fl_accessibility_semantics_store_class_init( |
| 71 | + FlAccessibilitySemanticsStoreClass* klass) { |
| 72 | + GObjectClass* object_class = G_OBJECT_CLASS(klass); |
| 73 | + object_class->dispose = fl_accessibility_semantics_store_dispose; |
| 74 | +} |
| 75 | + |
| 76 | +static void fl_accessibility_semantics_store_init( |
| 77 | + FlAccessibilitySemanticsStore* self) { |
| 78 | + self->nodes_by_id = |
| 79 | + g_hash_table_new_full(g_direct_hash, g_direct_equal, nullptr, |
| 80 | + fl_accessibility_semantics_node_free); |
| 81 | +} |
| 82 | + |
| 83 | +FlAccessibilitySemanticsStore* fl_accessibility_semantics_store_new( |
| 84 | + FlutterViewId view_id) { |
| 85 | + FlAccessibilitySemanticsStore* self = FL_ACCESSIBILITY_SEMANTICS_STORE( |
| 86 | + g_object_new(fl_accessibility_semantics_store_get_type(), nullptr)); |
| 87 | + self->view_id = view_id; |
| 88 | + return self; |
| 89 | +} |
| 90 | + |
| 91 | +void fl_accessibility_semantics_store_handle_update( |
| 92 | + FlAccessibilitySemanticsStore* self, |
| 93 | + const FlutterSemanticsUpdate2* update) { |
| 94 | + g_return_if_fail(FL_IS_ACCESSIBILITY_SEMANTICS_STORE(self)); |
| 95 | + g_return_if_fail(update != nullptr); |
| 96 | + |
| 97 | + if (update->view_id != self->view_id) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + for (size_t i = 0; i < update->node_count; i++) { |
| 102 | + FlutterSemanticsNode2* semantics = update->nodes[i]; |
| 103 | + FlAccessibilitySemanticsNode* node = |
| 104 | + fl_accessibility_semantics_node_new(semantics); |
| 105 | + g_hash_table_replace(self->nodes_by_id, GINT_TO_POINTER(node->id), node); |
| 106 | + if (node->id == 0) { |
| 107 | + self->root_node_present = TRUE; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +const FlAccessibilitySemanticsNode* |
| 113 | +fl_accessibility_semantics_store_lookup_node( |
| 114 | + FlAccessibilitySemanticsStore* self, |
| 115 | + int32_t node_id) { |
| 116 | + g_return_val_if_fail(FL_IS_ACCESSIBILITY_SEMANTICS_STORE(self), nullptr); |
| 117 | + |
| 118 | + return static_cast<const FlAccessibilitySemanticsNode*>( |
| 119 | + g_hash_table_lookup(self->nodes_by_id, GINT_TO_POINTER(node_id))); |
| 120 | +} |
| 121 | + |
| 122 | +gboolean fl_accessibility_semantics_store_has_root( |
| 123 | + FlAccessibilitySemanticsStore* self) { |
| 124 | + g_return_val_if_fail(FL_IS_ACCESSIBILITY_SEMANTICS_STORE(self), FALSE); |
| 125 | + |
| 126 | + return self->root_node_present; |
| 127 | +} |
0 commit comments