react-native-nitro-input 0.3.1 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +114 -0
- package/README.md +75 -3
- package/android/src/main/cpp/JMaskEngine.cpp +45 -0
- package/android/src/main/cpp/JMaskEngine.hpp +9 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +49 -4
- package/android/src/main/java/com/margelo/nitro/nitroinput/MaskEngine.kt +16 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +266 -8
- package/android/src/main/java/com/margelo/nitro/nitroinput/UnmountHandoffs.kt +57 -0
- package/cpp/MaskEngine.cpp +294 -33
- package/cpp/MaskEngine.hpp +68 -0
- package/ios/HybridNitroInputView.swift +37 -4
- package/ios/NitroInputCommands.mm +10 -0
- package/ios/NitroInputView.swift +474 -20
- package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +8 -0
- package/lib/commonjs/src/NitroInput.js +116 -15
- package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +8 -0
- package/lib/module/src/NitroInput.js +117 -16
- package/lib/typescript/src/NitroInput.d.ts +55 -1
- package/lib/typescript/src/index.d.ts +1 -1
- package/lib/typescript/src/specs/NitroInput.nitro.d.ts +65 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +124 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +17 -0
- package/nitrogen/generated/android/c++/JNitroInputCharacterMapping.hpp +61 -0
- package/nitrogen/generated/android/c++/JNitroInputMaskAffinity.hpp +64 -0
- package/nitrogen/generated/android/c++/JNitroInputTextCase.hpp +61 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +40 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +52 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputCharacterMapping.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputMaskAffinity.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputTextCase.kt +24 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +25 -11
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +9 -0
- package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +68 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +48 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +9 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +111 -0
- package/nitrogen/generated/ios/swift/NitroInputCharacterMapping.swift +34 -0
- package/nitrogen/generated/ios/swift/NitroInputMaskAffinity.swift +48 -0
- package/nitrogen/generated/ios/swift/NitroInputTextCase.swift +44 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +17 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +26 -0
- package/nitrogen/generated/shared/c++/NitroInputCharacterMapping.hpp +87 -0
- package/nitrogen/generated/shared/c++/NitroInputMaskAffinity.hpp +84 -0
- package/nitrogen/generated/shared/c++/NitroInputTextCase.hpp +80 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +16 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +27 -0
- package/nitrogen/generated/shared/json/NitroInputViewConfig.json +8 -0
- package/package.json +2 -2
- package/src/NitroInput.tsx +190 -11
- package/src/index.ts +3 -0
- package/src/specs/NitroInput.nitro.ts +68 -0
|
@@ -79,6 +79,9 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
79
79
|
/** A caller-defined slot character for [Format.mask]. */
|
|
80
80
|
data class MaskNotation(val character: String, val characterSet: String, val isOptional: Boolean)
|
|
81
81
|
|
|
82
|
+
/** One character replaced by another before masking. */
|
|
83
|
+
data class MaskMapping(val from: String, val to: String)
|
|
84
|
+
|
|
82
85
|
/**
|
|
83
86
|
* The frame the view draws for itself: the outline (or fill) and the floating
|
|
84
87
|
* label. Separate from [Format] because it changes independently. Lengths are
|
|
@@ -110,6 +113,16 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
110
113
|
val maskNotations: List<MaskNotation> = emptyList(),
|
|
111
114
|
val maskAutocomplete: Boolean = true,
|
|
112
115
|
val maskAutoSkip: Boolean = false,
|
|
116
|
+
/** Mask mode: alternative patterns and how the best is chosen (`NitroInputMaskAffinity.value`). */
|
|
117
|
+
val maskAffinityFormats: List<String> = emptyList(),
|
|
118
|
+
val maskAffinityStrategy: Int = 0,
|
|
119
|
+
/** Mask mode: `NitroInputTextCase.value`. */
|
|
120
|
+
val maskTextCase: Int = 0,
|
|
121
|
+
val maskCharacterMap: List<MaskMapping> = emptyList(),
|
|
122
|
+
/** Mask mode: what an empty slot shows in the tail; empty = its notation. */
|
|
123
|
+
val maskSlotPlaceholder: String = "",
|
|
124
|
+
/** Mask mode: keep the rest of the placeholder after the typed text. */
|
|
125
|
+
val keepPlaceholder: Boolean = false,
|
|
113
126
|
val fractionDigits: Int = 2,
|
|
114
127
|
val maxIntegerDigits: Int = 15,
|
|
115
128
|
val groupingSeparator: String = ",",
|
|
@@ -170,6 +183,8 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
170
183
|
val showSoftInputOnFocus: Boolean = true,
|
|
171
184
|
val selectTextOnFocus: Boolean = false,
|
|
172
185
|
val clearTextOnFocus: Boolean = false,
|
|
186
|
+
/** How long a focused field leaving the screen keeps the IME up for the next one; 0 is off. */
|
|
187
|
+
val keyboardHandoffMs: Double = 0.0,
|
|
173
188
|
val contextMenuHidden: Boolean = false,
|
|
174
189
|
val spellCheck: Boolean = true,
|
|
175
190
|
/**
|
|
@@ -218,11 +233,20 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
218
233
|
val old = field
|
|
219
234
|
field = value
|
|
220
235
|
formatter.setFormat(value.fractionDigits, value.maxIntegerDigits, value.groupingSeparator, value.decimalSeparator)
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
236
|
+
val maskChanged = old.mask != value.mask || old.maskNotations != value.maskNotations ||
|
|
237
|
+
old.maskAffinityFormats != value.maskAffinityFormats || old.maskAffinityStrategy != value.maskAffinityStrategy ||
|
|
238
|
+
old.maskTextCase != value.maskTextCase || old.maskCharacterMap != value.maskCharacterMap ||
|
|
239
|
+
old.maskSlotPlaceholder != value.maskSlotPlaceholder
|
|
240
|
+
if (value.mode == Mode.MASK && (old.mode != Mode.MASK || maskChanged)) {
|
|
224
241
|
maskEngine.clearNotations()
|
|
225
242
|
value.maskNotations.forEach { maskEngine.addNotation(it.character, it.characterSet, it.isOptional) }
|
|
243
|
+
maskEngine.clearAffinityFormats()
|
|
244
|
+
value.maskAffinityFormats.forEach { maskEngine.addAffinityFormat(it) }
|
|
245
|
+
maskEngine.setAffinityStrategy(value.maskAffinityStrategy)
|
|
246
|
+
maskEngine.setTextCase(value.maskTextCase)
|
|
247
|
+
maskEngine.clearCharacterMap()
|
|
248
|
+
value.maskCharacterMap.forEach { maskEngine.addCharacterMapping(it.from, it.to) }
|
|
249
|
+
maskEngine.setSlotPlaceholder(value.maskSlotPlaceholder)
|
|
226
250
|
// A bad pattern leaves the engine inactive; the field then behaves as
|
|
227
251
|
// plain text rather than refusing every keystroke.
|
|
228
252
|
maskEngine.setFormat(value.mask)
|
|
@@ -382,6 +406,23 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
382
406
|
return offsetForTap(x)
|
|
383
407
|
}
|
|
384
408
|
|
|
409
|
+
private val ghostPaint = android.text.TextPaint()
|
|
410
|
+
|
|
411
|
+
override fun onDraw(canvas: Canvas) {
|
|
412
|
+
super.onDraw(canvas)
|
|
413
|
+
// Plain mode has no overlay, so `keepPlaceholder`'s tail is drawn here:
|
|
414
|
+
// from where the field's own text ends, on its baseline, in its paint
|
|
415
|
+
// and the hint colour. (onDraw's canvas is already scrolled.)
|
|
416
|
+
if (!ready || !keyboard.plain || layoutDirection == LAYOUT_DIRECTION_RTL) return
|
|
417
|
+
val tail = ghostTail
|
|
418
|
+
val textLayout = layout ?: return
|
|
419
|
+
if (tail.isEmpty()) return
|
|
420
|
+
ghostPaint.set(paint)
|
|
421
|
+
ghostPaint.color = currentHintTextColor
|
|
422
|
+
val x = compoundPaddingLeft + textLayout.getPrimaryHorizontal(length())
|
|
423
|
+
canvas.drawText(tail, x, baseline.toFloat(), ghostPaint)
|
|
424
|
+
}
|
|
425
|
+
|
|
385
426
|
override fun onSelectionChanged(selStart: Int, selEnd: Int) {
|
|
386
427
|
super.onSelectionChanged(selStart, selEnd)
|
|
387
428
|
if (!ready) return
|
|
@@ -418,7 +459,23 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
418
459
|
override fun onDestroyActionMode(mode: android.view.ActionMode?) {}
|
|
419
460
|
}
|
|
420
461
|
|
|
421
|
-
private val editText = HiddenEditText(context)
|
|
462
|
+
private val editText = HiddenEditText(context).apply {
|
|
463
|
+
// Its id mirrors the host's React tag (see `setId`); saved state would
|
|
464
|
+
// then restore the text by id, which the props own instead.
|
|
465
|
+
isSaveEnabled = false
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* The React tag, mirrored onto the focusable `EditText`: keyboard-controller
|
|
470
|
+
* identifies the focused input by that view's id, and without it sees none,
|
|
471
|
+
* so its KeyboardAwareScrollView never scrolls to this field.
|
|
472
|
+
*/
|
|
473
|
+
override fun setId(id: Int) {
|
|
474
|
+
super.setId(id)
|
|
475
|
+
// `setId` can run from the superclass constructor, before `editText` exists.
|
|
476
|
+
@Suppress("SENSELESS_COMPARISON")
|
|
477
|
+
if (editText != null) editText.id = id
|
|
478
|
+
}
|
|
422
479
|
private val overlay = object : View(context) {
|
|
423
480
|
override fun onDraw(canvas: Canvas) {
|
|
424
481
|
super.onDraw(canvas)
|
|
@@ -482,6 +539,14 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
482
539
|
private var pendingSizeReport = false
|
|
483
540
|
private var frameScheduled = false
|
|
484
541
|
private var applying = false
|
|
542
|
+
/** Clearing a stale focus flag in `focus()`: not a blur anyone should hear of. */
|
|
543
|
+
private var resyncingFocus = false
|
|
544
|
+
/**
|
|
545
|
+
* A `focus()` asked for while the view was off the window (uptime ms it
|
|
546
|
+
* stays good until; 0 = none), as a screen that is navigated back to
|
|
547
|
+
* refocuses its field before react-native-screens puts it back.
|
|
548
|
+
*/
|
|
549
|
+
private var pendingFocusUntil = 0L
|
|
485
550
|
private var batchDepth = 0
|
|
486
551
|
private var feedPending = false
|
|
487
552
|
private var feedCaret = -1
|
|
@@ -541,10 +606,13 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
541
606
|
defaultHighlightColor = editText.highlightColor
|
|
542
607
|
editText.addTextChangedListener(Watcher())
|
|
543
608
|
editText.setOnFocusChangeListener { _, focused ->
|
|
609
|
+
if (resyncingFocus) return@setOnFocusChangeListener
|
|
544
610
|
restartBlink()
|
|
545
611
|
syncLabelProgress(animated = true)
|
|
546
612
|
syncFocusProgress(animated = true)
|
|
547
613
|
if (focused) {
|
|
614
|
+
// This field took the keyboard over from a stand-in, if one held it.
|
|
615
|
+
KeyboardHandoff.claimed()
|
|
548
616
|
if (keyboard.clearTextOnFocus) {
|
|
549
617
|
clear()
|
|
550
618
|
} else if (keyboard.selectTextOnFocus) {
|
|
@@ -949,6 +1017,11 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
949
1017
|
editText.maxLines = 1
|
|
950
1018
|
editText.minLines = 1
|
|
951
1019
|
}
|
|
1020
|
+
// setSingleLine swaps the transformation the password input type installed
|
|
1021
|
+
// for a single-line one, and a plain field draws the EditText's own text.
|
|
1022
|
+
if (k.secureTextEntry) {
|
|
1023
|
+
editText.transformationMethod = android.text.method.PasswordTransformationMethod.getInstance()
|
|
1024
|
+
}
|
|
952
1025
|
editText.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI or when (k.returnKeyType) {
|
|
953
1026
|
ReturnKeyType.DEFAULT -> EditorInfo.IME_ACTION_UNSPECIFIED
|
|
954
1027
|
ReturnKeyType.DONE -> EditorInfo.IME_ACTION_DONE
|
|
@@ -1143,7 +1216,32 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1143
1216
|
|
|
1144
1217
|
fun focus() {
|
|
1145
1218
|
if (!keyboard.editable) return
|
|
1219
|
+
if (!isAttachedToWindow || !isShown) {
|
|
1220
|
+
// Off the window, or on it but hidden (a hidden <Activity>, or a
|
|
1221
|
+
// subtree shown in the same commit that asks for focus): an invisible
|
|
1222
|
+
// view cannot take focus. Honour it once the view is attached and shown.
|
|
1223
|
+
pendingFocusUntil = SystemClock.uptimeMillis() + 1000
|
|
1224
|
+
return
|
|
1225
|
+
}
|
|
1226
|
+
pendingFocusUntil = 0
|
|
1146
1227
|
editText.isFocusableInTouchMode = true
|
|
1228
|
+
val focusedInWindow = editText.rootView?.findFocus()
|
|
1229
|
+
if (editText.isFocused && focusedInWindow !== editText) {
|
|
1230
|
+
// Flagged focused while another view has the window's focus: a screen
|
|
1231
|
+
// container (react-native-screens' ScreensFrameLayout) skips
|
|
1232
|
+
// `clearFocus()` while a screen is invisible mid-transition, so a field
|
|
1233
|
+
// on a screen that was covered comes back still flagged. `requestFocus()`
|
|
1234
|
+
// is a no-op on it then, and the IME ignores it. The view that really
|
|
1235
|
+
// has focus lets go first (clearing ours alone would cut its focus chain
|
|
1236
|
+
// and leave it flagged in turn), then the stale flag is dropped quietly.
|
|
1237
|
+
focusedInWindow?.clearFocus()
|
|
1238
|
+
resyncingFocus = true
|
|
1239
|
+
try {
|
|
1240
|
+
editText.clearFocus()
|
|
1241
|
+
} finally {
|
|
1242
|
+
resyncingFocus = false
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1147
1245
|
if (!editText.hasFocus()) editText.requestFocus()
|
|
1148
1246
|
// `showSoftInputOnFocus: false` keeps focus and the caret but no keyboard;
|
|
1149
1247
|
// the EditText honours it on its own, so only the explicit ask is guarded.
|
|
@@ -1184,10 +1282,26 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1184
1282
|
}
|
|
1185
1283
|
|
|
1186
1284
|
fun blur() {
|
|
1187
|
-
|
|
1285
|
+
pendingFocusUntil = 0
|
|
1286
|
+
if (!editText.hasFocus()) {
|
|
1287
|
+
// `Keyboard.dismiss()` while a field that just left the screen holds the
|
|
1288
|
+
// keyboard for the next one: nothing is focused, so let it go now.
|
|
1289
|
+
KeyboardHandoff.letGo()
|
|
1290
|
+
return
|
|
1291
|
+
}
|
|
1292
|
+
editText.clearFocus()
|
|
1188
1293
|
inputMethodManager()?.hideSoftInputFromWindow(editText.windowToken, 0)
|
|
1189
1294
|
}
|
|
1190
1295
|
|
|
1296
|
+
/**
|
|
1297
|
+
* About to be unmounted (`NitroInput` calls this before the view is removed):
|
|
1298
|
+
* hand the IME to a stand-in so it stays up for the next field to focus.
|
|
1299
|
+
*/
|
|
1300
|
+
fun handOffKeyboardIfFocused() {
|
|
1301
|
+
if (keyboard.keyboardHandoffMs <= 0 || !keyboard.showSoftInputOnFocus || !editText.hasFocus()) return
|
|
1302
|
+
KeyboardHandoff.hold(editText, keyboard.keyboardHandoffMs)
|
|
1303
|
+
}
|
|
1304
|
+
|
|
1191
1305
|
/** Empties the field, reflowing the characters away. */
|
|
1192
1306
|
fun clear() = setProgrammatic("", notify = true)
|
|
1193
1307
|
|
|
@@ -1241,6 +1355,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1241
1355
|
*/
|
|
1242
1356
|
fun resetForRecycle() {
|
|
1243
1357
|
worklets = Worklets()
|
|
1358
|
+
pendingFocusUntil = 0
|
|
1244
1359
|
inputFrame = Frame()
|
|
1245
1360
|
labelProgress = 0f
|
|
1246
1361
|
notchProgress = 0f
|
|
@@ -1288,8 +1403,28 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1288
1403
|
restartBlink()
|
|
1289
1404
|
syncAccessibilityFromHost()
|
|
1290
1405
|
maybeAutoFocus()
|
|
1406
|
+
focusIfPending()
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
override fun onVisibilityChanged(changedView: View, visibility: Int) {
|
|
1410
|
+
super.onVisibilityChanged(changedView, visibility)
|
|
1411
|
+
if (isShown) focusIfPending()
|
|
1291
1412
|
}
|
|
1292
1413
|
|
|
1414
|
+
/** A `focus()` asked for while the view was off the window or hidden. */
|
|
1415
|
+
private fun focusIfPending() {
|
|
1416
|
+
if (pendingFocusUntil <= 0 || !isAttachedToWindow || !isShown) return
|
|
1417
|
+
// Only while the keyboard is still up (held for this field, or another
|
|
1418
|
+
// field has it): a keyboard dismissed during the transition stays down.
|
|
1419
|
+
val wanted = SystemClock.uptimeMillis() < pendingFocusUntil && (KeyboardHandoff.isHolding || imeVisible())
|
|
1420
|
+
pendingFocusUntil = 0
|
|
1421
|
+
if (wanted) post { focus() }
|
|
1422
|
+
}
|
|
1423
|
+
|
|
1424
|
+
private fun imeVisible(): Boolean =
|
|
1425
|
+
androidx.core.view.ViewCompat.getRootWindowInsets(this)
|
|
1426
|
+
?.isVisible(androidx.core.view.WindowInsetsCompat.Type.ime()) == true
|
|
1427
|
+
|
|
1293
1428
|
override fun onDetachedFromWindow() {
|
|
1294
1429
|
stopAnimation()
|
|
1295
1430
|
removeCallbacks(blink)
|
|
@@ -1528,7 +1663,12 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1528
1663
|
// A plain field draws its own text and never feeds the engine, but its
|
|
1529
1664
|
// width still has to reach React for `autoWidth`; every text, font, affix
|
|
1530
1665
|
// and hint change comes through here.
|
|
1531
|
-
if (keyboard.plain) {
|
|
1666
|
+
if (keyboard.plain) {
|
|
1667
|
+
// The EditText draws the kept placeholder's tail itself (HiddenEditText.onDraw).
|
|
1668
|
+
editText.invalidate()
|
|
1669
|
+
reportIntrinsicSize()
|
|
1670
|
+
return
|
|
1671
|
+
}
|
|
1532
1672
|
val f = fonts
|
|
1533
1673
|
engine.setReduceMotion(animationsDisabled())
|
|
1534
1674
|
engine.beginText()
|
|
@@ -1549,7 +1689,13 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1549
1689
|
// body glyph before a suffix carry the seams instead.
|
|
1550
1690
|
if (signed) addRun(shown.substring(0, 1), Role.BODY, f, placeholder = false)
|
|
1551
1691
|
addRun(format.prefix, Role.PREFIX, f, placeholder = false, lastSpacing = f.prefixSeam)
|
|
1692
|
+
val tail = if (showPlaceholder) "" else ghostTail
|
|
1552
1693
|
addRun(if (signed) shown.substring(1) else shown, Role.BODY, f, placeholder = showPlaceholder,
|
|
1694
|
+
lastSpacing = if (format.suffix.isNotEmpty() && tail.isEmpty()) f.suffixSeam else null)
|
|
1695
|
+
// `keepPlaceholder`: the rest of the mask trails the text as placeholder
|
|
1696
|
+
// glyphs. They come after every typed glyph, so the caret - which counts
|
|
1697
|
+
// body glyphs in order - never lands among them.
|
|
1698
|
+
addRun(tail, Role.BODY, f, placeholder = true,
|
|
1553
1699
|
lastSpacing = if (format.suffix.isNotEmpty()) f.suffixSeam else null)
|
|
1554
1700
|
addRun(format.suffix, Role.SUFFIX, f, placeholder = false)
|
|
1555
1701
|
engine.commitText(caret, now())
|
|
@@ -1720,7 +1866,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1720
1866
|
val body = when {
|
|
1721
1867
|
text.isEmpty() -> effectivePlaceholder
|
|
1722
1868
|
keyboard.secureTextEntry -> "\u2022".repeat(text.codePointCount(0, text.length))
|
|
1723
|
-
else -> text
|
|
1869
|
+
else -> text + ghostTail
|
|
1724
1870
|
}
|
|
1725
1871
|
// The edit text's own letter spacing follows every glyph; the affixes take their room with their seams.
|
|
1726
1872
|
val f = fonts
|
|
@@ -1940,7 +2086,47 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1940
2086
|
* waits until the label has floated clear rather than printing over it.
|
|
1941
2087
|
*/
|
|
1942
2088
|
private val effectivePlaceholder: String
|
|
1943
|
-
get() = if (inputFrame.hasLabel && !labelShouldFloat) "" else
|
|
2089
|
+
get() = if (inputFrame.hasLabel && !labelShouldFloat) "" else basePlaceholder
|
|
2090
|
+
|
|
2091
|
+
/**
|
|
2092
|
+
* `keepPlaceholder` without a `placeholder` shows the mask itself, empty
|
|
2093
|
+
* slots as `maskSlotPlaceholder`: "+1 (___) ___-____".
|
|
2094
|
+
*/
|
|
2095
|
+
private val basePlaceholder: String
|
|
2096
|
+
get() {
|
|
2097
|
+
if (format.placeholder.isEmpty() && format.mode == Mode.MASK && format.keepPlaceholder && maskEngine.isActive()) {
|
|
2098
|
+
maskEngine.applyAll("", 0, true, false, false)
|
|
2099
|
+
return maskEngine.lastTailPlaceholder()
|
|
2100
|
+
}
|
|
2101
|
+
return format.placeholder
|
|
2102
|
+
}
|
|
2103
|
+
|
|
2104
|
+
/**
|
|
2105
|
+
* `keepPlaceholder`: what is still to come after the typed text, drawn in the
|
|
2106
|
+
* placeholder colour. The caller's placeholder from where the text ends -
|
|
2107
|
+
* "1234 5" leaves "678 9012" of "1234 5678 9012" - or, without one, the rest
|
|
2108
|
+
* of the mask. The placeholder is written for `mask`, so when one of
|
|
2109
|
+
* `maskAffinityFormats` has taken over (a 4-6-5 Amex under a 4-4-4-4
|
|
2110
|
+
* placeholder) its characters no longer line up, and that format's own tail
|
|
2111
|
+
* is shown instead. A complete text has nothing to come.
|
|
2112
|
+
*/
|
|
2113
|
+
private val ghostTail: String
|
|
2114
|
+
get() {
|
|
2115
|
+
if (format.mode != Mode.MASK || !format.keepPlaceholder || text.isEmpty() || keyboard.secureTextEntry ||
|
|
2116
|
+
!maskEngine.isActive()
|
|
2117
|
+
) {
|
|
2118
|
+
return ""
|
|
2119
|
+
}
|
|
2120
|
+
val typed = text.codePointCount(0, text.length)
|
|
2121
|
+
maskEngine.applyAll(text, typed, true, false, false)
|
|
2122
|
+
if (maskEngine.lastComplete()) return ""
|
|
2123
|
+
val placeholder = format.placeholder
|
|
2124
|
+
if (placeholder.isNotEmpty() && maskEngine.lastFormatIndex() == 0) {
|
|
2125
|
+
val total = placeholder.codePointCount(0, placeholder.length)
|
|
2126
|
+
return if (typed < total) placeholder.substring(placeholder.offsetByCodePoints(0, typed)) else ""
|
|
2127
|
+
}
|
|
2128
|
+
return maskEngine.lastTailPlaceholder()
|
|
2129
|
+
}
|
|
1944
2130
|
|
|
1945
2131
|
private fun resolvedStrokeColor(): Int {
|
|
1946
2132
|
val base = inputFrame.strokeColor ?: mutedTextColor
|
|
@@ -2255,3 +2441,75 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
2255
2441
|
private val ROLES = Role.values()
|
|
2256
2442
|
}
|
|
2257
2443
|
}
|
|
2444
|
+
|
|
2445
|
+
/**
|
|
2446
|
+
* Keeps the IME up while a focused field is removed (its screen popped or its
|
|
2447
|
+
* element unmounted), for the next field to take over.
|
|
2448
|
+
*
|
|
2449
|
+
* Android hides the IME when the focused editor is removed from the window,
|
|
2450
|
+
* and shows it again when the next field asks: a pop from one field's screen
|
|
2451
|
+
* back to another's drops the keyboard and raises it. Moving focus to another
|
|
2452
|
+
* editor keeps it up instead, so a 1x1 stand-in `EditText` configured like the
|
|
2453
|
+
* leaving field takes focus first; the next field to focus takes the IME from
|
|
2454
|
+
* it in place. If none does within the hold, the stand-in hides the IME.
|
|
2455
|
+
* It has no id, which keyboard-controller reads as "no focused input".
|
|
2456
|
+
*/
|
|
2457
|
+
internal object KeyboardHandoff {
|
|
2458
|
+
private val handler = Handler(Looper.getMainLooper())
|
|
2459
|
+
private var holder: android.widget.EditText? = null
|
|
2460
|
+
private val timeout = Runnable { letGo() }
|
|
2461
|
+
|
|
2462
|
+
val isHolding: Boolean get() = holder != null
|
|
2463
|
+
|
|
2464
|
+
fun hold(from: android.widget.EditText, milliseconds: Double) {
|
|
2465
|
+
val content = from.rootView?.findViewById<android.view.ViewGroup>(android.R.id.content) ?: return
|
|
2466
|
+
drop()
|
|
2467
|
+
val field = android.widget.EditText(from.context).apply {
|
|
2468
|
+
inputType = from.inputType
|
|
2469
|
+
imeOptions = from.imeOptions
|
|
2470
|
+
alpha = 0f
|
|
2471
|
+
translationX = -10000f
|
|
2472
|
+
background = null
|
|
2473
|
+
isSaveEnabled = false
|
|
2474
|
+
isFocusableInTouchMode = true
|
|
2475
|
+
importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_NO
|
|
2476
|
+
}
|
|
2477
|
+
content.addView(field, FrameLayout.LayoutParams(1, 1))
|
|
2478
|
+
if (!field.requestFocus()) {
|
|
2479
|
+
content.removeView(field)
|
|
2480
|
+
return
|
|
2481
|
+
}
|
|
2482
|
+
holder = field
|
|
2483
|
+
handler.postDelayed(timeout, milliseconds.toLong())
|
|
2484
|
+
}
|
|
2485
|
+
|
|
2486
|
+
/** A field took the IME over: the stand-in can go, without hiding it. */
|
|
2487
|
+
fun claimed() {
|
|
2488
|
+
val field = holder ?: return
|
|
2489
|
+
if (field.hasFocus()) return
|
|
2490
|
+
handler.removeCallbacks(timeout)
|
|
2491
|
+
holder = null
|
|
2492
|
+
// Not in the middle of the focus change that got us here.
|
|
2493
|
+
handler.post { (field.parent as? android.view.ViewGroup)?.removeView(field) }
|
|
2494
|
+
}
|
|
2495
|
+
|
|
2496
|
+
/** Nothing claimed the IME in time, or `Keyboard.dismiss()`: hide it. */
|
|
2497
|
+
fun letGo() {
|
|
2498
|
+
handler.removeCallbacks(timeout)
|
|
2499
|
+
val field = holder ?: return
|
|
2500
|
+
holder = null
|
|
2501
|
+
if (field.hasFocus()) {
|
|
2502
|
+
val imm = field.context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
|
|
2503
|
+
imm?.hideSoftInputFromWindow(field.windowToken, 0)
|
|
2504
|
+
field.clearFocus()
|
|
2505
|
+
}
|
|
2506
|
+
(field.parent as? android.view.ViewGroup)?.removeView(field)
|
|
2507
|
+
}
|
|
2508
|
+
|
|
2509
|
+
private fun drop() {
|
|
2510
|
+
handler.removeCallbacks(timeout)
|
|
2511
|
+
val field = holder ?: return
|
|
2512
|
+
holder = null
|
|
2513
|
+
(field.parent as? android.view.ViewGroup)?.removeView(field)
|
|
2514
|
+
}
|
|
2515
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
package com.margelo.nitro.nitroinput
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.bridge.ReactContext
|
|
4
|
+
import com.facebook.react.bridge.UIManager
|
|
5
|
+
import com.facebook.react.bridge.UIManagerListener
|
|
6
|
+
import com.facebook.react.common.annotations.UnstableReactNativeAPI
|
|
7
|
+
import com.facebook.react.uimanager.UIManagerHelper
|
|
8
|
+
import com.facebook.react.uimanager.common.UIManagerType
|
|
9
|
+
import java.util.Collections
|
|
10
|
+
import java.util.IdentityHashMap
|
|
11
|
+
import java.util.concurrent.ConcurrentLinkedQueue
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Runs `keyboardHandoffMs` handoffs on the UI thread right before Fabric
|
|
15
|
+
* applies the next batch of view changes.
|
|
16
|
+
*
|
|
17
|
+
* `NitroInput` asks for the handoff from React's commit, on the JS thread,
|
|
18
|
+
* ahead of the commit that removes the field. A message posted from there to
|
|
19
|
+
* the UI thread does not reliably get there first: the UI thread can already be
|
|
20
|
+
* in the frame that applies the removal (seen 42 ms late on a Galaxy A22), and
|
|
21
|
+
* a removed editor takes the IME with it. Fabric's `willMountItems` runs on the
|
|
22
|
+
* UI thread before any view in the batch is touched, so the field still has
|
|
23
|
+
* focus when the stand-in takes it over.
|
|
24
|
+
*/
|
|
25
|
+
@OptIn(UnstableReactNativeAPI::class)
|
|
26
|
+
internal object UnmountHandoffs : UIManagerListener {
|
|
27
|
+
private val pending = ConcurrentLinkedQueue<NitroInputView>()
|
|
28
|
+
private val listeningTo = Collections.newSetFromMap(IdentityHashMap<UIManager, Boolean>())
|
|
29
|
+
|
|
30
|
+
/** Queues `view`'s handoff for the next batch; false when there is no Fabric UIManager to wait for. */
|
|
31
|
+
fun request(context: ReactContext, view: NitroInputView): Boolean {
|
|
32
|
+
val uiManager = UIManagerHelper.getUIManager(context, UIManagerType.FABRIC) ?: return false
|
|
33
|
+
synchronized(listeningTo) {
|
|
34
|
+
if (listeningTo.add(uiManager)) uiManager.addUIManagerEventListener(this)
|
|
35
|
+
}
|
|
36
|
+
pending.add(view)
|
|
37
|
+
return true
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private fun drain() {
|
|
41
|
+
while (true) {
|
|
42
|
+
val view = pending.poll() ?: return
|
|
43
|
+
view.handOffKeyboardIfFocused()
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
override fun willMountItems(uiManager: UIManager) = drain()
|
|
48
|
+
|
|
49
|
+
// Every frame, mount items or not: a request whose batch never came still runs.
|
|
50
|
+
override fun didDispatchMountItems(uiManager: UIManager) = drain()
|
|
51
|
+
|
|
52
|
+
override fun willDispatchViewUpdates(uiManager: UIManager) {}
|
|
53
|
+
|
|
54
|
+
override fun didMountItems(uiManager: UIManager) {}
|
|
55
|
+
|
|
56
|
+
override fun didScheduleMountItems(uiManager: UIManager) {}
|
|
57
|
+
}
|