Repository navigation
[A11y] Connect the accessibility override controls to the app #10065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
victorsanni
wants to merge
4
commits into
flutter:master
Choose a base branch
from
victorsanni:a11y-override-controls
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f34f947
Connect the accessibility override controls to the app
victorsanni 2274ba0
Fix edge cases of the view metrics override client
victorsanni 9ea7b64
Merge branch 'master' into a11y-override-controls
victorsanni acdb58a
Restore what the page shows after a hot restart
victorsanni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,6 +12,7 @@ import 'package:logging/logging.dart'; | |||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| import '../../service/service_extensions.dart' as extensions; | ||||||||||||||||||||||||||
| import '../../service/service_registrations.dart' as registrations; | ||||||||||||||||||||||||||
| import '../../service/view_metrics_override_client.dart'; | ||||||||||||||||||||||||||
| import '../../shared/framework/screen.dart'; | ||||||||||||||||||||||||||
| import '../../shared/framework/screen_controllers.dart'; | ||||||||||||||||||||||||||
| import '../../shared/globals.dart'; | ||||||||||||||||||||||||||
|
|
@@ -46,15 +47,12 @@ class AccessibilityController extends DevToolsScreenController | |||||||||||||||||||||||||
| void init() { | ||||||||||||||||||||||||||
| super.init(); | ||||||||||||||||||||||||||
| _initServiceExtensionStates(); | ||||||||||||||||||||||||||
| _initViewMetricsOverrides(); | ||||||||||||||||||||||||||
| _initSemanticsTree(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| void _initListeners() { | ||||||||||||||||||||||||||
| addAutoDisposeListener(brightness, _onBrightnessChanged); | ||||||||||||||||||||||||||
| addAutoDisposeListener(textScale, _onTextScaleChanged); | ||||||||||||||||||||||||||
| addAutoDisposeListener(boldText, _onBoldTextChanged); | ||||||||||||||||||||||||||
| addAutoDisposeListener(screenReader, _onScreenReaderChanged); | ||||||||||||||||||||||||||
| addAutoDisposeListener(highContrast, _onHighContrastChanged); | ||||||||||||||||||||||||||
| addAutoDisposeListener(semanticsRoots, _onSemanticsRootsChanged); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -123,21 +121,100 @@ class AccessibilityController extends DevToolsScreenController | |||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| void _onTextScaleChanged() { | ||||||||||||||||||||||||||
| // TODO(hannah-hyj): Implement VM service extension call for text scale override. | ||||||||||||||||||||||||||
| // The metrics of `ext.flutter.viewMetricsOverride` that the overrides pane | ||||||||||||||||||||||||||
| // controls. Brightness stays on `ext.flutter.brightnessOverride`. | ||||||||||||||||||||||||||
| static const _textScalingMetric = 'textScaling'; | ||||||||||||||||||||||||||
| static const _boldTextMetric = 'boldText'; | ||||||||||||||||||||||||||
| static const _accessibleNavigationMetric = 'accessibleNavigation'; | ||||||||||||||||||||||||||
| static const _highContrastMetric = 'highContrast'; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /// The metrics that the overrides pane shows, which the client sets again | ||||||||||||||||||||||||||
| /// after a hot restart, so that the pane shows the same overrides as before | ||||||||||||||||||||||||||
| /// the restart. | ||||||||||||||||||||||||||
| static const _paneMetrics = <String>{ | ||||||||||||||||||||||||||
| _textScalingMetric, | ||||||||||||||||||||||||||
| _boldTextMetric, | ||||||||||||||||||||||||||
| _accessibleNavigationMetric, | ||||||||||||||||||||||||||
| _highContrastMetric, | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /// The client that overrides the text scale, bold text, screen reader and | ||||||||||||||||||||||||||
| /// high contrast settings of the app, or null before [init]. | ||||||||||||||||||||||||||
| ViewMetricsOverrideClient? _viewMetricsOverrideClient; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| void _initViewMetricsOverrides() { | ||||||||||||||||||||||||||
| final client = _viewMetricsOverrideClient = ViewMetricsOverrideClient( | ||||||||||||||||||||||||||
| restoredMetrics: _paneMetrics, | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| void updateFromApp() { | ||||||||||||||||||||||||||
| final overrides = client.overrides.value; | ||||||||||||||||||||||||||
| _overridesAvailable.value = client.available.value; | ||||||||||||||||||||||||||
| _textScale.value = _textScaleFactor(overrides[_textScalingMetric]); | ||||||||||||||||||||||||||
| _boldText.value = overrides[_boldTextMetric] == true; | ||||||||||||||||||||||||||
| _screenReader.value = overrides[_accessibleNavigationMetric] == true; | ||||||||||||||||||||||||||
| _highContrast.value = overrides[_highContrastMetric] == true; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| updateFromApp(); | ||||||||||||||||||||||||||
| addAutoDisposeListener(client.available, updateFromApp); | ||||||||||||||||||||||||||
| addAutoDisposeListener(client.overrides, updateFromApp); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| void _onBoldTextChanged() { | ||||||||||||||||||||||||||
| // TODO(hannah-hyj): Implement VM service extension call for bold text override. | ||||||||||||||||||||||||||
| /// Overrides the text scale factor of the app with [factor], or stops | ||||||||||||||||||||||||||
| /// overriding it if [factor] is null. | ||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||
| /// The override scales every font size linearly by [factor], which has to be | ||||||||||||||||||||||||||
| /// finite and not negative. | ||||||||||||||||||||||||||
| Future<void> setTextScale(double? factor) { | ||||||||||||||||||||||||||
| assert( | ||||||||||||||||||||||||||
| factor == null || (factor.isFinite && factor >= 0), | ||||||||||||||||||||||||||
| 'The text scale factor must be finite and not negative, got $factor.', | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| return _setOverride( | ||||||||||||||||||||||||||
| _textScalingMetric, | ||||||||||||||||||||||||||
| factor == null | ||||||||||||||||||||||||||
| ? null | ||||||||||||||||||||||||||
| : [ | ||||||||||||||||||||||||||
| [1, factor], | ||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| void _onScreenReaderChanged() { | ||||||||||||||||||||||||||
| // TODO(hannah-hyj): Implement VM service extension call for screen reader / semantics debugger. | ||||||||||||||||||||||||||
| // e.g. using 'ext.flutter.showSemanticsDebugger'. | ||||||||||||||||||||||||||
| /// Makes the app use bold text, or the bold text setting of the platform | ||||||||||||||||||||||||||
| /// again if [enabled] is false. | ||||||||||||||||||||||||||
| Future<void> setBoldText(bool enabled) => | ||||||||||||||||||||||||||
| _setFlagOverride(_boldTextMetric, enabled); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /// Makes the app behave as though a screen reader is on, or follow the | ||||||||||||||||||||||||||
| /// platform again if [enabled] is false. | ||||||||||||||||||||||||||
| Future<void> setScreenReader(bool enabled) => | ||||||||||||||||||||||||||
| _setFlagOverride(_accessibleNavigationMetric, enabled); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /// Makes the app use high contrast, or the high contrast setting of the | ||||||||||||||||||||||||||
| /// platform again if [enabled] is false. | ||||||||||||||||||||||||||
| Future<void> setHighContrast(bool enabled) => | ||||||||||||||||||||||||||
| _setFlagOverride(_highContrastMetric, enabled); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Turning an override off stops overriding the flag rather than overriding | ||||||||||||||||||||||||||
| // it to false, so that the app goes back to the setting of the platform. | ||||||||||||||||||||||||||
| Future<void> _setFlagOverride(String metric, bool enabled) => | ||||||||||||||||||||||||||
| _setOverride(metric, enabled ? true : null); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Future<void> _setOverride(String metric, Object? value) async { | ||||||||||||||||||||||||||
| await _viewMetricsOverrideClient?.updateOverrides({metric: value}); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| void _onHighContrastChanged() { | ||||||||||||||||||||||||||
| // TODO(hannah-hyj): Implement VM service extension call for high contrast override. | ||||||||||||||||||||||||||
| /// Returns the ratio of the first point of [textScaling], which is the text | ||||||||||||||||||||||||||
| /// scale factor that the app reports, or null if text scale is not | ||||||||||||||||||||||||||
| /// overridden. | ||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||
| /// Like the app, this only accepts a first point whose unscaled size is | ||||||||||||||||||||||||||
| /// greater than zero, and whose scaled size is not negative. | ||||||||||||||||||||||||||
| static double? _textScaleFactor(Object? textScaling) { | ||||||||||||||||||||||||||
| if (textScaling case [[final num unscaled, final num scaled], ...]) { | ||||||||||||||||||||||||||
| if (unscaled > 0 && scaled >= 0) return scaled / unscaled; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+213
to
218
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [CONCERN] Defensively check if
Suggested change
References
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||
|
|
@@ -147,10 +224,33 @@ class AccessibilityController extends DevToolsScreenController | |||||||||||||||||||||||||
| final brightness = ValueNotifier<BrightnessOverride>( | ||||||||||||||||||||||||||
| BrightnessOverride.system, | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| final textScale = ValueNotifier<double>(1.0); | ||||||||||||||||||||||||||
| final boldText = ValueNotifier<bool>(false); | ||||||||||||||||||||||||||
| final screenReader = ValueNotifier<bool>(false); | ||||||||||||||||||||||||||
| final highContrast = ValueNotifier<bool>(false); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /// Whether the connected app supports the text scale, bold text, screen | ||||||||||||||||||||||||||
| /// reader and high contrast overrides. | ||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||
| /// See [ViewMetricsOverrideClient.available] for the apps that do. | ||||||||||||||||||||||||||
| ValueListenable<bool> get overridesAvailable => _overridesAvailable; | ||||||||||||||||||||||||||
| final _overridesAvailable = ValueNotifier<bool>(false); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /// The text scale factor that the app is overridden to, or null if the app | ||||||||||||||||||||||||||
| /// uses the text scale that the platform reports. | ||||||||||||||||||||||||||
| ValueListenable<double?> get textScale => _textScale; | ||||||||||||||||||||||||||
| final _textScale = ValueNotifier<double?>(null); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /// Whether the app is overridden to use bold text. | ||||||||||||||||||||||||||
| ValueListenable<bool> get boldText => _boldText; | ||||||||||||||||||||||||||
| final _boldText = ValueNotifier<bool>(false); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /// Whether the app is overridden to behave as though a screen reader is on. | ||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||
| /// This overrides `AccessibilityFeatures.accessibleNavigation`, which | ||||||||||||||||||||||||||
| /// changes how widgets behave. It does not start a screen reader. | ||||||||||||||||||||||||||
| ValueListenable<bool> get screenReader => _screenReader; | ||||||||||||||||||||||||||
| final _screenReader = ValueNotifier<bool>(false); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /// Whether the app is overridden to use high contrast. | ||||||||||||||||||||||||||
| ValueListenable<bool> get highContrast => _highContrast; | ||||||||||||||||||||||||||
| final _highContrast = ValueNotifier<bool>(false); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| final semanticsRoots = ValueNotifier<List<SemanticsNodeModel>>([]); | ||||||||||||||||||||||||||
| final selectedSemanticsNode = ValueNotifier<SemanticsNodeModel?>(null); | ||||||||||||||||||||||||||
|
|
@@ -321,11 +421,13 @@ class AccessibilityController extends DevToolsScreenController | |||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||
| void dispose() { | ||||||||||||||||||||||||||
| unawaited(_disposeSemanticsOnApp()); | ||||||||||||||||||||||||||
| _viewMetricsOverrideClient?.dispose(); | ||||||||||||||||||||||||||
| brightness.dispose(); | ||||||||||||||||||||||||||
| textScale.dispose(); | ||||||||||||||||||||||||||
| boldText.dispose(); | ||||||||||||||||||||||||||
| screenReader.dispose(); | ||||||||||||||||||||||||||
| highContrast.dispose(); | ||||||||||||||||||||||||||
| _overridesAvailable.dispose(); | ||||||||||||||||||||||||||
| _textScale.dispose(); | ||||||||||||||||||||||||||
| _boldText.dispose(); | ||||||||||||||||||||||||||
| _screenReader.dispose(); | ||||||||||||||||||||||||||
| _highContrast.dispose(); | ||||||||||||||||||||||||||
| semanticsRoots.dispose(); | ||||||||||||||||||||||||||
| selectedSemanticsNode.dispose(); | ||||||||||||||||||||||||||
| semanticsTreeLoading.dispose(); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[CONCERN] Catching the exception here and pushing a notification via
notificationServiceprovides a much better user experience. It ensures that if the service extension call fails, the user is notified immediately rather than the UI silently failing to apply the override.References