Skip to content
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

Add high contrast API to AccessibilityInfo #742

Merged
merged 4 commits into from
Mar 18, 2021

Conversation

appden
Copy link
Collaborator

@appden appden commented Mar 6, 2021

Please select one of the following

  • I am removing an existing difference between facebook/react-native and microsoft/react-native-macos 👍
  • I am cherry-picking a change from Facebook's react-native into microsoft/react-native-macos 👍
  • I am making a fix / change for the macOS implementation of react-native
  • I am making a change required for Microsoft usage of react-native

Summary

This adds the isHighContrastEnabled() method and highContrastChanged event to the AccessibilityInfo module. This matches the existing API conventions we have in React Native macOS for other accessibility features. React Native Windows has a similar API with its AppTheme module, but that module doesn't exist for other React Native platforms and this is more appropriate in AccessibilityInfo.

Changelog

[macOS] [Added] - Add high contrast API to AccessibilityInfo

Test Plan

Tested in RNTester. Observed live updates when changing the Increase contrast setting under System Preferences.

Microsoft Reviewers: Open in CodeFlow

@appden appden requested a review from alloy as a code owner March 6, 2021 19:39
@appden appden force-pushed the high-contrast branch 3 times, most recently from e5978d3 to 3ec1253 Compare March 8, 2021 22:57
/**
* macOS only
*/
isHighContrastEnabled: function(): Promise<boolean> {

Choose a reason for hiding this comment

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

should we call it hightcontrast when for apple platform it is increased contrast? high contrast is pretty Window's term right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not really. Apple also uses the term "high contrast", such as with the appearance names (i.e. NSAppearanceNameAccessibilityHighContrastAqua), and I think the industry in general refers to this accessibility mode as "high contrast". 😀

Choose a reason for hiding this comment

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

I'm happy to be shown I'm wrong here but I get the impression from Apple's support docs that Increase Contrast is the overarching feature encapsulating many different contrasts whereas HighContrast is one of many contrast options within it (e.g. NSAppearanceNameAqua, NSAppearanceNameDarkAqua, NSAppearanceNameAccessibilityHighContrastDarkAqua, NSAppearanceNameVibrantDark , NSAppearanceNameAccessibilityHighContrastVibrantDark).

If other platforms call it "High Contrast" then I think we just have to choose the one that makes the most sense to go by because I get the impression it won't match up perfectly for all platforms.

Choose a reason for hiding this comment

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

Since we're writing this feature primarily for macOS, I'd also lean towards IncreaseContrast, but again am happy to see how I'm misunderstanding Apple's docs :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We also plan to implement this API on iOS, Android, and Windows, which already use "high contrast" terminology in their APIs (i.e. UIAccessibilityContrastHigh on iOS, isHighTextContrastEnabled() on Android, HighContrast() on Windows and the -ms-high-contrast media query in Edge). I really think "high contrast" is right choice for this API.

Choose a reason for hiding this comment

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

Yea the main argument against using the macOS terminology is to keep it consistent cross-plat. I can see why that's important enough to keep it

@@ -53,7 +53,7 @@ - (void)dealloc
[[NSWorkspace sharedWorkspace] removeObserver:self
forKeyPath:@"voiceOverEnabled"
context:AccessibilityVoiceOverChangeContext];
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];

Choose a reason for hiding this comment

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

do we need this line? "If your app targets iOS 9.0 and later or macOS 10.11 and later, and you used addObserver:selector:name:object:, you do not need to unregister the observer. If you forget or are unable to remove the observer, the system cleans up the next time it would have posted to it." https://developer.apple.com/documentation/foundation/nsnotificationcenter/1413994-removeobserver

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

True, but it's certainly more explicit to just leave this because otherwise somebody needs to know this behavior when reading this code.

Choose a reason for hiding this comment

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

Don't we not support iOS 8.0 anymore? I'm always in favor of deleting code that has no functional benefit :)

