|
| 1 | +// Copyright 2014 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 | +import 'package:flutter/foundation.dart'; |
| 6 | +import 'package:flutter/rendering.dart'; |
| 7 | + |
| 8 | +import 'service_extensions.dart'; |
| 9 | + |
| 10 | +/// Service that handles accessibility and semantics inspection. |
| 11 | +class AccessibilityInspector { |
| 12 | + AccessibilityInspector._(); |
| 13 | + |
| 14 | + /// The active [AccessibilityInspector] instance. |
| 15 | + static final AccessibilityInspector instance = AccessibilityInspector._(); |
| 16 | + |
| 17 | + SemanticsHandle? _semanticsHandle; |
| 18 | + |
| 19 | + /// Registers accessibility-related VM service extensions. |
| 20 | + void initServiceExtensions( |
| 21 | + void Function({required String name, required ServiceExtensionCallback callback}) |
| 22 | + registerServiceExtension, |
| 23 | + ) { |
| 24 | + registerServiceExtension( |
| 25 | + name: AccessibilityServiceExtensions.getSemanticsTree.extensionName, |
| 26 | + callback: _getSemanticsTree, |
| 27 | + ); |
| 28 | + registerServiceExtension( |
| 29 | + name: AccessibilityServiceExtensions.enableSemantics.extensionName, |
| 30 | + callback: _enableSemantics, |
| 31 | + ); |
| 32 | + registerServiceExtension( |
| 33 | + name: AccessibilityServiceExtensions.disposeSemantics.extensionName, |
| 34 | + callback: _disposeSemantics, |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + /// Reset the helper state (primarily used in tests). |
| 39 | + @visibleForTesting |
| 40 | + void resetAllState() { |
| 41 | + _semanticsHandle?.dispose(); |
| 42 | + _semanticsHandle = null; |
| 43 | + } |
| 44 | + |
| 45 | + Future<Map<String, Object?>> _enableSemantics(Map<String, String> parameters) async { |
| 46 | + _semanticsHandle ??= SemanticsBinding.instance.ensureSemantics(); |
| 47 | + return const <String, Object?>{}; |
| 48 | + } |
| 49 | + |
| 50 | + Future<Map<String, Object?>> _disposeSemantics(Map<String, String> parameters) async { |
| 51 | + resetAllState(); |
| 52 | + return const <String, Object?>{}; |
| 53 | + } |
| 54 | + |
| 55 | + Future<Map<String, Object?>> _getSemanticsTree(Map<String, String> parameters) async { |
| 56 | + if (!SemanticsBinding.instance.semanticsEnabled) { |
| 57 | + return <String, Object?>{'error': 'Semantics not enabled.'}; |
| 58 | + } |
| 59 | + final PipelineOwner? pipelineOwner = _findPipelineOwner(); |
| 60 | + final SemanticsOwner? semanticsOwner = pipelineOwner?.semanticsOwner; |
| 61 | + if (semanticsOwner == null) { |
| 62 | + return <String, Object?>{'error': 'No PipelineOwner with SemanticsOwner found'}; |
| 63 | + } |
| 64 | + final SemanticsNode? root = semanticsOwner.rootSemanticsNode; |
| 65 | + if (root == null) { |
| 66 | + RendererBinding.instance.ensureVisualUpdate(); |
| 67 | + return <String, Object?>{'error': 'rootSemanticsNode is null', 'needsFrame': true}; |
| 68 | + } |
| 69 | + |
| 70 | + final nodes = <String, Object?>{}; |
| 71 | + final visited = <int>{}; |
| 72 | + final queue = <SemanticsNode>[root]; |
| 73 | + while (queue.isNotEmpty) { |
| 74 | + final SemanticsNode node = queue.removeLast(); |
| 75 | + if (!visited.add(node.id)) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + nodes[node.id.toString()] = node.toJson(); |
| 79 | + for (final SemanticsNode child in node.debugListChildrenInOrder( |
| 80 | + DebugSemanticsDumpOrder.traversalOrder, |
| 81 | + )) { |
| 82 | + if (!visited.contains(child.id)) { |
| 83 | + queue.add(child); |
| 84 | + } |
| 85 | + } |
| 86 | + for (final SemanticsNode child in node.debugListChildrenInOrder( |
| 87 | + DebugSemanticsDumpOrder.inverseHitTest, |
| 88 | + )) { |
| 89 | + if (!visited.contains(child.id)) { |
| 90 | + queue.add(child); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return <String, Object?>{'data': nodes}; |
| 96 | + } |
| 97 | + |
| 98 | + // TODO(hannahjin): This returns the first SemanticsOwner of any RenderView. |
| 99 | + // This getSemanticsTree feature is used in DevTools, which currently only supports |
| 100 | + // single-view inspection. Add multi-view support when DevTools needs it. |
| 101 | + PipelineOwner? _findPipelineOwner() { |
| 102 | + for (final RenderView renderView in RendererBinding.instance.renderViews) { |
| 103 | + if (renderView.owner?.semanticsOwner != null) { |
| 104 | + return renderView.owner; |
| 105 | + } |
| 106 | + } |
| 107 | + final PipelineOwner deprecatedOwner = RendererBinding.instance.pipelineOwner; |
| 108 | + if (deprecatedOwner.semanticsOwner != null) { |
| 109 | + return deprecatedOwner; |
| 110 | + } |
| 111 | + return null; |
| 112 | + } |
| 113 | +} |
0 commit comments