react-native-nitro-input 0.3.8 → 0.4.1

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.
Files changed (108) hide show
  1. package/CHANGELOG.md +105 -0
  2. package/README.md +6 -1
  3. package/android/src/main/java/com/margelo/nitro/nitroinput/DigitSlots.kt +143 -0
  4. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputMeasure.kt +52 -0
  5. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +80 -80
  6. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +18 -18
  7. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroTextView.kt +14 -14
  8. package/android/src/main/java/com/margelo/nitro/nitroinput/InputFonts.kt +244 -0
  9. package/android/src/main/java/com/margelo/nitro/nitroinput/InputSizing.kt +404 -0
  10. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +168 -338
  11. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +178 -30
  12. package/ios/HybridNitroInputView.swift +73 -75
  13. package/ios/HybridNitroNumberView.swift +18 -18
  14. package/ios/HybridNitroTextView.swift +14 -14
  15. package/ios/InputSizing.swift +628 -0
  16. package/ios/NitroInputView.swift +180 -209
  17. package/ios/NitroNumberView.swift +98 -23
  18. package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +14 -8
  19. package/lib/commonjs/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -6
  20. package/lib/commonjs/nitrogen/generated/shared/json/NitroTextViewConfig.json +6 -6
  21. package/lib/commonjs/src/NitroInput.js +166 -23
  22. package/lib/commonjs/src/NitroNumber.js +1 -1
  23. package/lib/commonjs/src/NitroText.js +6 -6
  24. package/lib/commonjs/src/specs/NitroInputMeasure.nitro.js +2 -0
  25. package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +14 -8
  26. package/lib/module/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -6
  27. package/lib/module/nitrogen/generated/shared/json/NitroTextViewConfig.json +6 -6
  28. package/lib/module/src/NitroInput.js +167 -24
  29. package/lib/module/src/NitroNumber.js +1 -1
  30. package/lib/module/src/NitroText.js +6 -6
  31. package/lib/module/src/specs/NitroInputMeasure.nitro.js +1 -0
  32. package/lib/typescript/src/NitroInput.d.ts +20 -16
  33. package/lib/typescript/src/specs/NitroInput.nitro.d.ts +21 -8
  34. package/lib/typescript/src/specs/NitroInputMeasure.nitro.d.ts +69 -0
  35. package/lib/typescript/src/specs/NitroNumber.nitro.d.ts +6 -6
  36. package/lib/typescript/src/specs/NitroText.nitro.d.ts +6 -6
  37. package/nitro.json +10 -0
  38. package/nitrogen/generated/android/NitroInput+autolinking.cmake +2 -0
  39. package/nitrogen/generated/android/NitroInputOnLoad.cpp +16 -0
  40. package/nitrogen/generated/android/c++/JHybridNitroInputMeasureSpec.cpp +93 -0
  41. package/nitrogen/generated/android/c++/JHybridNitroInputMeasureSpec.hpp +63 -0
  42. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +94 -40
  43. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +28 -16
  44. package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.cpp +30 -30
  45. package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.hpp +12 -12
  46. package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.cpp +30 -30
  47. package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.hpp +12 -12
  48. package/nitrogen/generated/android/c++/JNitroInputSize.hpp +61 -0
  49. package/nitrogen/generated/android/c++/JNitroInputSizing.hpp +292 -0
  50. package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +54 -24
  51. package/nitrogen/generated/android/c++/views/JHybridNitroNumberViewStateUpdater.cpp +18 -18
  52. package/nitrogen/generated/android/c++/views/JHybridNitroTextViewStateUpdater.cpp +18 -18
  53. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputMeasureSpec.kt +56 -0
  54. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +44 -8
  55. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberViewSpec.kt +6 -6
  56. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroTextViewSpec.kt +6 -6
  57. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputSize.kt +56 -0
  58. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputSizing.kt +256 -0
  59. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.cpp +17 -0
  60. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +29 -0
  61. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +11 -0
  62. package/nitrogen/generated/ios/NitroInputAutolinking.mm +8 -0
  63. package/nitrogen/generated/ios/NitroInputAutolinking.swift +12 -0
  64. package/nitrogen/generated/ios/c++/HybridNitroInputMeasureSpecSwift.cpp +11 -0
  65. package/nitrogen/generated/ios/c++/HybridNitroInputMeasureSpecSwift.hpp +112 -0
  66. package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +68 -32
  67. package/nitrogen/generated/ios/c++/HybridNitroNumberViewSpecSwift.hpp +24 -24
  68. package/nitrogen/generated/ios/c++/HybridNitroTextViewSpecSwift.hpp +24 -24
  69. package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +68 -32
  70. package/nitrogen/generated/ios/c++/views/HybridNitroNumberViewComponent.mm +24 -24
  71. package/nitrogen/generated/ios/c++/views/HybridNitroTextViewComponent.mm +24 -24
  72. package/nitrogen/generated/ios/swift/HybridNitroInputMeasureSpec.swift +55 -0
  73. package/nitrogen/generated/ios/swift/HybridNitroInputMeasureSpec_cxx.swift +138 -0
  74. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +14 -8
  75. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +90 -24
  76. package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec.swift +6 -6
  77. package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec_cxx.swift +18 -18
  78. package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec.swift +6 -6
  79. package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec_cxx.swift +18 -18
  80. package/nitrogen/generated/ios/swift/NitroInputSize.swift +34 -0
  81. package/nitrogen/generated/ios/swift/NitroInputSizing.swift +252 -0
  82. package/nitrogen/generated/shared/c++/HybridNitroInputMeasureSpec.cpp +21 -0
  83. package/nitrogen/generated/shared/c++/HybridNitroInputMeasureSpec.hpp +66 -0
  84. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +28 -16
  85. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +28 -16
  86. package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.cpp +12 -12
  87. package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.hpp +12 -12
  88. package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.cpp +12 -12
  89. package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.hpp +12 -12
  90. package/nitrogen/generated/shared/c++/NitroInputSize.hpp +87 -0
  91. package/nitrogen/generated/shared/c++/NitroInputSizing.hpp +271 -0
  92. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +28 -16
  93. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +42 -24
  94. package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.cpp +12 -12
  95. package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.hpp +18 -18
  96. package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.cpp +12 -12
  97. package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.hpp +18 -18
  98. package/nitrogen/generated/shared/json/NitroInputViewConfig.json +14 -8
  99. package/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -6
  100. package/nitrogen/generated/shared/json/NitroTextViewConfig.json +6 -6
  101. package/package.json +4 -2
  102. package/src/NitroInput.tsx +215 -64
  103. package/src/NitroNumber.tsx +6 -6
  104. package/src/NitroText.tsx +6 -6
  105. package/src/specs/NitroInput.nitro.ts +21 -8
  106. package/src/specs/NitroInputMeasure.nitro.ts +78 -0
  107. package/src/specs/NitroNumber.nitro.ts +6 -6
  108. package/src/specs/NitroText.nitro.ts +6 -6
