Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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});
}
Comment on lines +203 to 205

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

[CONCERN] Catching the exception here and pushing a notification via notificationService provides 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.

Suggested change
Future<void> _setOverride(String metric, Object? value) async {
await _viewMetricsOverrideClient?.updateOverrides({metric: value});
}
Future<void> _setOverride(String metric, Object? value) async {
try {
await _viewMetricsOverrideClient?.updateOverrides({metric: value});
} catch (e) {
notificationService.push('Failed to set override: $e');
}
}
References
  1. Prioritize logic, performance on the UI thread, and architectural consistency. (link)


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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

[CONCERN] Defensively check if unscaled is zero to avoid division by zero, which would result in double.infinity or double.nan and potentially cause layout or rendering issues in the UI.

Suggested change
static double? _textScaleFactor(Object? textScaling) {
if (textScaling case [[final num unscaled, final num scaled], ...]) {
return scaled / unscaled;
}
return null;
}
static double? _textScaleFactor(Object? textScaling) {
if (textScaling case [[final num unscaled, final num scaled], ...]) {
return unscaled == 0 ? null : scaled / unscaled;
}
return null;
}
References
  1. Prioritize logic, performance on the UI thread, and architectural consistency. (link)


@override
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
Loading
Loading