@@ -21,6 +21,7 @@ extern NSString *const RCTAccessibilityManagerDidUpdateMultiplierNotification; /

@property (nonatomic, assign) BOOL isBoldTextEnabled;
@property (nonatomic, assign) BOOL isGrayscaleEnabled;
@property (nonatomic, assign) BOOL isHighContrastEnabled; // TODO(macOS ISS#2323203)

Choose a reason for hiding this comment

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

even if we leave the JS prop to be high contrast, I still think it is more natural that Apple has boolean property named isIncreasedContrastEnabled. what do you think?

Choose a reason for hiding this comment

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

What if we leave as is but add a comment "maps to IncreaseContrast for macOS" or something like that?

Anyone whose written macOS code before may wonder how these two relate to each other so I think it should be documented somewhere

@@ -23,12 +23,12 @@ @implementation RCTAccessibilityManager

static void *AccessibilityVoiceOverChangeContext = &AccessibilityVoiceOverChangeContext;

+ (BOOL)requiresMainQueueSetup
+ (BOOL)requiresMainQueueSetup
{
return NO;

Choose a reason for hiding this comment

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

for my own understanding, is this correct? anything we ask for NSWorkspace, we should be on the UIthread right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Great point! I'll change this.

@@ -39,11 +39,11 @@ - (instancetype)init
selector:@selector(accessibilityDisplayOptionsChange:)
name:NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification
object:nil];
_isHighContrastEnabled = [[NSWorkspace sharedWorkspace] accessibilityDisplayShouldIncreaseContrast];

Choose a reason for hiding this comment

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

Not your fault but while we're touching these can we cache this to save all the extra time calling into the same method over and over again?

NSWorkspace *sharedWorkspace = [NSWorkspace sharedWorkspace and just use that var throughout the method

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That would be a micro-optimization since calling these singleton methods take microseconds and I think it's fine to continue following the convention in React Native of calling them for each use (see [NSNotificationCenter defaultCenter] calls in other native modules).

Choose a reason for hiding this comment

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

I'd argue those should get fixed too. Functionally speaking it will only improve performance (albeit a small amount) with no downsides if we fix up these calls everywhere.

@@ -124,9 +130,16 @@ - (void)observeValueForKeyPath:(NSString *)keyPath

- (void)accessibilityDisplayOptionsChange:(NSNotification *)notification
{
BOOL newHighContrastEnabled = [[NSWorkspace sharedWorkspace] accessibilityDisplayShouldIncreaseContrast];

Choose a reason for hiding this comment

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

Same here, might as well be efficient and cache the sharedWorkspace

@@ -39,11 +39,11 @@ - (instancetype)init
selector:@selector(accessibilityDisplayOptionsChange:)
name:NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification
object:nil];
_isHighContrastEnabled = [[NSWorkspace sharedWorkspace] accessibilityDisplayShouldIncreaseContrast];

Choose a reason for hiding this comment

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

I'd argue those should get fixed too. Functionally speaking it will only improve performance (albeit a small amount) with no downsides if we fix up these calls everywhere.

@@ -21,6 +21,7 @@ extern NSString *const RCTAccessibilityManagerDidUpdateMultiplierNotification; /

@property (nonatomic, assign) BOOL isBoldTextEnabled;
@property (nonatomic, assign) BOOL isGrayscaleEnabled;
@property (nonatomic, assign) BOOL isHighContrastEnabled; // TODO(macOS ISS#2323203)

Choose a reason for hiding this comment

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

What if we leave as is but add a comment "maps to IncreaseContrast for macOS" or something like that?

Anyone whose written macOS code before may wonder how these two relate to each other so I think it should be documented somewhere

@HeyImChris HeyImChris merged commit e9e4980 into microsoft:master Mar 18, 2021
Saadnajmi added a commit to Saadnajmi/react-native-macos that referenced this pull request Mar 22, 2021
Saadnajmi added a commit to Saadnajmi/react-native-macos that referenced this pull request Mar 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants