react-native-nitro-input 0.3.8 → 0.4.0
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 +89 -0
- package/README.md +5 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputMeasure.kt +52 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +74 -80
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +18 -18
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroTextView.kt +14 -14
- package/android/src/main/java/com/margelo/nitro/nitroinput/InputFonts.kt +199 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/InputSizing.kt +398 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +112 -333
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +178 -30
- package/ios/HybridNitroInputView.swift +69 -75
- package/ios/HybridNitroNumberView.swift +18 -18
- package/ios/HybridNitroTextView.swift +14 -14
- package/ios/InputSizing.swift +534 -0
- package/ios/NitroInputView.swift +102 -198
- package/ios/NitroNumberView.swift +98 -23
- package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +12 -8
- package/lib/commonjs/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -6
- package/lib/commonjs/nitrogen/generated/shared/json/NitroTextViewConfig.json +6 -6
- package/lib/commonjs/src/NitroInput.js +164 -23
- package/lib/commonjs/src/NitroNumber.js +1 -1
- package/lib/commonjs/src/NitroText.js +6 -6
- package/lib/commonjs/src/specs/NitroInputMeasure.nitro.js +2 -0
- package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +12 -8
- package/lib/module/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -6
- package/lib/module/nitrogen/generated/shared/json/NitroTextViewConfig.json +6 -6
- package/lib/module/src/NitroInput.js +165 -24
- package/lib/module/src/NitroNumber.js +1 -1
- package/lib/module/src/NitroText.js +6 -6
- package/lib/module/src/specs/NitroInputMeasure.nitro.js +1 -0
- package/lib/typescript/src/NitroInput.d.ts +9 -16
- package/lib/typescript/src/specs/NitroInput.nitro.d.ts +17 -8
- package/lib/typescript/src/specs/NitroInputMeasure.nitro.d.ts +67 -0
- package/lib/typescript/src/specs/NitroNumber.nitro.d.ts +6 -6
- package/lib/typescript/src/specs/NitroText.nitro.d.ts +6 -6
- package/nitro.json +10 -0
- package/nitrogen/generated/android/NitroInput+autolinking.cmake +2 -0
- package/nitrogen/generated/android/NitroInputOnLoad.cpp +16 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputMeasureSpec.cpp +93 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputMeasureSpec.hpp +63 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +76 -40
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +24 -16
- package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.cpp +30 -30
- package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.hpp +12 -12
- package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.cpp +30 -30
- package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.hpp +12 -12
- package/nitrogen/generated/android/c++/JNitroInputSize.hpp +61 -0
- package/nitrogen/generated/android/c++/JNitroInputSizing.hpp +284 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +44 -24
- package/nitrogen/generated/android/c++/views/JHybridNitroNumberViewStateUpdater.cpp +18 -18
- package/nitrogen/generated/android/c++/views/JHybridNitroTextViewStateUpdater.cpp +18 -18
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputMeasureSpec.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +32 -8
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberViewSpec.kt +6 -6
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroTextViewSpec.kt +6 -6
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputSize.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputSizing.kt +246 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.cpp +17 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +29 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +11 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.mm +8 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.swift +12 -0
- package/nitrogen/generated/ios/c++/HybridNitroInputMeasureSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNitroInputMeasureSpecSwift.hpp +112 -0
- package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +56 -32
- package/nitrogen/generated/ios/c++/HybridNitroNumberViewSpecSwift.hpp +24 -24
- package/nitrogen/generated/ios/c++/HybridNitroTextViewSpecSwift.hpp +24 -24
- package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +56 -32
- package/nitrogen/generated/ios/c++/views/HybridNitroNumberViewComponent.mm +24 -24
- package/nitrogen/generated/ios/c++/views/HybridNitroTextViewComponent.mm +24 -24
- package/nitrogen/generated/ios/swift/HybridNitroInputMeasureSpec.swift +55 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputMeasureSpec_cxx.swift +138 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +12 -8
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +68 -24
- package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec.swift +6 -6
- package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec_cxx.swift +18 -18
- package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec.swift +6 -6
- package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec_cxx.swift +18 -18
- package/nitrogen/generated/ios/swift/NitroInputSize.swift +34 -0
- package/nitrogen/generated/ios/swift/NitroInputSizing.swift +242 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputMeasureSpec.cpp +21 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputMeasureSpec.hpp +66 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +24 -16
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +24 -16
- package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.cpp +12 -12
- package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.hpp +12 -12
- package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.cpp +12 -12
- package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.hpp +12 -12
- package/nitrogen/generated/shared/c++/NitroInputSize.hpp +87 -0
- package/nitrogen/generated/shared/c++/NitroInputSizing.hpp +263 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +24 -16
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +36 -24
- package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.cpp +12 -12
- package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.hpp +18 -18
- package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.cpp +12 -12
- package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.hpp +18 -18
- package/nitrogen/generated/shared/json/NitroInputViewConfig.json +12 -8
- package/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -6
- package/nitrogen/generated/shared/json/NitroTextViewConfig.json +6 -6
- package/package.json +4 -2
- package/src/NitroInput.tsx +201 -64
- package/src/NitroNumber.tsx +6 -6
- package/src/NitroText.tsx +6 -6
- package/src/specs/NitroInput.nitro.ts +17 -8
- package/src/specs/NitroInputMeasure.nitro.ts +76 -0
- package/src/specs/NitroNumber.nitro.ts +6 -6
- 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
|
|
@@ -232,25 +228,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
232
228
|
if (field == value) return
|
|
233
229
|
val old = field
|
|
234
230
|
field = value
|
|
235
|
-
|
|
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
|
-
}
|
|
231
|
+
InputText.configure(formatter, maskEngine, old, value)
|
|
254
232
|
if (old.mode != value.mode) {
|
|
255
233
|
rebuildFonts()
|
|
256
234
|
applyKeyboard()
|
|
@@ -325,9 +303,45 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
325
303
|
if (field == value) return
|
|
326
304
|
field = value
|
|
327
305
|
applyEditTextLayout()
|
|
306
|
+
// The gravity alone did nothing: the plain field's text alignment, set
|
|
307
|
+
// when it became plain (before `textAlign` arrived in the same commit),
|
|
308
|
+
// overrides it, so a centred or right-aligned field stayed at the start.
|
|
309
|
+
applyTextAlignment()
|
|
328
310
|
overlay.invalidate()
|
|
329
311
|
}
|
|
330
312
|
|
|
313
|
+
/**
|
|
314
|
+
* A plain field's own text alignment. `AUTO` is the start edge of the layout
|
|
315
|
+
* direction; `LEFT` and `RIGHT` are absolute, which only
|
|
316
|
+
* `TEXT_ALIGNMENT_GRAVITY` over an absolute gravity gives - `VIEW_START` /
|
|
317
|
+
* `VIEW_END` would flip with the direction. A reflowing field's edit text is
|
|
318
|
+
* hidden behind the overlay and stays at the start.
|
|
319
|
+
*/
|
|
320
|
+
private fun applyTextAlignment() {
|
|
321
|
+
editText.textAlignment = if (!keyboard.plain) TEXT_ALIGNMENT_VIEW_START else when (alignment) {
|
|
322
|
+
Alignment.AUTO -> TEXT_ALIGNMENT_VIEW_START
|
|
323
|
+
Alignment.CENTER -> TEXT_ALIGNMENT_CENTER
|
|
324
|
+
Alignment.LEFT, Alignment.RIGHT -> TEXT_ALIGNMENT_GRAVITY
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* The style's padding and border, in dp. React Native lays an iOS view's
|
|
330
|
+
* content out inside them, but never hands a Nitro view on Android its
|
|
331
|
+
* padding, so the text ran to the field's edges: they inset the edit text,
|
|
332
|
+
* the overlay and the frame here instead (JS sends them; see `NitroInput`).
|
|
333
|
+
*/
|
|
334
|
+
var contentInsets: android.graphics.RectF = android.graphics.RectF()
|
|
335
|
+
set(value) {
|
|
336
|
+
if (field == value) return
|
|
337
|
+
field = value
|
|
338
|
+
setPadding(
|
|
339
|
+
(value.left * density).roundToInt(), (value.top * density).roundToInt(),
|
|
340
|
+
(value.right * density).roundToInt(), (value.bottom * density).roundToInt(),
|
|
341
|
+
)
|
|
342
|
+
invalidate()
|
|
343
|
+
}
|
|
344
|
+
|
|
331
345
|
/** The outline / fill and floating label this view draws for itself. */
|
|
332
346
|
var inputFrame: Frame = Frame()
|
|
333
347
|
set(value) {
|
|
@@ -420,7 +434,19 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
420
434
|
ghostPaint.set(paint)
|
|
421
435
|
ghostPaint.color = currentHintTextColor
|
|
422
436
|
val x = compoundPaddingLeft + textLayout.getPrimaryHorizontal(length())
|
|
437
|
+
// Inside the text area, as the field clips its own text and hint: drawn
|
|
438
|
+
// after super.onDraw has restored the canvas, a tail longer than the
|
|
439
|
+
// room left ran over the padding to the field's edge (a full field, or
|
|
440
|
+
// a long placeholder in a narrow one).
|
|
441
|
+
canvas.save()
|
|
442
|
+
canvas.clipRect(
|
|
443
|
+
(scrollX + compoundPaddingLeft).toFloat(),
|
|
444
|
+
scrollY.toFloat(),
|
|
445
|
+
(scrollX + width - compoundPaddingRight).toFloat(),
|
|
446
|
+
(scrollY + height).toFloat(),
|
|
447
|
+
)
|
|
423
448
|
canvas.drawText(tail, x, baseline.toFloat(), ghostPaint)
|
|
449
|
+
canvas.restore()
|
|
424
450
|
}
|
|
425
451
|
|
|
426
452
|
override fun onSelectionChanged(selStart: Int, selEnd: Int) {
|
|
@@ -528,7 +554,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
528
554
|
private var resolvedPlaceholderColor = mutedTextColor
|
|
529
555
|
/** `caret.color`, or the theme's accent. */
|
|
530
556
|
private var resolvedCaretColor = themeCaretColor
|
|
531
|
-
private var fonts:
|
|
557
|
+
private var fonts: InputFonts = makeFonts(Typography())
|
|
532
558
|
private val glyphs = ArrayList<Glyph>()
|
|
533
559
|
/** Reused per frame so the JNI hop never allocates; grown on demand. */
|
|
534
560
|
private var frameBuffer = DoubleArray(3 + 11 * 64)
|
|
@@ -687,172 +713,6 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
687
713
|
|
|
688
714
|
// region Fonts
|
|
689
715
|
|
|
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
716
|
private fun resolveThemeColor(attr: Int, fallback: Int): Int {
|
|
857
717
|
val value = TypedValue()
|
|
858
718
|
return if (context.theme.resolveAttribute(attr, value, true)) {
|
|
@@ -895,9 +755,12 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
895
755
|
|
|
896
756
|
private fun charString(codePoint: Int): String = charCache.getOrPut(codePoint) { String(Character.toChars(codePoint)) }
|
|
897
757
|
|
|
758
|
+
private fun makeFonts(t: Typography) =
|
|
759
|
+
InputFonts(t, density, resources.configuration.fontScale, context.assets, themeTextColor)
|
|
760
|
+
|
|
898
761
|
private fun rebuildFonts() {
|
|
899
762
|
deriveColors()
|
|
900
|
-
fonts =
|
|
763
|
+
fonts = makeFonts(typography)
|
|
901
764
|
applyEditTextLayout()
|
|
902
765
|
reportIntrinsicSize()
|
|
903
766
|
overlay.invalidate()
|
|
@@ -930,7 +793,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
930
793
|
// put space between its first line and the stroke: it pays for both edges.
|
|
931
794
|
val side = frameSideInsetPx.roundToInt()
|
|
932
795
|
val framePadding = if (inputFrame.draws && keyboard.multiline) {
|
|
933
|
-
(FRAME_PADDING_DP * density).roundToInt()
|
|
796
|
+
(InputFrame.FRAME_PADDING_DP * density).roundToInt()
|
|
934
797
|
} else {
|
|
935
798
|
0
|
|
936
799
|
}
|
|
@@ -949,9 +812,9 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
949
812
|
// Relative: the prefix sits at the start edge and the suffix at the end,
|
|
950
813
|
// so under a right-to-left layout the room for them swaps sides too.
|
|
951
814
|
editText.setPaddingRelative(
|
|
952
|
-
side + f.affixRoom(format.prefix,
|
|
815
|
+
side + f.affixRoom(format.prefix, InputRole.PREFIX).roundToInt(),
|
|
953
816
|
frameTopInsetPx.roundToInt() + framePadding + labelOverhang,
|
|
954
|
-
side + f.affixRoom(format.suffix,
|
|
817
|
+
side + f.affixRoom(format.suffix, InputRole.SUFFIX).roundToInt(),
|
|
955
818
|
framePadding,
|
|
956
819
|
)
|
|
957
820
|
applyAffixes()
|
|
@@ -1017,6 +880,10 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1017
880
|
val selectionStart = editText.selectionStart
|
|
1018
881
|
val selectionEnd = editText.selectionEnd
|
|
1019
882
|
editText.inputType = textType
|
|
883
|
+
// A password input type switches the field to monospace, so its bullets
|
|
884
|
+
// were wider than the field measures them; React Native's TextInput puts
|
|
885
|
+
// the typeface back the same way.
|
|
886
|
+
if (k.secureTextEntry) editText.typeface = fonts.body.typeface
|
|
1020
887
|
if (rawType != null) editText.setRawInputType(rawType)
|
|
1021
888
|
if (k.multiline) {
|
|
1022
889
|
editText.setSingleLine(false)
|
|
@@ -1116,11 +983,11 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1116
983
|
* nothing filled it, so a currency symbol was simply a gap.
|
|
1117
984
|
*/
|
|
1118
985
|
private fun applyAffixes() {
|
|
1119
|
-
prefixView = affixView(prefixView, format.prefix,
|
|
1120
|
-
suffixView = affixView(suffixView, format.suffix,
|
|
986
|
+
prefixView = affixView(prefixView, format.prefix, InputRole.PREFIX, Gravity.START)
|
|
987
|
+
suffixView = affixView(suffixView, format.suffix, InputRole.SUFFIX, Gravity.END)
|
|
1121
988
|
}
|
|
1122
989
|
|
|
1123
|
-
private fun affixView(existing: TextView?, affix: String, role:
|
|
990
|
+
private fun affixView(existing: TextView?, affix: String, role: InputRole, edge: Int): TextView? {
|
|
1124
991
|
if (!keyboard.plain || affix.isEmpty()) {
|
|
1125
992
|
existing?.let { removeView(it) }
|
|
1126
993
|
return null
|
|
@@ -1146,7 +1013,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1146
1013
|
val extra = seam.roundToInt().coerceAtLeast(0)
|
|
1147
1014
|
view.setPaddingRelative(view.paddingStart + if (edge == Gravity.END) extra else 0, view.paddingTop,
|
|
1148
1015
|
view.paddingEnd + if (edge == Gravity.START) extra else 0, view.paddingBottom)
|
|
1149
|
-
view.translationY = if (role ==
|
|
1016
|
+
view.translationY = if (role == InputRole.PREFIX) fonts.prefixOffset else fonts.suffixOffset
|
|
1150
1017
|
view.setTextColor(resolvedTextColor)
|
|
1151
1018
|
if (view.text?.toString() != affix) view.text = affix
|
|
1152
1019
|
return view
|
|
@@ -1179,14 +1046,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1179
1046
|
applyNativeCursor()
|
|
1180
1047
|
applyAutoSize()
|
|
1181
1048
|
applyAffixes()
|
|
1182
|
-
|
|
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
|
-
}
|
|
1049
|
+
applyTextAlignment()
|
|
1190
1050
|
// The width now comes from the field, not the engine.
|
|
1191
1051
|
reportIntrinsicSize()
|
|
1192
1052
|
} else {
|
|
@@ -1405,6 +1265,13 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1405
1265
|
caret = Caret()
|
|
1406
1266
|
alignment = Alignment.AUTO
|
|
1407
1267
|
}
|
|
1268
|
+
// Last: the defaults above fed the engine an empty text, which it would
|
|
1269
|
+
// take for its first, so the next element's text slid in instead of
|
|
1270
|
+
// showing as it is (the engine snaps only its first text).
|
|
1271
|
+
engine.reset()
|
|
1272
|
+
glyphs.clear()
|
|
1273
|
+
contentWidth = 0f
|
|
1274
|
+
fed = false
|
|
1408
1275
|
// onTextChange, onFocusChange, onSubmit and onIntrinsicSizeChange are the
|
|
1409
1276
|
// hybrid's wiring, not the element's props: they stay across recycling
|
|
1410
1277
|
// (the hybrid clears its own callback props).
|
|
@@ -1482,16 +1349,8 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1482
1349
|
// region Text flow
|
|
1483
1350
|
|
|
1484
1351
|
/** Text mode truncates to `maxLength`; number mode formats. */
|
|
1485
|
-
private fun conform(value: String): String
|
|
1486
|
-
|
|
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
|
-
}
|
|
1352
|
+
private fun conform(value: String): String =
|
|
1353
|
+
InputText.conform(value, format, formatter, maskEngine, keyboard.maxLength)
|
|
1495
1354
|
|
|
1496
1355
|
private fun setProgrammatic(value: String, notify: Boolean) {
|
|
1497
1356
|
var next = value
|
|
@@ -1693,35 +1552,19 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1693
1552
|
reportIntrinsicSize()
|
|
1694
1553
|
return
|
|
1695
1554
|
}
|
|
1696
|
-
val f = fonts
|
|
1697
1555
|
engine.setReduceMotion(animationsDisabled())
|
|
1698
1556
|
engine.beginText()
|
|
1699
1557
|
val hint = effectivePlaceholder
|
|
1700
1558
|
val showPlaceholder = text.isEmpty() && hint.isNotEmpty()
|
|
1701
1559
|
// `secureTextEntry` masks what the overlay draws: the EditText keeps the
|
|
1702
1560
|
// real text (the IME and autofill need it), every drawn glyph is a bullet.
|
|
1703
|
-
val body = if (keyboard.secureTextEntry)
|
|
1704
|
-
val
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
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)
|
|
1561
|
+
val body = if (keyboard.secureTextEntry) InputText.bullets(text) else text
|
|
1562
|
+
val numberKinds = format.mode == Mode.NUMBER
|
|
1563
|
+
// The same walk sizes the field before it mounts (InputSizing).
|
|
1564
|
+
InputGlyphs.walk(fonts, format, if (showPlaceholder) hint else body, showPlaceholder, ghostTail) { cp, role, placeholder, advance ->
|
|
1565
|
+
val kind = if (numberKinds && role == InputRole.BODY) formatter.kindOf(cp) else 0
|
|
1566
|
+
engine.addGlyph(cp, role.ordinal, kind, advance.toDouble(), placeholder)
|
|
1567
|
+
}
|
|
1725
1568
|
engine.commitText(caret, now())
|
|
1726
1569
|
fed = true
|
|
1727
1570
|
reportIntrinsicSize()
|
|
@@ -1729,23 +1572,6 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1729
1572
|
overlay.invalidate()
|
|
1730
1573
|
}
|
|
1731
1574
|
|
|
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
1575
|
/** Nearest glyph boundary to a tap, as a UTF-16 offset into the edit text. */
|
|
1750
1576
|
private fun offsetForTap(x: Float): Int {
|
|
1751
1577
|
val layout = contentLayout(overlay.width, overlay.height, contentWidth)
|
|
@@ -1854,7 +1680,8 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1854
1680
|
get() = if (typography.lineHeightPx > 0f) typography.lineHeightPx else fonts.lineHeight
|
|
1855
1681
|
|
|
1856
1682
|
private fun intrinsicHeightPx(): Float {
|
|
1857
|
-
|
|
1683
|
+
// A single line is centred in its height; a frame adds its padding (and a filled label's line).
|
|
1684
|
+
if (!keyboard.multiline) return InputFrame.singleLineHeightPx(fonts.lineHeight, inputFrame, fonts, density)
|
|
1858
1685
|
// The frame's padding counts on both paths. Leaving it off this one was
|
|
1859
1686
|
// enough to lose it entirely: at mount there is no layout yet, so this
|
|
1860
1687
|
// branch answered, and by the time the frame arrived and set the padding
|
|
@@ -1887,23 +1714,16 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
1887
1714
|
*/
|
|
1888
1715
|
private fun contentWidthPx(): Float {
|
|
1889
1716
|
if (!keyboard.plain) return engine.targetWidth().toFloat()
|
|
1890
|
-
|
|
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)
|
|
1717
|
+
return InputSizing.plainWidthPx(fonts, format, InputSizing.plainBody(text, effectivePlaceholder, ghostTail, keyboard.secureTextEntry))
|
|
1900
1718
|
}
|
|
1901
1719
|
|
|
1902
1720
|
private fun reportIntrinsicSize() {
|
|
1903
1721
|
// The reported size is always the full-size one: with shrink-to-fit the view
|
|
1904
1722
|
// keeps its height and the scaled text is centred inside it when drawing.
|
|
1905
|
-
|
|
1906
|
-
val
|
|
1723
|
+
// The same rounding as the measure that sized the field before it mounted.
|
|
1724
|
+
val size = InputSizing.reported(InputFrame.widthPx(contentWidthPx(), inputFrame, fonts, density), intrinsicHeightPx(), density)
|
|
1725
|
+
val widthDp = size.width.toFloat()
|
|
1726
|
+
val heightDp = size.height.toFloat()
|
|
1907
1727
|
if (abs(widthDp - lastReportedWidth) <= 0.01f && abs(heightDp - lastReportedHeight) <= 0.01f) {
|
|
1908
1728
|
pendingSizeReport = false
|
|
1909
1729
|
return
|
|
@@ -2035,7 +1855,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
2035
1855
|
// Sliding glyphs pass through the line box: clip so they appear from its
|
|
2036
1856
|
// edges, with a 0.15 em margin so a comma's tail is never cut at rest.
|
|
2037
1857
|
if (slides) {
|
|
2038
|
-
val band = min(lineHeight / 3f, 0.15f * f.paint(
|
|
1858
|
+
val band = min(lineHeight / 3f, 0.15f * f.paint(InputRole.BODY).textSize)
|
|
2039
1859
|
canvas.clipRect(-1e5f, -band, 1e5f, lineHeight + band)
|
|
2040
1860
|
}
|
|
2041
1861
|
if (g.scale != 1f) canvas.scale(g.scale, g.scale, g.x + g.width / 2f, lineHeight / 2f)
|
|
@@ -2069,10 +1889,10 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
2069
1889
|
* the corner that the notch never opens onto the arc itself.
|
|
2070
1890
|
*/
|
|
2071
1891
|
private val labelInsetPx: Float
|
|
2072
|
-
get() =
|
|
1892
|
+
get() = InputFrame.labelInsetPx(inputFrame, density)
|
|
2073
1893
|
|
|
2074
1894
|
private val frameSideInsetPx: Float
|
|
2075
|
-
get() =
|
|
1895
|
+
get() = InputFrame.sideInsetPx(inputFrame, density)
|
|
2076
1896
|
|
|
2077
1897
|
/**
|
|
2078
1898
|
* Room at the top for the floated label - which only the filled variant
|
|
@@ -2082,20 +1902,12 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
2082
1902
|
* text stays centred the way it would with no label at all.
|
|
2083
1903
|
*/
|
|
2084
1904
|
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
|
-
}
|
|
1905
|
+
get() = InputFrame.topInsetPx(inputFrame, restingLabelSizePx, density)
|
|
2090
1906
|
|
|
2091
|
-
private val restingLabelSizePx: Float get() = fonts.paint(
|
|
1907
|
+
private val restingLabelSizePx: Float get() = fonts.paint(InputRole.BODY).textSize
|
|
2092
1908
|
|
|
2093
1909
|
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
|
-
}
|
|
1910
|
+
get() = InputFrame.floatedLabelSizePx(inputFrame, restingLabelSizePx, density)
|
|
2099
1911
|
|
|
2100
1912
|
/** The label floats once focused or holding text - or always, if asked. */
|
|
2101
1913
|
private val labelShouldFloat: Boolean
|
|
@@ -2117,40 +1929,11 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
2117
1929
|
* slots as `maskSlotPlaceholder`: "+1 (___) ___-____".
|
|
2118
1930
|
*/
|
|
2119
1931
|
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
|
-
}
|
|
1932
|
+
get() = InputText.basePlaceholder(format, maskEngine)
|
|
2127
1933
|
|
|
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
|
-
*/
|
|
1934
|
+
/** `keepPlaceholder`: what is still to come after the typed text ([InputText.ghostTail]). */
|
|
2137
1935
|
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
|
-
}
|
|
1936
|
+
get() = InputText.ghostTail(text, format, maskEngine, keyboard.secureTextEntry)
|
|
2154
1937
|
|
|
2155
1938
|
private fun resolvedStrokeColor(): Int {
|
|
2156
1939
|
val base = inputFrame.strokeColor ?: mutedTextColor
|
|
@@ -2203,7 +1986,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
2203
1986
|
}
|
|
2204
1987
|
|
|
2205
1988
|
private fun rebuildLabelPaint() {
|
|
2206
|
-
val body = fonts.paint(
|
|
1989
|
+
val body = fonts.paint(InputRole.BODY)
|
|
2207
1990
|
labelPaint.typeface = body.typeface
|
|
2208
1991
|
labelPaint.textSize = restingLabelSizePx
|
|
2209
1992
|
}
|
|
@@ -2284,7 +2067,18 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
2284
2067
|
|
|
2285
2068
|
override fun onDraw(canvas: Canvas) {
|
|
2286
2069
|
super.onDraw(canvas)
|
|
2287
|
-
|
|
2070
|
+
// Inside the style's padding and border, as React Native lays the frame
|
|
2071
|
+
// out on iOS (see [contentInsets]).
|
|
2072
|
+
val boxWidth = width - paddingLeft - paddingRight
|
|
2073
|
+
val boxHeight = height - paddingTop - paddingBottom
|
|
2074
|
+
if (!inputFrame.draws || boxWidth <= 0 || boxHeight <= 0) return
|
|
2075
|
+
val saved = canvas.save()
|
|
2076
|
+
canvas.translate(paddingLeft.toFloat(), paddingTop.toFloat())
|
|
2077
|
+
drawFrame(canvas, boxWidth, boxHeight)
|
|
2078
|
+
canvas.restoreToCount(saved)
|
|
2079
|
+
}
|
|
2080
|
+
|
|
2081
|
+
private fun drawFrame(canvas: Canvas, width: Int, height: Int) {
|
|
2288
2082
|
|
|
2289
2083
|
val filled = inputFrame.variant == Variant.FILLED
|
|
2290
2084
|
val stroke = strokeWidthPx
|
|
@@ -2427,27 +2221,12 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
2427
2221
|
companion object {
|
|
2428
2222
|
/** Matches iOS's `.placeholderText`, which is the label colour at 30-40%. */
|
|
2429
2223
|
private const val PLACEHOLDER_ALPHA = 0.38f
|
|
2430
|
-
private const val ALL_DIGITS = "0123456789"
|
|
2431
2224
|
private const val BLINK_MS = 500L
|
|
2432
2225
|
private const val CARET_WIDTH_DP = 2f
|
|
2433
2226
|
/** Fraction of the line height the caret leaves free (split between top and bottom). */
|
|
2434
2227
|
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
2228
|
/** Breathing room the notch leaves on each side of the label. */
|
|
2447
2229
|
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
2230
|
/**
|
|
2452
2231
|
* Taken from Material's own text fields. MUI's OutlinedInput runs the label
|
|
2453
2232
|
* 200ms on the standard decelerate curve and the legend's width 100ms after
|
|
@@ -2462,7 +2241,7 @@ class NitroInputView(context: Context) : FrameLayout(context) {
|
|
|
2462
2241
|
private const val NOTCH_CLOSE_MS = 50L
|
|
2463
2242
|
/** Material's standard decelerate, the same curve the iOS side uses. */
|
|
2464
2243
|
private val DECELERATE = PathInterpolator(0f, 0f, 0.2f, 1f)
|
|
2465
|
-
private val ROLES =
|
|
2244
|
+
private val ROLES = InputRole.values()
|
|
2466
2245
|
}
|
|
2467
2246
|
}
|
|
2468
2247
|
|