This commit is contained in:
nbayati
2022-07-06 21:33:28 -07:00
committed by GitHub
parent e66e83671f
commit f03c0fcb00
4 changed files with 238 additions and 32 deletions

View File

@@ -4,6 +4,7 @@
import 'dart:async';
import 'dart:convert';
import 'dart:html' as html;
import 'dart:typed_data';
import 'package:meta/meta.dart';
@@ -33,8 +34,48 @@ import 'window.dart';
/// This may be overridden in tests, for example, to pump fake frames.
ui.VoidCallback? scheduleFrameCallback;
/// Signature of functions added as a listener to high contrast changes
typedef HighContrastListener = void Function(bool enabled);
typedef _KeyDataResponseCallback = void Function(bool handled);
/// Determines if high contrast is enabled using media query 'forced-colors: active' for Windows
class HighContrastSupport {
static HighContrastSupport instance = HighContrastSupport();
static const String _highContrastMediaQueryString = '(forced-colors: active)';
final List<HighContrastListener> _listeners = <HighContrastListener>[];
/// Reference to css media query that indicates whether high contrast is on.
final html.MediaQueryList _highContrastMediaQuery = html.window.matchMedia(_highContrastMediaQueryString);
bool get isHighContrastEnabled => _highContrastMediaQuery.matches;
/// Adds function to the list of listeners on high contrast changes
void addListener(HighContrastListener listener) {
if (_listeners.isEmpty) {
_highContrastMediaQuery.addListener(_onHighContrastChange);
}
_listeners.add(listener);
}
/// Removes function from the list of listeners on high contrast changes
void removeListener(HighContrastListener listener) {
_listeners.remove(listener);
if (_listeners.isEmpty) {
_highContrastMediaQuery.removeListener(_onHighContrastChange);
}
}
void _onHighContrastChange(html.Event event) {
final html.MediaQueryListEvent mqEvent = event as html.MediaQueryListEvent;
final bool isHighContrastEnabled = mqEvent.matches!;
for (final HighContrastListener listener in _listeners) {
listener(isHighContrastEnabled);
}
}
}
/// Platform event dispatcher.
///
/// This is the central entry point for platform messages and configuration
@@ -42,23 +83,41 @@ typedef _KeyDataResponseCallback = void Function(bool handled);
class EnginePlatformDispatcher extends ui.PlatformDispatcher {
/// Private constructor, since only dart:ui is supposed to create one of
/// these.
EnginePlatformDispatcher._() {
EnginePlatformDispatcher() {
_addBrightnessMediaQueryListener();
HighContrastSupport.instance.addListener(_updateHighContrast);
_addFontSizeObserver();
registerHotRestartListener(dispose);
}
/// The [EnginePlatformDispatcher] singleton.
static EnginePlatformDispatcher get instance => _instance;
static final EnginePlatformDispatcher _instance =
EnginePlatformDispatcher._();
static final EnginePlatformDispatcher _instance = EnginePlatformDispatcher();
/// The current platform configuration.
@override
ui.PlatformConfiguration configuration = ui.PlatformConfiguration(
locales: parseBrowserLanguages(),
textScaleFactor: findBrowserTextScaleFactor(),
accessibilityFeatures: computeAccessibilityFeatures(),
);
/// Compute accessibility features based on the current value of high contrast flag
static EngineAccessibilityFeatures computeAccessibilityFeatures() {
final EngineAccessibilityFeaturesBuilder builder =
EngineAccessibilityFeaturesBuilder(0);
if (HighContrastSupport.instance.isHighContrastEnabled) {
builder.highContrast = true;
}
return builder.build();
}
void dispose() {
_removeBrightnessMediaQueryListener();
_disconnectFontSizeObserver();
HighContrastSupport.instance.removeListener(_updateHighContrast);
}
/// Receives all events related to platform configuration changes.
@override
ui.VoidCallback? get onPlatformConfigurationChanged =>
@@ -821,9 +880,6 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
attributes: true,
attributeFilter: <String>[styleAttribute],
);
registerHotRestartListener(() {
_disconnectFontSizeObserver();
});
}
/// Remove the observer for font-size changes in the browser's <html> element.
@@ -885,6 +941,18 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
@override
String? get systemFontFamily => configuration.systemFontFamily;
/// Updates [_highContrast] and invokes [onHighContrastModeChanged]
/// callback if [_highContrast] changed.
void _updateHighContrast(bool value) {
if (configuration.accessibilityFeatures.highContrast != value) {
final EngineAccessibilityFeatures original =
configuration.accessibilityFeatures as EngineAccessibilityFeatures;
configuration = configuration.copyWith(
accessibilityFeatures: original.copyWith(highContrast: value));
invokeOnPlatformConfigurationChanged();
}
}
/// Reference to css media query that indicates the user theme preference on the web.
final DomMediaQueryList _brightnessMediaQuery =
domWindow.matchMedia('(prefers-color-scheme: dark)');
@@ -907,9 +975,6 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
mqEvent.matches! ? ui.Brightness.dark : ui.Brightness.light);
});
_brightnessMediaQuery.addListener(_brightnessMediaQueryListener);
registerHotRestartListener(() {
_removeBrightnessMediaQueryListener();
});
}
/// Remove the callback function for listening changes in [_brightnessMediaQuery] value.

