-
Notifications
You must be signed in to change notification settings - Fork 486
Expand file tree
/
Copy pathUserInterface.tsx
More file actions
213 lines (175 loc) · 6.95 KB
/
UserInterface.tsx
File metadata and controls
213 lines (175 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* UserInterface.tsx
*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT license.
*
* RN implementation of the ReactXP interfaces related to
* UI (layout measurements, etc.).
*/
import * as React from 'react';
import * as RN from 'react-native';
import AppConfig from '../common/AppConfig';
import assert from '../common/assert';
import * as RX from '../common/Interfaces';
import { Defer } from '../common/utils/PromiseDefer';
import MainViewStore from './MainViewStore';
export class UserInterface extends RX.UserInterface {
private _touchLatencyThresholhdMs: number | undefined;
private _isNavigatingWithKeyboard = false;
private _rootViewUsingPropsFactory: RN.ComponentProvider | undefined;
private _rootViewRegistry: {[id: string]: number};
constructor() {
super();
RN.Dimensions.addEventListener('change', event => {
this.contentSizeMultiplierChangedEvent.fire(event.window.fontScale);
});
this.keyboardNavigationEvent.subscribe(this._keyboardNavigationStateChanged);
this._rootViewRegistry = {};
}
measureLayoutRelativeToWindow(component: React.Component<any, any>): Promise<RX.Types.LayoutInfo> {
const deferred = new Defer<RX.Types.LayoutInfo>();
const nodeHandle = RN.findNodeHandle(component);
assert(!!nodeHandle);
RN.NativeModules.UIManager.measureInWindow(
nodeHandle, (x: number, y: number, width: number, height: number) => {
deferred.resolve({
x: x,
y: y,
width: width,
height: height,
});
},
);
return deferred.promise();
}
measureLayoutRelativeToAncestor(component: React.Component<any, any>,
ancestor: React.Component<any, any>): Promise<RX.Types.LayoutInfo> {
const deferred = new Defer<RX.Types.LayoutInfo>();
const nodeHandle = RN.findNodeHandle(component);
const ancestorNodeHander = RN.findNodeHandle(ancestor);
RN.NativeModules.UIManager.measureLayout(
nodeHandle, ancestorNodeHander, () => {
deferred.reject('UIManager.measureLayout() failed');
},
(x: number, y: number, width: number, height: number, pageX: number, pageY: number) => {
deferred.resolve({
x: x,
y: y,
width: width,
height: height,
});
},
);
return deferred.promise();
}
measureWindow(rootViewId?: string): RX.Types.LayoutInfo {
let dimensions = RN.Dimensions.get('window');
if (rootViewId && RN.Platform.OS === 'windows') {
try {
dimensions = RN.Dimensions.get(rootViewId as any);
} catch (e) {
// Can happen if RNW doesn't support multi view dimensions tracking yet
console.warn('Couldn\'t get dimensions for rootViewId ' + rootViewId +
' check if RNW already supports multi view dimensions tracking');
}
}
return {
x: 0,
y: 0,
width: dimensions.width,
height: dimensions.height,
};
}
getContentSizeMultiplier(): Promise<number> {
const deferred = new Defer<number>();
// TODO: #727532 Remove conditional after implementing UIManager.getContentSizeMultiplier for UWP
// TODO:(alregner) Remove conditional after implementing UIManager.getContentSizeMultiplier for macos
if (RN.Platform.OS === 'windows' || RN.Platform.OS === 'macos') {
deferred.resolve(1);
} else {
deferred.resolve(RN.PixelRatio.getFontScale());
}
return deferred.promise();
}
useCustomScrollbars(enable = true) {
// Nothing to do
}
dismissKeyboard() {
// Work around the fact that the react-native type definition file
// doesn't properly specify RN.TextInput.State as static.
const staticState = (RN.TextInput as any).State as RN.TextInputState;
staticState.blurTextInput(staticState.currentlyFocusedField());
}
isHighPixelDensityScreen() {
const ratio = RN.PixelRatio.get();
const isHighDef = ratio > 1;
return isHighDef;
}
getPixelRatio(): number {
return RN.PixelRatio.get();
}
setMainView(element: React.ReactElement<any>) {
MainViewStore.setMainView(element);
}
setContextWrapper(contextWrapper: ((rootView: React.ReactElement<any>) => React.ReactElement<any>)): void {
MainViewStore.setContextWrapper(contextWrapper);
}
registerRootViewUsingPropsFactory(factory: RN.ComponentProvider) {
this._rootViewUsingPropsFactory = factory;
}
registerRootView(viewKey: string, getComponentFunc: Function) {
if (this._rootViewUsingPropsFactory) {
const RootViewUsingProps = this._rootViewUsingPropsFactory();
RN.AppRegistry.registerComponent(viewKey, () => {
class RootViewWrapper extends React.Component<any, any> {
render() {
return (
<RootViewUsingProps
reactxp_mainViewType={ getComponentFunc() }
{ ...this.props }
/>
);
}
}
return RootViewWrapper;
});
}
}
renderMainView() {
// Nothing to do
}
enableTouchLatencyEvents(latencyThresholdMs: number): void {
this._touchLatencyThresholhdMs = latencyThresholdMs;
}
evaluateTouchLatency(e: RX.Types.SyntheticEvent) {
if (this._touchLatencyThresholhdMs) {
const latency = Date.now() - e.timeStamp.valueOf();
if (latency > this._touchLatencyThresholhdMs) {
this.touchLatencyEvent.fire(latency);
}
}
}
isNavigatingWithKeyboard(): boolean {
return this._isNavigatingWithKeyboard;
}
private _keyboardNavigationStateChanged = (isNavigatingWithKeyboard: boolean) => {
this._isNavigatingWithKeyboard = isNavigatingWithKeyboard;
};
notifyRootViewInstanceCreated(rootViewId: string, nodeHandle: number): void {
if (AppConfig.isDevelopmentMode()) {
const existing = this.findNodeHandleByRootViewId(rootViewId);
if (existing && existing !== nodeHandle) {
console.warn('Duplicate reactxp_rootViewId!');
}
}
this._rootViewRegistry[rootViewId] = nodeHandle;
}
notifyRootViewInstanceDestroyed(rootViewId: string): void {
delete this._rootViewRegistry[rootViewId];
}
findNodeHandleByRootViewId(rootViewId: string): number | undefined {
return this._rootViewRegistry[rootViewId];
}
}
export default new UserInterface();