-
Notifications
You must be signed in to change notification settings - Fork 487
Expand file tree
/
Copy pathUserInterface.ts
More file actions
150 lines (120 loc) · 4.62 KB
/
UserInterface.ts
File metadata and controls
150 lines (120 loc) · 4.62 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
/**
* UserInterface.ts
*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT license.
*
* Web-specific implementation of the ReactXP interfaces related to
* UI (layout measurements, etc.).
*/
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import * as RX from '../common/Interfaces';
import { Defer } from '../common/utils/PromiseDefer';
import FrontLayerViewManager from './FrontLayerViewManager';
import ScrollViewConfig from './ScrollViewConfig';
export class UserInterface extends RX.UserInterface {
private _isNavigatingWithKeyboard = false;
constructor() {
super();
this.keyboardNavigationEvent.subscribe(this._keyboardNavigationStateChanged);
}
measureLayoutRelativeToWindow(component: React.Component<any, any>): Promise<RX.Types.LayoutInfo> {
const deferred = new Defer<RX.Types.LayoutInfo>();
let componentDomNode: HTMLElement | null = null;
try {
componentDomNode = ReactDOM.findDOMNode(component) as HTMLElement | null;
} catch {
// Component is no longer mounted.
}
if (!componentDomNode) {
deferred.reject('measureLayoutRelativeToWindow failed');
} else {
const componentBoundingRect = componentDomNode.getBoundingClientRect();
deferred.resolve({
x: componentBoundingRect.left,
y: componentBoundingRect.top,
width: componentBoundingRect.width,
height: componentBoundingRect.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>();
let componentDomNode: HTMLElement | null = null;
let ancestorDomNode: HTMLElement | null = null;
try {
componentDomNode = ReactDOM.findDOMNode(component) as HTMLElement | null;
ancestorDomNode = ReactDOM.findDOMNode(ancestor) as HTMLElement | null;
} catch {
// Components are no longer mounted.
}
if (!componentDomNode || !ancestorDomNode) {
deferred.reject('measureLayoutRelativeToAncestor failed');
} else {
const componentBoundingRect = componentDomNode.getBoundingClientRect();
const ancestorBoundingRect = ancestorDomNode.getBoundingClientRect();
deferred.resolve({
x: componentBoundingRect.left - ancestorBoundingRect.left,
y: componentBoundingRect.top - ancestorBoundingRect.top,
width: componentBoundingRect.width,
height: componentBoundingRect.height,
});
}
return deferred.promise();
}
measureWindow(rootViewId?: string): RX.Types.LayoutInfo {
// Mo multi window support, default to main window
return {
x: 0,
y: 0,
width: window.innerWidth,
height: window.innerHeight,
};
}
getContentSizeMultiplier(): Promise<number> {
// Browsers don't support font-specific scaling. They scale all of their
// UI elements the same.
return Promise.resolve(1);
}
isHighPixelDensityScreen(): boolean {
return this.getPixelRatio() > 1;
}
getPixelRatio(): number {
let pixelRatio = 0;
if (window.devicePixelRatio) {
pixelRatio = window.devicePixelRatio;
}
return pixelRatio;
}
setMainView(element: React.ReactElement<any>): void {
FrontLayerViewManager.setMainView(element);
}
setContextWrapper(contextWrapper: ((rootView: React.ReactElement<any>) => React.ReactElement<any>)): void {
FrontLayerViewManager.setContextWrapper(contextWrapper);
}
registerRootView(viewKey: string, getComponentFunc: Function): void {
// Nothing to do
}
useCustomScrollbars(enable = true): void {
ScrollViewConfig.setUseCustomScrollbars(enable);
}
dismissKeyboard(): void {
// Nothing to do
}
enableTouchLatencyEvents(latencyThresholdMs: number): void {
// Nothing to do
}
evaluateTouchLatency(e: RX.Types.MouseEvent): void {
// Nothing to do
}
isNavigatingWithKeyboard(): boolean {
return this._isNavigatingWithKeyboard;
}
private _keyboardNavigationStateChanged = (isNavigatingWithKeyboard: boolean): void => {
this._isNavigatingWithKeyboard = isNavigatingWithKeyboard;
};
}
export default new UserInterface();