-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Screen.kt
258 lines (230 loc) · 9.84 KB
/
Screen.kt
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package com.swmansion.rnscreens
import android.annotation.SuppressLint
import android.content.pm.ActivityInfo
import android.graphics.Paint
import android.os.Parcelable
import android.util.SparseArray
import android.view.ViewGroup
import android.view.WindowManager
import android.webkit.WebView
import com.facebook.react.bridge.GuardedRunnable
import com.facebook.react.bridge.ReactContext
import com.facebook.react.uimanager.UIManagerModule
@SuppressLint("ViewConstructor")
class Screen constructor(context: ReactContext?) : FabricEnabledViewGroup(context) {
var fragment: ScreenFragment? = null
var container: ScreenContainer<*>? = null
var activityState: ActivityState? = null
private set
private var mTransitioning = false
var stackPresentation = StackPresentation.PUSH
var replaceAnimation = ReplaceAnimation.POP
var stackAnimation = StackAnimation.DEFAULT
var isGestureEnabled = true
var screenOrientation: Int? = null
private set
private var mStatusBarStyle: String? = null
private var mStatusBarHidden: Boolean? = null
private var mStatusBarTranslucent: Boolean? = null
private var mStatusBarColor: Int? = null
private var mNavigationBarColor: Int? = null
private var mNavigationBarHidden: Boolean? = null
var isStatusBarAnimated: Boolean? = null
private var mNativeBackButtonDismissalEnabled = true
init {
// we set layout params as WindowManager.LayoutParams to workaround the issue with TextInputs
// not displaying modal menus (e.g., copy/paste or selection). The missing menus are due to the
// fact that TextView implementation is expected to be attached to window when layout happens.
// Then, at the moment of layout it checks whether window type is in a reasonable range to tell
// whether it should enable selection controls (see Editor.java#prepareCursorControllers).
// With screens, however, the text input component can be laid out before it is attached, in
// that case TextView tries to get window type property from the oldest existing parent, which
// in this case is a Screen class, as it is the root of the screen that is about to be attached.
// Setting params this way is not the most elegant way to solve this problem but workarounds it
// for the time being
layoutParams = WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_APPLICATION)
}
override fun dispatchSaveInstanceState(container: SparseArray<Parcelable>) {
// do nothing, react native will keep the view hierarchy so no need to serialize/deserialize
// view's states. The side effect of restoring is that TextInput components would trigger
// set-text events which may confuse text input handling.
}
override fun dispatchRestoreInstanceState(container: SparseArray<Parcelable>) {
// ignore restoring instance state too as we are not saving anything anyways.
}
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
if (changed) {
val width = r - l
val height = b - t
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
updateScreenSizeFabric(width, height)
} else {
updateScreenSizePaper(width, height)
}
}
}
private fun updateScreenSizePaper(width: Int, height: Int) {
val reactContext = context as ReactContext
reactContext.runOnNativeModulesQueueThread(
object : GuardedRunnable(reactContext) {
override fun runGuarded() {
reactContext
.getNativeModule(UIManagerModule::class.java)
?.updateNodeSize(id, width, height)
}
})
}
val headerConfig: ScreenStackHeaderConfig?
get() = getChildAt(0) as? ScreenStackHeaderConfig
/**
* While transitioning this property allows to optimize rendering behavior on Android and provide
* a correct blending options for the animated screen. It is turned on automatically by the
* container when transitioning is detected and turned off immediately after
*/
fun setTransitioning(transitioning: Boolean) {
if (mTransitioning == transitioning) {
return
}
mTransitioning = transitioning
val isWebViewInScreen = hasWebView(this)
if (isWebViewInScreen && layerType != LAYER_TYPE_HARDWARE) {
return
}
super.setLayerType(
if (transitioning && !isWebViewInScreen) LAYER_TYPE_HARDWARE else LAYER_TYPE_NONE,
null
)
}
private fun hasWebView(viewGroup: ViewGroup): Boolean {
for (i in 0 until viewGroup.childCount) {
val child = viewGroup.getChildAt(i)
if (child is WebView) {
return true
} else if (child is ViewGroup) {
if (hasWebView(child)) {
return true
}
}
}
return false
}
override fun setLayerType(layerType: Int, paint: Paint?) {
// ignore - layer type is controlled by `transitioning` prop
}
fun setActivityState(activityState: ActivityState) {
if (activityState == this.activityState) {
return
}
this.activityState = activityState
container?.notifyChildUpdate()
}
fun setScreenOrientation(screenOrientation: String?) {
if (screenOrientation == null) {
this.screenOrientation = null
return
}
ScreenWindowTraits.applyDidSetOrientation()
this.screenOrientation = when (screenOrientation) {
"all" -> ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
"portrait" -> ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT
"portrait_up" -> ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
"portrait_down" -> ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
"landscape" -> ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
"landscape_left" -> ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
"landscape_right" -> ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
else -> ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
}
fragment?.let { ScreenWindowTraits.setOrientation(this, it.tryGetActivity()) }
}
// Accepts one of 4 accessibility flags
// developer.android.com/reference/android/view/View#attr_android:importantForAccessibility
fun changeAccessibilityMode(mode: Int) {
this.importantForAccessibility = mode
this.headerConfig?.toolbar?.importantForAccessibility = mode
}
var statusBarStyle: String?
get() = mStatusBarStyle
set(statusBarStyle) {
if (statusBarStyle != null) {
ScreenWindowTraits.applyDidSetStatusBarAppearance()
}
mStatusBarStyle = statusBarStyle
fragment?.let { ScreenWindowTraits.setStyle(this, it.tryGetActivity(), it.tryGetContext()) }
}
var isStatusBarHidden: Boolean?
get() = mStatusBarHidden
set(statusBarHidden) {
if (statusBarHidden != null) {
ScreenWindowTraits.applyDidSetStatusBarAppearance()
}
mStatusBarHidden = statusBarHidden
fragment?.let { ScreenWindowTraits.setHidden(this, it.tryGetActivity()) }
}
var isStatusBarTranslucent: Boolean?
get() = mStatusBarTranslucent
set(statusBarTranslucent) {
if (statusBarTranslucent != null) {
ScreenWindowTraits.applyDidSetStatusBarAppearance()
}
mStatusBarTranslucent = statusBarTranslucent
fragment?.let {
ScreenWindowTraits.setTranslucent(
this,
it.tryGetActivity(),
it.tryGetContext()
)
}
}
var statusBarColor: Int?
get() = mStatusBarColor
set(statusBarColor) {
if (statusBarColor != null) {
ScreenWindowTraits.applyDidSetStatusBarAppearance()
}
mStatusBarColor = statusBarColor
fragment?.let { ScreenWindowTraits.setColor(this, it.tryGetActivity(), it.tryGetContext()) }
}
var navigationBarColor: Int?
get() = mNavigationBarColor
set(navigationBarColor) {
if (navigationBarColor != null) {
ScreenWindowTraits.applyDidSetNavigationBarAppearance()
}
mNavigationBarColor = navigationBarColor
fragment?.let { ScreenWindowTraits.setNavigationBarColor(this, it.tryGetActivity()) }
}
var isNavigationBarHidden: Boolean?
get() = mNavigationBarHidden
set(navigationBarHidden) {
if (navigationBarHidden != null) {
ScreenWindowTraits.applyDidSetNavigationBarAppearance()
}
mNavigationBarHidden = navigationBarHidden
fragment?.let {
ScreenWindowTraits.setNavigationBarHidden(
this,
it.tryGetActivity(),
)
}
}
var nativeBackButtonDismissalEnabled: Boolean
get() = mNativeBackButtonDismissalEnabled
set(enableNativeBackButtonDismissal) {
mNativeBackButtonDismissalEnabled = enableNativeBackButtonDismissal
}
enum class StackPresentation {
PUSH, MODAL, TRANSPARENT_MODAL
}
enum class StackAnimation {
DEFAULT, NONE, FADE, SLIDE_FROM_BOTTOM, SLIDE_FROM_RIGHT, SLIDE_FROM_LEFT, FADE_FROM_BOTTOM
}
enum class ReplaceAnimation {
PUSH, POP
}
enum class ActivityState {
INACTIVE, TRANSITIONING_OR_BELOW_TOP, ON_TOP
}
enum class WindowTraits {
ORIENTATION, COLOR, STYLE, TRANSLUCENT, HIDDEN, ANIMATED, NAVIGATION_BAR_COLOR, NAVIGATION_BAR_HIDDEN
}
}