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

feat(HighContrast): Expose the XAML HighContrastChanged event to JS #1443

Merged
merged 3 commits into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 35 additions & 12 deletions Libraries/Components/AccessibilityInfo/AccessibilityInfo.windows.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
Expand All @@ -11,32 +11,55 @@
*/
'use strict';

type ChangeEventName = $Enum<{
change: string,
}>;
var NativeModules = require('NativeModules');
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');

var RCTAccessibilityInfo = NativeModules.AccessibilityInfo;
var HIGH_CONTRAST_EVENT = 'highContrastDidChange';
var _subscriptions = new Map();

var warning = require('fbjs/lib/warning');
type AccessibilityEventName = $Enum<{
[HIGH_CONTRAST_EVENT]: string,
}>;

var AccessibilityInfo = {

fetch: function(): Promise<*> {
initialHighContrast: RCTAccessibilityInfo.initialHighContrast,

fetch: function(): Promise {
return new Promise((resolve, reject) => {
reject('AccessibilityInfo is not supported on this platform.');
resolve(false);
Copy link
Contributor

@rigdern rigdern Oct 12, 2017

Choose a reason for hiding this comment

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

resolve(false); [](start = 6, length = 15)

Can you put a comment about how UWP doesn't provide any way to query whether or not a screen reader is being used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

});
},

fetchIsHighContrast: function (): Promise {
return RCTAccessibilityInfo.fetchIsHighContrast();
},

addEventListener: function (
eventName: ChangeEventName,
eventName: AccessibilityEventName,
handler: Function
): void {
warning(false, 'AccessibilityInfo is not supported on this platform.');
): Object {
if (eventName !== HIGH_CONTRAST_EVENT) {
return {
remove() { }
};
}

var listener = RCTDeviceEventEmitter.addListener(eventName, enabled => handler(enabled));

_subscriptions.set(handler, listener);

return listener;
},

removeEventListener: function(
eventName: ChangeEventName,
eventName: AccessibilityEventName,
handler: Function
): void {
warning(false, 'AccessibilityInfo is not supported on this platform.');
var listener = _subscriptions.get(handler);
listener && listener.remove();
_subscriptions.delete(handler);
},

};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using ReactNative.Bridge;
using ReactNative.Modules.Core;
using System.Collections.Generic;
using Windows.UI.ViewManagement;

namespace ReactNative.Modules.Accessibilityinfo
{
class AccessibilityInfoModule : ReactContextNativeModuleBase
{
private readonly AccessibilitySettings _accessibility = new AccessibilitySettings();
private readonly UISettings _settings = new UISettings();

private string GetRgbaString(UIElementType type)
{
var color = _settings.UIElementColor(type);
return "rgba(" + color.R + "," + color.G + "," + color.B + "," + color.A + ")";
}

private IDictionary<string, string> GetHighContrastColors()
Copy link
Contributor

Choose a reason for hiding this comment

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

private IDictionary<string, string> GetHighContrastColors() [](start = 8, length = 59)

Maybe put a TODO about how we want to expose these colors to JavaScript in the future

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

{
return new Dictionary<string, string>
{
{ "windowText", GetRgbaString(UIElementType.WindowText) },
{ "hotlight", GetRgbaString(UIElementType.Hotlight) },
{ "grayText", GetRgbaString(UIElementType.GrayText) },
{ "highlightText", GetRgbaString(UIElementType.HighlightText) },
{ "highlight", GetRgbaString(UIElementType.Highlight) },
{ "buttonText", GetRgbaString(UIElementType.ButtonText) },
{ "buttonFace", GetRgbaString(UIElementType.ButtonFace) },
{ "window", GetRgbaString(UIElementType.Window) },
};
}

public AccessibilityInfoModule(ReactContext reactContext)
: base(reactContext)
{

}

public override string Name => "AccessibilityInfo";

public override IReadOnlyDictionary<string, object> Constants
{
get
{
return new Dictionary<string, object>
{
// TODO: It would be better to have a sync GethIsHighContrast,
// but this is not supported by the framework at the moment.
{ "initialHighContrast", _accessibility.HighContrast },
};
}
}

public override void Initialize()
{
_accessibility.HighContrastChanged += OnHighContrastChanged;
Copy link
Contributor

Choose a reason for hiding this comment

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

_accessibility.HighContrastChanged += OnHighContrastChanged; [](start = 12, length = 60)

Should we do this in Initialize or in the constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Eric suggested to use Initialize.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. What's the reasoning behind preferring Initialize over the constructor? When does Initialize get called relative to Constants?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Constructor is called before the JS bundle is loaded, so you could potentially notify event to JS before anything there to listen. If you call after Initialize, bundle load operation has already been queued.


In reply to: 144430206 [](ancestors = 144430206)

Copy link
Contributor

Choose a reason for hiding this comment

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

Firing an event before anything is listening sounds pretty harmless.

So it sounds like it doesn't matter too much whether you register for the event in Initialize or in the constructor. Do you agree?


In reply to: 145428282 [](ancestors = 145428282,144430206)

}

public override void OnReactInstanceDispose()
{
_accessibility.HighContrastChanged -= OnHighContrastChanged;
}

private void OnHighContrastChanged(AccessibilitySettings sender, object args)
{
Context.GetJavaScriptModule<RCTDeviceEventEmitter>()
.emit("highContrastDidChange", sender.HighContrast);
}

[ReactMethod]
public void fetchIsHighContrast(IPromise promise)
{
promise.Resolve(_accessibility.HighContrast);
}
}
}
1 change: 1 addition & 0 deletions ReactWindows/ReactNative/ReactNative.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
<Compile Include="DevSupport\ShakeAccelerometer.cs" />
<Compile Include="DevSupport\WebSocketJavaScriptExecutor.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Modules\AccessibilityInfo\AccessibilityInfoModule.cs" />
<Compile Include="Modules\Clipboard\ClipboardInstance.cs" />
<Compile Include="Modules\Clipboard\ClipboardModule.cs" />
<Compile Include="Modules\Clipboard\IClipboardInstance.cs" />
Expand Down
2 changes: 2 additions & 0 deletions ReactWindows/ReactNative/Shell/MainReactPackage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ReactNative.Animated;
using ReactNative.Bridge;
using ReactNative.Modules.Accessibilityinfo;
using ReactNative.Modules.AppState;
using ReactNative.Modules.Clipboard;
using ReactNative.Modules.Core;
Expand Down Expand Up @@ -46,6 +47,7 @@ public IReadOnlyList<INativeModule> CreateNativeModules(ReactContext reactContex
{
return new List<INativeModule>
{
new AccessibilityInfoModule(reactContext),
new AppStateModule(reactContext),
new AsyncStorageModule(),
//new CameraRollManager(reactContext),
Expand Down