Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 58e7e56

Browse files
RtgrVRutgerVromans
authored andcommitted
Access on/off labels settings for switches on iOS
1 parent 196a8c3 commit 58e7e56

File tree

9 files changed

+127
-13
lines changed

9 files changed

+127
-13
lines changed

lib/ui/window.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,7 @@ class AccessibilityFeatures {
771771
static const int _kBoldTextIndex = 1 << 3;
772772
static const int _kReduceMotionIndex = 1 << 4;
773773
static const int _kHighContrastIndex = 1 << 5;
774+
static const int _kOnOffSwitchLabelsIndex = 1 << 6;
774775

775776
// A bitfield which represents each enabled feature.
776777
final int _index;
@@ -803,6 +804,11 @@ class AccessibilityFeatures {
803804
/// Only supported on iOS.
804805
bool get highContrast => _kHighContrastIndex & _index != 0;
805806

807+
/// The platform is requesting to show on/off labels inside switches.
808+
///
809+
/// Only supported on iOS.
810+
bool get onOffSwitchLabels => _kOnOffSwitchLabelsIndex & _index != 0;
811+
806812
@override
807813
String toString() {
808814
final List<String> features = <String>[];
@@ -818,6 +824,8 @@ class AccessibilityFeatures {
818824
features.add('reduceMotion');
819825
if (highContrast)
820826
features.add('highContrast');
827+
if (onOffSwitchLabels)
828+
features.add('onOffSwitchLabels');
821829
return 'AccessibilityFeatures$features';
822830
}
823831

lib/ui/window/platform_configuration.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ enum class AccessibilityFeatureFlag : int32_t {
3535
kBoldText = 1 << 3,
3636
kReduceMotion = 1 << 4,
3737
kHighContrast = 1 << 5,
38+
kOnOffSwitchLabels = 1 << 6,
3839
};
3940

4041
//--------------------------------------------------------------------------

lib/web_ui/lib/window.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ class AccessibilityFeatures {
148148
static const int _kBoldTextIndex = 1 << 3;
149149
static const int _kReduceMotionIndex = 1 << 4;
150150
static const int _kHighContrastIndex = 1 << 5;
151+
static const int _kOnOffSwitchLabelsIndex = 1 << 6;
151152

152153
// A bitfield which represents each enabled feature.
153154
final int _index;
@@ -158,6 +159,7 @@ class AccessibilityFeatures {
158159
bool get boldText => _kBoldTextIndex & _index != 0;
159160
bool get reduceMotion => _kReduceMotionIndex & _index != 0;
160161
bool get highContrast => _kHighContrastIndex & _index != 0;
162+
bool get onOffSwitchLabels => _kOnOffSwitchLabelsIndex & _index != 0;
161163

162164
@override
163165
String toString() {
@@ -180,6 +182,9 @@ class AccessibilityFeatures {
180182
if (highContrast) {
181183
features.add('highContrast');
182184
}
185+
if (onOffSwitchLabels) {
186+
features.add('onOffSwitchLabels');
187+
}
183188
return 'AccessibilityFeatures$features';
184189
}
185190

shell/platform/darwin/ios/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ source_set("flutter_framework_source") {
9696
"framework/Source/accessibility_bridge.mm",
9797
"framework/Source/accessibility_text_entry.h",
9898
"framework/Source/accessibility_text_entry.mm",
99+
"framework/Source/accessibility_value_provider.h",
100+
"framework/Source/accessibility_value_provider.mm",
99101
"framework/Source/connection_collection.h",
100102
"framework/Source/connection_collection.mm",
101103
"framework/Source/platform_message_response_darwin.h",

shell/platform/darwin/ios/framework/Source/FlutterViewController.mm

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,13 @@ - (void)setupNotificationCenterObservers {
331331
name:UIAccessibilityDarkerSystemColorsStatusDidChangeNotification
332332
object:nil];
333333

334+
if (@available(iOS 13.0, *)) {
335+
[center addObserver:self
336+
selector:@selector(onAccessibilityStatusChanged:)
337+
name:UIAccessibilityOnOffSwitchLabelsDidChangeNotification
338+
object:nil];
339+
}
340+
334341
[center addObserver:self
335342
selector:@selector(onUserSettingsChanged:)
336343
name:UIContentSizeCategoryDidChangeNotification
@@ -1417,19 +1424,7 @@ - (void)onAccessibilityStatusChanged:(NSNotification*)notification {
14171424
return;
14181425
}
14191426
auto platformView = [_engine.get() platformView];
1420-
int32_t flags = 0;
1421-
if (UIAccessibilityIsInvertColorsEnabled()) {
1422-
flags |= static_cast<int32_t>(flutter::AccessibilityFeatureFlag::kInvertColors);
1423-
}
1424-
if (UIAccessibilityIsReduceMotionEnabled()) {
1425-
flags |= static_cast<int32_t>(flutter::AccessibilityFeatureFlag::kReduceMotion);
1426-
}
1427-
if (UIAccessibilityIsBoldTextEnabled()) {
1428-
flags |= static_cast<int32_t>(flutter::AccessibilityFeatureFlag::kBoldText);
1429-
}
1430-
if (UIAccessibilityDarkerSystemColorsEnabled()) {
1431-
flags |= static_cast<int32_t>(flutter::AccessibilityFeatureFlag::kHighContrast);
1432-
}
1427+
int32_t flags = [self accessibilityFlags];
14331428
#if TARGET_OS_SIMULATOR
14341429
// There doesn't appear to be any way to determine whether the accessibility
14351430
// inspector is enabled on the simulator. We conservatively always turn on the
@@ -1447,6 +1442,27 @@ - (void)onAccessibilityStatusChanged:(NSNotification*)notification {
14471442
#endif
14481443
}
14491444

1445+
- (int32_t)accessibilityFlags {
1446+
int32_t flags = 0;
1447+
if (UIAccessibilityIsInvertColorsEnabled()) {
1448+
flags |= static_cast<int32_t>(flutter::AccessibilityFeatureFlag::kInvertColors);
1449+
}
1450+
if (UIAccessibilityIsReduceMotionEnabled()) {
1451+
flags |= static_cast<int32_t>(flutter::AccessibilityFeatureFlag::kReduceMotion);
1452+
}
1453+
if (UIAccessibilityIsBoldTextEnabled()) {
1454+
flags |= static_cast<int32_t>(flutter::AccessibilityFeatureFlag::kBoldText);
1455+
}
1456+
if (UIAccessibilityDarkerSystemColorsEnabled()) {
1457+
flags |= static_cast<int32_t>(flutter::AccessibilityFeatureFlag::kHighContrast);
1458+
}
1459+
if ([AccessibilityValueProvider accessibilityIsOnOffSwitchLabelsEnabled]) {
1460+
flags |= static_cast<int32_t>(flutter::AccessibilityFeatureFlag::kOnOffSwitchLabels);
1461+
}
1462+
1463+
return flags;
1464+
}
1465+
14501466
#pragma mark - Set user settings
14511467

14521468
- (void)traitCollectionDidChange:(UITraitCollection*)previousTraitCollection {

shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterFakeKeyEvents.h"
1515
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h"
1616
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h"
17+
#import "flutter/shell/platform/darwin/ios/framework/Source/accessibility_value_provider.h"
1718
#import "flutter/shell/platform/embedder/embedder.h"
1819

1920
FLUTTER_ASSERT_ARC
@@ -627,6 +628,48 @@ - (void)testItReportsHighContrastWhenTraitCollectionRequestsIt {
627628
[mockTraitCollection stopMocking];
628629
}
629630

631+
- (void)testItReportsNoAccessibilityFlagsSet {
632+
if (@available(iOS 13, *)) {
633+
// noop
634+
} else {
635+
return;
636+
}
637+
638+
// Setup test.
639+
FlutterViewController* viewController =
640+
[[FlutterViewController alloc] initWithEngine:self.mockEngine nibName:nil bundle:nil];
641+
642+
id valueProvider = OCMClassMock([AccessibilityValueProvider class]);
643+
OCMStub([valueProvider accessibilityIsOnOffSwitchLabelsEnabled]).andReturn(false);
644+
645+
// Exercise behavior under test.
646+
int32_t flags = [viewController accessibilityFlags];
647+
648+
// Verify behavior.
649+
XCTAssert(flags == 0);
650+
}
651+
652+
- (void)testItReportsAccessibilityOnOffSwitchLabelsFlagSet {
653+
if (@available(iOS 13, *)) {
654+
// noop
655+
} else {
656+
return;
657+
}
658+
659+
// Setup test.
660+
FlutterViewController* viewController =
661+
[[FlutterViewController alloc] initWithEngine:self.mockEngine nibName:nil bundle:nil];
662+
663+
id valueProvider = OCMClassMock([AccessibilityValueProvider class]);
664+
OCMStub([valueProvider accessibilityIsOnOffSwitchLabelsEnabled]).andReturn(true);
665+
666+
// Exercise behavior under test.
667+
int32_t flags = [viewController accessibilityFlags];
668+
669+
// Verify behavior.
670+
XCTAssert(flags == 1 << 6);
671+
}
672+
630673
- (void)testPerformOrientationUpdateForcesOrientationChange {
631674
[self orientationTestWithOrientationUpdate:UIInterfaceOrientationMaskPortrait
632675
currentOrientation:UIInterfaceOrientationLandscapeLeft

shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ extern NSNotificationName const FlutterViewControllerShowHomeIndicator;
4545
nextAction:(void (^)())nextAction API_AVAILABLE(ios(13.4));
4646
- (void)addInternalPlugins;
4747
- (void)deregisterNotifications;
48+
- (int32_t)accessibilityFlags;
4849
@end
4950

5051
#endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERVIEWCONTROLLER_INTERNAL_H_
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_ACCESSIBILITY_VALUE_PROVIDER_H_
6+
#define SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_ACCESSIBILITY_VALUE_PROVIDER_H_
7+
8+
/**
9+
* An accessibility value provider used to provide accessibility setting values from UIKit.
10+
* To make the behavior testable.
11+
*/
12+
@interface AccessibilityValueProvider : NSObject
13+
14+
+ (BOOL)accessibilityIsOnOffSwitchLabelsEnabled;
15+
16+
@end
17+
18+
#endif // SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_ACCESSIBILITY_VALUE_PROVIDER_H_
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#import <UIKit/UIKit.h>
6+
7+
#import "flutter/shell/platform/darwin/ios/framework/Source/accessibility_value_provider.h"
8+
9+
@implementation AccessibilityValueProvider {
10+
}
11+
12+
+ (BOOL)accessibilityIsOnOffSwitchLabelsEnabled {
13+
if (@available(iOS 13, *)) {
14+
return UIAccessibilityIsOnOffSwitchLabelsEnabled();
15+
} else {
16+
return NO;
17+
}
18+
}
19+
20+
@end

0 commit comments

Comments
 (0)