react-native-nitro-input 0.3.7 → 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.
Files changed (129) hide show
  1. package/CHANGELOG.md +133 -0
  2. package/README.md +9 -4
  3. package/android/CMakeLists.txt +1 -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 +74 -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 +199 -0
  9. package/android/src/main/java/com/margelo/nitro/nitroinput/InputSizing.kt +398 -0
  10. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +112 -333
  11. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +178 -30
  12. package/cpp/RollingEngine.cpp +68 -5
  13. package/cpp/RollingEngine.hpp +13 -0
  14. package/cpp/numberformat/FormatFigure.cpp +134 -0
  15. package/cpp/numberformat/FormatFigure.hpp +65 -0
  16. package/cpp/numberformat/HybridNitroNumberFormat.cpp +32 -10
  17. package/cpp/numberformat/HybridNitroNumberFormat.hpp +9 -1
  18. package/ios/HybridNitroInputView.swift +69 -75
  19. package/ios/HybridNitroNumberView.swift +18 -18
  20. package/ios/HybridNitroTextView.swift +14 -14
  21. package/ios/InputSizing.swift +534 -0
  22. package/ios/NitroInputView.swift +102 -198
  23. package/ios/NitroNumberView.swift +98 -23
  24. package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +12 -8
  25. package/lib/commonjs/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -6
  26. package/lib/commonjs/nitrogen/generated/shared/json/NitroTextViewConfig.json +6 -6
  27. package/lib/commonjs/src/NitroInput.js +164 -23
  28. package/lib/commonjs/src/NitroNumber.js +19 -11
  29. package/lib/commonjs/src/NitroText.js +6 -6
  30. package/lib/commonjs/src/NumberFormat.js +20 -0
  31. package/lib/commonjs/src/formatProps.js +57 -81
  32. package/lib/commonjs/src/specs/NitroInputMeasure.nitro.js +2 -0
  33. package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +12 -8
  34. package/lib/module/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -6
  35. package/lib/module/nitrogen/generated/shared/json/NitroTextViewConfig.json +6 -6
  36. package/lib/module/src/NitroInput.js +165 -24
  37. package/lib/module/src/NitroNumber.js +20 -12
  38. package/lib/module/src/NitroText.js +6 -6
  39. package/lib/module/src/NumberFormat.js +18 -0
  40. package/lib/module/src/formatProps.js +54 -81
  41. package/lib/module/src/specs/NitroInputMeasure.nitro.js +1 -0
  42. package/lib/typescript/src/NitroInput.d.ts +9 -16
  43. package/lib/typescript/src/NitroNumber.d.ts +25 -7
  44. package/lib/typescript/src/NumberFormat.d.ts +15 -1
  45. package/lib/typescript/src/formatProps.d.ts +23 -18
  46. package/lib/typescript/src/specs/NitroInput.nitro.d.ts +17 -8
  47. package/lib/typescript/src/specs/NitroInputMeasure.nitro.d.ts +67 -0
  48. package/lib/typescript/src/specs/NitroNumber.nitro.d.ts +6 -6
  49. package/lib/typescript/src/specs/NitroText.nitro.d.ts +6 -6
  50. package/lib/typescript/src/specs/NumberFormat.nitro.d.ts +33 -0
  51. package/nitro.json +10 -0
  52. package/nitrogen/generated/android/NitroInput+autolinking.cmake +2 -0
  53. package/nitrogen/generated/android/NitroInputOnLoad.cpp +16 -0
  54. package/nitrogen/generated/android/c++/JHybridNitroInputMeasureSpec.cpp +93 -0
  55. package/nitrogen/generated/android/c++/JHybridNitroInputMeasureSpec.hpp +63 -0
  56. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +76 -40
  57. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +24 -16
  58. package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.cpp +30 -30
  59. package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.hpp +12 -12
  60. package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.cpp +30 -30
  61. package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.hpp +12 -12
  62. package/nitrogen/generated/android/c++/JNitroInputSize.hpp +61 -0
  63. package/nitrogen/generated/android/c++/JNitroInputSizing.hpp +284 -0
  64. package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +44 -24
  65. package/nitrogen/generated/android/c++/views/JHybridNitroNumberViewStateUpdater.cpp +18 -18
  66. package/nitrogen/generated/android/c++/views/JHybridNitroTextViewStateUpdater.cpp +18 -18
  67. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputMeasureSpec.kt +56 -0
  68. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +32 -8
  69. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberViewSpec.kt +6 -6
  70. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroTextViewSpec.kt +6 -6
  71. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputSize.kt +56 -0
  72. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputSizing.kt +246 -0
  73. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.cpp +17 -0
  74. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +29 -0
  75. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +11 -0
  76. package/nitrogen/generated/ios/NitroInputAutolinking.mm +8 -0
  77. package/nitrogen/generated/ios/NitroInputAutolinking.swift +12 -0
  78. package/nitrogen/generated/ios/c++/HybridNitroInputMeasureSpecSwift.cpp +11 -0
  79. package/nitrogen/generated/ios/c++/HybridNitroInputMeasureSpecSwift.hpp +112 -0
  80. package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +56 -32
  81. package/nitrogen/generated/ios/c++/HybridNitroNumberViewSpecSwift.hpp +24 -24
  82. package/nitrogen/generated/ios/c++/HybridNitroTextViewSpecSwift.hpp +24 -24
  83. package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +56 -32
  84. package/nitrogen/generated/ios/c++/views/HybridNitroNumberViewComponent.mm +24 -24
  85. package/nitrogen/generated/ios/c++/views/HybridNitroTextViewComponent.mm +24 -24
  86. package/nitrogen/generated/ios/swift/HybridNitroInputMeasureSpec.swift +55 -0
  87. package/nitrogen/generated/ios/swift/HybridNitroInputMeasureSpec_cxx.swift +138 -0
  88. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +12 -8
  89. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +68 -24
  90. package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec.swift +6 -6
  91. package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec_cxx.swift +18 -18
  92. package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec.swift +6 -6
  93. package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec_cxx.swift +18 -18
  94. package/nitrogen/generated/ios/swift/NitroInputSize.swift +34 -0
  95. package/nitrogen/generated/ios/swift/NitroInputSizing.swift +242 -0
  96. package/nitrogen/generated/shared/c++/HybridNitroInputMeasureSpec.cpp +21 -0
  97. package/nitrogen/generated/shared/c++/HybridNitroInputMeasureSpec.hpp +66 -0
  98. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +24 -16
  99. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +24 -16
  100. package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.cpp +2 -0
  101. package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.hpp +8 -0
  102. package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.cpp +12 -12
  103. package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.hpp +12 -12
  104. package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.cpp +12 -12
  105. package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.hpp +12 -12
  106. package/nitrogen/generated/shared/c++/NitroInputSize.hpp +87 -0
  107. package/nitrogen/generated/shared/c++/NitroInputSizing.hpp +263 -0
  108. package/nitrogen/generated/shared/c++/NumberFigure.hpp +95 -0
  109. package/nitrogen/generated/shared/c++/NumberFormatLayout.hpp +112 -0
  110. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +24 -16
  111. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +36 -24
  112. package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.cpp +12 -12
  113. package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.hpp +18 -18
  114. package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.cpp +12 -12
  115. package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.hpp +18 -18
  116. package/nitrogen/generated/shared/json/NitroInputViewConfig.json +12 -8
  117. package/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -6
  118. package/nitrogen/generated/shared/json/NitroTextViewConfig.json +6 -6
  119. package/package.json +5 -3
  120. package/src/NitroInput.tsx +201 -64
  121. package/src/NitroNumber.tsx +54 -25
  122. package/src/NitroText.tsx +6 -6
  123. package/src/NumberFormat.ts +22 -0
  124. package/src/formatProps.ts +59 -99
  125. package/src/specs/NitroInput.nitro.ts +17 -8
  126. package/src/specs/NitroInputMeasure.nitro.ts +76 -0
  127. package/src/specs/NitroNumber.nitro.ts +6 -6
  128. package/src/specs/NitroText.nitro.ts +6 -6
  129. package/src/specs/NumberFormat.nitro.ts +35 -0
