From 17f86df8bdd0892e8a56cf4e379b88d91cb815ca Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Mon, 5 Jun 2023 11:16:06 -0700 Subject: [PATCH] Migrate SemanticsBinding to onSemanticsActionEvent (#128254) Follow-up to https://github.com/flutter/engine/pull/42493. --- .../flutter/lib/src/foundation/binding.dart | 9 ++-- .../flutter/lib/src/semantics/binding.dart | 43 +++++-------------- .../test/semantics/semantics_owner_test.dart | 1 + packages/flutter_test/lib/src/window.dart | 23 ++-------- 4 files changed, 20 insertions(+), 56 deletions(-) diff --git a/packages/flutter/lib/src/foundation/binding.dart b/packages/flutter/lib/src/foundation/binding.dart index 3b19fc2cc8e..8532f6d64c5 100644 --- a/packages/flutter/lib/src/foundation/binding.dart +++ b/packages/flutter/lib/src/foundation/binding.dart @@ -221,11 +221,12 @@ abstract class BindingBase { /// [BindingBase], e.g., [ServicesBinding], [RendererBinding], and /// [WidgetsBinding]. Each of these bindings define behaviors that interact /// with a [ui.PlatformDispatcher], e.g., [ServicesBinding] registers - /// listeners with the [ChannelBuffers], and [RendererBinding] + /// listeners with the [ChannelBuffers], [RendererBinding] /// registers [ui.PlatformDispatcher.onMetricsChanged], - /// [ui.PlatformDispatcher.onTextScaleFactorChanged], - /// [ui.PlatformDispatcher.onSemanticsEnabledChanged], and - /// [ui.PlatformDispatcher.onSemanticsAction] handlers. + /// [ui.PlatformDispatcher.onTextScaleFactorChanged], and [SemanticsBinding] + /// registers [ui.PlatformDispatcher.onSemanticsEnabledChanged], + /// [ui.PlatformDispatcher.onSemanticsActionEvent], and + /// [ui.PlatformDispatcher.onAccessibilityFeaturesChanged] handlers. /// /// Each of these other bindings could individually access a /// [ui.PlatformDispatcher] statically, but that would preclude the ability to diff --git a/packages/flutter/lib/src/semantics/binding.dart b/packages/flutter/lib/src/semantics/binding.dart index eb2a6d739a3..c5d0a40eb55 100644 --- a/packages/flutter/lib/src/semantics/binding.dart +++ b/packages/flutter/lib/src/semantics/binding.dart @@ -2,14 +2,14 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:ui' as ui show AccessibilityFeatures, SemanticsAction, SemanticsUpdateBuilder; +import 'dart:ui' as ui show AccessibilityFeatures, SemanticsActionEvent, SemanticsUpdateBuilder; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'debug.dart'; -export 'dart:ui' show AccessibilityFeatures, SemanticsUpdateBuilder; +export 'dart:ui' show AccessibilityFeatures, SemanticsActionEvent, SemanticsUpdateBuilder; /// The glue between the semantics layer and the Flutter engine. mixin SemanticsBinding on BindingBase { @@ -20,7 +20,7 @@ mixin SemanticsBinding on BindingBase { _accessibilityFeatures = platformDispatcher.accessibilityFeatures; platformDispatcher ..onSemanticsEnabledChanged = _handleSemanticsEnabledChanged - ..onSemanticsAction = _handleSemanticsAction + ..onSemanticsActionEvent = _handleSemanticsActionEvent ..onAccessibilityFeaturesChanged = handleAccessibilityFeaturesChanged; _handleSemanticsEnabledChanged(); } @@ -111,12 +111,12 @@ mixin SemanticsBinding on BindingBase { } } - void _handleSemanticsAction(int id, ui.SemanticsAction action, ByteData? args) { - performSemanticsAction(SemanticsActionEvent( - nodeId: id, - type: action, - arguments: args != null ? const StandardMessageCodec().decodeMessage(args) : null, - )); + void _handleSemanticsActionEvent(ui.SemanticsActionEvent action) { + final Object? arguments = action.arguments; + final ui.SemanticsActionEvent decodedAction = arguments is ByteData + ? action.copyWith(arguments: const StandardMessageCodec().decodeMessage(arguments)) + : action; + performSemanticsAction(decodedAction); } /// Called whenever the platform requests an action to be performed on a @@ -130,9 +130,9 @@ mixin SemanticsBinding on BindingBase { /// perform the given `action` on the [SemanticsNode] specified by /// [SemanticsActionEvent.nodeId]. /// - /// See [dart:ui.PlatformDispatcher.onSemanticsAction]. + /// See [dart:ui.PlatformDispatcher.onSemanticsActionEvent]. @protected - void performSemanticsAction(SemanticsActionEvent action); + void performSemanticsAction(ui.SemanticsActionEvent action); /// The currently active set of [AccessibilityFeatures]. /// @@ -180,27 +180,6 @@ mixin SemanticsBinding on BindingBase { } } -/// An event to request a [SemanticsAction] of [type] to be performed on the -/// [SemanticsNode] identified by [nodeId]. -/// -/// Used by [SemanticsBinding.performSemanticsAction]. -@immutable -class SemanticsActionEvent { - /// Creates a [SemanticsActionEvent]. - /// - /// The [type] and [nodeId] are required. - const SemanticsActionEvent({required this.type, required this.nodeId, this.arguments}); - - /// The type of action to be performed. - final ui.SemanticsAction type; - - /// The id of the [SemanticsNode] on which the action is to be performed. - final int nodeId; - - /// Optional arguments for the action. - final Object? arguments; -} - /// A reference to the semantics information generated by the framework. /// /// Semantics information are only collected when there are clients interested diff --git a/packages/flutter/test/semantics/semantics_owner_test.dart b/packages/flutter/test/semantics/semantics_owner_test.dart index 4b87fa60eb0..18c93f5eb62 100644 --- a/packages/flutter/test/semantics/semantics_owner_test.dart +++ b/packages/flutter/test/semantics/semantics_owner_test.dart @@ -42,6 +42,7 @@ void main() { tester.binding.performSemanticsAction(SemanticsActionEvent( type: SemanticsAction.showOnScreen, nodeId: nodeId, + viewId: tester.view.viewId, )); semantics.dispose(); }); diff --git a/packages/flutter_test/lib/src/window.dart b/packages/flutter_test/lib/src/window.dart index 527dc0db31d..82ca7d9aabf 100644 --- a/packages/flutter_test/lib/src/window.dart +++ b/packages/flutter_test/lib/src/window.dart @@ -395,10 +395,10 @@ class TestPlatformDispatcher implements PlatformDispatcher { } @override - SemanticsActionCallback? get onSemanticsAction => _platformDispatcher.onSemanticsAction; + SemanticsActionEventCallback? get onSemanticsActionEvent => _platformDispatcher.onSemanticsActionEvent; @override - set onSemanticsAction(SemanticsActionCallback? callback) { - _platformDispatcher.onSemanticsAction = callback; + set onSemanticsActionEvent(SemanticsActionEventCallback? callback) { + _platformDispatcher.onSemanticsActionEvent = callback; } @override @@ -1880,23 +1880,6 @@ class TestWindow implements SingletonFlutterWindow { platformDispatcher.onSemanticsEnabledChanged = callback; } - @Deprecated( - 'Use WidgetTester.platformDispatcher.onSemanticsAction instead. ' - 'Deprecated to prepare for the upcoming multi-window support. ' - 'This feature was deprecated after v3.9.0-0.1.pre.' - ) - @override - SemanticsActionCallback? get onSemanticsAction => platformDispatcher.onSemanticsAction; - @Deprecated( - 'Use WidgetTester.platformDispatcher.onSemanticsAction instead. ' - 'Deprecated to prepare for the upcoming multi-window support. ' - 'This feature was deprecated after v3.9.0-0.1.pre.' - ) - @override - set onSemanticsAction(SemanticsActionCallback? callback) { - platformDispatcher.onSemanticsAction = callback; - } - @Deprecated( 'Use WidgetTester.platformDispatcher.accessibilityFeatures instead. ' 'Deprecated to prepare for the upcoming multi-window support. '