-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathOverlayView.js
235 lines (209 loc) · 5.52 KB
/
OverlayView.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import React, { PureComponent } from 'react'
import {
Animated,
Modal,
Platform,
StatusBar,
TouchableWithoutFeedback,
StyleSheet,
Text,
View,
Dimensions
} from 'react-native'
import PropTypes from 'prop-types'
import { getStatusBarHeight } from 'react-native-status-bar-height'
import CircleActionItem from './CircleActionItem'
const STATUS_BAR_OFFSET = (Platform.OS === 'android' ? 0 : 0)
const isIOS = Platform.OS === 'ios'
const { width, height } = Dimensions.get('window')
const styles = StyleSheet.create({
overlayBackground: {
flex: 1
},
open: {
position: 'absolute',
flex: 1,
justifyContent: 'center',
// Android pan handlers crash without this declaration:
backgroundColor: 'black',
}
})
export default class OverlayView extends PureComponent {
static propTypes = {
frame: PropTypes.shape({
x: PropTypes.number,
y: PropTypes.number,
width: PropTypes.number,
height: PropTypes.number,
}),
springConfig: PropTypes.shape({
tension: PropTypes.number,
friction: PropTypes.number,
}),
backgroundColor: PropTypes.string,
isOpen: PropTypes.bool,
onOpen: PropTypes.func,
close: PropTypes.func,
willClose: PropTypes.func
}
static defaultProps = {
backgroundColor: 'white',
}
state = {
target: {
x: 0,
y: 0,
opacity: 1,
},
openVal: new Animated.Value(0),
}
componentDidMount() {
if (this.props.isOpened) {
this.open()
}
}
open = () => {
// if (isIOS) {
// StatusBar.setHidden(true, 'fade')
// }
this.setState({
target: {
opacity: 0.92,
}
})
Animated.timing(
this.state.openVal,
{
toValue: 1,
duration: 200
}
).start(() => {
this.props.didOpen()
})
}
close = () => {
this.props.willClose()
if (isIOS) {
StatusBar.setHidden(false, 'fade')
}
Animated.timing(
this.state.openVal,
{
toValue: 0
}
).start(() => {
this.props.close()
})
}
componentWillReceiveProps(props) {
if (this.props.isOpened != props.isOpened && props.isOpened) {
this.open()
}
}
getPositionLabel(pageY) {
if (pageY <= height / 2) {
return { bottom: 20 }
}
return { top: Platform.OS === 'ios' ? 20 + getStatusBarHeight() : 20 }
}
getStartAngle(pageX, pageY) {
let startAngle = Math.PI / 4
if (pageX < 90) {
startAngle = Math.PI / 5 * 3
if (pageY < 150) {
startAngle = startAngle + Math.PI / 4
} else if (pageY > height - 90) {
startAngle = startAngle - Math.PI / 10
}
} else if (pageX > width - 90) {
startAngle = - Math.PI / 10
if (pageY < 150) {
startAngle = startAngle - Math.PI / 4
} else if (pageY > height - 90) {
startAngle = startAngle + Math.PI / 10
}
}
return startAngle
}
render() {
const {
isOpened,
renderHeader,
swipeToDismiss,
frame,
backgroundColor,
items
} = this.props
const {
openVal,
target
} = this.state
const bgOpacity = {
opacity: openVal.interpolate({ inputRange: [0, 1], outputRange: [0, target.opacity] })
}
const background = (
<Animated.View style={[styles.overlayBackground, { backgroundColor: backgroundColor }, bgOpacity]}></Animated.View>
)
const contentStyle = {
position: 'absolute',
top: frame.y + STATUS_BAR_OFFSET,
left: frame.x,
width: frame.width,
height: frame.height,
zIndex: 10
}
const content = (
<View style={contentStyle}>
<TouchableWithoutFeedback onPress={() => this.close()}>
{this.props.children}
</TouchableWithoutFeedback>
</View>
)
const d = 80
let selectingIndex = -1
const startAngle = this.getStartAngle(this.props.touchOrigin.pageX, this.props.touchOrigin.pageY)
const positionLabel = this.getPositionLabel(this.props.touchOrigin.pageY)
const itemNodes = items.map((item, index) => {
const { pX, pY } = this.props.touchUpdate
const angle = (Math.PI / 4 * index) + startAngle
const targetX = - Math.cos(angle) * d + this.props.touchOrigin.pageX - 25
const targetY = - Math.sin(angle) * d + this.props.touchOrigin.pageY - 25
const isSelecting = (pX >= targetX && pX <= targetX + 50) && (pY >= targetY && pY <= targetY + 50)
if (isSelecting) {
selectingIndex = index
}
return <CircleActionItem
styleLabel={this.props.styleLabel}
styleBgLabel={this.props.styleBgLabel}
inactiveColor={this.props.inactiveColor}
deactiveColor={this.props.deactiveColor}
item={item}
isSelecting={isSelecting}
key={index}
targetX={targetX}
targetY={targetY}
ox={this.props.touchOrigin.pageX}
oy={this.props.touchOrigin.pageY}
/>
})
this.props.setSelectedIndex(selectingIndex)
const label = (selectingIndex !== -1) && (
<View
style={[this.props.styleBgLabel,
{ backgroundColor: items[selectingIndex].activeColor },
positionLabel
]}>
<Text style={this.props.styleLabel}>{items[selectingIndex].label}</Text>
</View>
)
return (
<Modal visible={isOpened} transparent={true} onRequestClose={this.close.bind(this)}>
{background}
{content}
{itemNodes}
{label}
{/* <CircleActionItem x={this.props.touchUpdate.pX} y={this.props.touchUpdate.pY}/> */}
</Modal>
)
}
}