View File

@@ -102,29 +102,71 @@ class EngineAccessibilityFeatures implements ui.AccessibilityFeatures {
bool? highContrast,
bool? onOffSwitchLabels})
{
int value = 0;
if (accessibleNavigation ?? this.accessibleNavigation) {
value = value | _kAccessibleNavigation;
}
if (invertColors ?? this.invertColors) {
value = value | _kInvertColorsIndex;
}
if (disableAnimations ?? this.disableAnimations) {
value = value | _kDisableAnimationsIndex;
}
if (boldText ?? this.boldText) {
value = value | _kBoldTextIndex;
}
if (reduceMotion ?? this.reduceMotion) {
value = value | _kReduceMotionIndex;
}
if (highContrast ?? this.highContrast) {
value = value | _kHighContrastIndex;
}
if (onOffSwitchLabels ?? this.onOffSwitchLabels) {
value = value | _kOnOffSwitchLabelsIndex;
}
return EngineAccessibilityFeatures(value);
final EngineAccessibilityFeaturesBuilder builder = EngineAccessibilityFeaturesBuilder(0);
builder.accessibleNavigation = accessibleNavigation ?? this.accessibleNavigation;
builder.invertColors = invertColors ?? this.invertColors;
builder.disableAnimations = disableAnimations ?? this.disableAnimations;
builder.boldText = boldText ?? this.boldText;
builder.reduceMotion = reduceMotion ?? this.reduceMotion;
builder.highContrast = highContrast ?? this.highContrast;
builder.onOffSwitchLabels = onOffSwitchLabels ?? this.onOffSwitchLabels;
return builder.build();
}
}
class EngineAccessibilityFeaturesBuilder {
EngineAccessibilityFeaturesBuilder(this._index);
int _index = 0;
bool get accessibleNavigation => EngineAccessibilityFeatures._kAccessibleNavigation & _index != 0;
bool get invertColors => EngineAccessibilityFeatures._kInvertColorsIndex & _index != 0;
bool get disableAnimations => EngineAccessibilityFeatures._kDisableAnimationsIndex & _index != 0;
bool get boldText => EngineAccessibilityFeatures._kBoldTextIndex & _index != 0;
bool get reduceMotion => EngineAccessibilityFeatures._kReduceMotionIndex & _index != 0;
bool get highContrast => EngineAccessibilityFeatures._kHighContrastIndex & _index != 0;
bool get onOffSwitchLabels => EngineAccessibilityFeatures._kOnOffSwitchLabelsIndex & _index != 0;
set accessibleNavigation(bool value) {
const int accessibleNavigation = EngineAccessibilityFeatures._kAccessibleNavigation;
_index = value? _index | accessibleNavigation : _index & ~accessibleNavigation;
}
set invertColors(bool value) {
const int invertColors = EngineAccessibilityFeatures._kInvertColorsIndex;
_index = value? _index | invertColors : _index & ~invertColors;
}
set disableAnimations(bool value) {
const int disableAnimations = EngineAccessibilityFeatures._kDisableAnimationsIndex;
_index = value? _index | disableAnimations : _index & ~disableAnimations;
}
set boldText(bool value) {
const int boldText = EngineAccessibilityFeatures._kBoldTextIndex;
_index = value? _index | boldText : _index & ~boldText;
}
set reduceMotion(bool value) {
const int reduceMotion = EngineAccessibilityFeatures._kReduceMotionIndex;
_index = value? _index | reduceMotion : _index & ~reduceMotion;
}
set highContrast(bool value) {
const int highContrast = EngineAccessibilityFeatures._kHighContrastIndex;
_index = value? _index | highContrast : _index & ~highContrast;
}
set onOffSwitchLabels(bool value) {
const int onOffSwitchLabels = EngineAccessibilityFeatures._kOnOffSwitchLabelsIndex;
_index = value? _index | onOffSwitchLabels : _index & ~onOffSwitchLabels;
}
/// Creates and returns an instance of EngineAccessibilityFeatures based on the value of _index
EngineAccessibilityFeatures build() {
return EngineAccessibilityFeatures(_index);
}
}

