react-native-nitro-input 0.4.0 → 0.4.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 +44 -0
- package/README.md +2 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/DigitSlots.kt +143 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +6 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +24 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/InputFonts.kt +50 -5
- package/android/src/main/java/com/margelo/nitro/nitroinput/InputSizing.kt +7 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +117 -6
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +290 -137
- package/ios/HybridNitroInputView.swift +4 -0
- package/ios/InputSizing.swift +98 -4
- package/ios/NitroInputView.swift +79 -12
- package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +2 -0
- package/lib/commonjs/src/NitroInput.js +3 -1
- package/lib/commonjs/src/NitroNumber.js +94 -29
- package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +2 -0
- package/lib/module/src/NitroInput.js +3 -1
- package/lib/module/src/NitroNumber.js +94 -29
- package/lib/typescript/src/NitroInput.d.ts +11 -0
- package/lib/typescript/src/specs/NitroInput.nitro.d.ts +4 -0
- package/lib/typescript/src/specs/NitroInputMeasure.nitro.d.ts +2 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +18 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +4 -0
- package/nitrogen/generated/android/c++/JNitroInputSizing.hpp +9 -1
- package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +10 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +12 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputSizing.kt +12 -2
- package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +12 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +12 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +2 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +22 -0
- package/nitrogen/generated/ios/swift/NitroInputSizing.swift +12 -2
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +4 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +4 -0
- package/nitrogen/generated/shared/c++/NitroInputSizing.hpp +9 -1
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +4 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +6 -0
- package/nitrogen/generated/shared/json/NitroInputViewConfig.json +2 -0
- package/package.json +1 -1
- package/src/NitroInput.tsx +14 -0
- package/src/NitroNumber.tsx +121 -54
- package/src/specs/NitroInput.nitro.ts +4 -0
- package/src/specs/NitroInputMeasure.nitro.ts +2 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,49 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.3
|
|
4
|
+
|
|
5
|
+
0.4.2 was never published; its change is part of this release, with the
|
|
6
|
+
selection-drag regression it brought fixed before it shipped.
|
|
7
|
+
|
|
8
|
+
- **Android: a long-press selection drag or cursor drag inside a scroll view
|
|
9
|
+
or pager stays with the field (#73).** The field's edit text never asked
|
|
10
|
+
its parent not to intercept, so a `ScrollView` (or `KeyboardAwareScrollView`,
|
|
11
|
+
or a pager) could take the touch on the first move and cancel the selection
|
|
12
|
+
or cursor drag. The field now holds the touch from the start, keeps it once
|
|
13
|
+
a long-press has started a drag, and otherwise hands it back once the finger
|
|
14
|
+
has moved past the touch slop, unless the field can scroll its own text in
|
|
15
|
+
that direction. A vertical swipe that starts on a field still scrolls the
|
|
16
|
+
page, and a horizontal one still pages a pager, read-only fields included.
|
|
17
|
+
- **Android: a vertical swipe that starts on a field with more text than fits
|
|
18
|
+
scrolls the page.** A single-line field holding a long value can scroll its
|
|
19
|
+
text sideways, and that no longer makes it keep vertical swipes.
|
|
20
|
+
|
|
21
|
+
React Native's own `TextInput` (`ReactEditText`) has both bugs; NitroInput
|
|
22
|
+
has neither.
|
|
23
|
+
- **Android: `NitroNumber` no longer leaks views or hybrids**, behind the
|
|
24
|
+
out-of-memory crashes on low-end phones. The glyph-mask warm-up queue held
|
|
25
|
+
each dropped view until its tasks ran and grew without bound when the
|
|
26
|
+
typography changed often; its tasks are now plain data, owned and cancelled
|
|
27
|
+
per view, and capped. Callback props rooted the component that held them, so
|
|
28
|
+
Hermes never collected an unmounted `NitroNumber`'s hybrid; they now reach the
|
|
29
|
+
component through a `WeakRef`, and the hybrid drops them when its view goes.
|
|
30
|
+
|
|
31
|
+
## 0.4.1
|
|
32
|
+
|
|
33
|
+
- **`NitroInput`'s `tabularNums` prop: tabular digits in any font.** Digits
|
|
34
|
+
were tabular only in fonts that ship tabular figures: in one without (Open
|
|
35
|
+
Runde) a typed "1" was far narrower than the placeholder's "0" it replaced,
|
|
36
|
+
so `keepPlaceholder`'s tail slid left on each keystroke (by 14 dp over a
|
|
37
|
+
card number), and an amount changed width as its digits did.
|
|
38
|
+
`tabularNums` lays every digit out as wide as the widest, centred, as
|
|
39
|
+
`NitroNumber`'s columns are: in the field's own text, the placeholder, the
|
|
40
|
+
tail and the measure, on both platforms. Unset, the field asks for the
|
|
41
|
+
font's tabular figures as before, at no cost; `false` asks for its
|
|
42
|
+
proportional ones (as does a style's `fontVariant: ['proportional-nums']`).
|
|
43
|
+
- **Android: a plain field's text and placeholder ask for tabular figures.**
|
|
44
|
+
The overlay's paints did, the edit text did not, so a font whose tabular
|
|
45
|
+
figures are off by default (ABC Diatype) shifted the tail the same way.
|
|
46
|
+
|
|
3
47
|
## 0.4.0
|
|
4
48
|
|
|
5
49
|
- **Android: `NitroNumber`'s numeric transition no longer blocks the UI
|
package/README.md
CHANGED
|
@@ -521,6 +521,7 @@ screen behaves like theirs with these settings:
|
|
|
521
521
|
| `affixAlign` | `'baseline' \| 'center' \| 'top' \| 'bottom'` | `'baseline'` | How they line up with the text: `top` pins glyph tops, `bottom` the bottom of the ink (a currency code sits on the digits' baseline). |
|
|
522
522
|
| `prefixAlign` / `suffixAlign` | same | `affixAlign` | Per-affix override. |
|
|
523
523
|
| `letterSpacing` | number | `0` | Points added after every glyph, like `Text`'s; a smaller affix gets it in proportion to its size. |
|
|
524
|
+
| `tabularNums` | boolean | unset | Unset: the font's tabular figures where it has them (the system fonts do), at no cost. `true`: every digit as wide as the widest even in a font without them (Open Runde), laid out that wide and centred, so a `keepPlaceholder` tail never moves and an amount keeps its width as it changes; a little work on each change. `false`: the font's proportional figures, as is a style's `fontVariant: ['proportional-nums']`. |
|
|
524
525
|
| `prefixSpacing` / `suffixSpacing` | number | the letter spacing | Points between the prefix and the text, and between the text and the suffix. |
|
|
525
526
|
| `prefixOffset` / `suffixOffset` | number | `0` | Points an affix is moved down after its alignment (negative: up). |
|
|
526
527
|
| `format` | `NumberFormat` | none | The amount follows it: prefix and suffix, separators, fraction digits, where the sign goes. Sets `mode="number"`; the individual props override it. |
|
|
@@ -587,7 +588,7 @@ screen behaves like theirs with these settings:
|
|
|
587
588
|
| `onKeyPress` | `(event) => void` | – | Before the text changes: the character, `'Backspace'` or `'Enter'`. |
|
|
588
589
|
| `onNativeRef` | `(ref) => void` | – | Receives the Nitro object on mount. |
|
|
589
590
|
| `id` / `aria-label` | `string` | – | React Native's HTML-style aliases for `nativeID` / `accessibilityLabel`, resolved here because a Nitro view is handed the raw props; each wins over the older spelling. |
|
|
590
|
-
| `style`, `testID`, … | `ViewProps`; `style` as `TextInput`'s | – | Regular view props. `testID` and the accessibility label are forwarded to the system field, the element VoiceOver, TalkBack and e2e tools interact with. The field takes its width from its parent like a `TextInput`; see `autoWidth`. As on a `TextInput`, `style` can carry the typography (`fontSize`, `fontWeight`, `fontFamily`, `color`, `letterSpacing`, `lineHeight`, `textAlign`, `textAlignVertical`; the props of the same name win), and its padding and border inset the text and add to a size the field works out for itself. Two deliberate differences: digits are
|
|
591
|
+
| `style`, `testID`, … | `ViewProps`; `style` as `TextInput`'s | – | Regular view props. `testID` and the accessibility label are forwarded to the system field, the element VoiceOver, TalkBack and e2e tools interact with. The field takes its width from its parent like a `TextInput`; see `autoWidth`. As on a `TextInput`, `style` can carry the typography (`fontSize`, `fontWeight`, `fontFamily`, `color`, `letterSpacing`, `lineHeight`, `textAlign`, `textAlignVertical`; the props of the same name win), and its padding and border inset the text and add to a size the field works out for itself. Two deliberate differences: digits are tabular by default where the font has tabular figures (Android's Roboto has them anyway, and a field keeps its width whether it reflows or not; see `tabularNums` for a font without them), and Android gets no default padding (iOS's `TextInput` has none; React Native's Android one keeps the platform field's). |
|
|
591
592
|
|
|
592
593
|
`keyboardType`: `'default' | 'number-pad' | 'decimal-pad' | 'numeric' | 'email-address' | 'phone-pad' | 'url' | 'ascii-capable' | 'numbers-and-punctuation'`.
|
|
593
594
|
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
package com.margelo.nitro.nitroinput
|
|
2
|
+
|
|
3
|
+
import android.graphics.Canvas
|
|
4
|
+
import android.graphics.Paint
|
|
5
|
+
import android.graphics.Typeface
|
|
6
|
+
import android.text.NoCopySpan
|
|
7
|
+
import android.text.Spannable
|
|
8
|
+
import android.text.TextPaint
|
|
9
|
+
import android.text.style.ReplacementSpan
|
|
10
|
+
import kotlin.math.ceil
|
|
11
|
+
|
|
12
|
+
private const val DIGITS = "0123456789"
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Tabular figures for a font that has none.
|
|
16
|
+
*
|
|
17
|
+
* The paints ask for `tnum`, but a font without the feature (Open Runde) keeps
|
|
18
|
+
* its proportional digits: every typed "1" was narrower than the placeholder's
|
|
19
|
+
* "0" it replaced, so `keepPlaceholder`'s tail slid left on each keystroke and
|
|
20
|
+
* an amount changed width as its digits did. With `tabularNums` every digit
|
|
21
|
+
* is given a slot as wide as the widest digit instead, centred in it, as
|
|
22
|
+
* NitroNumber's columns are. In a font with tabular figures the digits are
|
|
23
|
+
* all that wide already, and nothing changes.
|
|
24
|
+
*
|
|
25
|
+
* In the edit text a slot is a [ReplacementSpan], whose width is a whole
|
|
26
|
+
* pixel, so a slot is the widest digit and one letter spacing rounded up, and
|
|
27
|
+
* [InputFonts] measures and draws with that same width. (Letter spacing on the
|
|
28
|
+
* digit was the first try: from API 35 the platform trims it at either end of
|
|
29
|
+
* a line, so the first and last digit went without.)
|
|
30
|
+
*/
|
|
31
|
+
internal object DigitSlots {
|
|
32
|
+
fun isDigit(codePoint: Int): Boolean = codePoint in '0'.code..'9'.code
|
|
33
|
+
|
|
34
|
+
/** Each digit's advance in a paint, letter spacing aside, and the widest of them. */
|
|
35
|
+
class Digits(val widths: FloatArray) {
|
|
36
|
+
val widest = widths.max()
|
|
37
|
+
|
|
38
|
+
/** A slot in px, for a letter spacing of [spacing] px. */
|
|
39
|
+
fun slot(spacing: Float): Int = ceil(widest + spacing - 0.001f).toInt()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The digits of [paint] (its letter spacing aside), or null when they are
|
|
44
|
+
* one width already (within a hundredth of the font size).
|
|
45
|
+
*/
|
|
46
|
+
fun measure(paint: Paint): Digits? {
|
|
47
|
+
val bare = Paint(paint)
|
|
48
|
+
bare.letterSpacing = 0f
|
|
49
|
+
val widths = FloatArray(10) { bare.measureText(DIGITS, it, it + 1) }
|
|
50
|
+
if (widths.max() - widths.min() <= paint.textSize * 0.01f) return null
|
|
51
|
+
return Digits(widths)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private data class Key(val typeface: Typeface?, val size: Float, val features: String?)
|
|
55
|
+
|
|
56
|
+
// For the edit text's paint, which the spans are handed a copy of on every layout pass.
|
|
57
|
+
private val cache = HashMap<Key, Digits?>()
|
|
58
|
+
|
|
59
|
+
private fun digits(paint: Paint): Digits? = synchronized(cache) {
|
|
60
|
+
val key = Key(paint.typeface, paint.textSize, paint.fontFeatureSettings)
|
|
61
|
+
if (key in cache) cache[key] else measure(paint).also {
|
|
62
|
+
if (cache.size > 32) cache.clear()
|
|
63
|
+
cache[key] = it
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private fun spacing(paint: Paint): Float = paint.letterSpacing * paint.textSize
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Re-spans every digit of [text] for [paint] (the edit text's), or only
|
|
71
|
+
* clears the spans when its font has tabular figures, or when [off].
|
|
72
|
+
*/
|
|
73
|
+
fun apply(text: Spannable, paint: Paint, off: Boolean) {
|
|
74
|
+
val on = !off && paint.textSize > 0f && digits(paint) != null
|
|
75
|
+
// Only what changed: every span set or removed lays the edit text out
|
|
76
|
+
// again, and a keystroke adds one digit (the spans of the others move
|
|
77
|
+
// with their characters). Re-spanning the lot cost ~1.5 ms a keystroke.
|
|
78
|
+
val spanned = BooleanArray(text.length)
|
|
79
|
+
for (span in text.getSpans(0, text.length, Slot::class.java)) {
|
|
80
|
+
val start = text.getSpanStart(span)
|
|
81
|
+
val keep = on && text.getSpanEnd(span) == start + 1 && isDigit(text[start].code) && !spanned[start]
|
|
82
|
+
if (keep) spanned[start] = true else text.removeSpan(span)
|
|
83
|
+
}
|
|
84
|
+
if (!on) return
|
|
85
|
+
for (i in text.indices) {
|
|
86
|
+
if (!spanned[i] && isDigit(text[i].code)) text.setSpan(Slot(), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Draws [text] at [x] in [paint] as the spans lay it out in the edit text:
|
|
92
|
+
* digits in their slots, the runs between them whole.
|
|
93
|
+
*/
|
|
94
|
+
fun draw(canvas: Canvas, text: String, x: Float, y: Float, paint: Paint) {
|
|
95
|
+
val digits = digits(paint)
|
|
96
|
+
if (digits == null) {
|
|
97
|
+
canvas.drawText(text, x, y, paint)
|
|
98
|
+
return
|
|
99
|
+
}
|
|
100
|
+
val slot = digits.slot(spacing(paint))
|
|
101
|
+
val spacing = paint.letterSpacing
|
|
102
|
+
var left = x
|
|
103
|
+
var start = 0
|
|
104
|
+
while (start < text.length) {
|
|
105
|
+
if (isDigit(text[start].code)) {
|
|
106
|
+
paint.letterSpacing = 0f
|
|
107
|
+
canvas.drawText(text, start, start + 1, left + (slot - digits.widths[text[start] - '0']) / 2f, y, paint)
|
|
108
|
+
paint.letterSpacing = spacing
|
|
109
|
+
left += slot
|
|
110
|
+
start++
|
|
111
|
+
continue
|
|
112
|
+
}
|
|
113
|
+
var end = start + 1
|
|
114
|
+
while (end < text.length && !isDigit(text[end].code)) end++
|
|
115
|
+
canvas.drawText(text, start, end, left, y, paint)
|
|
116
|
+
left += paint.measureText(text, start, end)
|
|
117
|
+
start = end
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* One digit, drawn centred in its slot. Not copied with the text (the
|
|
123
|
+
* clipboard, a saved state): the watcher puts the spans back on whatever
|
|
124
|
+
* text the field ends up with.
|
|
125
|
+
*/
|
|
126
|
+
private class Slot : ReplacementSpan(), NoCopySpan {
|
|
127
|
+
private val bare = TextPaint()
|
|
128
|
+
|
|
129
|
+
override fun getSize(paint: Paint, text: CharSequence, start: Int, end: Int, fm: Paint.FontMetricsInt?): Int {
|
|
130
|
+
if (fm != null) paint.getFontMetricsInt(fm)
|
|
131
|
+
val digits = digits(paint) ?: return ceil(paint.measureText(text, start, end)).toInt()
|
|
132
|
+
return digits.slot(spacing(paint))
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
override fun draw(canvas: Canvas, text: CharSequence, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint) {
|
|
136
|
+
bare.set(paint)
|
|
137
|
+
bare.letterSpacing = 0f
|
|
138
|
+
val digits = digits(paint)
|
|
139
|
+
val inset = if (digits == null) 0f else (digits.slot(spacing(paint)) - digits.widths[text[start] - '0']) / 2f
|
|
140
|
+
canvas.drawText(text, start, end, x + inset, y.toFloat(), bare)
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -129,6 +129,10 @@ class HybridNitroInputView(private val context: ThemedReactContext) : HybridNitr
|
|
|
129
129
|
set(v) { field = v; markConfigDirty() }
|
|
130
130
|
override var textLetterSpacing: Double = 0.0
|
|
131
131
|
set(v) { field = v; markConfigDirty() }
|
|
132
|
+
override var tabularNums: Boolean = true
|
|
133
|
+
set(v) { field = v; markConfigDirty() }
|
|
134
|
+
override var slotDigits: Boolean = false
|
|
135
|
+
set(v) { field = v; markConfigDirty() }
|
|
132
136
|
override var prefixSpacing: Double = Double.POSITIVE_INFINITY
|
|
133
137
|
set(v) { field = v; markConfigDirty() }
|
|
134
138
|
override var suffixSpacing: Double = Double.POSITIVE_INFINITY
|
|
@@ -423,6 +427,8 @@ class HybridNitroInputView(private val context: ThemedReactContext) : HybridNitr
|
|
|
423
427
|
suffixFontSize = Double.NaN
|
|
424
428
|
signPlacement = NitroInputSignPlacement.BEFOREAFFIX
|
|
425
429
|
textLetterSpacing = 0.0
|
|
430
|
+
tabularNums = true
|
|
431
|
+
slotDigits = false
|
|
426
432
|
prefixSpacing = Double.POSITIVE_INFINITY
|
|
427
433
|
suffixSpacing = Double.POSITIVE_INFINITY
|
|
428
434
|
prefixOffset = 0.0
|
|
@@ -214,7 +214,8 @@ class HybridNitroNumberView(private val context: ThemedReactContext) : HybridNit
|
|
|
214
214
|
override var onSizeChange: ((width: Double, height: Double) -> Unit)? = null
|
|
215
215
|
set(v) {
|
|
216
216
|
field = v
|
|
217
|
-
|
|
217
|
+
// Not `rollingView`: on a dropped hybrid that would make a fresh view to send nothing.
|
|
218
|
+
onMain { attachedView?.resendIntrinsicSize() }
|
|
218
219
|
}
|
|
219
220
|
|
|
220
221
|
// endregion
|
|
@@ -294,6 +295,7 @@ class HybridNitroNumberView(private val context: ThemedReactContext) : HybridNit
|
|
|
294
295
|
*/
|
|
295
296
|
override fun onDropView() {
|
|
296
297
|
recycled = false
|
|
298
|
+
releaseCallbacks()
|
|
297
299
|
onMain {
|
|
298
300
|
attachedView?.release()
|
|
299
301
|
// With view recycling on, Fabric asks for prepareForRecycle right after
|
|
@@ -305,9 +307,30 @@ class HybridNitroNumberView(private val context: ThemedReactContext) : HybridNit
|
|
|
305
307
|
|
|
306
308
|
/** JS called `dispose()` on the ref: same as a drop. */
|
|
307
309
|
override fun dispose() {
|
|
310
|
+
releaseCallbacks()
|
|
308
311
|
onMain { attachedView?.release() }
|
|
309
312
|
}
|
|
310
313
|
|
|
314
|
+
/**
|
|
315
|
+
* Lets go of the JS functions this hybrid holds (`onSizeChange` and the
|
|
316
|
+
* animation and reveal events). Each is a JS function kept alive from
|
|
317
|
+
* native, so Hermes counts it as a root; its closure reaches the
|
|
318
|
+
* component's refs, one of which holds the JS handle to this hybrid, and
|
|
319
|
+
* that handle keeps this hybrid. Held past the drop, that loop is one
|
|
320
|
+
* Hermes can never collect, however large `memorySize` says it is: every
|
|
321
|
+
* dropped NitroNumber stayed (its hybrid, Nitro's C++ part and the
|
|
322
|
+
* function wrappers), ~7,000 of them after a few minutes of a scrolling
|
|
323
|
+
* list. Dropped, the handle is ordinary garbage. A recycled view gets
|
|
324
|
+
* its callbacks back with its new element's props.
|
|
325
|
+
*/
|
|
326
|
+
private fun releaseCallbacks() {
|
|
327
|
+
onSizeChange = null
|
|
328
|
+
onRevealEnd = null
|
|
329
|
+
onRevealMilestone = null
|
|
330
|
+
onAnimationStart = null
|
|
331
|
+
onAnimationEnd = null
|
|
332
|
+
}
|
|
333
|
+
|
|
311
334
|
/**
|
|
312
335
|
* Reported to the JS garbage collector so a dropped view's handle counts as
|
|
313
336
|
* the memory it holds rather than as an empty object; that is what makes
|
|
@@ -86,9 +86,54 @@ internal class InputFonts(
|
|
|
86
86
|
InputRole.SUFFIX -> suffix
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
/**
|
|
89
|
+
/**
|
|
90
|
+
* The body's digits when they are slotted (`tabularNums` set) and the font
|
|
91
|
+
* has no tabular figures: each is then laid out in a slot as wide as the
|
|
92
|
+
* widest ([DigitSlots]); null otherwise. The affixes keep their own: they
|
|
93
|
+
* never change under the typing.
|
|
94
|
+
*/
|
|
95
|
+
private val bodyDigits: DigitSlots.Digits? = if (t.slotDigits) DigitSlots.measure(body) else null
|
|
96
|
+
|
|
97
|
+
/** A slotted body digit's advance, its letter spacing aside: the edit text's slot less that spacing. */
|
|
98
|
+
private val bodySlot: Float = bodyDigits?.let { it.slot(bodySpacing) - bodySpacing } ?: 0f
|
|
99
|
+
|
|
100
|
+
private fun slotted(codePoint: Int, role: InputRole): Boolean =
|
|
101
|
+
role == InputRole.BODY && bodyDigits != null && DigitSlots.isDigit(codePoint)
|
|
102
|
+
|
|
103
|
+
/** Advance of one character, in px: a slotted digit's is the slot's. */
|
|
90
104
|
fun width(codePoint: Int, role: InputRole): Float =
|
|
91
|
-
widthCaches[role.ordinal].getOrPut(codePoint) {
|
|
105
|
+
widthCaches[role.ordinal].getOrPut(codePoint) {
|
|
106
|
+
if (slotted(codePoint, role)) bodySlot else paint(role).measureText(charString(codePoint))
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** How far to draw a character in from the left of its advance: a slotted digit is centred in it. */
|
|
110
|
+
fun inset(codePoint: Int, role: InputRole): Float {
|
|
111
|
+
if (!slotted(codePoint, role)) return 0f
|
|
112
|
+
return (bodySlot - bodyDigits!!.widths[codePoint - '0'.code]) / 2f
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* [text] in the body paint as the edit text lays it out, without letter
|
|
117
|
+
* spacing: whole, or, when its digits are slotted, in runs split at each
|
|
118
|
+
* digit, which is where the slots' spans split it.
|
|
119
|
+
*/
|
|
120
|
+
fun plainWidth(text: String): Float {
|
|
121
|
+
if (bodyDigits == null) return body.measureText(text)
|
|
122
|
+
var total = 0f
|
|
123
|
+
var start = 0
|
|
124
|
+
while (start < text.length) {
|
|
125
|
+
if (DigitSlots.isDigit(text[start].code)) {
|
|
126
|
+
total += width(text[start].code, InputRole.BODY)
|
|
127
|
+
start++
|
|
128
|
+
continue
|
|
129
|
+
}
|
|
130
|
+
var end = start + 1
|
|
131
|
+
while (end < text.length && !DigitSlots.isDigit(text[end].code)) end++
|
|
132
|
+
total += body.measureText(text, start, end)
|
|
133
|
+
start = end
|
|
134
|
+
}
|
|
135
|
+
return total
|
|
136
|
+
}
|
|
92
137
|
|
|
93
138
|
/** Sum of the per-character advances of [text] (what the engine lays out). */
|
|
94
139
|
fun width(text: String, role: InputRole): Float {
|
|
@@ -174,9 +219,9 @@ internal class InputFonts(
|
|
|
174
219
|
paint.typeface = makeTypeface(t)
|
|
175
220
|
paint.textSize = sizeDp * density
|
|
176
221
|
paint.color = t.color ?: defaultColor
|
|
177
|
-
// Tabular figures
|
|
178
|
-
// in text mode too, and proportional digits would jitter as they change.
|
|
179
|
-
paint.fontFeatureSettings = "tnum"
|
|
222
|
+
// Tabular figures unless asked not to, as on iOS: the engine slides digit
|
|
223
|
+
// runs in text mode too, and proportional digits would jitter as they change.
|
|
224
|
+
paint.fontFeatureSettings = if (t.tabularNums) "tnum" else "pnum"
|
|
180
225
|
return paint
|
|
181
226
|
}
|
|
182
227
|
|
|
@@ -41,6 +41,8 @@ internal interface InputSizingProps {
|
|
|
41
41
|
val textWeight: Double
|
|
42
42
|
val textFamily: String
|
|
43
43
|
val textLetterSpacing: Double
|
|
44
|
+
val tabularNums: Boolean
|
|
45
|
+
val slotDigits: Boolean
|
|
44
46
|
val prefixSpacing: Double
|
|
45
47
|
val suffixSpacing: Double
|
|
46
48
|
val allowFontScaling: Boolean
|
|
@@ -96,6 +98,8 @@ internal fun sizingTypographyOf(p: InputSizingProps, density: Float): Typography
|
|
|
96
98
|
fontWeight = clampedInt(p.textWeight, 100, 900, 400),
|
|
97
99
|
fontFamily = p.textFamily.ifEmpty { null },
|
|
98
100
|
letterSpacing = if (p.textLetterSpacing.isFinite()) p.textLetterSpacing.toFloat() else 0f,
|
|
101
|
+
tabularNums = p.tabularNums,
|
|
102
|
+
slotDigits = p.slotDigits,
|
|
99
103
|
prefixSpacing = p.prefixSpacing.takeIf { it.isFinite() }?.toFloat(),
|
|
100
104
|
suffixSpacing = p.suffixSpacing.takeIf { it.isFinite() }?.toFloat(),
|
|
101
105
|
allowFontScaling = p.allowFontScaling,
|
|
@@ -328,7 +332,7 @@ internal object InputSizing {
|
|
|
328
332
|
*/
|
|
329
333
|
fun plainWidthPx(f: InputFonts, format: Format, body: String): Float =
|
|
330
334
|
f.affixRoom(format.prefix, InputRole.PREFIX) +
|
|
331
|
-
f.
|
|
335
|
+
f.plainWidth(body) + f.bodySpacing * body.codePointCount(0, body.length) +
|
|
332
336
|
f.affixRoom(format.suffix, InputRole.SUFFIX)
|
|
333
337
|
|
|
334
338
|
/** What a plain field measures: the placeholder when empty, bullets when secure, else the text and its tail. */
|
|
@@ -386,6 +390,8 @@ internal class SizingProps(s: NitroInputSizing) : InputSizingProps {
|
|
|
386
390
|
override val textWeight = s.textWeight
|
|
387
391
|
override val textFamily = s.textFamily
|
|
388
392
|
override val textLetterSpacing = s.textLetterSpacing
|
|
393
|
+
override val tabularNums = s.tabularNums
|
|
394
|
+
override val slotDigits = s.slotDigits
|
|
389
395
|
override val prefixSpacing = s.prefixSpacing
|
|
390
396
|
override val suffixSpacing = s.suffixSpacing
|
|
391
397
|
override val allowFontScaling = s.allowFontScaling
|
|
@@ -24,7 +24,9 @@ import android.util.TypedValue
|
|
|
24
24
|
import android.view.Choreographer
|
|
25
25
|
import android.view.Gravity
|
|
26
26
|
import android.view.KeyEvent
|
|
27
|
+
import android.view.MotionEvent
|
|
27
28
|
import android.view.View
|
|
29
|
+
import android.view.ViewConfiguration
|
|
28
30
|
import android.view.animation.PathInterpolator
|
|
29
31
|
import android.view.inputmethod.EditorInfo
|
|
30
32
|
import android.view.inputmethod.InputConnection
|
|
@@ -142,6 +144,10 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
142
144
|
val suffixAlign: AffixAlign = AffixAlign.BASELINE,
|
|
143
145
|
/** Dp added after every glyph, as `Text`'s letterSpacing; an affix gets it in proportion to its size. */
|
|
144
146
|
val letterSpacing: Float = 0f,
|
|
147
|
+
/** The font's tabular figures (`tnum`) rather than its proportional ones. */
|
|
148
|
+
val tabularNums: Boolean = true,
|
|
149
|
+
/** Every digit laid out as wide as the widest, centred ([DigitSlots]): tabular figures for a font without them. */
|
|
150
|
+
val slotDigits: Boolean = false,
|
|
145
151
|
/** Dp between the prefix and the text, and between the text and the suffix, in place of the letter spacing there. */
|
|
146
152
|
val prefixSpacing: Float? = null,
|
|
147
153
|
val suffixSpacing: Float? = null,
|
|
@@ -412,6 +418,64 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
412
418
|
private inner class HiddenEditText(context: Context) : AppCompatEditText(context) {
|
|
413
419
|
var ready = false
|
|
414
420
|
|
|
421
|
+
// Touch hand-off to scrolling parents (#73). The parent may not intercept
|
|
422
|
+
// until the finger has moved past the touch slop; the touch then goes back
|
|
423
|
+
// to it unless the field can scroll its own text in the direction the
|
|
424
|
+
// finger moves. Holding the whole touch instead (any move but a vertical
|
|
425
|
+
// one) broke paging a horizontal pager from a field.
|
|
426
|
+
//
|
|
427
|
+
// React Native's ReactEditText decides on the first move and keeps the
|
|
428
|
+
// touch whenever the field can scroll in any direction. Two of its bugs
|
|
429
|
+
// stay out of here:
|
|
430
|
+
// - A long-press that fired before the decision keeps the touch: the
|
|
431
|
+
// Editor then runs a selection drag (and has already disallowed
|
|
432
|
+
// intercepting) or has just placed the cursor. Handing the touch back
|
|
433
|
+
// let a ScrollView take a selection drag that drifts a little down, or a
|
|
434
|
+
// pager one that runs sideways, and the selection stuck on the first
|
|
435
|
+
// word. A long-press after the hand-back disallows intercepting again
|
|
436
|
+
// by itself, as does a cursor drag in a focused field.
|
|
437
|
+
// - A field whose single line of text is longer than the field can scroll
|
|
438
|
+
// only sideways, so a vertical swipe that starts on it is the page's.
|
|
439
|
+
private var detectScrollMovement = false
|
|
440
|
+
private var longPressed = false
|
|
441
|
+
private var downX = 0f
|
|
442
|
+
private var downY = 0f
|
|
443
|
+
private val touchSlop = ViewConfiguration.get(context).scaledTouchSlop
|
|
444
|
+
|
|
445
|
+
override fun onTouchEvent(event: MotionEvent): Boolean {
|
|
446
|
+
when (event.actionMasked) {
|
|
447
|
+
MotionEvent.ACTION_DOWN -> {
|
|
448
|
+
detectScrollMovement = true
|
|
449
|
+
longPressed = false
|
|
450
|
+
downX = event.x
|
|
451
|
+
downY = event.y
|
|
452
|
+
parent?.requestDisallowInterceptTouchEvent(true)
|
|
453
|
+
}
|
|
454
|
+
MotionEvent.ACTION_MOVE -> if (detectScrollMovement) {
|
|
455
|
+
val dx = abs(event.x - downX)
|
|
456
|
+
val dy = abs(event.y - downY)
|
|
457
|
+
if (longPressed) {
|
|
458
|
+
detectScrollMovement = false
|
|
459
|
+
} else if (dx > touchSlop || dy > touchSlop) {
|
|
460
|
+
val scrollsItself = if (dy > dx) {
|
|
461
|
+
canScrollVertically(-1) || canScrollVertically(1)
|
|
462
|
+
} else {
|
|
463
|
+
canScrollHorizontally(-1) || canScrollHorizontally(1)
|
|
464
|
+
}
|
|
465
|
+
if (!scrollsItself) parent?.requestDisallowInterceptTouchEvent(false)
|
|
466
|
+
detectScrollMovement = false
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
return super.onTouchEvent(event)
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
override fun performLongClick(): Boolean {
|
|
474
|
+
val handled = super.performLongClick()
|
|
475
|
+
if (handled) longPressed = true
|
|
476
|
+
return handled
|
|
477
|
+
}
|
|
478
|
+
|
|
415
479
|
override fun getOffsetForPosition(x: Float, y: Float): Int {
|
|
416
480
|
// A plain field draws its own text and the engine holds no glyphs for
|
|
417
481
|
// it, so the EditText's own mapping is the right one. (Going through the
|
|
@@ -445,7 +509,11 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
445
509
|
(scrollX + width - compoundPaddingRight).toFloat(),
|
|
446
510
|
(scrollY + height).toFloat(),
|
|
447
511
|
)
|
|
448
|
-
|
|
512
|
+
if (typography.slotDigits) {
|
|
513
|
+
DigitSlots.draw(canvas, tail, x, baseline.toFloat(), ghostPaint)
|
|
514
|
+
} else {
|
|
515
|
+
canvas.drawText(tail, x, baseline.toFloat(), ghostPaint)
|
|
516
|
+
}
|
|
449
517
|
canvas.restore()
|
|
450
518
|
}
|
|
451
519
|
|
|
@@ -639,6 +707,16 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
639
707
|
editText.setSingleLine()
|
|
640
708
|
defaultHighlightColor = editText.highlightColor
|
|
641
709
|
editText.addTextChangedListener(Watcher())
|
|
710
|
+
// After it, and on every change, the engine's own writes included.
|
|
711
|
+
editText.addTextChangedListener(object : TextWatcher {
|
|
712
|
+
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
|
|
713
|
+
|
|
714
|
+
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
|
|
715
|
+
|
|
716
|
+
override fun afterTextChanged(s: Editable) {
|
|
717
|
+
DigitSlots.apply(s, editText.paint, off = !slotsDigits)
|
|
718
|
+
}
|
|
719
|
+
})
|
|
642
720
|
editText.setOnFocusChangeListener { _, focused ->
|
|
643
721
|
if (resyncingFocus) return@setOnFocusChangeListener
|
|
644
722
|
restartBlink()
|
|
@@ -770,6 +848,13 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
770
848
|
private fun applyEditTextLayout() {
|
|
771
849
|
val f = fonts
|
|
772
850
|
editText.typeface = f.body.typeface
|
|
851
|
+
// The paints' tabular figures too, which the field's own text, its hint and
|
|
852
|
+
// keepPlaceholder's tail (drawn in a copy of its paint) did not get: with a
|
|
853
|
+
// font whose digits are proportional unless asked (ABC Diatype) every typed
|
|
854
|
+
// "1" was narrower than the "0" of the placeholder it replaced, and the
|
|
855
|
+
// rest of the placeholder slid left by the difference on each keystroke.
|
|
856
|
+
// A font with no tabular figures at all is slotted (applyDigitSlots).
|
|
857
|
+
editText.fontFeatureSettings = f.body.fontFeatureSettings
|
|
773
858
|
// Spaced like the overlay, so the selection highlight lines up with the drawn glyphs.
|
|
774
859
|
editText.letterSpacing = if (f.body.textSize > 0f) f.bodySpacing / f.body.textSize else 0f
|
|
775
860
|
// Not `setTextSize` directly: the platform ignores it while auto-sizing is
|
|
@@ -829,6 +914,22 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
829
914
|
Alignment.CENTER -> Gravity.CENTER_HORIZONTAL
|
|
830
915
|
Alignment.RIGHT -> Gravity.RIGHT
|
|
831
916
|
}
|
|
917
|
+
applyDigitSlots()
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
/**
|
|
921
|
+
* Gives the edit text's digits, and its hint's, the widest digit's advance
|
|
922
|
+
* when the font has no tabular figures ([DigitSlots]), as [InputFonts] does
|
|
923
|
+
* for the overlay and the measure. A secure field's bullets stand where the
|
|
924
|
+
* digits are, so it has none.
|
|
925
|
+
*/
|
|
926
|
+
/** Whether the edit text's own digits are slotted: when asked, and not under a secure field's bullets. */
|
|
927
|
+
private val slotsDigits: Boolean
|
|
928
|
+
get() = typography.slotDigits && !keyboard.secureTextEntry
|
|
929
|
+
|
|
930
|
+
private fun applyDigitSlots() {
|
|
931
|
+
editText.text?.let { DigitSlots.apply(it, editText.paint, off = !slotsDigits) }
|
|
932
|
+
applyHint(force = true)
|
|
832
933
|
}
|
|
833
934
|
|
|
834
935
|
// endregion
|
|
@@ -883,7 +984,10 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
883
984
|
// A password input type switches the field to monospace, so its bullets
|
|
884
985
|
// were wider than the field measures them; React Native's TextInput puts
|
|
885
986
|
// the typeface back the same way.
|
|
886
|
-
if (k.secureTextEntry)
|
|
987
|
+
if (k.secureTextEntry) {
|
|
988
|
+
editText.typeface = fonts.body.typeface
|
|
989
|
+
editText.fontFeatureSettings = fonts.body.fontFeatureSettings
|
|
990
|
+
}
|
|
887
991
|
if (rawType != null) editText.setRawInputType(rawType)
|
|
888
992
|
if (k.multiline) {
|
|
889
993
|
editText.setSingleLine(false)
|
|
@@ -902,6 +1006,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
902
1006
|
if (k.secureTextEntry) {
|
|
903
1007
|
editText.transformationMethod = android.text.method.PasswordTransformationMethod.getInstance()
|
|
904
1008
|
}
|
|
1009
|
+
applyDigitSlots()
|
|
905
1010
|
val length = editText.text?.length ?: 0
|
|
906
1011
|
if (selectionStart >= 0 && (editText.selectionStart != selectionStart || editText.selectionEnd != selectionEnd)) {
|
|
907
1012
|
editText.setSelection(minOf(selectionStart, length), minOf(maxOf(selectionStart, selectionEnd), length))
|
|
@@ -1006,6 +1111,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1006
1111
|
if (edge == Gravity.END) side else 0, 0)
|
|
1007
1112
|
view.gravity = Gravity.CENTER_VERTICAL
|
|
1008
1113
|
view.typeface = paint.typeface
|
|
1114
|
+
view.fontFeatureSettings = paint.fontFeatureSettings
|
|
1009
1115
|
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, paint.textSize)
|
|
1010
1116
|
view.letterSpacing = if (paint.textSize > 0f) fonts.spacing(role) / paint.textSize else 0f
|
|
1011
1117
|
// The seam against the text, and the affix's own offset.
|
|
@@ -1860,10 +1966,12 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1860
1966
|
}
|
|
1861
1967
|
if (g.scale != 1f) canvas.scale(g.scale, g.scale, g.x + g.width / 2f, lineHeight / 2f)
|
|
1862
1968
|
val baseline = f.baseline(role, str, 0f) + g.y * lineHeight
|
|
1969
|
+
// A digit in a font without tabular figures, centred in its slot.
|
|
1970
|
+
val inset = f.inset(g.character, role)
|
|
1863
1971
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
|
1864
|
-
glyphPositions = f.shaped(g.character, role).draw(canvas, g.x, baseline, paint, glyphPositions)
|
|
1972
|
+
glyphPositions = f.shaped(g.character, role).draw(canvas, g.x + inset, baseline, paint, glyphPositions)
|
|
1865
1973
|
} else {
|
|
1866
|
-
canvas.drawText(str, g.x, baseline, paint)
|
|
1974
|
+
canvas.drawText(str, g.x + inset, baseline, paint)
|
|
1867
1975
|
}
|
|
1868
1976
|
canvas.restore()
|
|
1869
1977
|
}
|
|
@@ -2053,9 +2161,12 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
2053
2161
|
}
|
|
2054
2162
|
|
|
2055
2163
|
/** The placeholder waits for a resting label to float clear of the line. */
|
|
2056
|
-
private fun applyHint() {
|
|
2164
|
+
private fun applyHint(force: Boolean = false) {
|
|
2057
2165
|
val hint = effectivePlaceholder
|
|
2058
|
-
if (editText.hint?.toString()
|
|
2166
|
+
if (!force && editText.hint?.toString() == hint) return
|
|
2167
|
+
val spanned = android.text.SpannableString(hint)
|
|
2168
|
+
DigitSlots.apply(spanned, editText.paint, off = !typography.slotDigits)
|
|
2169
|
+
editText.hint = spanned
|
|
2059
2170
|
}
|
|
2060
2171
|
|
|
2061
2172
|
/** Re-feeds the engine so the placeholder follows the label. */
|