-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathKeyboardControllerModuleImpl.kt
More file actions
99 lines (83 loc) · 2.49 KB
/
KeyboardControllerModuleImpl.kt
File metadata and controls
99 lines (83 loc) · 2.49 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
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.ReactApplicationContext
import com.facebook.react.bridge.UiThreadUtil
import com.reactnativekeyboardcontroller.interactive.KeyboardAnimationController
import com.reactnativekeyboardcontroller.traversal.FocusedInputHolder
import com.reactnativekeyboardcontroller.traversal.ViewHierarchyNavigator
class KeyboardControllerModuleImpl(
private val mReactContext: ReactApplicationContext,
) {
private val controller = KeyboardAnimationController()
private val mDefaultMode: Int = getCurrentMode()
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)
}
}
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
companion object {
const val NAME = "KeyboardController"
}
}