From 23d258df574c53ab4b0427bd1bcd7155d00d1426 Mon Sep 17 00:00:00 2001 From: Alex Wallen Date: Wed, 26 Oct 2022 10:20:41 -1000 Subject: [PATCH] Remove deprecated `updateSemantics` API usage. (#113382) --- .../flutter/lib/src/rendering/binding.dart | 6 +++ .../flutter/lib/src/rendering/object.dart | 11 +++- packages/flutter/lib/src/rendering/view.dart | 11 +++- .../flutter/lib/src/semantics/semantics.dart | 24 +++++++-- .../flutter/test/rendering/object_test.dart | 52 +++++++++++++++++-- .../test/semantics/semantics_test.dart | 6 ++- packages/flutter_test/lib/src/window.dart | 12 ----- 7 files changed, 100 insertions(+), 22 deletions(-) diff --git a/packages/flutter/lib/src/rendering/binding.dart b/packages/flutter/lib/src/rendering/binding.dart index a9e0196063..95472d9859 100644 --- a/packages/flutter/lib/src/rendering/binding.dart +++ b/packages/flutter/lib/src/rendering/binding.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'dart:developer'; +import 'dart:ui' as ui show SemanticsUpdate; import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; @@ -31,6 +32,7 @@ mixin RendererBinding on BindingBase, ServicesBinding, SchedulerBinding, Gesture _pipelineOwner = PipelineOwner( onNeedVisualUpdate: ensureVisualUpdate, onSemanticsOwnerCreated: _handleSemanticsOwnerCreated, + onSemanticsUpdate: _handleSemanticsUpdate, onSemanticsOwnerDisposed: _handleSemanticsOwnerDisposed, ); platformDispatcher @@ -367,6 +369,10 @@ mixin RendererBinding on BindingBase, ServicesBinding, SchedulerBinding, Gesture renderView.scheduleInitialSemantics(); } + void _handleSemanticsUpdate(ui.SemanticsUpdate update) { + renderView.updateSemantics(update); + } + void _handleSemanticsOwnerDisposed() { renderView.clearSemantics(); } diff --git a/packages/flutter/lib/src/rendering/object.dart b/packages/flutter/lib/src/rendering/object.dart index dcae4692d7..167e6b332a 100644 --- a/packages/flutter/lib/src/rendering/object.dart +++ b/packages/flutter/lib/src/rendering/object.dart @@ -4,6 +4,7 @@ import 'dart:developer'; import 'dart:ui' as ui show PictureRecorder; +import 'dart:ui'; import 'package:flutter/animation.dart'; import 'package:flutter/foundation.dart'; @@ -894,6 +895,7 @@ class PipelineOwner { PipelineOwner({ this.onNeedVisualUpdate, this.onSemanticsOwnerCreated, + this.onSemanticsUpdate, this.onSemanticsOwnerDisposed, }); @@ -912,6 +914,12 @@ class PipelineOwner { /// semantics tree. final VoidCallback? onSemanticsOwnerCreated; + /// Called whenever this pipeline owner's semantics owner emits a [SemanticsUpdate]. + /// + /// Typical implementations will delegate the [SemanticsUpdate] to a [FlutterView] + /// that can handle the [SemanticsUpdate]. + final SemanticsUpdateCallback? onSemanticsUpdate; + /// Called whenever this pipeline owner disposes its semantics owner. /// /// Typical implementations will tear down the semantics tree. @@ -1183,7 +1191,8 @@ class PipelineOwner { _outstandingSemanticsHandles += 1; if (_outstandingSemanticsHandles == 1) { assert(_semanticsOwner == null); - _semanticsOwner = SemanticsOwner(); + assert(onSemanticsUpdate != null, 'Attempted to open a semantics handle without an onSemanticsUpdate callback.'); + _semanticsOwner = SemanticsOwner(onSemanticsUpdate: onSemanticsUpdate!); onSemanticsOwnerCreated?.call(); } return SemanticsHandle._(this, listener); diff --git a/packages/flutter/lib/src/rendering/view.dart b/packages/flutter/lib/src/rendering/view.dart index da3bf44d4f..45be360983 100644 --- a/packages/flutter/lib/src/rendering/view.dart +++ b/packages/flutter/lib/src/rendering/view.dart @@ -4,7 +4,7 @@ import 'dart:developer'; import 'dart:io' show Platform; -import 'dart:ui' as ui show FlutterView, Scene, SceneBuilder; +import 'dart:ui' as ui show FlutterView, Scene, SceneBuilder, SemanticsUpdate; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; @@ -247,6 +247,15 @@ class RenderView extends RenderObject with RenderObjectWithChildMixin } } + /// Sends the provided [SemanticsUpdate] to the [FlutterView] associated with + /// this [RenderView]. + /// + /// A [SemanticsUpdate] is produced by a [SemanticsOwner] during the + /// [EnginePhase.flushSemantics] phase. + void updateSemantics(ui.SemanticsUpdate update) { + _window.updateSemantics(update); + } + void _updateSystemChrome() { // Take overlay style from the place where a system status bar and system // navigation bar are placed to update system style overlay. diff --git a/packages/flutter/lib/src/semantics/semantics.dart b/packages/flutter/lib/src/semantics/semantics.dart index d60a72231e..0ada4ca7c5 100644 --- a/packages/flutter/lib/src/semantics/semantics.dart +++ b/packages/flutter/lib/src/semantics/semantics.dart @@ -48,6 +48,11 @@ typedef SetTextHandler = void Function(String text); /// Returned by [SemanticsConfiguration.getActionHandler]. typedef SemanticsActionHandler = void Function(Object? args); +/// Signature for a function that receives a semantics update and returns no result. +/// +/// Used by [SemanticsOwner.onSemanticsUpdate]. +typedef SemanticsUpdateCallback = void Function(ui.SemanticsUpdate update); + /// A tag for a [SemanticsNode]. /// /// Tags can be interpreted by the parent of a [SemanticsNode] @@ -3052,6 +3057,19 @@ class _TraversalSortNode implements Comparable<_TraversalSortNode> { /// obtain a [SemanticsHandle]. This will create a [SemanticsOwner] if /// necessary. class SemanticsOwner extends ChangeNotifier { + /// Creates a [SemanticsOwner] that manages zero or more [SemanticsNode] objects. + SemanticsOwner({ + required this.onSemanticsUpdate, + }); + + /// The [onSemanticsUpdate] callback is expected to dispatch [SemanticsUpdate]s + /// to the [FlutterView] that is associated with this [PipelineOwner] and/or + /// [SemanticsOwner]. + /// + /// A [SemanticsOwner] calls [onSemanticsUpdate] during [sendSemanticsUpdate] + /// after the [SemanticsUpdate] has been build, but before the [SemanticsOwner]'s + /// listeners have been notified. + final SemanticsUpdateCallback onSemanticsUpdate; final Set _dirtyNodes = {}; final Map _nodes = {}; final Set _detachedNodes = {}; @@ -3069,7 +3087,7 @@ class SemanticsOwner extends ChangeNotifier { super.dispose(); } - /// Update the semantics using [dart:ui.PlatformDispatcher.updateSemantics]. + /// Update the semantics using [onSemanticsUpdate]. void sendSemanticsUpdate() { if (_dirtyNodes.isEmpty) { return; @@ -3118,9 +3136,7 @@ class SemanticsOwner extends ChangeNotifier { final CustomSemanticsAction action = CustomSemanticsAction.getAction(actionId)!; builder.updateCustomAction(id: actionId, label: action.label, hint: action.hint, overrideId: action.action?.index ?? -1); } - // TODO(a-wallen): https://github.com/flutter/flutter/issues/112221 - // ignore: deprecated_member_use - SemanticsBinding.instance.platformDispatcher.updateSemantics(builder.build()); + onSemanticsUpdate(builder.build()); notifyListeners(); } diff --git a/packages/flutter/test/rendering/object_test.dart b/packages/flutter/test/rendering/object_test.dart index 5049405560..150d07c662 100644 --- a/packages/flutter/test/rendering/object_test.dart +++ b/packages/flutter/test/rendering/object_test.dart @@ -18,9 +18,12 @@ void main() { // Initialize all bindings because owner.flushSemantics() requires a window final TestRenderObject renderObject = TestRenderObject(); int onNeedVisualUpdateCallCount = 0; - final PipelineOwner owner = PipelineOwner(onNeedVisualUpdate: () { - onNeedVisualUpdateCallCount +=1; - }); + final PipelineOwner owner = PipelineOwner( + onNeedVisualUpdate: () { + onNeedVisualUpdateCallCount +=1; + }, + onSemanticsUpdate: (ui.SemanticsUpdate update) {} + ); owner.ensureSemantics(); renderObject.attach(owner); renderObject.layout(const BoxConstraints.tightForFinite()); // semantics are only calculated if layout information is up to date. @@ -31,6 +34,49 @@ void main() { expect(onNeedVisualUpdateCallCount, 2); }); + test('onSemanticsUpdate is called during flushSemantics.', () { + int onSemanticsUpdateCallCount = 0; + final PipelineOwner owner = PipelineOwner( + onSemanticsUpdate: (ui.SemanticsUpdate update) { + onSemanticsUpdateCallCount += 1; + }, + ); + owner.ensureSemantics(); + + expect(onSemanticsUpdateCallCount, 0); + + final TestRenderObject renderObject = TestRenderObject(); + renderObject.attach(owner); + renderObject.layout(const BoxConstraints.tightForFinite()); + owner.flushSemantics(); + + expect(onSemanticsUpdateCallCount, 1); + }); + + test('Enabling semantics without configuring onSemanticsUpdate is invalid.', () { + final PipelineOwner pipelineOwner = PipelineOwner(); + expect(() => pipelineOwner.ensureSemantics(), throwsAssertionError); + }); + + + test('onSemanticsUpdate during sendSemanticsUpdate.', () { + int onSemanticsUpdateCallCount = 0; + final SemanticsOwner owner = SemanticsOwner( + onSemanticsUpdate: (ui.SemanticsUpdate update) { + onSemanticsUpdateCallCount += 1; + }, + ); + + final SemanticsNode node = SemanticsNode.root(owner: owner); + node.rect = Rect.largest; + + expect(onSemanticsUpdateCallCount, 0); + + owner.sendSemanticsUpdate(); + + expect(onSemanticsUpdateCallCount, 1); + }); + test('detached RenderObject does not do semantics', () { final TestRenderObject renderObject = TestRenderObject(); expect(renderObject.attached, isFalse); diff --git a/packages/flutter/test/semantics/semantics_test.dart b/packages/flutter/test/semantics/semantics_test.dart index 89f6d73339..85916b0594 100644 --- a/packages/flutter/test/semantics/semantics_test.dart +++ b/packages/flutter/test/semantics/semantics_test.dart @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:ui'; + import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:vector_math/vector_math_64.dart'; @@ -698,7 +700,9 @@ void main() { }); test('Semantics id does not repeat', () { - final SemanticsOwner owner = SemanticsOwner(); + final SemanticsOwner owner = SemanticsOwner( + onSemanticsUpdate: (SemanticsUpdate update) {}, + ); const int expectId = 1400; SemanticsNode? nodeToRemove; for (int i = 0; i < kMaxFrameworkAccessibilityIdentifier; i++) { diff --git a/packages/flutter_test/lib/src/window.dart b/packages/flutter_test/lib/src/window.dart index 9207367d49..9fdaf2319f 100644 --- a/packages/flutter_test/lib/src/window.dart +++ b/packages/flutter_test/lib/src/window.dart @@ -463,11 +463,6 @@ class TestWindow implements ui.SingletonFlutterWindow { platformDispatcher.onAccessibilityFeaturesChanged = callback; } - @override - void updateSemantics(ui.SemanticsUpdate update) { - platformDispatcher.updateSemantics(update); - } - @override void setIsolateDebugName(String name) { platformDispatcher.setIsolateDebugName(name); @@ -851,13 +846,6 @@ class TestPlatformDispatcher implements ui.PlatformDispatcher { _platformDispatcher.onAccessibilityFeaturesChanged = callback; } - @override - void updateSemantics(ui.SemanticsUpdate update) { - // TODO(a-wallen): https://github.com/flutter/flutter/issues/112221 - // ignore: deprecated_member_use - _platformDispatcher.updateSemantics(update); - } - @override void setIsolateDebugName(String name) { _platformDispatcher.setIsolateDebugName(name);