-
-
Notifications
You must be signed in to change notification settings - Fork 132
/
layout.js
124 lines (120 loc) · 2.82 KB
/
layout.js
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
/**
* Actual info about current layout.
* Initially reads from Dimensions, then listen updates.
*
* Provides following values:
* - width
* - height
* - orientation
* - aspect-ratio
*/
class Layout {
/**
* Constructor
*/
constructor() {
this._state = Object.create(null);
}
/**
* Init layout
* @param {Object} params
* @param {Number} [params.marginTop] margin top to adjust height for top status bar
*/
//init(params = {}) {
// const {width, height} = Dimensions.get('window');
// this._state.marginTop = typeof params.marginTop === 'number' ? params.marginTop : 0;
// this.calc({width, height});
//}
/**
* Sets width & height
* @param {Number} width
* @param {Number} height
*/
set({width, height}) {
const isFirst = Object.keys(this._state).length === 0;
// save width/height only for the first time (when AppContainer is rendered without children)
// next time just check orientation
if (isFirst) {
this._state.width = width;
this._state.height = height;
}
// const newOrientation = width > height ? 'landscape' : 'portrait';
if (width !== this._state.width || height !== this._state.height) {
Object.assign(this._state, {
width,
height,
// workingHeight: height - this._state.marginTop,
orientation: width > height ? 'landscape' : 'portrait',
aspectRatio: width / height,
});
return true;
}
return false;
}
/**
* Update layout state
* @param {Object} params
* @param {String} params.orientation
*/
//update(params) {
// if (params.orientation !== this._state.orientation) {
// this.calc({width: this._state.height, height: this._state.width});
// this.onUpdated.dispatch();
// }
//}
/**
* Width
* @returns {Number}
*/
get width() {
return this._state.width;
}
/**
* Height
* @returns {Number}
*/
get height() {
return this._state.height;
// return this._state.workingHeight;
}
/**
* Orientation
* @returns {String} landscape|portrait
*/
get orientation() {
return this._state.orientation;
}
/**
* aspectRatio
* @returns {Number}
*/
get aspectRatio() {
return this._state.aspectRatio;
}
/**
* Is orientation landscape
* @returns {Boolean}
*/
get isLandscape() {
return this._state.orientation === 'landscape';
}
/**
* Is orientation portrait
* @returns {Boolean}
*/
get isPortrait() {
return this._state.orientation === 'portrait';
}
/**
* Margin top - difference between Dimensions.get('window').height and real working height
* Noticed only on Android as Dimensions return height with top status bar.
* @returns {Number}
*/
get marginTop() {
return this._state.marginTop;
}
}
/**
* Make a singleton
*/
export default new Layout();