@@ -8,9 +8,7 @@ import android.graphics.Canvas
8
8
  import android.graphics.Color
9
9
  import android.graphics.Paint
10
10
  import android.graphics.Path
11
- import android.graphics.Rect
12
11
  import android.graphics.RectF
13
- import android.graphics.Typeface
14
12
  import android.os.Build
15
13
  import android.os.Handler
16
14
  import android.os.Looper
@@ -35,9 +33,7 @@ import android.widget.FrameLayout
35
33
  import android.widget.TextView
36
34
  import androidx.appcompat.widget.AppCompatEditText
37
35
  import androidx.core.widget.TextViewCompat
38
- import com.facebook.react.common.assets.ReactFontManager
39
36
  import kotlin.math.abs
40
- import kotlin.math.ceil
41
37
  import kotlin.math.max
42
38
  import kotlin.math.min
43
39
  import kotlin.math.roundToInt
@@ -146,6 +142,10 @@ class NitroInputView(context: Context) : FrameLayout(context) {
146
142
  val suffixAlign: AffixAlign = AffixAlign.BASELINE,
147
143
  /** Dp added after every glyph, as `Text`'s letterSpacing; an affix gets it in proportion to its size. */
148
144
  val letterSpacing: Float = 0f,
145
+ /** The font's tabular figures (`tnum`) rather than its proportional ones. */
146
+ val tabularNums: Boolean = true,
147
+ /** Every digit laid out as wide as the widest, centred ([DigitSlots]): tabular figures for a font without them. */
148
+ val slotDigits: Boolean = false,
149
149
  /** Dp between the prefix and the text, and between the text and the suffix, in place of the letter spacing there. */
150
150
  val prefixSpacing: Float? = null,
151
151
  val suffixSpacing: Float? = null,
@@ -232,25 +232,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
232
232
  if (field == value) return
233
233
  val old = field
234
234
  field = value
235
- formatter.setFormat(value.fractionDigits, value.maxIntegerDigits, value.groupingSeparator, value.decimalSeparator)
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)) {
241
- maskEngine.clearNotations()
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)
250
- // A bad pattern leaves the engine inactive; the field then behaves as
251
- // plain text rather than refusing every keystroke.
252
- maskEngine.setFormat(value.mask)
253
- }
235
+ InputText.configure(formatter, maskEngine, old, value)
254
236
  if (old.mode != value.mode) {
255
237
  rebuildFonts()
256
238
  applyKeyboard()
@@ -325,9 +307,45 @@ class NitroInputView(context: Context) : FrameLayout(context) {
325
307
  if (field == value) return
326
308
  field = value
327
309
  applyEditTextLayout()
310
+ // The gravity alone did nothing: the plain field's text alignment, set
311
+ // when it became plain (before `textAlign` arrived in the same commit),
312
+ // overrides it, so a centred or right-aligned field stayed at the start.
313
+ applyTextAlignment()
328
314
  overlay.invalidate()
329
315
  }
330
316
 
317
+ /**
318
+ * A plain field's own text alignment. `AUTO` is the start edge of the layout
319
+ * direction; `LEFT` and `RIGHT` are absolute, which only
320
+ * `TEXT_ALIGNMENT_GRAVITY` over an absolute gravity gives - `VIEW_START` /
321
+ * `VIEW_END` would flip with the direction. A reflowing field's edit text is
322
+ * hidden behind the overlay and stays at the start.
323
+ */
324
+ private fun applyTextAlignment() {
325
+ editText.textAlignment = if (!keyboard.plain) TEXT_ALIGNMENT_VIEW_START else when (alignment) {
326
+ Alignment.AUTO -> TEXT_ALIGNMENT_VIEW_START
327
+ Alignment.CENTER -> TEXT_ALIGNMENT_CENTER
328
+ Alignment.LEFT, Alignment.RIGHT -> TEXT_ALIGNMENT_GRAVITY
329
+ }
330
+ }
331
+
332
+ /**
333
+ * The style's padding and border, in dp. React Native lays an iOS view's
334
+ * content out inside them, but never hands a Nitro view on Android its
335
+ * padding, so the text ran to the field's edges: they inset the edit text,
336
+ * the overlay and the frame here instead (JS sends them; see `NitroInput`).
337
+ */
338
+ var contentInsets: android.graphics.RectF = android.graphics.RectF()
339
+ set(value) {
340
+ if (field == value) return
341
+ field = value
342
+ setPadding(
343
+ (value.left * density).roundToInt(), (value.top * density).roundToInt(),
344
+ (value.right * density).roundToInt(), (value.bottom * density).roundToInt(),
345
+ )
346
+ invalidate()
347
+ }
348
+
331
349
  /** The outline / fill and floating label this view draws for itself. */
332
350
  var inputFrame: Frame = Frame()
333
351
  set(value) {
@@ -420,7 +438,23 @@ class NitroInputView(context: Context) : FrameLayout(context) {
420
438
  ghostPaint.set(paint)
421
439
  ghostPaint.color = currentHintTextColor
422
440
  val x = compoundPaddingLeft + textLayout.getPrimaryHorizontal(length())
423
- canvas.drawText(tail, x, baseline.toFloat(), ghostPaint)
441
+ // Inside the text area, as the field clips its own text and hint: drawn
442
+ // after super.onDraw has restored the canvas, a tail longer than the
443
+ // room left ran over the padding to the field's edge (a full field, or
444
+ // a long placeholder in a narrow one).
445
+ canvas.save()
446
+ canvas.clipRect(
447
+ (scrollX + compoundPaddingLeft).toFloat(),
448
+ scrollY.toFloat(),
449
+ (scrollX + width - compoundPaddingRight).toFloat(),
450
+ (scrollY + height).toFloat(),
451
+ )
452
+ if (typography.slotDigits) {
453
+ DigitSlots.draw(canvas, tail, x, baseline.toFloat(), ghostPaint)
454
+ } else {
455
+ canvas.drawText(tail, x, baseline.toFloat(), ghostPaint)
456
+ }
457
+ canvas.restore()
424
458
  }
425
459
 
426
460
  override fun onSelectionChanged(selStart: Int, selEnd: Int) {
@@ -528,7 +562,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
528
562
  private var resolvedPlaceholderColor = mutedTextColor
529
563
  /** `caret.color`, or the theme's accent. */
530
564
  private var resolvedCaretColor = themeCaretColor
531
- private var fonts: FontSet = FontSet(Typography())
565
+ private var fonts: InputFonts = makeFonts(Typography())
532
566
  private val glyphs = ArrayList<Glyph>()
533
567
  /** Reused per frame so the JNI hop never allocates; grown on demand. */
534
568
  private var frameBuffer = DoubleArray(3 + 11 * 64)
@@ -613,6 +647,16 @@ class NitroInputView(context: Context) : FrameLayout(context) {
613
647
  editText.setSingleLine()
614
648
  defaultHighlightColor = editText.highlightColor
615
649
  editText.addTextChangedListener(Watcher())
650
+ // After it, and on every change, the engine's own writes included.
651
+ editText.addTextChangedListener(object : TextWatcher {
652
+ override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
653
+
654
+ override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
655
+
656
+ override fun afterTextChanged(s: Editable) {
657
+ DigitSlots.apply(s, editText.paint, off = !slotsDigits)
658
+ }
659
+ })
616
660
  editText.setOnFocusChangeListener { _, focused ->
617
661
  if (resyncingFocus) return@setOnFocusChangeListener
618
662
  restartBlink()
@@ -687,172 +731,6 @@ class NitroInputView(context: Context) : FrameLayout(context) {
687
731
 
688
732
  // region Fonts
689
733
 
690
- private enum class Role { PREFIX, BODY, SUFFIX }
691
-
692
- /** Body / prefix / suffix paints with per-character advance caches. */
693
- private inner class FontSet(t: Typography) {
694
- private val scale = systemFontMultiplier(t)
695
- val body = makePaint(t, t.fontSize * scale)
696
- val prefix = makePaint(t, t.prefixFontSize * scale)
697
- val suffix = makePaint(t, t.suffixFontSize * scale)
698
- private val prefixAlign = t.prefixAlign
699
- private val suffixAlign = t.suffixAlign
700
- /** Letter spacing after a body / prefix / suffix glyph, the seams (null: the letter spacing) and the affix offsets, in px. */
701
- val bodySpacing = t.letterSpacing * scale * density
702
- val prefixLetterSpacing = bodySpacing * (t.prefixFontSize / t.fontSize)
703
- val suffixLetterSpacing = bodySpacing * (t.suffixFontSize / t.fontSize)
704
- val prefixSeam = t.prefixSpacing?.let { it * scale * density }
705
- val suffixSeam = t.suffixSpacing?.let { it * scale * density }
706
- val prefixOffset = t.prefixOffset * scale * density
707
- val suffixOffset = t.suffixOffset * scale * density
708
- private val bodyMetrics: Paint.FontMetrics = body.fontMetrics
709
- /** The font's own line box, before any `lineHeight` override. */
710
- val fontLineHeight: Float = ceil(bodyMetrics.descent - bodyMetrics.ascent)
711
- /** Height of the line box in px: `lineHeight` when given, else the font's. */
712
- val lineHeight: Float = if (t.lineHeightPx > 0f) t.lineHeightPx else fontLineHeight
713
- /**
714
- * How far to push the glyphs down inside the line box. The platform stacks
715
- * the extra leading above the line, so without this the run rides high;
716
- * with a line height *tighter* than the font it rides low, and the same
717
- * halving corrects both. React Native skips the tighter case.
718
- */
719
- val baselineNudge: Float = (lineHeight - fontLineHeight) / 2f
720
- private val widthCaches = Array(3) { HashMap<Int, Float>() }
721
- private val capHeightCache = HashMap<Role, Float>()
722
- // By role, then text: a key built from both was a string allocated for
723
- // every affix glyph on every frame of a reflow.
724
- private val inkDescentCaches = Array(3) { HashMap<String, Float>() }
725
- // Each role's metrics, read once: `Paint.fontMetrics` allocates, and the
726
- // affixes' baselines are asked for on every frame of a reflow.
727
- private val metrics = arrayOf(prefix.fontMetrics, bodyMetrics, suffix.fontMetrics)
728
- private val shapedCaches = Array(3) { HashMap<Int, ShapedText>() }
729
-
730
- /**
731
- * One character of [role] shaped once, for [Canvas.drawGlyphs]: `drawText`
732
- * runs the text shaper on every call, and a reflow draws every glyph on
733
- * every frame of its animation (see NitroNumberView's `shaped`).
734
- */
735
- @androidx.annotation.RequiresApi(Build.VERSION_CODES.S)
736
- fun shaped(codePoint: Int, role: Role): ShapedText =
737
- shapedCaches[role.ordinal].getOrPut(codePoint) { ShapedText.of(charString(codePoint), paint(role)) }
738
-
739
- fun paint(role: Role): TextPaint = when (role) {
740
- Role.BODY -> body
741
- Role.PREFIX -> prefix
742
- Role.SUFFIX -> suffix
743
- }
744
-
745
- /** Advance of one character, in px. */
746
- fun width(codePoint: Int, role: Role): Float =
747
- widthCaches[role.ordinal].getOrPut(codePoint) { paint(role).measureText(charString(codePoint)) }
748
-
749
- /** Sum of the per-character advances of [text] (what the engine lays out). */
750
- fun width(text: String, role: Role): Float {
751
- var total = 0f
752
- var i = 0
753
- while (i < text.length) {
754
- val cp = text.codePointAt(i)
755
- total += width(cp, role)
756
- i += Character.charCount(cp)
757
- }
758
- return total
759
- }
760
-
761
- /** Letter spacing after a glyph of [role], in px. */
762
- fun spacing(role: Role): Float = when (role) {
763
- Role.BODY -> bodySpacing
764
- Role.PREFIX -> prefixLetterSpacing
765
- Role.SUFFIX -> suffixLetterSpacing
766
- }
767
-
768
- /** The room an affix takes beside the text: its glyphs, their spacing and the seam, in px. */
769
- fun affixRoom(affix: String, role: Role): Float {
770
- if (affix.isEmpty()) return 0f
771
- val count = affix.codePointCount(0, affix.length)
772
- val glyphs = width(affix, role)
773
- if (role == Role.PREFIX) return glyphs + prefixLetterSpacing * (count - 1) + (prefixSeam ?: prefixLetterSpacing)
774
- // The edit text's own letter spacing already follows its last glyph.
775
- return glyphs + suffixLetterSpacing * count + ((suffixSeam ?: bodySpacing) - bodySpacing)
776
- }
777
-
778
- /** Baseline y for `role` drawing `text`, given the top of the body line box. */
779
- fun baseline(role: Role, text: String, lineTop: Float): Float {
780
- val aligned = alignedBaseline(role, text, lineTop)
781
- return when (role) {
782
- Role.BODY -> aligned
783
- Role.PREFIX -> aligned + prefixOffset
784
- Role.SUFFIX -> aligned + suffixOffset
785
- }
786
- }
787
-
788
- private fun alignedBaseline(role: Role, text: String, lineTop: Float): Float {
789
- // `baselineNudge` re-centres the run when `lineHeight` differs from the
790
- // font's: the extra goes above the line, so the body sits that far lower.
791
- // Everything pinned to the body follows it; `CENTER` does not, because it
792
- // centres in the line box itself, which is already the new height.
793
- val bodyBaseline = lineTop + baselineNudge - bodyMetrics.ascent
794
- if (role == Role.BODY) return bodyBaseline
795
- val m = metrics[role.ordinal]
796
- return when (if (role == Role.PREFIX) prefixAlign else suffixAlign) {
797
- AffixAlign.BASELINE -> bodyBaseline
798
- AffixAlign.CENTER -> lineTop + (lineHeight - (m.descent - m.ascent)) / 2f - m.ascent
799
- AffixAlign.TOP -> bodyBaseline - capHeight(Role.BODY) + capHeight(role)
800
- // Pin the bottom of the ink, not of the line boxes: the digits' ink ends
801
- // on the baseline, so "USD" sits on it too instead of hanging down to
802
- // where a comma's tail reaches.
803
- AffixAlign.BOTTOM -> bodyBaseline + inkDescent(ALL_DIGITS, Role.BODY) - inkDescent(text, role)
804
- }
805
- }
806
-
807
- /** How far `text`'s ink hangs below the baseline (0 for digits and capitals). */
808
- private fun inkDescent(text: String, role: Role): Float = inkDescentCaches[role.ordinal].getOrPut(text) {
809
- val bounds = Rect()
810
- paint(role).getTextBounds(text, 0, text.length, bounds)
811
- max(0, bounds.bottom).toFloat()
812
- }
813
-
814
- private fun capHeight(role: Role): Float = capHeightCache.getOrPut(role) {
815
- val bounds = Rect()
816
- paint(role).getTextBounds("0", 0, 1, bounds)
817
- -bounds.top.toFloat()
818
- }
819
- }
820
-
821
- /** System font scale for `allowFontScaling`, capped by `maxFontSizeMultiplier`. */
822
- private fun systemFontMultiplier(t: Typography): Float {
823
- if (!t.allowFontScaling) return 1f
824
- val multiplier = context.resources.configuration.fontScale
825
- return if (t.maxFontSizeMultiplier > 0f) min(multiplier, t.maxFontSizeMultiplier) else multiplier
826
- }
827
-
828
- private fun makePaint(t: Typography, sizeDp: Float): TextPaint {
829
- val paint = TextPaint(Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG)
830
- paint.typeface = makeTypeface(t)
831
- paint.textSize = sizeDp * density
832
- paint.color = t.color ?: themeTextColor
833
- // Tabular figures in every mode, as on iOS: the engine slides digit runs
834
- // in text mode too, and proportional digits would jitter as they change.
835
- paint.fontFeatureSettings = "tnum"
836
- return paint
837
- }
838
-
839
- private fun makeTypeface(t: Typography): Typeface {
840
- val family = t.fontFamily
841
- return try {
842
- if (!family.isNullOrEmpty()) {
843
- ReactFontManager.getInstance().getTypeface(family, Typeface.NORMAL, t.fontWeight, context.assets)
844
- } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
845
- Typeface.create(Typeface.DEFAULT, t.fontWeight, false)
846
- } else if (t.fontWeight >= 600) {
847
- Typeface.DEFAULT_BOLD
848
- } else {
849
- Typeface.DEFAULT
850
- }
851
- } catch (e: Throwable) {
852
- if (t.fontWeight >= 600) Typeface.DEFAULT_BOLD else Typeface.DEFAULT
853
- }
854
- }
855
-
856
734
  private fun resolveThemeColor(attr: Int, fallback: Int): Int {
857
735
  val value = TypedValue()
858
736
  return if (context.theme.resolveAttribute(attr, value, true)) {
@@ -895,9 +773,12 @@ class NitroInputView(context: Context) : FrameLayout(context) {
895
773
 
896
774
  private fun charString(codePoint: Int): String = charCache.getOrPut(codePoint) { String(Character.toChars(codePoint)) }
897
775
 
776
+ private fun makeFonts(t: Typography) =
777
+ InputFonts(t, density, resources.configuration.fontScale, context.assets, themeTextColor)
778
+
898
779
  private fun rebuildFonts() {
899
780
  deriveColors()
900
- fonts = FontSet(typography)
781
+ fonts = makeFonts(typography)
901
782
  applyEditTextLayout()
902
783
  reportIntrinsicSize()
903
784
  overlay.invalidate()
@@ -907,6 +788,13 @@ class NitroInputView(context: Context) : FrameLayout(context) {
907
788
  private fun applyEditTextLayout() {
908
789
  val f = fonts
909
790
  editText.typeface = f.body.typeface
791
+ // The paints' tabular figures too, which the field's own text, its hint and
792
+ // keepPlaceholder's tail (drawn in a copy of its paint) did not get: with a
793
+ // font whose digits are proportional unless asked (ABC Diatype) every typed
794
+ // "1" was narrower than the "0" of the placeholder it replaced, and the
795
+ // rest of the placeholder slid left by the difference on each keystroke.
796
+ // A font with no tabular figures at all is slotted (applyDigitSlots).
797
+ editText.fontFeatureSettings = f.body.fontFeatureSettings
910
798
  // Spaced like the overlay, so the selection highlight lines up with the drawn glyphs.
911
799
  editText.letterSpacing = if (f.body.textSize > 0f) f.bodySpacing / f.body.textSize else 0f
912
800
  // Not `setTextSize` directly: the platform ignores it while auto-sizing is
@@ -930,7 +818,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
930
818
  // put space between its first line and the stroke: it pays for both edges.
931
819
  val side = frameSideInsetPx.roundToInt()
932
820
  val framePadding = if (inputFrame.draws && keyboard.multiline) {
933
- (FRAME_PADDING_DP * density).roundToInt()
821
+ (InputFrame.FRAME_PADDING_DP * density).roundToInt()
934
822
  } else {
935
823
  0
936
824
  }
@@ -949,9 +837,9 @@ class NitroInputView(context: Context) : FrameLayout(context) {
949
837
  // Relative: the prefix sits at the start edge and the suffix at the end,
950
838
  // so under a right-to-left layout the room for them swaps sides too.
951
839
  editText.setPaddingRelative(
952
- side + f.affixRoom(format.prefix, Role.PREFIX).roundToInt(),
840
+ side + f.affixRoom(format.prefix, InputRole.PREFIX).roundToInt(),
953
841
  frameTopInsetPx.roundToInt() + framePadding + labelOverhang,
954
- side + f.affixRoom(format.suffix, Role.SUFFIX).roundToInt(),
842
+ side + f.affixRoom(format.suffix, InputRole.SUFFIX).roundToInt(),
955
843
  framePadding,
956
844
  )
957
845
  applyAffixes()
@@ -966,6 +854,22 @@ class NitroInputView(context: Context) : FrameLayout(context) {
966
854
  Alignment.CENTER -> Gravity.CENTER_HORIZONTAL
967
855
  Alignment.RIGHT -> Gravity.RIGHT
968
856
  }
857
+ applyDigitSlots()
858
+ }
859
+
860
+ /**
861
+ * Gives the edit text's digits, and its hint's, the widest digit's advance
862
+ * when the font has no tabular figures ([DigitSlots]), as [InputFonts] does
863
+ * for the overlay and the measure. A secure field's bullets stand where the
864
+ * digits are, so it has none.
865
+ */
866
+ /** Whether the edit text's own digits are slotted: when asked, and not under a secure field's bullets. */
867
+ private val slotsDigits: Boolean
868
+ get() = typography.slotDigits && !keyboard.secureTextEntry
869
+
870
+ private fun applyDigitSlots() {
871
+ editText.text?.let { DigitSlots.apply(it, editText.paint, off = !slotsDigits) }
872
+ applyHint(force = true)
969
873
  }
970
874
 
971
875
  // endregion
@@ -1017,6 +921,13 @@ class NitroInputView(context: Context) : FrameLayout(context) {
1017
921
  val selectionStart = editText.selectionStart
1018
922
  val selectionEnd = editText.selectionEnd
1019
923
  editText.inputType = textType
924
+ // A password input type switches the field to monospace, so its bullets
925
+ // were wider than the field measures them; React Native's TextInput puts
926
+ // the typeface back the same way.
927
+ if (k.secureTextEntry) {
928
+ editText.typeface = fonts.body.typeface
929
+ editText.fontFeatureSettings = fonts.body.fontFeatureSettings
930
+ }
1020
931
  if (rawType != null) editText.setRawInputType(rawType)
1021
932
  if (k.multiline) {
1022
933
  editText.setSingleLine(false)
@@ -1035,6 +946,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
1035
946
  if (k.secureTextEntry) {
1036
947
  editText.transformationMethod = android.text.method.PasswordTransformationMethod.getInstance()
1037
948
  }
949
+ applyDigitSlots()
1038
950
  val length = editText.text?.length ?: 0
1039
951
  if (selectionStart >= 0 && (editText.selectionStart != selectionStart || editText.selectionEnd != selectionEnd)) {
1040
952
  editText.setSelection(minOf(selectionStart, length), minOf(maxOf(selectionStart, selectionEnd), length))
@@ -1116,11 +1028,11 @@ class NitroInputView(context: Context) : FrameLayout(context) {
1116
1028
  * nothing filled it, so a currency symbol was simply a gap.
1117
1029
  */
1118
1030
  private fun applyAffixes() {
1119
- prefixView = affixView(prefixView, format.prefix, Role.PREFIX, Gravity.START)
1120
- suffixView = affixView(suffixView, format.suffix, Role.SUFFIX, Gravity.END)
1031
+ prefixView = affixView(prefixView, format.prefix, InputRole.PREFIX, Gravity.START)
1032
+ suffixView = affixView(suffixView, format.suffix, InputRole.SUFFIX, Gravity.END)
1121
1033
  }
1122
1034
 
1123
- private fun affixView(existing: TextView?, affix: String, role: Role, edge: Int): TextView? {
1035
+ private fun affixView(existing: TextView?, affix: String, role: InputRole, edge: Int): TextView? {
1124
1036
  if (!keyboard.plain || affix.isEmpty()) {
1125
1037
  existing?.let { removeView(it) }
1126
1038
  return null
@@ -1139,6 +1051,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
1139
1051
  if (edge == Gravity.END) side else 0, 0)
1140
1052
  view.gravity = Gravity.CENTER_VERTICAL
1141
1053
  view.typeface = paint.typeface
1054
+ view.fontFeatureSettings = paint.fontFeatureSettings
1142
1055
  view.setTextSize(TypedValue.COMPLEX_UNIT_PX, paint.textSize)
1143
1056
  view.letterSpacing = if (paint.textSize > 0f) fonts.spacing(role) / paint.textSize else 0f
1144
1057
  // The seam against the text, and the affix's own offset.
@@ -1146,7 +1059,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
1146
1059
  val extra = seam.roundToInt().coerceAtLeast(0)
1147
1060
  view.setPaddingRelative(view.paddingStart + if (edge == Gravity.END) extra else 0, view.paddingTop,
1148
1061
  view.paddingEnd + if (edge == Gravity.START) extra else 0, view.paddingBottom)
1149
- view.translationY = if (role == Role.PREFIX) fonts.prefixOffset else fonts.suffixOffset
1062
+ view.translationY = if (role == InputRole.PREFIX) fonts.prefixOffset else fonts.suffixOffset
1150
1063
  view.setTextColor(resolvedTextColor)
1151
1064
  if (view.text?.toString() != affix) view.text = affix
1152
1065
  return view
@@ -1179,14 +1092,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
1179
1092
  applyNativeCursor()
1180
1093
  applyAutoSize()
1181
1094
  applyAffixes()
1182
- // `AUTO` is the start edge of the layout direction; `LEFT` and `RIGHT`
1183
- // are absolute, which only `TEXT_ALIGNMENT_GRAVITY` over an absolute
1184
- // gravity gives - `VIEW_START` / `VIEW_END` would flip with the direction.
1185
- editText.textAlignment = when (alignment) {
1186
- Alignment.AUTO -> TEXT_ALIGNMENT_VIEW_START
1187
- Alignment.CENTER -> TEXT_ALIGNMENT_CENTER
1188
- Alignment.LEFT, Alignment.RIGHT -> TEXT_ALIGNMENT_GRAVITY
1189
- }
1095
+ applyTextAlignment()
1190
1096
  // The width now comes from the field, not the engine.
1191
1097
  reportIntrinsicSize()
1192
1098
  } else {
@@ -1405,6 +1311,13 @@ class NitroInputView(context: Context) : FrameLayout(context) {
1405
1311
  caret = Caret()
1406
1312
  alignment = Alignment.AUTO
1407
1313
  }
1314
+ // Last: the defaults above fed the engine an empty text, which it would
1315
+ // take for its first, so the next element's text slid in instead of
1316
+ // showing as it is (the engine snaps only its first text).
1317
+ engine.reset()
1318
+ glyphs.clear()
1319
+ contentWidth = 0f
1320
+ fed = false
1408
1321
  // onTextChange, onFocusChange, onSubmit and onIntrinsicSizeChange are the
1409
1322
  // hybrid's wiring, not the element's props: they stay across recycling
1410
1323
  // (the hybrid clears its own callback props).
@@ -1482,16 +1395,8 @@ class NitroInputView(context: Context) : FrameLayout(context) {
1482
1395
  // region Text flow
1483
1396
 
1484
1397
  /** Text mode truncates to `maxLength`; number mode formats. */
1485
- private fun conform(value: String): String {
1486
- if (format.mode == Mode.NUMBER) return formatter.normalize(value)
1487
- if (format.mode == Mode.MASK) {
1488
- return maskEngine.applyAll(value, value.codePointCount(0, value.length), true, format.maskAutocomplete, false)
1489
- }
1490
- val limit = keyboard.maxLength
1491
- if (limit <= 0) return value
1492
- val end = utf16Index(value, limit)
1493
- return value.substring(0, end)
1494
- }
1398
+ private fun conform(value: String): String =
1399
+ InputText.conform(value, format, formatter, maskEngine, keyboard.maxLength)
1495
1400
 
1496
1401
  private fun setProgrammatic(value: String, notify: Boolean) {
1497
1402
  var next = value
@@ -1693,35 +1598,19 @@ class NitroInputView(context: Context) : FrameLayout(context) {
1693
1598
  reportIntrinsicSize()
1694
1599
  return
1695
1600
  }
1696
- val f = fonts
1697
1601
  engine.setReduceMotion(animationsDisabled())
1698
1602
  engine.beginText()
1699
1603
  val hint = effectivePlaceholder
1700
1604
  val showPlaceholder = text.isEmpty() && hint.isNotEmpty()
1701
1605
  // `secureTextEntry` masks what the overlay draws: the EditText keeps the
1702
1606
  // real text (the IME and autofill need it), every drawn glyph is a bullet.
1703
- val body = if (keyboard.secureTextEntry) "\u2022".repeat(text.codePointCount(0, text.length)) else text
1704
- val shown = if (showPlaceholder) hint else body
1705
-
1706
- // A negative amount reads "-$1,234.56", not "$-1,234.56": the sign belongs
1707
- // to the amount, not to the digits after the symbol. It stays a *body*
1708
- // glyph - it is part of the text, and the caret counts body glyphs in the
1709
- // order they are added - and is only laid out ahead of the prefix.
1710
- val signed = format.signBeforeAffix && !showPlaceholder && format.prefix.isNotEmpty() &&
1711
- shown.isNotEmpty() && isSign(shown[0])
1712
- // Advances carry the letter spacing; the last prefix glyph and the last
1713
- // body glyph before a suffix carry the seams instead.
1714
- if (signed) addRun(shown.substring(0, 1), Role.BODY, f, placeholder = false)
1715
- addRun(format.prefix, Role.PREFIX, f, placeholder = false, lastSpacing = f.prefixSeam)
1716
- val tail = if (showPlaceholder) "" else ghostTail
1717
- addRun(if (signed) shown.substring(1) else shown, Role.BODY, f, placeholder = showPlaceholder,
1718
- lastSpacing = if (format.suffix.isNotEmpty() && tail.isEmpty()) f.suffixSeam else null)
1719
- // `keepPlaceholder`: the rest of the mask trails the text as placeholder
1720
- // glyphs. They come after every typed glyph, so the caret - which counts
1721
- // body glyphs in order - never lands among them.
1722
- addRun(tail, Role.BODY, f, placeholder = true,
1723
- lastSpacing = if (format.suffix.isNotEmpty()) f.suffixSeam else null)
1724
- addRun(format.suffix, Role.SUFFIX, f, placeholder = false)
1607
+ val body = if (keyboard.secureTextEntry) InputText.bullets(text) else text
1608
+ val numberKinds = format.mode == Mode.NUMBER
1609
+ // The same walk sizes the field before it mounts (InputSizing).
1610
+ InputGlyphs.walk(fonts, format, if (showPlaceholder) hint else body, showPlaceholder, ghostTail) { cp, role, placeholder, advance ->
1611
+ val kind = if (numberKinds && role == InputRole.BODY) formatter.kindOf(cp) else 0
1612
+ engine.addGlyph(cp, role.ordinal, kind, advance.toDouble(), placeholder)
1613
+ }
1725
1614
  engine.commitText(caret, now())
1726
1615
  fed = true
1727
1616
  reportIntrinsicSize()
@@ -1729,23 +1618,6 @@ class NitroInputView(context: Context) : FrameLayout(context) {
1729
1618
  overlay.invalidate()
1730
1619
  }
1731
1620
 
1732
- /** A leading minus, in either the keyboard's spelling or the typographic one. */
1733
- private fun isSign(c: Char): Boolean = c == '-' || c == '\u2212'
1734
-
1735
- private fun addRun(s: String, role: Role, f: FontSet, placeholder: Boolean, lastSpacing: Float? = null) {
1736
- val numberKinds = role == Role.BODY && format.mode == Mode.NUMBER
1737
- val spacing = f.spacing(role)
1738
- var i = 0
1739
- while (i < s.length) {
1740
- val cp = s.codePointAt(i)
1741
- val kind = if (numberKinds) formatter.kindOf(cp) else 0
1742
- val next = i + Character.charCount(cp)
1743
- val after = if (next >= s.length && lastSpacing != null) lastSpacing else spacing
1744
- engine.addGlyph(cp, role.ordinal, kind, (f.width(cp, role) + after).toDouble(), placeholder)
1745
- i = next
1746
- }
1747
- }
1748
-
1749
1621
  /** Nearest glyph boundary to a tap, as a UTF-16 offset into the edit text. */
1750
1622
  private fun offsetForTap(x: Float): Int {
1751
1623
  val layout = contentLayout(overlay.width, overlay.height, contentWidth)
@@ -1854,7 +1726,8 @@ class NitroInputView(context: Context) : FrameLayout(context) {
1854
1726
  get() = if (typography.lineHeightPx > 0f) typography.lineHeightPx else fonts.lineHeight
1855
1727
 
1856
1728
  private fun intrinsicHeightPx(): Float {
1857
- if (!keyboard.multiline) return fonts.lineHeight
1729
+ // A single line is centred in its height; a frame adds its padding (and a filled label's line).
1730
+ if (!keyboard.multiline) return InputFrame.singleLineHeightPx(fonts.lineHeight, inputFrame, fonts, density)
1858
1731
  // The frame's padding counts on both paths. Leaving it off this one was
1859
1732
  // enough to lose it entirely: at mount there is no layout yet, so this
1860
1733
  // branch answered, and by the time the frame arrived and set the padding
@@ -1887,23 +1760,16 @@ class NitroInputView(context: Context) : FrameLayout(context) {
1887
1760
  */
1888
1761
  private fun contentWidthPx(): Float {
1889
1762
  if (!keyboard.plain) return engine.targetWidth().toFloat()
1890
- val body = when {
1891
- text.isEmpty() -> effectivePlaceholder
1892
- keyboard.secureTextEntry -> "\u2022".repeat(text.codePointCount(0, text.length))
1893
- else -> text + ghostTail
1894
- }
1895
- // The edit text's own letter spacing follows every glyph; the affixes take their room with their seams.
1896
- val f = fonts
1897
- return f.affixRoom(format.prefix, Role.PREFIX) +
1898
- f.paint(Role.BODY).measureText(body) + f.bodySpacing * body.codePointCount(0, body.length) +
1899
- f.affixRoom(format.suffix, Role.SUFFIX)
1763
+ return InputSizing.plainWidthPx(fonts, format, InputSizing.plainBody(text, effectivePlaceholder, ghostTail, keyboard.secureTextEntry))
1900
1764
  }
1901
1765
 
1902
1766
  private fun reportIntrinsicSize() {
1903
1767
  // The reported size is always the full-size one: with shrink-to-fit the view
1904
1768
  // keeps its height and the scaled text is centred inside it when drawing.
1905
- val widthDp = ceil(contentWidthPx() / density + 2f)
1906
- val heightDp = ceil(intrinsicHeightPx() / density)
1769
+ // The same rounding as the measure that sized the field before it mounted.
1770
+ val size = InputSizing.reported(InputFrame.widthPx(contentWidthPx(), inputFrame, fonts, density), intrinsicHeightPx(), density)
1771
+ val widthDp = size.width.toFloat()
1772
+ val heightDp = size.height.toFloat()
1907
1773
  if (abs(widthDp - lastReportedWidth) <= 0.01f && abs(heightDp - lastReportedHeight) <= 0.01f) {
1908
1774
  pendingSizeReport = false
1909
1775
  return
@@ -2035,15 +1901,17 @@ class NitroInputView(context: Context) : FrameLayout(context) {
2035
1901
  // Sliding glyphs pass through the line box: clip so they appear from its
2036
1902
  // edges, with a 0.15 em margin so a comma's tail is never cut at rest.
2037
1903
  if (slides) {
2038
- val band = min(lineHeight / 3f, 0.15f * f.paint(Role.BODY).textSize)
1904
+ val band = min(lineHeight / 3f, 0.15f * f.paint(InputRole.BODY).textSize)
2039
1905
  canvas.clipRect(-1e5f, -band, 1e5f, lineHeight + band)
2040
1906
  }
2041
1907
  if (g.scale != 1f) canvas.scale(g.scale, g.scale, g.x + g.width / 2f, lineHeight / 2f)
2042
1908
  val baseline = f.baseline(role, str, 0f) + g.y * lineHeight
1909
+ // A digit in a font without tabular figures, centred in its slot.
1910
+ val inset = f.inset(g.character, role)
2043
1911
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
2044
- glyphPositions = f.shaped(g.character, role).draw(canvas, g.x, baseline, paint, glyphPositions)
1912
+ glyphPositions = f.shaped(g.character, role).draw(canvas, g.x + inset, baseline, paint, glyphPositions)
2045
1913
  } else {
2046
- canvas.drawText(str, g.x, baseline, paint)
1914
+ canvas.drawText(str, g.x + inset, baseline, paint)
2047
1915
  }
2048
1916
  canvas.restore()
2049
1917
  }
@@ -2069,10 +1937,10 @@ class NitroInputView(context: Context) : FrameLayout(context) {
2069
1937
  * the corner that the notch never opens onto the arc itself.
2070
1938
  */
2071
1939
  private val labelInsetPx: Float
2072
- get() = max(LABEL_INSET_DP * density, inputFrame.cornerRadius * density + LABEL_CORNER_GAP_DP * density)
1940
+ get() = InputFrame.labelInsetPx(inputFrame, density)
2073
1941
 
2074
1942
  private val frameSideInsetPx: Float
2075
- get() = if (inputFrame.draws) labelInsetPx else 0f
1943
+ get() = InputFrame.sideInsetPx(inputFrame, density)
2076
1944
 
2077
1945
  /**
2078
1946
  * Room at the top for the floated label - which only the filled variant
@@ -2082,20 +1950,12 @@ class NitroInputView(context: Context) : FrameLayout(context) {
2082
1950
  * text stays centred the way it would with no label at all.
2083
1951
  */
2084
1952
  private val frameTopInsetPx: Float
2085
- get() {
2086
- if (!inputFrame.hasLabel || inputFrame.variant != Variant.FILLED) return 0f
2087
- // 1.4 is a fudge, not a Material metric - see the note on iOS.
2088
- return floatedLabelSizePx * 1.4f
2089
- }
1953
+ get() = InputFrame.topInsetPx(inputFrame, restingLabelSizePx, density)
2090
1954
 
2091
- private val restingLabelSizePx: Float get() = fonts.paint(Role.BODY).textSize
1955
+ private val restingLabelSizePx: Float get() = fonts.paint(InputRole.BODY).textSize
2092
1956
 
2093
1957
  private val floatedLabelSizePx: Float
2094
- get() {
2095
- val explicit = inputFrame.labelFontSize
2096
- if (explicit > 0f) return explicit * density
2097
- return max(MIN_FLOATED_LABEL_DP * density, restingLabelSizePx * FLOATED_LABEL_RATIO)
2098
- }
1958
+ get() = InputFrame.floatedLabelSizePx(inputFrame, restingLabelSizePx, density)
2099
1959
 
2100
1960
  /** The label floats once focused or holding text - or always, if asked. */
2101
1961
  private val labelShouldFloat: Boolean
@@ -2117,40 +1977,11 @@ class NitroInputView(context: Context) : FrameLayout(context) {
2117
1977
  * slots as `maskSlotPlaceholder`: "+1 (___) ___-____".
2118
1978
  */
2119
1979
  private val basePlaceholder: String
2120
- get() {
2121
- if (format.placeholder.isEmpty() && format.mode == Mode.MASK && format.keepPlaceholder && maskEngine.isActive()) {
2122
- maskEngine.applyAll("", 0, true, false, false)
2123
- return maskEngine.lastTailPlaceholder()
2124
- }
2125
- return format.placeholder
2126
- }
1980
+ get() = InputText.basePlaceholder(format, maskEngine)
2127
1981
 
2128
- /**
2129
- * `keepPlaceholder`: what is still to come after the typed text, drawn in the
2130
- * placeholder colour. The caller's placeholder from where the text ends -
2131
- * "1234 5" leaves "678 9012" of "1234 5678 9012" - or, without one, the rest
2132
- * of the mask. The placeholder is written for `mask`, so when one of
2133
- * `maskAffinityFormats` has taken over (a 4-6-5 Amex under a 4-4-4-4
2134
- * placeholder) its characters no longer line up, and that format's own tail
2135
- * is shown instead. A complete text has nothing to come.
2136
- */
1982
+ /** `keepPlaceholder`: what is still to come after the typed text ([InputText.ghostTail]). */
2137
1983
  private val ghostTail: String
2138
- get() {
2139
- if (format.mode != Mode.MASK || !format.keepPlaceholder || text.isEmpty() || keyboard.secureTextEntry ||
2140
- !maskEngine.isActive()
2141
- ) {
2142
- return ""
2143
- }
2144
- val typed = text.codePointCount(0, text.length)
2145
- maskEngine.applyAll(text, typed, true, false, false)
2146
- if (maskEngine.lastComplete()) return ""
2147
- val placeholder = format.placeholder
2148
- if (placeholder.isNotEmpty() && maskEngine.lastFormatIndex() == 0) {
2149
- val total = placeholder.codePointCount(0, placeholder.length)
2150
- return if (typed < total) placeholder.substring(placeholder.offsetByCodePoints(0, typed)) else ""
2151
- }
2152
- return maskEngine.lastTailPlaceholder()
2153
- }
1984
+ get() = InputText.ghostTail(text, format, maskEngine, keyboard.secureTextEntry)
2154
1985
 
2155
1986
  private fun resolvedStrokeColor(): Int {
2156
1987
  val base = inputFrame.strokeColor ?: mutedTextColor
@@ -2203,7 +2034,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
2203
2034
  }
2204
2035
 
2205
2036
  private fun rebuildLabelPaint() {
2206
- val body = fonts.paint(Role.BODY)
2037
+ val body = fonts.paint(InputRole.BODY)
2207
2038
  labelPaint.typeface = body.typeface
2208
2039
  labelPaint.textSize = restingLabelSizePx
2209
2040
  }
@@ -2270,9 +2101,12 @@ class NitroInputView(context: Context) : FrameLayout(context) {
2270
2101
  }
2271
2102
 
2272
2103
  /** The placeholder waits for a resting label to float clear of the line. */
2273
- private fun applyHint() {
2104
+ private fun applyHint(force: Boolean = false) {
2274
2105
  val hint = effectivePlaceholder
2275
- if (editText.hint?.toString() != hint) editText.hint = hint
2106
+ if (!force && editText.hint?.toString() == hint) return
2107
+ val spanned = android.text.SpannableString(hint)
2108
+ DigitSlots.apply(spanned, editText.paint, off = !typography.slotDigits)
2109
+ editText.hint = spanned
2276
2110
  }
2277
2111
 
2278
2112
  /** Re-feeds the engine so the placeholder follows the label. */
@@ -2284,7 +2118,18 @@ class NitroInputView(context: Context) : FrameLayout(context) {
2284
2118
 
2285
2119
  override fun onDraw(canvas: Canvas) {
2286
2120
  super.onDraw(canvas)
2287
- if (!inputFrame.draws || width <= 0 || height <= 0) return
2121
+ // Inside the style's padding and border, as React Native lays the frame
2122
+ // out on iOS (see [contentInsets]).
2123
+ val boxWidth = width - paddingLeft - paddingRight
2124
+ val boxHeight = height - paddingTop - paddingBottom
2125
+ if (!inputFrame.draws || boxWidth <= 0 || boxHeight <= 0) return
2126
+ val saved = canvas.save()
2127
+ canvas.translate(paddingLeft.toFloat(), paddingTop.toFloat())
2128
+ drawFrame(canvas, boxWidth, boxHeight)
2129
+ canvas.restoreToCount(saved)
2130
+ }
2131
+
2132
+ private fun drawFrame(canvas: Canvas, width: Int, height: Int) {
2288
2133
 
2289
2134
  val filled = inputFrame.variant == Variant.FILLED
2290
2135
  val stroke = strokeWidthPx
@@ -2427,27 +2272,12 @@ class NitroInputView(context: Context) : FrameLayout(context) {
2427
2272
  companion object {
2428
2273
  /** Matches iOS's `.placeholderText`, which is the label colour at 30-40%. */
2429
2274
  private const val PLACEHOLDER_ALPHA = 0.38f
2430
- private const val ALL_DIGITS = "0123456789"
2431
2275
  private const val BLINK_MS = 500L
2432
2276
  private const val CARET_WIDTH_DP = 2f
2433
2277
  /** Fraction of the line height the caret leaves free (split between top and bottom). */
2434
2278
  private const val CARET_INSET = 0.1f
2435
- /** How far the label starts from the frame's left edge, at a minimum. */
2436
- private const val LABEL_INSET_DP = 16f
2437
- /**
2438
- * The room a framed field keeps above and below its text. Flat, unlike the
2439
- * side inset: that one grows with `cornerRadius` because the *label* has to
2440
- * clear the corner curve, which is a horizontal problem. The text sits in
2441
- * the middle of the box, so a rounder frame must not make the field taller.
2442
- */
2443
- private const val FRAME_PADDING_DP = 16f
2444
- /** Clearance the label keeps past the corner arc, so the notch opens onto the straight run. */
2445
- private const val LABEL_CORNER_GAP_DP = 8f
2446
2279
  /** Breathing room the notch leaves on each side of the label. */
2447
2280
  private const val LABEL_GAP_PADDING_DP = 4f
2448
- /** The floated label's size, as a fraction of the resting one. */
2449
- private const val FLOATED_LABEL_RATIO = 0.75f
2450
- private const val MIN_FLOATED_LABEL_DP = 9f
2451
2281
  /**
2452
2282
  * Taken from Material's own text fields. MUI's OutlinedInput runs the label
2453
2283
  * 200ms on the standard decelerate curve and the legend's width 100ms after
@@ -2462,7 +2292,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
2462
2292
  private const val NOTCH_CLOSE_MS = 50L
2463
2293
  /** Material's standard decelerate, the same curve the iOS side uses. */
2464
2294
  private val DECELERATE = PathInterpolator(0f, 0f, 0.2f, 1f)
2465
- private val ROLES = Role.values()
2295
+ private val ROLES = InputRole.values()
2466
2296
  }
2467
2297
  }
2468
2298