react-native-nitro-input 0.3.8 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +105 -0
- package/README.md +6 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/DigitSlots.kt +143 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputMeasure.kt +52 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +80 -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 +244 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/InputSizing.kt +404 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +168 -338
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +178 -30
- package/ios/HybridNitroInputView.swift +73 -75
- package/ios/HybridNitroNumberView.swift +18 -18
- package/ios/HybridNitroTextView.swift +14 -14
- package/ios/InputSizing.swift +628 -0
- package/ios/NitroInputView.swift +180 -209
- package/ios/NitroNumberView.swift +98 -23
- package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +14 -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 +166 -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 +14 -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 +167 -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 +20 -16
- package/lib/typescript/src/specs/NitroInput.nitro.d.ts +21 -8
- package/lib/typescript/src/specs/NitroInputMeasure.nitro.d.ts +69 -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 +94 -40
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +28 -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 +292 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +54 -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 +44 -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 +256 -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 +68 -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 +68 -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 +14 -8
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +90 -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 +252 -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 +28 -16
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +28 -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 +271 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +28 -16
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +42 -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 +14 -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 +215 -64
- package/src/NitroNumber.tsx +6 -6
- package/src/NitroText.tsx +6 -6
- package/src/specs/NitroInput.nitro.ts +21 -8
- package/src/specs/NitroInputMeasure.nitro.ts +78 -0
- package/src/specs/NitroNumber.nitro.ts +6 -6
- package/src/specs/NitroText.nitro.ts +6 -6
package/ios/NitroInputView.swift
CHANGED
|
@@ -23,12 +23,19 @@ import UIKit
|
|
|
23
23
|
final class NitroInputView: UIView {
|
|
24
24
|
|
|
25
25
|
private typealias Engine = margelo.nitro.nitroinput.ReflowEngine
|
|
26
|
-
|
|
27
|
-
|
|
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
|
|
@@ -137,6 +144,10 @@ final class NitroInputView: UIView {
|
|
|
137
144
|
var suffixAlign: AffixAlign = .baseline
|
|
138
145
|
/// Points added after every glyph, as `Text`'s letterSpacing; an affix gets it in proportion to its size.
|
|
139
146
|
var letterSpacing: CGFloat = 0
|
|
147
|
+
/// The font's tabular figures rather than its proportional ones.
|
|
148
|
+
var tabularNums = true
|
|
149
|
+
/// Every digit laid out as wide as the widest, centred (`InputMetrics.bodyDigitWidths`): tabular figures for a font without them.
|
|
150
|
+
var slotDigits = false
|
|
140
151
|
/// Points between the prefix and the text, and between the text and the suffix, in place of the letter spacing there.
|
|
141
152
|
var prefixSpacing: CGFloat? = nil
|
|
142
153
|
var suffixSpacing: CGFloat? = nil
|
|
@@ -286,6 +297,9 @@ final class NitroInputView: UIView {
|
|
|
286
297
|
didSet {
|
|
287
298
|
guard alignment != oldValue else { return }
|
|
288
299
|
field.textAlignment = textAlignment(for: alignment)
|
|
300
|
+
// The new alignment is the field's paragraph style, which the slots' indent rides on.
|
|
301
|
+
applyDigitSlots()
|
|
302
|
+
syncAccessibilityPlaceholder()
|
|
289
303
|
render()
|
|
290
304
|
}
|
|
291
305
|
}
|
|
@@ -342,7 +356,7 @@ final class NitroInputView: UIView {
|
|
|
342
356
|
|
|
343
357
|
// MARK: - Fonts
|
|
344
358
|
|
|
345
|
-
|
|
359
|
+
enum GlyphRole {
|
|
346
360
|
case body, prefix, suffix
|
|
347
361
|
}
|
|
348
362
|
|
|
@@ -357,6 +371,9 @@ final class NitroInputView: UIView {
|
|
|
357
371
|
/// Body / prefix / suffix fonts with per-glyph caches (advance widths, ink
|
|
358
372
|
/// metrics and pre-rasterized images in the text or placeholder colour).
|
|
359
373
|
fileprivate final class FontSet {
|
|
374
|
+
/// The fonts, spacing and advance widths, shared with the measure that
|
|
375
|
+
/// sizes a field before it mounts (InputSizing.swift).
|
|
376
|
+
let metrics: InputMetrics
|
|
360
377
|
let body: UIFont
|
|
361
378
|
let prefix: UIFont
|
|
362
379
|
let suffix: UIFont
|
|
@@ -380,7 +397,6 @@ final class NitroInputView: UIView {
|
|
|
380
397
|
/// Pixel density the glyph images are rendered at.
|
|
381
398
|
let renderScale: CGFloat
|
|
382
399
|
private var glyphCache: [GlyphKey: NSAttributedString] = [:]
|
|
383
|
-
private var widthCache: [GlyphKey: CGFloat] = [:]
|
|
384
400
|
private var imageCache: [GlyphKey: UIImage] = [:]
|
|
385
401
|
private var inkDescentCache: [GlyphKey: CGFloat] = [:]
|
|
386
402
|
|
|
@@ -426,42 +442,29 @@ final class NitroInputView: UIView {
|
|
|
426
442
|
init(_ t: Typography, traits: UITraitCollection) {
|
|
427
443
|
// The view's own display, not the main screen's (deprecated, and absent on visionOS).
|
|
428
444
|
renderScale = max(1, traits.displayScale)
|
|
429
|
-
|
|
430
|
-
body =
|
|
431
|
-
prefix =
|
|
432
|
-
suffix =
|
|
445
|
+
metrics = InputMetrics(t)
|
|
446
|
+
body = metrics.body
|
|
447
|
+
prefix = metrics.prefix
|
|
448
|
+
suffix = metrics.suffix
|
|
433
449
|
color = t.color.resolvedColor(with: traits)
|
|
434
450
|
placeholderColor = t.placeholderColor.resolvedColor(with: traits)
|
|
435
451
|
prefixAlign = t.prefixAlign
|
|
436
452
|
suffixAlign = t.suffixAlign
|
|
437
|
-
bodySpacing =
|
|
438
|
-
prefixLetterSpacing =
|
|
439
|
-
suffixLetterSpacing =
|
|
440
|
-
prefixSeam =
|
|
441
|
-
suffixSeam =
|
|
442
|
-
prefixOffset =
|
|
443
|
-
suffixOffset =
|
|
444
|
-
lineHeight =
|
|
453
|
+
bodySpacing = metrics.bodySpacing
|
|
454
|
+
prefixLetterSpacing = metrics.prefixLetterSpacing
|
|
455
|
+
suffixLetterSpacing = metrics.suffixLetterSpacing
|
|
456
|
+
prefixSeam = metrics.prefixSeam
|
|
457
|
+
suffixSeam = metrics.suffixSeam
|
|
458
|
+
prefixOffset = metrics.prefixOffset
|
|
459
|
+
suffixOffset = metrics.suffixOffset
|
|
460
|
+
lineHeight = metrics.lineHeight
|
|
445
461
|
}
|
|
446
462
|
|
|
447
463
|
/// 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
|
-
}
|
|
464
|
+
func spacing(for role: GlyphRole) -> CGFloat { metrics.spacing(for: role) }
|
|
455
465
|
|
|
456
466
|
/// 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
|
-
}
|
|
467
|
+
func affixRoom(_ affix: String, role: GlyphRole) -> CGFloat { metrics.affixRoom(affix, role: role) }
|
|
465
468
|
|
|
466
469
|
func font(for role: GlyphRole) -> UIFont {
|
|
467
470
|
switch role {
|
|
@@ -484,13 +487,7 @@ final class NitroInputView: UIView {
|
|
|
484
487
|
}
|
|
485
488
|
|
|
486
489
|
/// 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
|
-
}
|
|
490
|
+
func width(of text: String, role: GlyphRole) -> CGFloat { metrics.width(of: text, role: role) }
|
|
494
491
|
|
|
495
492
|
/// The glyph rasterized once (colour baked in, at screen density): a frame
|
|
496
493
|
/// only moves layers, the main thread never redraws a bitmap.
|
|
@@ -550,8 +547,9 @@ final class NitroInputView: UIView {
|
|
|
550
547
|
// MARK: - State
|
|
551
548
|
|
|
552
549
|
private var engine = Engine()
|
|
553
|
-
|
|
554
|
-
|
|
550
|
+
/// Configured through `InputText` (InputSizing.swift), as a measure's are.
|
|
551
|
+
var formatter = AmountFormatter()
|
|
552
|
+
var maskEngine = MaskEngine()
|
|
555
553
|
/// The outline / fill. Stroke-only, so the notch is a hole rather than a mask.
|
|
556
554
|
private lazy var frameLayer: CAShapeLayer = {
|
|
557
555
|
let shape = CAShapeLayer()
|
|
@@ -686,6 +684,8 @@ final class NitroInputView: UIView {
|
|
|
686
684
|
private struct GlyphEntry {
|
|
687
685
|
let layer: CALayer
|
|
688
686
|
let topOffset: CGFloat
|
|
687
|
+
/// A slotted digit's offset into its advance (`InputMetrics.inset`).
|
|
688
|
+
let leftInset: CGFloat
|
|
689
689
|
/// The render pass that last saw this glyph; one a pass missed has left.
|
|
690
690
|
var generation: UInt
|
|
691
691
|
/// What the layer was last given, so a frame only sets what moved: most
|
|
@@ -800,12 +800,18 @@ final class NitroInputView: UIView {
|
|
|
800
800
|
// field's own copy exists only so an empty field still has an
|
|
801
801
|
// accessibility value, hence clear.
|
|
802
802
|
let color = traits.plain ? typography.placeholderColor : UIColor.clear
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
803
|
+
// Its digits slotted like the text's, or the placeholder is narrower than
|
|
804
|
+
// the field measures it, and a typed digit moves the rest along. In the
|
|
805
|
+
// field's own attributes, which carry its alignment.
|
|
806
|
+
var slotAttributes = field.defaultTextAttributes
|
|
807
|
+
slotAttributes[.foregroundColor] = color
|
|
808
|
+
let next = placeholder.isEmpty
|
|
807
809
|
? nil
|
|
808
|
-
:
|
|
810
|
+
: fonts.metrics.slotted(placeholder, attributes: slotAttributes, lead: false)?.string
|
|
811
|
+
?? NSAttributedString(string: placeholder, attributes: [.foregroundColor: color])
|
|
812
|
+
if let current = field.attributedPlaceholder, let next, current.isEqual(to: next) { return }
|
|
813
|
+
if field.attributedPlaceholder == nil, next == nil { return }
|
|
814
|
+
field.attributedPlaceholder = next
|
|
809
815
|
}
|
|
810
816
|
|
|
811
817
|
override func didMoveToSuperview() {
|
|
@@ -853,6 +859,8 @@ final class NitroInputView: UIView {
|
|
|
853
859
|
private func applyLayoutDirection() {
|
|
854
860
|
engine.setRightToLeft(isRTL)
|
|
855
861
|
field.textAlignment = traits.plain ? textAlignment(for: alignment) : .left
|
|
862
|
+
applyDigitSlots()
|
|
863
|
+
syncAccessibilityPlaceholder()
|
|
856
864
|
if didBuildTextView {
|
|
857
865
|
textView.textAlignment = textAlignment(for: alignment)
|
|
858
866
|
applyTextViewTypography()
|
|
@@ -1145,6 +1153,11 @@ final class NitroInputView: UIView {
|
|
|
1145
1153
|
timing = Timing()
|
|
1146
1154
|
traits = Traits()
|
|
1147
1155
|
alignment = .auto
|
|
1156
|
+
// Last: the defaults above fed the engine an empty text (turning `plain`
|
|
1157
|
+
// off feeds it), which it would take for its first, so the next element's
|
|
1158
|
+
// text slid in instead of showing as it is (the engine snaps only its
|
|
1159
|
+
// first text).
|
|
1160
|
+
engine.reset()
|
|
1148
1161
|
// `onTextChange`, `onFocusChange`, `onSubmit` and `onIntrinsicSizeChange`
|
|
1149
1162
|
// are the hybrid's wiring, not the element's props: they stay across
|
|
1150
1163
|
// recycling (the hybrid clears its own callback props).
|
|
@@ -1258,59 +1271,14 @@ final class NitroInputView: UIView {
|
|
|
1258
1271
|
onIntrinsicSizeChange?(lastReportedSize)
|
|
1259
1272
|
}
|
|
1260
1273
|
|
|
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
|
-
}
|
|
1274
|
+
private func normalized(_ raw: String) -> String { normalizedText(raw) }
|
|
1273
1275
|
|
|
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
|
-
}
|
|
1276
|
+
private static func truncated(_ text: String, to maxLength: Int) -> String { InputText.truncated(text, to: maxLength) }
|
|
1280
1277
|
|
|
1281
1278
|
// MARK: - Configuration application
|
|
1282
1279
|
|
|
1283
1280
|
private func applyFormat(previous: Format) {
|
|
1284
|
-
|
|
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
|
-
}
|
|
1281
|
+
let maskChanged = configureEngines(previous: previous)
|
|
1314
1282
|
updateFieldInsets()
|
|
1315
1283
|
syncAccessibilityPlaceholder()
|
|
1316
1284
|
// A changed formatter or mode reformats what is in the field.
|
|
@@ -1362,10 +1330,49 @@ final class NitroInputView: UIView {
|
|
|
1362
1330
|
attributes[.font] = fonts.body
|
|
1363
1331
|
attributes[.foregroundColor] = traits.plain ? typography.color : UIColor.clear
|
|
1364
1332
|
field.defaultTextAttributes = attributes
|
|
1333
|
+
fieldSlotted = false
|
|
1334
|
+
applyDigitSlots()
|
|
1335
|
+
syncAccessibilityPlaceholder()
|
|
1365
1336
|
updateFieldInsets()
|
|
1366
1337
|
applyTextViewTypography()
|
|
1367
1338
|
}
|
|
1368
1339
|
|
|
1340
|
+
/// Whether the field's text carries `applyDigitSlots`' kerning.
|
|
1341
|
+
private var fieldSlotted = false
|
|
1342
|
+
|
|
1343
|
+
/// Lays the field's own digits out in the slots the overlay and the measure
|
|
1344
|
+
/// give them when the font has no tabular figures (`InputMetrics.slotted`):
|
|
1345
|
+
/// plain mode draws that text, and otherwise the caret and the selection
|
|
1346
|
+
/// follow it. Setting `attributedText` moves the selection, so it is put
|
|
1347
|
+
/// back; a composition in progress is left alone until it is committed.
|
|
1348
|
+
private func applyDigitSlots() {
|
|
1349
|
+
guard field.markedTextRange == nil else { return }
|
|
1350
|
+
let current = field.text ?? ""
|
|
1351
|
+
guard !traits.secureTextEntry, !current.isEmpty,
|
|
1352
|
+
let slotted = fonts.metrics.slotted(current, attributes: field.defaultTextAttributes, lead: false)?.string else {
|
|
1353
|
+
if fieldSlotted {
|
|
1354
|
+
// Back to one kerning for the whole text: re-setting the attributes applies them throughout.
|
|
1355
|
+
fieldSlotted = false
|
|
1356
|
+
let attributes = field.defaultTextAttributes
|
|
1357
|
+
field.defaultTextAttributes = attributes
|
|
1358
|
+
}
|
|
1359
|
+
return
|
|
1360
|
+
}
|
|
1361
|
+
if let existing = field.attributedText, existing.isEqual(to: slotted) { return }
|
|
1362
|
+
let start = field.beginningOfDocument
|
|
1363
|
+
let selection = field.selectedTextRange.map { (field.offset(from: start, to: $0.start), field.offset(from: start, to: $0.end)) }
|
|
1364
|
+
let wasSetting = isSettingText
|
|
1365
|
+
isSettingText = true
|
|
1366
|
+
field.attributedText = slotted
|
|
1367
|
+
if let (from, to) = selection,
|
|
1368
|
+
let fromPosition = field.position(from: field.beginningOfDocument, offset: from),
|
|
1369
|
+
let toPosition = field.position(from: field.beginningOfDocument, offset: to) {
|
|
1370
|
+
field.selectedTextRange = field.textRange(from: fromPosition, to: toPosition)
|
|
1371
|
+
}
|
|
1372
|
+
isSettingText = wasSetting
|
|
1373
|
+
fieldSlotted = true
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1369
1376
|
/// In reflow mode the overlay draws the prefix and suffix, so the field only
|
|
1370
1377
|
/// has to reserve the space. In `plain` mode nothing is drawing them, so the
|
|
1371
1378
|
/// field carries them itself as its left/right accessory views — UIKit insets
|
|
@@ -1631,6 +1638,8 @@ final class NitroInputView: UIView {
|
|
|
1631
1638
|
var attributes = field.defaultTextAttributes
|
|
1632
1639
|
attributes[.foregroundColor] = plain ? typography.color : UIColor.clear
|
|
1633
1640
|
field.defaultTextAttributes = attributes
|
|
1641
|
+
fieldSlotted = false
|
|
1642
|
+
applyDigitSlots()
|
|
1634
1643
|
updateFieldInsets()
|
|
1635
1644
|
// Turning plain on tore the overlay down and reset the engine, so on the
|
|
1636
1645
|
// way back it has nothing to draw until the next edit: hand it the text.
|
|
@@ -1727,6 +1736,7 @@ final class NitroInputView: UIView {
|
|
|
1727
1736
|
/// `changed` is false only for the first, unchanged application of the
|
|
1728
1737
|
/// `text` prop (the engine still needs feeding); a keystroke always changes.
|
|
1729
1738
|
private func textDidChange(caret: Int, reason: ChangeReason, changed: Bool = true) {
|
|
1739
|
+
applyDigitSlots()
|
|
1730
1740
|
feedEngine(caret: caret)
|
|
1731
1741
|
if reason != .prop {
|
|
1732
1742
|
eventCount += 1
|
|
@@ -1759,9 +1769,35 @@ final class NitroInputView: UIView {
|
|
|
1759
1769
|
return (start, end)
|
|
1760
1770
|
}
|
|
1761
1771
|
|
|
1772
|
+
private var batchDepth = 0
|
|
1773
|
+
private var feedPending = false
|
|
1774
|
+
private var feedCaret = -1
|
|
1775
|
+
|
|
1776
|
+
/// Runs `block` with engine feeds coalesced into one at the end, as Android's
|
|
1777
|
+
/// `batch` does. A prop commit applies the config and then the text; fed in
|
|
1778
|
+
/// between (turning `plain` off feeds the engine), the engine took the still
|
|
1779
|
+
/// empty field for its first text, which it shows as is, and animated the
|
|
1780
|
+
/// real one in: every reflowing amount slid in on mount.
|
|
1781
|
+
func batch(_ block: () -> Void) {
|
|
1782
|
+
batchDepth += 1
|
|
1783
|
+
block()
|
|
1784
|
+
batchDepth -= 1
|
|
1785
|
+
if batchDepth == 0, feedPending {
|
|
1786
|
+
feedPending = false
|
|
1787
|
+
let caret = feedCaret
|
|
1788
|
+
feedCaret = -1
|
|
1789
|
+
feedEngine(caret: caret)
|
|
1790
|
+
}
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1762
1793
|
/// Hands the engine the current text as glyphs (prefix, body or placeholder,
|
|
1763
1794
|
/// suffix) with their advance widths and commits it.
|
|
1764
1795
|
fileprivate func feedEngine(caret: Int) {
|
|
1796
|
+
if batchDepth > 0 {
|
|
1797
|
+
feedCaret = feedPending && feedCaret == -1 ? -1 : caret
|
|
1798
|
+
feedPending = true
|
|
1799
|
+
return
|
|
1800
|
+
}
|
|
1765
1801
|
// Plain mode has no overlay to feed: the field draws its own text. Its
|
|
1766
1802
|
// width still has to reach React for `autoWidth`, and this is the one
|
|
1767
1803
|
// place every text, font, affix and placeholder change comes through.
|
|
@@ -1772,51 +1808,15 @@ final class NitroInputView: UIView {
|
|
|
1772
1808
|
// `secureTextEntry` masks what the overlay draws: the hidden field keeps
|
|
1773
1809
|
// the real text (UIKit needs it for editing and autofill), but every glyph
|
|
1774
1810
|
// the user sees is a bullet.
|
|
1775
|
-
let body = traits.secureTextEntry
|
|
1776
|
-
? String(repeating: "\u{2022}", count: text.unicodeScalars.count)
|
|
1777
|
-
: text
|
|
1811
|
+
let body = traits.secureTextEntry ? InputText.bullets(text) : text
|
|
1778
1812
|
let showPlaceholder = body.isEmpty && !effectivePlaceholder.isEmpty
|
|
1779
|
-
let bodyText = showPlaceholder ? effectivePlaceholder : body
|
|
1780
1813
|
let numberKinds = format.mode == .number
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
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)
|
|
1814
|
+
// The same walk sizes the field before it mounts (InputSizing.swift).
|
|
1815
|
+
InputGlyphs.walk(fonts.metrics, format: format, shown: showPlaceholder ? effectivePlaceholder : body,
|
|
1816
|
+
showPlaceholder: showPlaceholder, tail: ghostTail) { scalar, role, placeholder, advance in
|
|
1817
|
+
// Number kinds for the text (or the placeholder in its place), never for the kept tail.
|
|
1818
|
+
let kind = numberKinds && role == .body && (showPlaceholder || !placeholder) ? formatter.kindOf(scalar.value) : Kind.text
|
|
1819
|
+
engine.addGlyph(scalar.value, Self.engineRole(role), kind, Double(advance), placeholder)
|
|
1820
1820
|
}
|
|
1821
1821
|
engine.commitText(Int32(caret), CACurrentMediaTime())
|
|
1822
1822
|
reportIntrinsicSize()
|
|
@@ -1878,9 +1878,7 @@ final class NitroInputView: UIView {
|
|
|
1878
1878
|
// MARK: - Offsets (UTF-16 ↔ code points)
|
|
1879
1879
|
|
|
1880
1880
|
/// 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
|
-
}
|
|
1881
|
+
static func isSign(_ scalar: Unicode.Scalar) -> Bool { InputText.isSign(scalar) }
|
|
1884
1882
|
|
|
1885
1883
|
static func codePointOffset(in string: String, utf16 offset: Int) -> Int {
|
|
1886
1884
|
let utf16 = string.utf16
|
|
@@ -1949,7 +1947,7 @@ final class NitroInputView: UIView {
|
|
|
1949
1947
|
}
|
|
1950
1948
|
|
|
1951
1949
|
/// Dynamic Type multiplier for `allowFontScaling`, capped by `maxFontSizeMultiplier`.
|
|
1952
|
-
|
|
1950
|
+
static func systemFontMultiplier(_ t: Typography) -> CGFloat {
|
|
1953
1951
|
guard t.allowFontScaling else { return 1 }
|
|
1954
1952
|
let multiplier = UIFontMetrics(forTextStyle: .body).scaledValue(for: 100) / 100
|
|
1955
1953
|
if t.maxFontSizeMultiplier > 0 {
|
|
@@ -1958,7 +1956,7 @@ final class NitroInputView: UIView {
|
|
|
1958
1956
|
return multiplier
|
|
1959
1957
|
}
|
|
1960
1958
|
|
|
1961
|
-
|
|
1959
|
+
static func makeFont(size: CGFloat, weight: CGFloat, family: String?, tabular: Bool = true) -> UIFont {
|
|
1962
1960
|
let uiWeight = uiWeight(weight)
|
|
1963
1961
|
let traits: [UIFontDescriptor.TraitKey: Any] = [.weight: uiWeight]
|
|
1964
1962
|
var base: UIFont
|
|
@@ -1971,13 +1969,16 @@ final class NitroInputView: UIView {
|
|
|
1971
1969
|
} else {
|
|
1972
1970
|
base = UIFont.systemFont(ofSize: size, weight: uiWeight)
|
|
1973
1971
|
}
|
|
1974
|
-
// Tabular digits
|
|
1975
|
-
let feature: [UIFontDescriptor.FeatureKey: Int] = [
|
|
1972
|
+
// Tabular digits, so a column keeps its width while its digit reflows, unless asked for the font's own.
|
|
1973
|
+
let feature: [UIFontDescriptor.FeatureKey: Int] = [
|
|
1974
|
+
.type: kNumberSpacingType,
|
|
1975
|
+
.selector: tabular ? kMonospacedNumbersSelector : kProportionalNumbersSelector,
|
|
1976
|
+
]
|
|
1976
1977
|
let descriptor = base.fontDescriptor.addingAttributes([.featureSettings: [feature]])
|
|
1977
1978
|
return UIFont(descriptor: descriptor, size: size)
|
|
1978
1979
|
}
|
|
1979
1980
|
|
|
1980
|
-
|
|
1981
|
+
static func uiWeight(_ w: CGFloat) -> UIFont.Weight {
|
|
1981
1982
|
switch w {
|
|
1982
1983
|
case ..<150: return .ultraLight
|
|
1983
1984
|
case ..<250: return .thin
|
|
@@ -2005,7 +2006,12 @@ final class NitroInputView: UIView {
|
|
|
2005
2006
|
}
|
|
2006
2007
|
|
|
2007
2008
|
private func intrinsicHeight() -> CGFloat {
|
|
2008
|
-
|
|
2009
|
+
// A single line is centred in its height; a frame adds its padding (and a filled label's line).
|
|
2010
|
+
// `lineHeight` sets a single line's height too, as on Android; it was the
|
|
2011
|
+
// font's own line box here whatever the prop said.
|
|
2012
|
+
guard traits.multiline else {
|
|
2013
|
+
return InputFrameMetrics.singleLineHeight(fonts.metrics.lineBoxHeight, frame: inputFrame, metrics: fonts.metrics)
|
|
2014
|
+
}
|
|
2009
2015
|
let line = lineBoxHeight
|
|
2010
2016
|
// Only the frame's room is on top of the text. The rest of the text view's
|
|
2011
2017
|
// inset is `lineBoxPadding`, which is the *first* line's share of the
|
|
@@ -2055,7 +2061,10 @@ final class NitroInputView: UIView {
|
|
|
2055
2061
|
if ghostLabel.superview !== field { field.addSubview(ghostLabel) }
|
|
2056
2062
|
var attributes = field.defaultTextAttributes
|
|
2057
2063
|
attributes[.foregroundColor] = typography.placeholderColor.resolvedColor(with: traitCollection)
|
|
2058
|
-
|
|
2064
|
+
// A first slotted digit's half-room before it moves the label instead (`InputMetrics.slotted`).
|
|
2065
|
+
let slotted = fonts.metrics.slotted(tail, attributes: attributes, lead: true)
|
|
2066
|
+
ghostLabel.attributedText = slotted?.string ?? NSAttributedString(string: tail, attributes: attributes)
|
|
2067
|
+
let lead = slotted?.lead ?? 0
|
|
2059
2068
|
let box = field.textRect(forBounds: field.bounds)
|
|
2060
2069
|
let typed = fonts.width(of: text, role: .body) + fonts.bodySpacing * CGFloat(text.unicodeScalars.count)
|
|
2061
2070
|
let tailWidth = fonts.width(of: tail, role: .body) + fonts.bodySpacing * CGFloat(tail.unicodeScalars.count)
|
|
@@ -2070,22 +2079,20 @@ final class NitroInputView: UIView {
|
|
|
2070
2079
|
let end = start + typed
|
|
2071
2080
|
// Text wider than the box scrolls inside the field; the tail has no place then.
|
|
2072
2081
|
ghostLabel.isHidden = typed > box.width
|
|
2073
|
-
ghostLabel.frame = CGRect(x: end, y: box.minY, width: min(tailWidth + 1, max(0, box.maxX - end)),
|
|
2082
|
+
ghostLabel.frame = CGRect(x: end + lead, y: box.minY, width: min(tailWidth - lead + 1, max(0, box.maxX - end - lead)),
|
|
2083
|
+
height: box.height)
|
|
2074
2084
|
}
|
|
2075
2085
|
|
|
2076
2086
|
private func contentWidth() -> CGFloat {
|
|
2077
2087
|
guard traits.plain else { return CGFloat(engine.targetWidth()) }
|
|
2078
|
-
|
|
2079
|
-
|
|
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)
|
|
2088
|
+
return InputSizing.plainWidth(fonts.metrics, format: format, text: text, hint: effectivePlaceholder, tail: ghostTail,
|
|
2089
|
+
secure: traits.secureTextEntry)
|
|
2085
2090
|
}
|
|
2086
2091
|
|
|
2087
2092
|
private func reportIntrinsicSize() {
|
|
2088
|
-
|
|
2093
|
+
// The same rounding as the measure that sized the field before it mounted.
|
|
2094
|
+
let size = InputSizing.reported(width: InputFrameMetrics.width(content: contentWidth(), frame: inputFrame, metrics: fonts.metrics),
|
|
2095
|
+
height: intrinsicHeight())
|
|
2089
2096
|
guard abs(size.width - lastReportedSize.width) > 0.01 || abs(size.height - lastReportedSize.height) > 0.01 else {
|
|
2090
2097
|
pendingSizeReport = false
|
|
2091
2098
|
return
|
|
@@ -2236,14 +2243,15 @@ final class NitroInputView: UIView {
|
|
|
2236
2243
|
glyphLayer.bounds = CGRect(origin: .zero, size: image.size)
|
|
2237
2244
|
}
|
|
2238
2245
|
clipLayer.addSublayer(glyphLayer)
|
|
2239
|
-
glyphLayers[g.id] = GlyphEntry(layer: glyphLayer, topOffset: fonts.top(for: role, text: text, lineTop: 0),
|
|
2246
|
+
glyphLayers[g.id] = GlyphEntry(layer: glyphLayer, topOffset: fonts.top(for: role, text: text, lineTop: 0),
|
|
2247
|
+
leftInset: fonts.metrics.inset(of: text, role: role), generation: generation)
|
|
2240
2248
|
index = glyphLayers.index(forKey: g.id)!
|
|
2241
2249
|
}
|
|
2242
2250
|
let entry = glyphLayers.values[index]
|
|
2243
2251
|
let glyphLayer = entry.layer
|
|
2244
2252
|
let size = glyphLayer.bounds.size
|
|
2245
2253
|
let top = band + entry.topOffset + CGFloat(g.y) * lineHeight
|
|
2246
|
-
let position = CGPoint(x: pad + CGFloat(g.x) + size.width / 2, y: top + size.height / 2)
|
|
2254
|
+
let position = CGPoint(x: pad + CGFloat(g.x) + entry.leftInset + size.width / 2, y: top + size.height / 2)
|
|
2247
2255
|
if position != entry.position {
|
|
2248
2256
|
glyphLayer.position = position
|
|
2249
2257
|
glyphLayers.values[index].position = position
|
|
@@ -2941,14 +2949,12 @@ extension NitroInputView: UITextFieldDelegate {
|
|
|
2941
2949
|
|
|
2942
2950
|
extension NitroInputView {
|
|
2943
2951
|
|
|
2944
|
-
/// Material's ratio: the floated label is three quarters of the body size.
|
|
2945
|
-
private static let floatedLabelRatio: CGFloat = 0.75
|
|
2946
2952
|
/// Breathing room either side of the label inside the notch.
|
|
2947
2953
|
private static let labelGapPadding: CGFloat = 4
|
|
2948
2954
|
/// Inset of the label (and the text below it) from the leading edge.
|
|
2949
2955
|
/// Material uses 16; a large corner radius needs more, or the notch opens
|
|
2950
2956
|
/// flush against the corner arc and the run of line before it disappears.
|
|
2951
|
-
private var labelInset: CGFloat {
|
|
2957
|
+
private var labelInset: CGFloat { InputFrameMetrics.labelInset(inputFrame) }
|
|
2952
2958
|
// Timings taken from Material's own text fields. The label runs the whole
|
|
2953
2959
|
// duration on the standard decelerate curve; the notch is staggered behind
|
|
2954
2960
|
// it, because the label has to travel most of the way to the top edge before
|
|
@@ -2981,33 +2987,10 @@ extension NitroInputView {
|
|
|
2981
2987
|
|
|
2982
2988
|
/// `keepPlaceholder` without a `placeholder` shows the mask itself, empty
|
|
2983
2989
|
/// slots as `maskSlotPlaceholder`: "+1 (___) ___-____".
|
|
2984
|
-
private var basePlaceholder: String {
|
|
2985
|
-
|
|
2986
|
-
|
|
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
|
-
}
|
|
2990
|
+
private var basePlaceholder: String { sharedBasePlaceholder }
|
|
2991
|
+
|
|
2992
|
+
/// `keepPlaceholder`: what is still to come after the typed text (`InputText.ghostTail`).
|
|
2993
|
+
var ghostTail: String { sharedGhostTail(secure: traits.secureTextEntry) }
|
|
3011
2994
|
|
|
3012
2995
|
/// The label floats once the field is focused or holds text - or always, if
|
|
3013
2996
|
/// the caller asked for that.
|
|
@@ -3020,9 +3003,7 @@ extension NitroInputView {
|
|
|
3020
3003
|
private var restingLabelFont: UIFont { fonts.font(for: .body) }
|
|
3021
3004
|
|
|
3022
3005
|
private var floatedLabelSize: CGFloat {
|
|
3023
|
-
|
|
3024
|
-
if explicit > 0 { return explicit }
|
|
3025
|
-
return max(9, restingLabelFont.pointSize * Self.floatedLabelRatio)
|
|
3006
|
+
InputFrameMetrics.floatedLabelSize(inputFrame, restingSize: restingLabelFont.pointSize)
|
|
3026
3007
|
}
|
|
3027
3008
|
|
|
3028
3009
|
// The frame is drawn in `CGColor`s, which have no appearance of their own:
|
|
@@ -3071,29 +3052,19 @@ extension NitroInputView {
|
|
|
3071
3052
|
/// The top inset the label needs when it is floated onto the outline: half of
|
|
3072
3053
|
/// it sits above the line, so the text below has to start lower.
|
|
3073
3054
|
var frameTopInset: CGFloat {
|
|
3074
|
-
// Only the filled variant needs the room
|
|
3075
|
-
//
|
|
3076
|
-
//
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
|
|
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
|
|
3055
|
+
// Only the filled variant needs the room (see `InputFrameMetrics.topInset`):
|
|
3056
|
+
// an outlined field's floated label sits on the stroke, and reserving space
|
|
3057
|
+
// for it left the text sitting half an inset low.
|
|
3058
|
+
InputFrameMetrics.topInset(inputFrame, restingSize: restingLabelFont.pointSize)
|
|
3059
|
+
}
|
|
3060
|
+
|
|
3061
|
+
/// The room a framed field keeps above and below its text (`InputFrameMetrics`).
|
|
3062
|
+
static let framePadding = InputFrameMetrics.framePadding
|
|
3092
3063
|
|
|
3093
3064
|
/// Horizontal room for the frame, so the text lines up under the label
|
|
3094
3065
|
/// instead of running into the stroke.
|
|
3095
3066
|
var frameSideInset: CGFloat {
|
|
3096
|
-
inputFrame
|
|
3067
|
+
InputFrameMetrics.sideInset(inputFrame)
|
|
3097
3068
|
}
|
|
3098
3069
|
|
|
3099
3070
|
func layoutFrame(animated: Bool) {
|