@@ -23,12 +23,19 @@ import UIKit
23
23
  final class NitroInputView: UIView {
24
24
 
25
25
  private typealias Engine = margelo.nitro.nitroinput.ReflowEngine
26
- private typealias AmountFormatter = margelo.nitro.nitroinput.AmountFormatter
27
- private typealias MaskEngine = margelo.nitro.nitroinput.MaskEngine
26
+ typealias AmountFormatter = margelo.nitro.nitroinput.AmountFormatter
27
+ typealias MaskEngine = margelo.nitro.nitroinput.MaskEngine
28
28
  private typealias Outline = margelo.nitro.nitroinput.OutlineGeometry
29
29
 
30
30
  // Engine roles / kinds (plain ints so the unscoped C++ enums need no bridging).
31
31
  private enum Role { static let prefix: Int32 = 0, body: Int32 = 1, suffix: Int32 = 2 }
32
+ private static func engineRole(_ role: GlyphRole) -> Int32 {
33
+ switch role {
34
+ case .prefix: return Role.prefix
35
+ case .body: return Role.body
36
+ case .suffix: return Role.suffix
37
+ }
38
+ }
32
39
  private enum Kind { static let text: Int32 = 0 }
33
40
 
34
41
  // MARK: - Configuration
@@ -342,7 +349,7 @@ final class NitroInputView: UIView {
342
349
 
343
350
  // MARK: - Fonts
344
351
 
345
- fileprivate enum GlyphRole {
352
+ enum GlyphRole {
346
353
  case body, prefix, suffix
347
354
  }
348
355
 
@@ -357,6 +364,9 @@ final class NitroInputView: UIView {
357
364
  /// Body / prefix / suffix fonts with per-glyph caches (advance widths, ink
358
365
  /// metrics and pre-rasterized images in the text or placeholder colour).
359
366
  fileprivate final class FontSet {
367
+ /// The fonts, spacing and advance widths, shared with the measure that
368
+ /// sizes a field before it mounts (InputSizing.swift).
369
+ let metrics: InputMetrics
360
370
  let body: UIFont
361
371
  let prefix: UIFont
362
372
  let suffix: UIFont
@@ -380,7 +390,6 @@ final class NitroInputView: UIView {
380
390
  /// Pixel density the glyph images are rendered at.
381
391
  let renderScale: CGFloat
382
392
  private var glyphCache: [GlyphKey: NSAttributedString] = [:]
383
- private var widthCache: [GlyphKey: CGFloat] = [:]
384
393
  private var imageCache: [GlyphKey: UIImage] = [:]
385
394
  private var inkDescentCache: [GlyphKey: CGFloat] = [:]
386
395
 
@@ -426,42 +435,29 @@ final class NitroInputView: UIView {
426
435
  init(_ t: Typography, traits: UITraitCollection) {
427
436
  // The view's own display, not the main screen's (deprecated, and absent on visionOS).
428
437
  renderScale = max(1, traits.displayScale)
429
- let scale = NitroInputView.systemFontMultiplier(t)
430
- body = NitroInputView.makeFont(size: t.fontSize * scale, weight: t.fontWeight, family: t.fontFamily)
431
- prefix = NitroInputView.makeFont(size: (t.prefixFontSize ?? t.fontSize) * scale, weight: t.fontWeight, family: t.fontFamily)
432
- suffix = NitroInputView.makeFont(size: (t.suffixFontSize ?? t.fontSize) * scale, weight: t.fontWeight, family: t.fontFamily)
438
+ metrics = InputMetrics(t)
439
+ body = metrics.body
440
+ prefix = metrics.prefix
441
+ suffix = metrics.suffix
433
442
  color = t.color.resolvedColor(with: traits)
434
443
  placeholderColor = t.placeholderColor.resolvedColor(with: traits)
435
444
  prefixAlign = t.prefixAlign
436
445
  suffixAlign = t.suffixAlign
437
- bodySpacing = t.letterSpacing * scale
438
- prefixLetterSpacing = t.letterSpacing * scale * (prefix.pointSize / body.pointSize)
439
- suffixLetterSpacing = t.letterSpacing * scale * (suffix.pointSize / body.pointSize)
440
- prefixSeam = t.prefixSpacing.map { $0 * scale }
441
- suffixSeam = t.suffixSpacing.map { $0 * scale }
442
- prefixOffset = t.prefixOffset * scale
443
- suffixOffset = t.suffixOffset * scale
444
- lineHeight = ceil(body.lineHeight)
446
+ bodySpacing = metrics.bodySpacing
447
+ prefixLetterSpacing = metrics.prefixLetterSpacing
448
+ suffixLetterSpacing = metrics.suffixLetterSpacing
449
+ prefixSeam = metrics.prefixSeam
450
+ suffixSeam = metrics.suffixSeam
451
+ prefixOffset = metrics.prefixOffset
452
+ suffixOffset = metrics.suffixOffset
453
+ lineHeight = metrics.lineHeight
445
454
  }
446
455
 
447
456
  /// Letter spacing after a glyph of `role`.
448
- func spacing(for role: GlyphRole) -> CGFloat {
449
- switch role {
450
- case .body: return bodySpacing
451
- case .prefix: return prefixLetterSpacing
452
- case .suffix: return suffixLetterSpacing
453
- }
454
- }
457
+ func spacing(for role: GlyphRole) -> CGFloat { metrics.spacing(for: role) }
455
458
 
456
459
  /// The room an affix takes beside the text: its glyphs, their spacing and the seam.
457
- func affixRoom(_ affix: String, role: GlyphRole) -> CGFloat {
458
- guard !affix.isEmpty else { return 0 }
459
- let glyphs = affix.unicodeScalars.reduce(CGFloat(0)) { $0 + width(of: String($1), role: role) }
460
- let count = CGFloat(affix.unicodeScalars.count)
461
- if role == .prefix { return glyphs + prefixLetterSpacing * (count - 1) + (prefixSeam ?? prefixLetterSpacing) }
462
- // The field's own kerning already spaces its last glyph by the body spacing.
463
- return glyphs + suffixLetterSpacing * count + ((suffixSeam ?? bodySpacing) - bodySpacing)
464
- }
460
+ func affixRoom(_ affix: String, role: GlyphRole) -> CGFloat { metrics.affixRoom(affix, role: role) }
465
461
 
466
462
  func font(for role: GlyphRole) -> UIFont {
467
463
  switch role {
@@ -484,13 +480,7 @@ final class NitroInputView: UIView {
484
480
  }
485
481
 
486
482
  /// Advance width of `text` in the role's font, in points.
487
- func width(of text: String, role: GlyphRole) -> CGFloat {
488
- let key = GlyphKey(text: text, role: role, placeholder: false)
489
- if let cached = widthCache[key] { return cached }
490
- let width = attributed(text, role: role).size().width
491
- widthCache[key] = width
492
- return width
493
- }
483
+ func width(of text: String, role: GlyphRole) -> CGFloat { metrics.width(of: text, role: role) }
494
484
 
495
485
  /// The glyph rasterized once (colour baked in, at screen density): a frame
496
486
  /// only moves layers, the main thread never redraws a bitmap.
@@ -550,8 +540,9 @@ final class NitroInputView: UIView {
550
540
  // MARK: - State
551
541
 
552
542
  private var engine = Engine()
553
- private var formatter = AmountFormatter()
554
- private var maskEngine = MaskEngine()
543
+ /// Configured through `InputText` (InputSizing.swift), as a measure's are.
544
+ var formatter = AmountFormatter()
545
+ var maskEngine = MaskEngine()
555
546
  /// The outline / fill. Stroke-only, so the notch is a hole rather than a mask.
556
547
  private lazy var frameLayer: CAShapeLayer = {
557
548
  let shape = CAShapeLayer()
@@ -1145,6 +1136,11 @@ final class NitroInputView: UIView {
1145
1136
  timing = Timing()
1146
1137
  traits = Traits()
1147
1138
  alignment = .auto
1139
+ // Last: the defaults above fed the engine an empty text (turning `plain`
1140
+ // off feeds it), which it would take for its first, so the next element's
1141
+ // text slid in instead of showing as it is (the engine snaps only its
1142
+ // first text).
1143
+ engine.reset()
1148
1144
  // `onTextChange`, `onFocusChange`, `onSubmit` and `onIntrinsicSizeChange`
1149
1145
  // are the hybrid's wiring, not the element's props: they stay across
1150
1146
  // recycling (the hybrid clears its own callback props).
@@ -1258,59 +1254,14 @@ final class NitroInputView: UIView {
1258
1254
  onIntrinsicSizeChange?(lastReportedSize)
1259
1255
  }
1260
1256
 
1261
- private func normalized(_ raw: String) -> String {
1262
- switch format.mode {
1263
- case .number:
1264
- return String(formatter.normalize(std.string(raw)).text)
1265
- case .mask:
1266
- let count = Int32(raw.unicodeScalars.count)
1267
- let result = maskEngine.apply(std.string(raw), count, true, format.maskAutocomplete, false)
1268
- return String(result.formattedText)
1269
- case .text:
1270
- return Self.truncated(raw, to: format.maxLength)
1271
- }
1272
- }
1257
+ private func normalized(_ raw: String) -> String { normalizedText(raw) }
1273
1258
 
1274
- private static func truncated(_ text: String, to maxLength: Int) -> String {
1275
- guard maxLength > 0, text.unicodeScalars.count > maxLength else { return text }
1276
- var view = String.UnicodeScalarView()
1277
- view.append(contentsOf: text.unicodeScalars.prefix(maxLength))
1278
- return String(view)
1279
- }
1259
+ private static func truncated(_ text: String, to maxLength: Int) -> String { InputText.truncated(text, to: maxLength) }
1280
1260
 
1281
1261
  // MARK: - Configuration application
1282
1262
 
1283
1263
  private func applyFormat(previous: Format) {
1284
- if format.mode == .number {
1285
- formatter.setFormat(Int32(max(0, format.fractionDigits)), Int32(max(1, format.maxIntegerDigits)),
1286
- std.string(format.groupingSeparator), std.string(format.decimalSeparator))
1287
- }
1288
- let maskChanged = format.mask != previous.mask || format.maskNotations != previous.maskNotations
1289
- || format.maskAffinityFormats != previous.maskAffinityFormats
1290
- || format.maskAffinityStrategy != previous.maskAffinityStrategy
1291
- || format.maskTextCase != previous.maskTextCase || format.maskCharacterMap != previous.maskCharacterMap
1292
- || format.maskSlotPlaceholder != previous.maskSlotPlaceholder
1293
- if format.mode == .mask, maskChanged || previous.mode != .mask {
1294
- maskEngine.clearNotations()
1295
- for notation in format.maskNotations {
1296
- maskEngine.addNotation(std.string(notation.character), std.string(notation.characterSet),
1297
- notation.isOptional)
1298
- }
1299
- maskEngine.clearAffinityFormats()
1300
- for alternative in format.maskAffinityFormats {
1301
- maskEngine.addAffinityFormat(std.string(alternative))
1302
- }
1303
- maskEngine.setAffinityStrategy(Int32(format.maskAffinityStrategy))
1304
- maskEngine.setTextCase(Int32(format.maskTextCase))
1305
- maskEngine.clearCharacterMap()
1306
- for mapping in format.maskCharacterMap {
1307
- maskEngine.addCharacterMapping(std.string(mapping.from), std.string(mapping.to))
1308
- }
1309
- maskEngine.setSlotPlaceholder(std.string(format.maskSlotPlaceholder))
1310
- // A bad pattern leaves the engine inactive; the field then behaves as
1311
- // plain text rather than refusing every keystroke.
1312
- _ = maskEngine.setFormat(std.string(format.mask))
1313
- }
1264
+ let maskChanged = configureEngines(previous: previous)
1314
1265
  updateFieldInsets()
1315
1266
  syncAccessibilityPlaceholder()
1316
1267
  // A changed formatter or mode reformats what is in the field.
@@ -1759,9 +1710,35 @@ final class NitroInputView: UIView {
1759
1710
  return (start, end)
1760
1711
  }
1761
1712
 
1713
+ private var batchDepth = 0
1714
+ private var feedPending = false
1715
+ private var feedCaret = -1
1716
+
1717
+ /// Runs `block` with engine feeds coalesced into one at the end, as Android's
1718
+ /// `batch` does. A prop commit applies the config and then the text; fed in
1719
+ /// between (turning `plain` off feeds the engine), the engine took the still
1720
+ /// empty field for its first text, which it shows as is, and animated the
1721
+ /// real one in: every reflowing amount slid in on mount.
1722
+ func batch(_ block: () -> Void) {
1723
+ batchDepth += 1
1724
+ block()
1725
+ batchDepth -= 1
1726
+ if batchDepth == 0, feedPending {
1727
+ feedPending = false
1728
+ let caret = feedCaret
1729
+ feedCaret = -1
1730
+ feedEngine(caret: caret)
1731
+ }
1732
+ }
1733
+
1762
1734
  /// Hands the engine the current text as glyphs (prefix, body or placeholder,
1763
1735
  /// suffix) with their advance widths and commits it.
1764
1736
  fileprivate func feedEngine(caret: Int) {
1737
+ if batchDepth > 0 {
1738
+ feedCaret = feedPending && feedCaret == -1 ? -1 : caret
1739
+ feedPending = true
1740
+ return
1741
+ }
1765
1742
  // Plain mode has no overlay to feed: the field draws its own text. Its
1766
1743
  // width still has to reach React for `autoWidth`, and this is the one
1767
1744
  // place every text, font, affix and placeholder change comes through.
@@ -1772,51 +1749,15 @@ final class NitroInputView: UIView {
1772
1749
  // `secureTextEntry` masks what the overlay draws: the hidden field keeps
1773
1750
  // the real text (UIKit needs it for editing and autofill), but every glyph
1774
1751
  // the user sees is a bullet.
1775
- let body = traits.secureTextEntry
1776
- ? String(repeating: "\u{2022}", count: text.unicodeScalars.count)
1777
- : text
1752
+ let body = traits.secureTextEntry ? InputText.bullets(text) : text
1778
1753
  let showPlaceholder = body.isEmpty && !effectivePlaceholder.isEmpty
1779
- let bodyText = showPlaceholder ? effectivePlaceholder : body
1780
1754
  let numberKinds = format.mode == .number
1781
-
1782
- // A negative amount reads "-$1,234.56", not "$-1,234.56": the sign belongs
1783
- // to the amount, not to the digits after the symbol. It stays a *body*
1784
- // glyph - it is part of the text, and the caret counts body glyphs in the
1785
- // order they are added - and is only laid out ahead of the prefix.
1786
- var rest = Array(bodyText.unicodeScalars)
1787
- let sign: Unicode.Scalar? =
1788
- format.signPlacement == .beforeAffix && !showPlaceholder && !format.prefix.isEmpty
1789
- && rest.first.map(Self.isSign) == true
1790
- ? rest.removeFirst()
1791
- : nil
1792
- // Advances carry the letter spacing; the last prefix glyph and the last
1793
- // body glyph before a suffix carry the seams instead.
1794
- let fonts = self.fonts
1795
- if let sign {
1796
- let kind = numberKinds ? formatter.kindOf(sign.value) : Kind.text
1797
- engine.addGlyph(sign.value, Role.body, kind, Double(fonts.width(of: String(sign), role: .body) + fonts.bodySpacing), false)
1798
- }
1799
- let prefixScalars = Array(format.prefix.unicodeScalars)
1800
- for (i, scalar) in prefixScalars.enumerated() {
1801
- let after = i == prefixScalars.count - 1 ? (fonts.prefixSeam ?? fonts.prefixLetterSpacing) : fonts.prefixLetterSpacing
1802
- engine.addGlyph(scalar.value, Role.prefix, Kind.text, Double(fonts.width(of: String(scalar), role: .prefix) + after), false)
1803
- }
1804
- let hasSuffix = !format.suffix.isEmpty
1805
- for (i, scalar) in rest.enumerated() {
1806
- let kind = numberKinds ? formatter.kindOf(scalar.value) : Kind.text
1807
- let after = hasSuffix && i == rest.count - 1 ? (fonts.suffixSeam ?? fonts.bodySpacing) : fonts.bodySpacing
1808
- engine.addGlyph(scalar.value, Role.body, kind, Double(fonts.width(of: String(scalar), role: .body) + after), showPlaceholder)
1809
- }
1810
- // `keepPlaceholder`: the rest of the mask trails the text as placeholder
1811
- // glyphs. They come after every typed glyph, so the caret - which counts
1812
- // body glyphs in order - never lands among them.
1813
- if !showPlaceholder {
1814
- for scalar in ghostTail.unicodeScalars {
1815
- engine.addGlyph(scalar.value, Role.body, Kind.text, Double(fonts.width(of: String(scalar), role: .body) + fonts.bodySpacing), true)
1816
- }
1817
- }
1818
- for scalar in format.suffix.unicodeScalars {
1819
- engine.addGlyph(scalar.value, Role.suffix, Kind.text, Double(fonts.width(of: String(scalar), role: .suffix) + fonts.suffixLetterSpacing), false)
1755
+ // The same walk sizes the field before it mounts (InputSizing.swift).
1756
+ InputGlyphs.walk(fonts.metrics, format: format, shown: showPlaceholder ? effectivePlaceholder : body,
1757
+ showPlaceholder: showPlaceholder, tail: ghostTail) { scalar, role, placeholder, advance in
1758
+ // Number kinds for the text (or the placeholder in its place), never for the kept tail.
1759
+ let kind = numberKinds && role == .body && (showPlaceholder || !placeholder) ? formatter.kindOf(scalar.value) : Kind.text
1760
+ engine.addGlyph(scalar.value, Self.engineRole(role), kind, Double(advance), placeholder)
1820
1761
  }
1821
1762
  engine.commitText(Int32(caret), CACurrentMediaTime())
1822
1763
  reportIntrinsicSize()
@@ -1878,9 +1819,7 @@ final class NitroInputView: UIView {
1878
1819
  // MARK: - Offsets (UTF-16 ↔ code points)
1879
1820
 
1880
1821
  /// A leading minus, in either the keyboard's spelling or the typographic one.
1881
- static func isSign(_ scalar: Unicode.Scalar) -> Bool {
1882
- scalar == "-" || scalar.value == 0x2212
1883
- }
1822
+ static func isSign(_ scalar: Unicode.Scalar) -> Bool { InputText.isSign(scalar) }
1884
1823
 
1885
1824
  static func codePointOffset(in string: String, utf16 offset: Int) -> Int {
1886
1825
  let utf16 = string.utf16
@@ -1949,7 +1888,7 @@ final class NitroInputView: UIView {
1949
1888
  }
1950
1889
 
1951
1890
  /// Dynamic Type multiplier for `allowFontScaling`, capped by `maxFontSizeMultiplier`.
1952
- private static func systemFontMultiplier(_ t: Typography) -> CGFloat {
1891
+ static func systemFontMultiplier(_ t: Typography) -> CGFloat {
1953
1892
  guard t.allowFontScaling else { return 1 }
1954
1893
  let multiplier = UIFontMetrics(forTextStyle: .body).scaledValue(for: 100) / 100
1955
1894
  if t.maxFontSizeMultiplier > 0 {
@@ -1958,7 +1897,7 @@ final class NitroInputView: UIView {
1958
1897
  return multiplier
1959
1898
  }
1960
1899
 
1961
- private static func makeFont(size: CGFloat, weight: CGFloat, family: String?) -> UIFont {
1900
+ static func makeFont(size: CGFloat, weight: CGFloat, family: String?) -> UIFont {
1962
1901
  let uiWeight = uiWeight(weight)
1963
1902
  let traits: [UIFontDescriptor.TraitKey: Any] = [.weight: uiWeight]
1964
1903
  var base: UIFont
@@ -1977,7 +1916,7 @@ final class NitroInputView: UIView {
1977
1916
  return UIFont(descriptor: descriptor, size: size)
1978
1917
  }
1979
1918
 
1980
- private static func uiWeight(_ w: CGFloat) -> UIFont.Weight {
1919
+ static func uiWeight(_ w: CGFloat) -> UIFont.Weight {
1981
1920
  switch w {
1982
1921
  case ..<150: return .ultraLight
1983
1922
  case ..<250: return .thin
@@ -2005,7 +1944,12 @@ final class NitroInputView: UIView {
2005
1944
  }
2006
1945
 
2007
1946
  private func intrinsicHeight() -> CGFloat {
2008
- guard traits.multiline else { return fonts.lineHeight }
1947
+ // A single line is centred in its height; a frame adds its padding (and a filled label's line).
1948
+ // `lineHeight` sets a single line's height too, as on Android; it was the
1949
+ // font's own line box here whatever the prop said.
1950
+ guard traits.multiline else {
1951
+ return InputFrameMetrics.singleLineHeight(fonts.metrics.lineBoxHeight, frame: inputFrame, metrics: fonts.metrics)
1952
+ }
2009
1953
  let line = lineBoxHeight
2010
1954
  // Only the frame's room is on top of the text. The rest of the text view's
2011
1955
  // inset is `lineBoxPadding`, which is the *first* line's share of the
@@ -2075,17 +2019,14 @@ final class NitroInputView: UIView {
2075
2019
 
2076
2020
  private func contentWidth() -> CGFloat {
2077
2021
  guard traits.plain else { return CGFloat(engine.targetWidth()) }
2078
- let body = text.isEmpty
2079
- ? effectivePlaceholder
2080
- : traits.secureTextEntry ? String(repeating: "\u{2022}", count: text.unicodeScalars.count) : text + ghostTail
2081
- // The field's own kerning follows every glyph; the affixes take their room with their seams.
2082
- return fonts.affixRoom(format.prefix, role: .prefix)
2083
- + fonts.width(of: body, role: .body) + fonts.bodySpacing * CGFloat(body.unicodeScalars.count)
2084
- + fonts.affixRoom(format.suffix, role: .suffix)
2022
+ return InputSizing.plainWidth(fonts.metrics, format: format, text: text, hint: effectivePlaceholder, tail: ghostTail,
2023
+ secure: traits.secureTextEntry)
2085
2024
  }
2086
2025
 
2087
2026
  private func reportIntrinsicSize() {
2088
- let size = CGSize(width: ceil(contentWidth() + 2), height: intrinsicHeight())
2027
+ // The same rounding as the measure that sized the field before it mounted.
2028
+ let size = InputSizing.reported(width: InputFrameMetrics.width(content: contentWidth(), frame: inputFrame, metrics: fonts.metrics),
2029
+ height: intrinsicHeight())
2089
2030
  guard abs(size.width - lastReportedSize.width) > 0.01 || abs(size.height - lastReportedSize.height) > 0.01 else {
2090
2031
  pendingSizeReport = false
2091
2032
  return
@@ -2941,14 +2882,12 @@ extension NitroInputView: UITextFieldDelegate {
2941
2882
 
2942
2883
  extension NitroInputView {
2943
2884
 
2944
- /// Material's ratio: the floated label is three quarters of the body size.
2945
- private static let floatedLabelRatio: CGFloat = 0.75
2946
2885
  /// Breathing room either side of the label inside the notch.
2947
2886
  private static let labelGapPadding: CGFloat = 4
2948
2887
  /// Inset of the label (and the text below it) from the leading edge.
2949
2888
  /// Material uses 16; a large corner radius needs more, or the notch opens
2950
2889
  /// flush against the corner arc and the run of line before it disappears.
2951
- private var labelInset: CGFloat { max(16, inputFrame.cornerRadius + 8) }
2890
+ private var labelInset: CGFloat { InputFrameMetrics.labelInset(inputFrame) }
2952
2891
  // Timings taken from Material's own text fields. The label runs the whole
2953
2892
  // duration on the standard decelerate curve; the notch is staggered behind
2954
2893
  // it, because the label has to travel most of the way to the top edge before
@@ -2981,33 +2920,10 @@ extension NitroInputView {
2981
2920
 
2982
2921
  /// `keepPlaceholder` without a `placeholder` shows the mask itself, empty
2983
2922
  /// slots as `maskSlotPlaceholder`: "+1 (___) ___-____".
2984
- private var basePlaceholder: String {
2985
- if format.placeholder.isEmpty, format.mode == .mask, format.keepPlaceholder, maskEngine.isActive() {
2986
- return String(maskEngine.apply(std.string(""), 0, true, false, false).tailPlaceholder)
2987
- }
2988
- return format.placeholder
2989
- }
2990
-
2991
- /// `keepPlaceholder`: what is still to come after the typed text, drawn in
2992
- /// the placeholder colour. The caller's placeholder from where the text ends
2993
- /// - "1234 5" leaves "678 9012" of "1234 5678 9012" - or, without one, the
2994
- /// rest of the mask. The placeholder is written for `mask`, so when one of
2995
- /// `maskAffinityFormats` has taken over (a 4-6-5 Amex under a 4-4-4-4
2996
- /// placeholder) its characters no longer line up, and that format's own
2997
- /// tail is shown instead. A complete text has nothing to come.
2998
- var ghostTail: String {
2999
- guard format.mode == .mask, format.keepPlaceholder, !text.isEmpty, !traits.secureTextEntry,
3000
- maskEngine.isActive() else { return "" }
3001
- let typed = text.unicodeScalars.count
3002
- let result = maskEngine.apply(std.string(text), Int32(typed), true, false, false)
3003
- if result.complete { return "" }
3004
- let placeholder = Array(format.placeholder.unicodeScalars)
3005
- if placeholder.isEmpty || result.formatIndex != 0 { return String(result.tailPlaceholder) }
3006
- guard typed < placeholder.count else { return "" }
3007
- var rest = String.UnicodeScalarView()
3008
- rest.append(contentsOf: placeholder[typed...])
3009
- return String(rest)
3010
- }
2923
+ private var basePlaceholder: String { sharedBasePlaceholder }
2924
+
2925
+ /// `keepPlaceholder`: what is still to come after the typed text (`InputText.ghostTail`).
2926
+ var ghostTail: String { sharedGhostTail(secure: traits.secureTextEntry) }
3011
2927
 
3012
2928
  /// The label floats once the field is focused or holds text - or always, if
3013
2929
  /// the caller asked for that.
@@ -3020,9 +2936,7 @@ extension NitroInputView {
3020
2936
  private var restingLabelFont: UIFont { fonts.font(for: .body) }
3021
2937
 
3022
2938
  private var floatedLabelSize: CGFloat {
3023
- let explicit = inputFrame.labelFontSize
3024
- if explicit > 0 { return explicit }
3025
- return max(9, restingLabelFont.pointSize * Self.floatedLabelRatio)
2939
+ InputFrameMetrics.floatedLabelSize(inputFrame, restingSize: restingLabelFont.pointSize)
3026
2940
  }
3027
2941
 
3028
2942
  // The frame is drawn in `CGColor`s, which have no appearance of their own:
@@ -3071,29 +2985,19 @@ extension NitroInputView {
3071
2985
  /// The top inset the label needs when it is floated onto the outline: half of
3072
2986
  /// it sits above the line, so the text below has to start lower.
3073
2987
  var frameTopInset: CGFloat {
3074
- // Only the filled variant needs the room. Its floated label lives inside
3075
- // the box, on its own line above the text. An outlined field's floated
3076
- // label sits *on* the stroke, outside the content box entirely - it
3077
- // intrudes into the top of the frame but never onto the text's line - so
3078
- // the text stays centred, which is what Material's outlined field does and
3079
- // what a field with no label at all does. Reserving space for it here left
3080
- // the text sitting half an inset low.
3081
- guard inputFrame.hasLabel, inputFrame.variant == .filled else { return 0 }
3082
- // 1.4 is a fudge, not a Material metric: enough of a gap under the
3083
- // floated label that the two lines do not crowd at the default sizes.
3084
- // Material's filled field is specified at a fixed 56dp with its own
3085
- // baselines; if this field is ever given fixed metrics, derive it there.
3086
- return floatedLabelSize * 1.4
3087
- }
3088
-
3089
- /// The room a framed field keeps above and below its text. Flat, because
3090
- /// nothing about the vertical middle of the box depends on its corners.
3091
- static let framePadding: CGFloat = 16
2988
+ // Only the filled variant needs the room (see `InputFrameMetrics.topInset`):
2989
+ // an outlined field's floated label sits on the stroke, and reserving space
2990
+ // for it left the text sitting half an inset low.
2991
+ InputFrameMetrics.topInset(inputFrame, restingSize: restingLabelFont.pointSize)
2992
+ }
2993
+
2994
+ /// The room a framed field keeps above and below its text (`InputFrameMetrics`).
2995
+ static let framePadding = InputFrameMetrics.framePadding
3092
2996
 
3093
2997
  /// Horizontal room for the frame, so the text lines up under the label
3094
2998
  /// instead of running into the stroke.
3095
2999
  var frameSideInset: CGFloat {
3096
- inputFrame.draws ? labelInset : 0
3000
+ InputFrameMetrics.sideInset(inputFrame)
3097
3001
  }
3098
3002
 
3099
3003
  func layoutFrame(animated: Bool) {