-
-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathKeyboardControllerModuleImpl.kt
More file actions
133 lines (115 loc) · 3.63 KB
/
KeyboardControllerModuleImpl.kt
File metadata and controls
133 lines (115 loc) · 3.63 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
package com.reactnativekeyboardcontroller.modules
import android.content.Context
import android.os.Build
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.UiThreadUtil
import com.reactnativekeyboardcontroller.extensions.dp
import com.reactnativekeyboardcontroller.extensions.screenLocation
import com.reactnativekeyboardcontroller.extensions.uiManager
import com.reactnativekeyboardcontroller.interactive.KeyboardAnimationController
import com.reactnativekeyboardcontroller.traversal.FocusedInputHolder
import com.reactnativekeyboardcontroller.traversal.ViewHierarchyNavigator
class KeyboardControllerModuleImpl(
private val mReactContext: ReactApplicationContext,
) {
private val uiManager = mReactContext.uiManager
private val controller = KeyboardAnimationController()
private val mDefaultMode: Int = getCurrentMode()
// region Module methods
fun setInputMode(mode: Int) {
setSoftInputMode(mode)
}
fun setDefaultMode() {
setSoftInputMode(mDefaultMode)
}
fun preload() {
// no-op on Android
}
fun dismiss(
keepFocus: Boolean,
animated: Boolean,
) {
val activity = mReactContext.currentActivity
val view: View? = FocusedInputHolder.get()
if (view != null) {
UiThreadUtil.runOnUiThread {
fun maybeClearFocus() {
if (!keepFocus) {
view.clearFocus()
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !animated) {
controller.startControlRequest(view) { insetsController ->
insetsController.finish(false)
view.post {
maybeClearFocus()
}
}
} else {
val imm = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
imm?.hideSoftInputFromWindow(view.windowToken, 0)
maybeClearFocus()
}
}
}
}
fun setFocusTo(direction: String) {
if (direction == "current") {
UiThreadUtil.runOnUiThread {
FocusedInputHolder.focus()
}
return
}
val view: View? = FocusedInputHolder.get()
if (view != null) {
ViewHierarchyNavigator.setFocusTo(direction, view)
}
}
fun viewPositionInWindow(
viewTag: Double,
promise: Promise,
) {
UiThreadUtil.runOnUiThread {
val view = uiManager?.resolveView(viewTag.toInt())
if (view == null) {
promise.reject("E_VIEW_NOT_FOUND", "Could not find view for tag")
return@runOnUiThread
}
val location = view.screenLocation
val map = Arguments.createMap()
map.putDouble("x", location[0].toFloat().dp)
map.putDouble("y", location[1].toFloat().dp)
map.putDouble("width", view.width.toFloat().dp)
map.putDouble("height", view.height.toFloat().dp)
promise.resolve(map)
}
}
// endregion
// region Helpers
private fun setSoftInputMode(mode: Int) {
UiThreadUtil.runOnUiThread {
if (getCurrentMode() != mode) {
mReactContext.currentActivity?.window?.setSoftInputMode(mode)
}
}
}
private fun getCurrentMode(): Int =
mReactContext
.currentActivity
?.window
?.attributes
?.softInputMode
?: WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED
// endregion
// region Module constants
fun getConstants(): MutableMap<String, Any> = mutableMapOf("keyboardBorderRadius" to 0)
companion object {
const val NAME = "KeyboardController"
}
// endregion
}