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
@@ -0,0 +1,404 @@
1
+ package com.margelo.nitro.nitroinput
2
+
3
+ import android.graphics.Paint
4
+ import android.text.TextPaint
5
+ import com.margelo.nitro.nitroinput.NitroInputView.Format
6
+ import com.margelo.nitro.nitroinput.NitroInputView.Frame
7
+ import com.margelo.nitro.nitroinput.NitroInputView.Mode
8
+ import com.margelo.nitro.nitroinput.NitroInputView.Typography
9
+ import kotlin.math.ceil
10
+ import kotlin.math.max
11
+
12
+ /**
13
+ * The props a field's size follows, under the names both [HybridNitroInputView]
14
+ * and [NitroInputSizing] give them, so one mapping serves the view and the
15
+ * measure that runs before it exists.
16
+ */
17
+ internal interface InputSizingProps {
18
+ val mode: NitroInputMode
19
+ val mask: String
20
+ val maskNotations: Array<NitroInputNotation>
21
+ val maskAutocomplete: Boolean
22
+ val maskAutoSkip: Boolean
23
+ val maskAffinityFormats: Array<String>
24
+ val maskAffinityStrategy: NitroInputMaskAffinity
25
+ val maskTextCase: NitroInputTextCase
26
+ val maskCharacterMap: Array<NitroInputCharacterMapping>
27
+ val maskSlotPlaceholder: String
28
+ val keepPlaceholder: Boolean
29
+ val fractionDigits: Double
30
+ val maxIntegerDigits: Double
31
+ val groupingSeparator: String
32
+ val decimalSeparator: String
33
+ val prefix: String
34
+ val suffix: String
35
+ val signPlacement: NitroInputSignPlacement
36
+ val placeholder: String
37
+ val textSize: Double
38
+ val textLineHeight: Double
39
+ val prefixFontSize: Double
40
+ val suffixFontSize: Double
41
+ val textWeight: Double
42
+ val textFamily: String
43
+ val textLetterSpacing: Double
44
+ val tabularNums: Boolean
45
+ val slotDigits: Boolean
46
+ val prefixSpacing: Double
47
+ val suffixSpacing: Double
48
+ val allowFontScaling: Boolean
49
+ val maxFontSizeMultiplier: Double
50
+ val variant: NitroInputVariant
51
+ val label: String
52
+ val labelBehavior: NitroInputLabelBehavior
53
+ val labelFontSize: Double
54
+ val cornerRadius: Double
55
+ }
56
+
57
+ internal fun finiteOr(value: Double, fallback: Double): Double = if (value.isFinite()) value else fallback
58
+
59
+ internal fun clampedInt(value: Double, lower: Int, upper: Int, fallback: Int): Int {
60
+ if (!value.isFinite()) return fallback
61
+ return Math.rint(value).coerceIn(lower.toDouble(), upper.toDouble()).toInt()
62
+ }
63
+
64
+ internal fun formatOf(p: InputSizingProps) = Format(
65
+ mode = when (p.mode) {
66
+ NitroInputMode.NUMBER -> Mode.NUMBER
67
+ NitroInputMode.MASK -> Mode.MASK
68
+ else -> Mode.TEXT
69
+ },
70
+ mask = p.mask,
71
+ maskNotations = p.maskNotations.map { NitroInputView.MaskNotation(it.character, it.characterSet, it.isOptional) },
72
+ maskAutocomplete = p.maskAutocomplete,
73
+ maskAutoSkip = p.maskAutoSkip,
74
+ maskAffinityFormats = p.maskAffinityFormats.toList(),
75
+ maskAffinityStrategy = p.maskAffinityStrategy.value,
76
+ maskTextCase = p.maskTextCase.value,
77
+ maskCharacterMap = p.maskCharacterMap.map { NitroInputView.MaskMapping(it.from, it.to) },
78
+ maskSlotPlaceholder = p.maskSlotPlaceholder,
79
+ keepPlaceholder = p.keepPlaceholder,
80
+ fractionDigits = clampedInt(p.fractionDigits, 0, 9, 2),
81
+ maxIntegerDigits = clampedInt(p.maxIntegerDigits, 1, 30, 15),
82
+ groupingSeparator = p.groupingSeparator,
83
+ decimalSeparator = p.decimalSeparator,
84
+ prefix = p.prefix,
85
+ suffix = p.suffix,
86
+ signBeforeAffix = p.signPlacement != NitroInputSignPlacement.AFTERAFFIX,
87
+ placeholder = p.placeholder,
88
+ )
89
+
90
+ /** The typography's sizing half; the view adds the colours, alignment and offsets. */
91
+ internal fun sizingTypographyOf(p: InputSizingProps, density: Float): Typography {
92
+ val size = finiteOr(p.textSize, 32.0).toFloat()
93
+ return Typography(
94
+ fontSize = size,
95
+ lineHeightPx = (max(0.0, finiteOr(p.textLineHeight, 0.0)) * density).toFloat(),
96
+ prefixFontSize = finiteOr(p.prefixFontSize, size.toDouble()).toFloat(),
97
+ suffixFontSize = finiteOr(p.suffixFontSize, size.toDouble()).toFloat(),
98
+ fontWeight = clampedInt(p.textWeight, 100, 900, 400),
99
+ fontFamily = p.textFamily.ifEmpty { null },
100
+ letterSpacing = if (p.textLetterSpacing.isFinite()) p.textLetterSpacing.toFloat() else 0f,
101
+ tabularNums = p.tabularNums,
102
+ slotDigits = p.slotDigits,
103
+ prefixSpacing = p.prefixSpacing.takeIf { it.isFinite() }?.toFloat(),
104
+ suffixSpacing = p.suffixSpacing.takeIf { it.isFinite() }?.toFloat(),
105
+ allowFontScaling = p.allowFontScaling,
106
+ maxFontSizeMultiplier = max(0.0, finiteOr(p.maxFontSizeMultiplier, 0.0)).toFloat(),
107
+ )
108
+ }
109
+
110
+ /** The frame's sizing half; the view adds the colours and the stroke width. */
111
+ internal fun sizingFrameOf(p: InputSizingProps) = Frame(
112
+ variant = when (p.variant) {
113
+ NitroInputVariant.OUTLINED -> NitroInputView.Variant.OUTLINED
114
+ NitroInputVariant.FILLED -> NitroInputView.Variant.FILLED
115
+ NitroInputVariant.NONE -> NitroInputView.Variant.NONE
116
+ },
117
+ label = p.label,
118
+ labelBehavior = when (p.labelBehavior) {
119
+ NitroInputLabelBehavior.ALWAYS -> NitroInputView.LabelBehavior.ALWAYS
120
+ NitroInputLabelBehavior.FLOAT -> NitroInputView.LabelBehavior.FLOAT
121
+ },
122
+ labelFontSize = max(0.0, finiteOr(p.labelFontSize, 0.0)).toFloat(),
123
+ cornerRadius = max(0.0, finiteOr(p.cornerRadius, 8.0)).toFloat(),
124
+ )
125
+
126
+ /**
127
+ * The room an outlined or filled frame keeps around its text and for its
128
+ * label, in px. The view lays itself out with these; [InputSizing] adds them
129
+ * to the size it reports. Mirrors `InputFrameMetrics` in InputSizing.swift.
130
+ */
131
+ internal object InputFrame {
132
+ /** How far the label starts from the frame's edge, at a minimum. */
133
+ const val LABEL_INSET_DP = 16f
134
+ /** Past the corner's curve, so the notch never opens onto the arc. */
135
+ const val LABEL_CORNER_GAP_DP = 8f
136
+ /**
137
+ * The room a framed field keeps above and below its text. Flat, unlike the
138
+ * side inset: that one grows with `cornerRadius` because the *label* has to
139
+ * clear the corner curve, which is a horizontal problem.
140
+ */
141
+ const val FRAME_PADDING_DP = 16f
142
+ const val FLOATED_LABEL_RATIO = 0.75f
143
+ const val MIN_FLOATED_LABEL_DP = 9f
144
+ /** A filled field's floated label line, as a multiple of its size (a fudge, not a Material metric). */
145
+ const val FILLED_LABEL_LINE = 1.4f
146
+
147
+ /** How far in the label - and so the text under it - starts. */
148
+ fun labelInsetPx(frame: Frame, density: Float): Float =
149
+ max(LABEL_INSET_DP * density, frame.cornerRadius * density + LABEL_CORNER_GAP_DP * density)
150
+
151
+ fun sideInsetPx(frame: Frame, density: Float): Float = if (frame.draws) labelInsetPx(frame, density) else 0f
152
+
153
+ fun floatedLabelSizePx(frame: Frame, restingSizePx: Float, density: Float): Float =
154
+ if (frame.labelFontSize > 0f) frame.labelFontSize * density
155
+ else max(MIN_FLOATED_LABEL_DP * density, restingSizePx * FLOATED_LABEL_RATIO)
156
+
157
+ /**
158
+ * Room at the top for the floated label - which only the filled variant
159
+ * needs. Its label floats inside the box, on its own line above the text. An
160
+ * outlined field's label sits *on* the stroke, outside the content box: it
161
+ * intrudes into the top of the frame but never onto the text's line.
162
+ */
163
+ fun topInsetPx(frame: Frame, restingSizePx: Float, density: Float): Float =
164
+ if (!frame.hasLabel || frame.variant != NitroInputView.Variant.FILLED) 0f
165
+ else floatedLabelSizePx(frame, restingSizePx, density) * FILLED_LABEL_LINE
166
+
167
+ /** The label at rest, in the body face and size: the widest it gets. */
168
+ fun restingLabelWidthPx(frame: Frame, fonts: InputFonts): Float {
169
+ if (!frame.hasLabel) return 0f
170
+ val body = fonts.paint(InputRole.BODY)
171
+ val paint = TextPaint(Paint.ANTI_ALIAS_FLAG)
172
+ paint.typeface = body.typeface
173
+ paint.textSize = body.textSize
174
+ return paint.measureText(frame.label)
175
+ }
176
+
177
+ /** A framed field's width: its content, or its label if wider, between the side insets. */
178
+ fun widthPx(contentPx: Float, frame: Frame, fonts: InputFonts, density: Float): Float {
179
+ if (!frame.draws) return contentPx
180
+ return 2f * sideInsetPx(frame, density) + max(contentPx, restingLabelWidthPx(frame, fonts))
181
+ }
182
+
183
+ /**
184
+ * A framed single line's height: the line with the frame's padding above and
185
+ * below it, and a filled field's label line. The line is centred in it, as it
186
+ * is in any height the caller gives. (A wrapping field adds its own.)
187
+ */
188
+ fun singleLineHeightPx(linePx: Float, frame: Frame, fonts: InputFonts, density: Float): Float {
189
+ if (!frame.draws) return linePx
190
+ return linePx + topInsetPx(frame, fonts.paint(InputRole.BODY).textSize, density) + 2f * FRAME_PADDING_DP * density
191
+ }
192
+ }
193
+
194
+ /**
195
+ * What a field shows for its format: the text conformed to it, the placeholder
196
+ * and `keepPlaceholder`'s tail. Pure functions of the format and the shared C++
197
+ * engines, which the view holds for its life and a measure configures afresh.
198
+ */
199
+ internal object InputText {
200
+ /** Points [formatter] and [maskEngine] at [value]; [old] skips a mask that did not change. */
201
+ fun configure(formatter: AmountFormatter, maskEngine: MaskEngine, old: Format?, value: Format) {
202
+ formatter.setFormat(value.fractionDigits, value.maxIntegerDigits, value.groupingSeparator, value.decimalSeparator)
203
+ val maskChanged = old == null || old.mask != value.mask || old.maskNotations != value.maskNotations ||
204
+ old.maskAffinityFormats != value.maskAffinityFormats || old.maskAffinityStrategy != value.maskAffinityStrategy ||
205
+ old.maskTextCase != value.maskTextCase || old.maskCharacterMap != value.maskCharacterMap ||
206
+ old.maskSlotPlaceholder != value.maskSlotPlaceholder
207
+ if (value.mode == Mode.MASK && (old == null || old.mode != Mode.MASK || maskChanged)) {
208
+ maskEngine.clearNotations()
209
+ value.maskNotations.forEach { maskEngine.addNotation(it.character, it.characterSet, it.isOptional) }
210
+ maskEngine.clearAffinityFormats()
211
+ value.maskAffinityFormats.forEach { maskEngine.addAffinityFormat(it) }
212
+ maskEngine.setAffinityStrategy(value.maskAffinityStrategy)
213
+ maskEngine.setTextCase(value.maskTextCase)
214
+ maskEngine.clearCharacterMap()
215
+ value.maskCharacterMap.forEach { maskEngine.addCharacterMapping(it.from, it.to) }
216
+ maskEngine.setSlotPlaceholder(value.maskSlotPlaceholder)
217
+ // A bad pattern leaves the engine inactive; the field then behaves as
218
+ // plain text rather than refusing every keystroke.
219
+ maskEngine.setFormat(value.mask)
220
+ }
221
+ }
222
+
223
+ /** [value] as the format holds it: formatted, masked, or cut to `maxLength` code points. */
224
+ fun conform(value: String, format: Format, formatter: AmountFormatter, maskEngine: MaskEngine, maxLength: Int): String {
225
+ if (format.mode == Mode.NUMBER) return formatter.normalize(value)
226
+ if (format.mode == Mode.MASK) {
227
+ return maskEngine.applyAll(value, value.codePointCount(0, value.length), true, format.maskAutocomplete, false)
228
+ }
229
+ if (maxLength <= 0) return value
230
+ val total = value.codePointCount(0, value.length)
231
+ return value.substring(0, value.offsetByCodePoints(0, maxLength.coerceIn(0, total)))
232
+ }
233
+
234
+ /**
235
+ * `keepPlaceholder` without a `placeholder` shows the mask itself, empty
236
+ * slots as `maskSlotPlaceholder`: "+1 (___) ___-____".
237
+ */
238
+ fun basePlaceholder(format: Format, maskEngine: MaskEngine): String {
239
+ if (format.placeholder.isEmpty() && format.mode == Mode.MASK && format.keepPlaceholder && maskEngine.isActive()) {
240
+ maskEngine.applyAll("", 0, true, false, false)
241
+ return maskEngine.lastTailPlaceholder()
242
+ }
243
+ return format.placeholder
244
+ }
245
+
246
+ /**
247
+ * `keepPlaceholder`: what is still to come after the typed text, drawn in the
248
+ * placeholder colour. The caller's placeholder from where the text ends -
249
+ * "1234 5" leaves "678 9012" of "1234 5678 9012" - or, without one, the rest
250
+ * of the mask. The placeholder is written for `mask`, so when one of
251
+ * `maskAffinityFormats` has taken over (a 4-6-5 Amex under a 4-4-4-4
252
+ * placeholder) its characters no longer line up, and that format's own tail
253
+ * is shown instead. A complete text has nothing to come.
254
+ */
255
+ fun ghostTail(text: String, format: Format, maskEngine: MaskEngine, secure: Boolean): String {
256
+ if (format.mode != Mode.MASK || !format.keepPlaceholder || text.isEmpty() || secure || !maskEngine.isActive()) {
257
+ return ""
258
+ }
259
+ val typed = text.codePointCount(0, text.length)
260
+ maskEngine.applyAll(text, typed, true, false, false)
261
+ if (maskEngine.lastComplete()) return ""
262
+ val placeholder = format.placeholder
263
+ if (placeholder.isNotEmpty() && maskEngine.lastFormatIndex() == 0) {
264
+ val total = placeholder.codePointCount(0, placeholder.length)
265
+ return if (typed < total) placeholder.substring(placeholder.offsetByCodePoints(0, typed)) else ""
266
+ }
267
+ return maskEngine.lastTailPlaceholder()
268
+ }
269
+
270
+ /** `secureTextEntry` draws a bullet for every code point. */
271
+ fun bullets(text: String): String = "•".repeat(text.codePointCount(0, text.length))
272
+
273
+ /** A leading minus, in either the keyboard's spelling or the typographic one. */
274
+ fun isSign(c: Char): Boolean = c == '-' || c == '−'
275
+ }
276
+
277
+ /** Receives one glyph of [InputGlyphs.walk]: its code point, role, whether it is placeholder, and its advance in px. */
278
+ internal fun interface GlyphSink {
279
+ fun glyph(codePoint: Int, role: InputRole, placeholder: Boolean, advance: Float)
280
+ }
281
+
282
+ /**
283
+ * The glyphs a reflowing field lays out, in order, with the advance each takes:
284
+ * the prefix, the text (or the placeholder), `keepPlaceholder`'s tail and the
285
+ * suffix. The view hands them to the engine; [InputSizing] adds them up, which
286
+ * is the engine's width at rest.
287
+ */
288
+ internal object InputGlyphs {
289
+ /**
290
+ * [shown] is the text or, when [showPlaceholder], the placeholder; [tail]
291
+ * is ignored then. Advances carry the letter spacing; the last prefix glyph
292
+ * and the last body glyph before a suffix carry the seams instead.
293
+ */
294
+ fun walk(f: InputFonts, format: Format, shown: String, showPlaceholder: Boolean, tail: String, sink: GlyphSink) {
295
+ // A negative amount reads "-$1,234.56", not "$-1,234.56": the sign belongs
296
+ // to the amount, not to the digits after the symbol. It stays a *body*
297
+ // glyph - it is part of the text, and the caret counts body glyphs in the
298
+ // order they are added - and is only laid out ahead of the prefix.
299
+ val signed = format.signBeforeAffix && !showPlaceholder && format.prefix.isNotEmpty() &&
300
+ shown.isNotEmpty() && InputText.isSign(shown[0])
301
+ if (signed) run(shown.substring(0, 1), InputRole.BODY, f, false, null, sink)
302
+ run(format.prefix, InputRole.PREFIX, f, false, f.prefixSeam, sink)
303
+ val kept = if (showPlaceholder) "" else tail
304
+ run(if (signed) shown.substring(1) else shown, InputRole.BODY, f, showPlaceholder,
305
+ if (format.suffix.isNotEmpty() && kept.isEmpty()) f.suffixSeam else null, sink)
306
+ // `keepPlaceholder`: the rest of the mask trails the text as placeholder
307
+ // glyphs. They come after every typed glyph, so the caret - which counts
308
+ // body glyphs in order - never lands among them.
309
+ run(kept, InputRole.BODY, f, true, if (format.suffix.isNotEmpty()) f.suffixSeam else null, sink)
310
+ run(format.suffix, InputRole.SUFFIX, f, false, null, sink)
311
+ }
312
+
313
+ private fun run(s: String, role: InputRole, f: InputFonts, placeholder: Boolean, lastSpacing: Float?, sink: GlyphSink) {
314
+ val spacing = f.spacing(role)
315
+ var i = 0
316
+ while (i < s.length) {
317
+ val cp = s.codePointAt(i)
318
+ val next = i + Character.charCount(cp)
319
+ val after = if (next >= s.length && lastSpacing != null) lastSpacing else spacing
320
+ sink.glyph(cp, role, placeholder, f.width(cp, role) + after)
321
+ i = next
322
+ }
323
+ }
324
+ }
325
+
326
+ /** The size a single-line field reports through `onSizeChange`, worked out the same way before and after it mounts. */
327
+ internal object InputSizing {
328
+ /**
329
+ * A plain field's content width in px: the text (or the placeholder) and the
330
+ * affixes measured with the paints the field draws them in. Its own letter
331
+ * spacing follows every glyph; the affixes take their room with their seams.
332
+ */
333
+ fun plainWidthPx(f: InputFonts, format: Format, body: String): Float =
334
+ f.affixRoom(format.prefix, InputRole.PREFIX) +
335
+ f.plainWidth(body) + f.bodySpacing * body.codePointCount(0, body.length) +
336
+ f.affixRoom(format.suffix, InputRole.SUFFIX)
337
+
338
+ /** What a plain field measures: the placeholder when empty, bullets when secure, else the text and its tail. */
339
+ fun plainBody(text: String, hint: String, tail: String, secure: Boolean): String = when {
340
+ text.isEmpty() -> hint
341
+ secure -> InputText.bullets(text)
342
+ else -> text + tail
343
+ }
344
+
345
+ /** A reflowing field's width in px at rest: the engine's target, the sum of its glyphs' advances. */
346
+ fun reflowWidthPx(f: InputFonts, format: Format, text: String, hint: String, tail: String, secure: Boolean): Float {
347
+ val body = if (secure) InputText.bullets(text) else text
348
+ val showPlaceholder = text.isEmpty() && hint.isNotEmpty()
349
+ var width = 0.0
350
+ // The engine clamps every advance at 0 (a negative letter spacing).
351
+ InputGlyphs.walk(f, format, if (showPlaceholder) hint else body, showPlaceholder, tail) { _, _, _, advance ->
352
+ width += max(0.0, advance.toDouble())
353
+ }
354
+ return width.toFloat()
355
+ }
356
+
357
+ /** Content width and line height in px to the reported size in dp. */
358
+ fun reported(widthPx: Float, heightPx: Float, density: Float): NitroInputSize =
359
+ NitroInputSize(ceil(widthPx / density + 2f).toDouble(), ceil(heightPx / density).toDouble())
360
+ }
361
+
362
+ /**
363
+ * [NitroInputSizing] read through [InputSizingProps], so [formatOf] and
364
+ * [sizingTypographyOf] serve the measure as they do the view.
365
+ */
366
+ internal class SizingProps(s: NitroInputSizing) : InputSizingProps {
367
+ override val mode = s.mode
368
+ override val mask = s.mask
369
+ override val maskNotations = s.maskNotations
370
+ override val maskAutocomplete = s.maskAutocomplete
371
+ override val maskAutoSkip = s.maskAutoSkip
372
+ override val maskAffinityFormats = s.maskAffinityFormats
373
+ override val maskAffinityStrategy = s.maskAffinityStrategy
374
+ override val maskTextCase = s.maskTextCase
375
+ override val maskCharacterMap = s.maskCharacterMap
376
+ override val maskSlotPlaceholder = s.maskSlotPlaceholder
377
+ override val keepPlaceholder = s.keepPlaceholder
378
+ override val fractionDigits = s.fractionDigits
379
+ override val maxIntegerDigits = s.maxIntegerDigits
380
+ override val groupingSeparator = s.groupingSeparator
381
+ override val decimalSeparator = s.decimalSeparator
382
+ override val prefix = s.prefix
383
+ override val suffix = s.suffix
384
+ override val signPlacement = s.signPlacement
385
+ override val placeholder = s.placeholder
386
+ override val textSize = s.textSize
387
+ override val textLineHeight = s.textLineHeight
388
+ override val prefixFontSize = s.prefixFontSize
389
+ override val suffixFontSize = s.suffixFontSize
390
+ override val textWeight = s.textWeight
391
+ override val textFamily = s.textFamily
392
+ override val textLetterSpacing = s.textLetterSpacing
393
+ override val tabularNums = s.tabularNums
394
+ override val slotDigits = s.slotDigits
395
+ override val prefixSpacing = s.prefixSpacing
396
+ override val suffixSpacing = s.suffixSpacing
397
+ override val allowFontScaling = s.allowFontScaling
398
+ override val maxFontSizeMultiplier = s.maxFontSizeMultiplier
399
+ override val variant = s.variant
400
+ override val label = s.label
401
+ override val labelBehavior = s.labelBehavior
402
+ override val labelFontSize = s.labelFontSize
403
+ override val cornerRadius = s.cornerRadius
404
+ }