View File

@@ -17,6 +17,21 @@ void main() {
void testMain() {
group('PlatformDispatcher', () {
test('high contrast in accessibilityFeatures has the correct value', () {
final MockHighContrastSupport mockHighContrast =
MockHighContrastSupport();
HighContrastSupport.instance = mockHighContrast;
final EnginePlatformDispatcher engineDispatcher =
EnginePlatformDispatcher();
expect(engineDispatcher.accessibilityFeatures.highContrast, isTrue);
mockHighContrast.isEnabled = false;
mockHighContrast.invokeListeners(mockHighContrast.isEnabled);
expect(engineDispatcher.accessibilityFeatures.highContrast, isFalse);
engineDispatcher.dispose();
});
test('responds to flutter/skia Skia.setResourceCacheMaxBytes', () async {
const MethodCodec codec = JSONMethodCodec();
final Completer<ByteData?> completer = Completer<ByteData?>();
@@ -146,3 +161,29 @@ void testMain() {
});
});
}
class MockHighContrastSupport implements HighContrastSupport {
bool isEnabled = true;
final List<HighContrastListener> _listeners = <HighContrastListener>[];
@override
bool get isHighContrastEnabled => isEnabled;
void invokeListeners(bool val) {
for (final HighContrastListener listener in _listeners) {
listener(val);
}
}
@override
void addListener(HighContrastListener listener) {
_listeners.add(listener);
}
@override
void removeListener(HighContrastListener listener) {
_listeners.remove(listener);
}
}

View File

@@ -75,6 +75,64 @@ void runSemanticsTests() {
group('platform view', () {
_testPlatformView();
});
group('accessibility builder', () {
_testEngineAccessibilityBuilder();
});
}
void _testEngineAccessibilityBuilder() {
final EngineAccessibilityFeaturesBuilder builder =
EngineAccessibilityFeaturesBuilder(0);
EngineAccessibilityFeatures features = builder.build();
test('accessible navigation', () {
expect(features.accessibleNavigation, isFalse);
builder.accessibleNavigation = true;
features = builder.build();
expect(features.accessibleNavigation, isTrue);
});
test('bold text', () {
expect(features.boldText, isFalse);
builder.boldText = true;
features = builder.build();
expect(features.boldText, isTrue);
});
test('disable animations', () {
expect(features.disableAnimations, isFalse);
builder.disableAnimations = true;
features = builder.build();
expect(features.disableAnimations, isTrue);
});
test('high contrast', () {
expect(features.highContrast, isFalse);
builder.highContrast = true;
features = builder.build();
expect(features.highContrast, isTrue);
});
test('invert colors', () {
expect(features.invertColors, isFalse);
builder.invertColors = true;
features = builder.build();
expect(features.invertColors, isTrue);
});
test('on off switch labels', () {
expect(features.onOffSwitchLabels, isFalse);
builder.onOffSwitchLabels = true;
features = builder.build();
expect(features.onOffSwitchLabels, isTrue);
});
test('reduce motion', () {
expect(features.reduceMotion, isFalse);
builder.reduceMotion = true;
features = builder.build();
expect(features.reduceMotion, isTrue);
});
}
void _testEngineSemanticsOwner() {