react-native-nitro-input 0.3.1 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +114 -0
- package/README.md +75 -3
- package/android/src/main/cpp/JMaskEngine.cpp +45 -0
- package/android/src/main/cpp/JMaskEngine.hpp +9 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +49 -4
- package/android/src/main/java/com/margelo/nitro/nitroinput/MaskEngine.kt +16 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +266 -8
- package/android/src/main/java/com/margelo/nitro/nitroinput/UnmountHandoffs.kt +57 -0
- package/cpp/MaskEngine.cpp +294 -33
- package/cpp/MaskEngine.hpp +68 -0
- package/ios/HybridNitroInputView.swift +37 -4
- package/ios/NitroInputCommands.mm +10 -0
- package/ios/NitroInputView.swift +474 -20
- package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +8 -0
- package/lib/commonjs/src/NitroInput.js +116 -15
- package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +8 -0
- package/lib/module/src/NitroInput.js +117 -16
- package/lib/typescript/src/NitroInput.d.ts +55 -1
- package/lib/typescript/src/index.d.ts +1 -1
- package/lib/typescript/src/specs/NitroInput.nitro.d.ts +65 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +124 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +17 -0
- package/nitrogen/generated/android/c++/JNitroInputCharacterMapping.hpp +61 -0
- package/nitrogen/generated/android/c++/JNitroInputMaskAffinity.hpp +64 -0
- package/nitrogen/generated/android/c++/JNitroInputTextCase.hpp +61 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +40 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +52 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputCharacterMapping.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputMaskAffinity.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputTextCase.kt +24 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +25 -11
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +9 -0
- package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +68 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +48 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +9 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +111 -0
- package/nitrogen/generated/ios/swift/NitroInputCharacterMapping.swift +34 -0
- package/nitrogen/generated/ios/swift/NitroInputMaskAffinity.swift +48 -0
- package/nitrogen/generated/ios/swift/NitroInputTextCase.swift +44 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +17 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +26 -0
- package/nitrogen/generated/shared/c++/NitroInputCharacterMapping.hpp +87 -0
- package/nitrogen/generated/shared/c++/NitroInputMaskAffinity.hpp +84 -0
- package/nitrogen/generated/shared/c++/NitroInputTextCase.hpp +80 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +16 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +27 -0
- package/nitrogen/generated/shared/json/NitroInputViewConfig.json +8 -0
- package/package.json +2 -2
- package/src/NitroInput.tsx +190 -11
- package/src/index.ts +3 -0
- package/src/specs/NitroInput.nitro.ts +68 -0
package/ios/NitroInputView.swift
CHANGED
|
@@ -44,6 +44,12 @@ final class NitroInputView: UIView {
|
|
|
44
44
|
var isOptional: Bool
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
/// One character replaced by another before masking.
|
|
48
|
+
struct MaskMapping: Equatable {
|
|
49
|
+
var from: String
|
|
50
|
+
var to: String
|
|
51
|
+
}
|
|
52
|
+
|
|
47
53
|
struct Format: Equatable {
|
|
48
54
|
var mode: Mode = .text
|
|
49
55
|
/// Mask mode: the pattern, e.g. "+1 ([000]) [000]-[0000]".
|
|
@@ -51,6 +57,17 @@ final class NitroInputView: UIView {
|
|
|
51
57
|
var maskNotations: [MaskNotation] = []
|
|
52
58
|
var maskAutocomplete: Bool = true
|
|
53
59
|
var maskAutoSkip: Bool = false
|
|
60
|
+
/// Mask mode: alternative patterns and how the best is chosen (MaskEngine's
|
|
61
|
+
/// `kAffinity…` constants).
|
|
62
|
+
var maskAffinityFormats: [String] = []
|
|
63
|
+
var maskAffinityStrategy: Int = 0
|
|
64
|
+
/// Mask mode: MaskEngine's `kTextCase…` constants.
|
|
65
|
+
var maskTextCase: Int = 0
|
|
66
|
+
var maskCharacterMap: [MaskMapping] = []
|
|
67
|
+
/// Mask mode: what an empty slot shows in the tail; empty = its notation.
|
|
68
|
+
var maskSlotPlaceholder: String = ""
|
|
69
|
+
/// Mask mode: keep the rest of the placeholder after the typed text.
|
|
70
|
+
var keepPlaceholder: Bool = false
|
|
54
71
|
var fractionDigits: Int = 2
|
|
55
72
|
var maxIntegerDigits: Int = 15
|
|
56
73
|
var groupingSeparator: String = ","
|
|
@@ -168,6 +185,11 @@ final class NitroInputView: UIView {
|
|
|
168
185
|
var showSoftInputOnFocus: Bool = true
|
|
169
186
|
var selectTextOnFocus: Bool = false
|
|
170
187
|
var clearTextOnFocus: Bool = false
|
|
188
|
+
/// How long a focused field that is leaving its window keeps the keyboard
|
|
189
|
+
/// up for the next field (see `KeyboardHandoff`); 0 is off.
|
|
190
|
+
var keyboardHandoffMs: Double = 0
|
|
191
|
+
/// The return key bar over the number pads (see `applyReturnAccessory`).
|
|
192
|
+
var returnKeyBar: Bool = true
|
|
171
193
|
var contextMenuHidden: Bool = false
|
|
172
194
|
var spellCheck: Bool = true
|
|
173
195
|
/// `testID` and `accessibilityLabel`, forwarded from JS so the hidden
|
|
@@ -582,6 +604,19 @@ final class NitroInputView: UIView {
|
|
|
582
604
|
/// A narrower settled size waiting for the current reflow to finish before it is reported.
|
|
583
605
|
private var pendingSizeReport = false
|
|
584
606
|
private var didAutoFocus = false
|
|
607
|
+
/// `focus()` arrived while the view had no window (a screen coming back on a
|
|
608
|
+
/// pop is re-added only as the transition starts): taken the moment the view
|
|
609
|
+
/// is back, unless it has gone stale.
|
|
610
|
+
private var pendingFocusUntil: CFTimeInterval = 0
|
|
611
|
+
/// When the field last stopped editing while on screen (media time; 0 = never).
|
|
612
|
+
private var endedEditingAt: CFTimeInterval = 0
|
|
613
|
+
/// Had the keyboard when its screen was covered: take it back on return.
|
|
614
|
+
private var restoreOnReturn = false
|
|
615
|
+
/// Set while this view asks for focus itself (focus(), autoFocus, a return
|
|
616
|
+
/// after a handoff): those go through at once, even mid-transition.
|
|
617
|
+
private var claimingFocus = false
|
|
618
|
+
/// A restore deferred to the end of the running transition is waiting.
|
|
619
|
+
private var focusAfterTransition = false
|
|
585
620
|
/// Set while this view writes the field's text itself, so the change handler
|
|
586
621
|
/// can tell a programmatic set from a user edit.
|
|
587
622
|
private var isSettingText = false
|
|
@@ -603,6 +638,8 @@ final class NitroInputView: UIView {
|
|
|
603
638
|
}()
|
|
604
639
|
/// True once `textView` has been built; reading the lazy var would build it.
|
|
605
640
|
private var didBuildTextView = false
|
|
641
|
+
/// The return key the number-pad bar shows, or nil when there is no bar.
|
|
642
|
+
private var returnAccessoryKey: UIReturnKeyType?
|
|
606
643
|
/// The width the multiline height was last measured against.
|
|
607
644
|
private var lastMeasuredWidth: CGFloat = 0
|
|
608
645
|
/// Scaled and aligned container for the content (font space).
|
|
@@ -640,6 +677,8 @@ final class NitroInputView: UIView {
|
|
|
640
677
|
|
|
641
678
|
override init(frame: CGRect) {
|
|
642
679
|
super.init(frame: frame)
|
|
680
|
+
// Starts it tracking the keyboard before any field needs to ask.
|
|
681
|
+
_ = KeyboardHandoff.shared
|
|
643
682
|
isOpaque = false
|
|
644
683
|
backgroundColor = .clear
|
|
645
684
|
clipsToBounds = false
|
|
@@ -755,6 +794,7 @@ final class NitroInputView: UIView {
|
|
|
755
794
|
}
|
|
756
795
|
field.frame = bounds
|
|
757
796
|
if didBuildTextView { textView.frame = bounds }
|
|
797
|
+
updateGhostLabel()
|
|
758
798
|
// Where the text wraps - and so how tall the box is - depends on the width,
|
|
759
799
|
// and layout is the first thing to know it. Measuring from inside the pass
|
|
760
800
|
// would re-enter it, so the report goes out after this one lands.
|
|
@@ -798,9 +838,115 @@ final class NitroInputView: UIView {
|
|
|
798
838
|
// The fonts were built against whatever traits the view had when it was
|
|
799
839
|
// made; the window's are the ones that count.
|
|
800
840
|
if window != nil { syncWithTraits(appearanceChanged: false) }
|
|
841
|
+
if window != nil, restoreOnReturn {
|
|
842
|
+
// Back on screen after its screen was covered while it had the keyboard.
|
|
843
|
+
// UIKit gives the keyboard back itself when nothing else has it - but a
|
|
844
|
+
// field on the covering screen with `keyboardHandoffMs` hands it to its
|
|
845
|
+
// stand-in as it leaves, and UIKit then sees a first responder and gives
|
|
846
|
+
// nothing back. So while the keyboard is still up (a field on the
|
|
847
|
+
// leaving screen, or its stand-in, has it), this field takes it, at the
|
|
848
|
+
// start of the way back as UIKit would. Down, UIKit's own restore runs.
|
|
849
|
+
restoreOnReturn = false
|
|
850
|
+
let handoff = KeyboardHandoff.shared
|
|
851
|
+
if traits.editable, !editor.isFirstResponder, handoff.isHolding || handoff.keyboardVisible {
|
|
852
|
+
// As UIKit's own restore: once the screen has settled (see
|
|
853
|
+
// `deferFocusWhileTransitioning`), or now when nothing is moving.
|
|
854
|
+
if !deferFocusWhileTransitioning() { claimFirstResponder() }
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
if window != nil, pendingFocusUntil > 0 {
|
|
858
|
+
// Only while the keyboard is still up (held for this field, or another
|
|
859
|
+
// field has it): a keyboard dismissed during the transition stays down.
|
|
860
|
+
let handoff = KeyboardHandoff.shared
|
|
861
|
+
let wanted = CACurrentMediaTime() < pendingFocusUntil && (handoff.isHolding || handoff.keyboardVisible)
|
|
862
|
+
pendingFocusUntil = 0
|
|
863
|
+
if wanted, traits.editable { claimFirstResponder() }
|
|
864
|
+
}
|
|
801
865
|
maybeAutoFocus()
|
|
802
866
|
}
|
|
803
867
|
|
|
868
|
+
override func willMove(toWindow newWindow: UIWindow?) {
|
|
869
|
+
// Before UIKit resigns the field on its way out, so the keyboard never
|
|
870
|
+
// starts to hide.
|
|
871
|
+
if newWindow == nil {
|
|
872
|
+
// Covered while it had the keyboard: UIKit resigns a field as the
|
|
873
|
+
// transition starts, a moment before its view leaves the window.
|
|
874
|
+
restoreOnReturn = editor.isFirstResponder || CACurrentMediaTime() - endedEditingAt < 1
|
|
875
|
+
handOffKeyboardIfFocused()
|
|
876
|
+
}
|
|
877
|
+
super.willMove(toWindow: newWindow)
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
/// A focused field leaving the screen hands the keyboard to a stand-in field
|
|
881
|
+
/// instead of dropping it, so the next field to focus takes it over in place.
|
|
882
|
+
func handOffKeyboardIfFocused() {
|
|
883
|
+
guard traits.keyboardHandoffMs > 0, traits.showSoftInputOnFocus,
|
|
884
|
+
let window, editor.isFirstResponder else { return }
|
|
885
|
+
KeyboardHandoff.shared.hold(like: editor, in: window, for: traits.keyboardHandoffMs)
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
/// `becomeFirstResponder`, retried next turn if UIKit declines because the
|
|
889
|
+
/// view is in a window but not ready to accept it yet.
|
|
890
|
+
private func claimFirstResponder() {
|
|
891
|
+
if claimOnce() { return }
|
|
892
|
+
DispatchQueue.main.async { [weak self] in
|
|
893
|
+
guard let self, self.window != nil else { return }
|
|
894
|
+
self.claimOnce()
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
@discardableResult
|
|
899
|
+
private func claimOnce() -> Bool {
|
|
900
|
+
claimingFocus = true
|
|
901
|
+
defer { claimingFocus = false }
|
|
902
|
+
return editor.becomeFirstResponder()
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
/// Sends the current selection to a delegate someone put in front of ours
|
|
906
|
+
/// (see `textFieldDidBeginEditing`). Ours reports selections itself.
|
|
907
|
+
private func announceSelectionToDelegate() {
|
|
908
|
+
guard editor.isFirstResponder else { return }
|
|
909
|
+
if let field = editor as? UITextField, let delegate = field.delegate, delegate !== self {
|
|
910
|
+
delegate.textFieldDidChangeSelection?(field)
|
|
911
|
+
} else if let view = editor as? UITextView, let delegate = view.delegate, delegate !== self {
|
|
912
|
+
delegate.textViewDidChangeSelection?(view)
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
/// The view controller this field's screen belongs to.
|
|
917
|
+
private var hostViewController: UIViewController? {
|
|
918
|
+
var responder: UIResponder? = self
|
|
919
|
+
while let next = responder?.next {
|
|
920
|
+
if let controller = next as? UIViewController { return controller }
|
|
921
|
+
responder = next
|
|
922
|
+
}
|
|
923
|
+
return nil
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
/// UIKit gives a field its focus back when you return to its screen (back,
|
|
927
|
+
/// swipe-back, a sheet closing), and does it as the transition starts. On
|
|
928
|
+
/// iOS 26 the keyboard then travels in with the sliding screen as a flat
|
|
929
|
+
/// dark snapshot of itself, and only turns into the real keyboard once the
|
|
930
|
+
/// screen has settled. The platform apps give focus back after the
|
|
931
|
+
/// transition, so the real keyboard rises from the bottom (Contacts'
|
|
932
|
+
/// search): this does the same. Returns true when the focus was deferred.
|
|
933
|
+
/// Focus this view asks for itself (`claimingFocus`) is never deferred:
|
|
934
|
+
/// that is `focus()` and `autoFocus`, which keep the keyboard up across a
|
|
935
|
+
/// push to a screen that focuses its own field.
|
|
936
|
+
fileprivate func deferFocusWhileTransitioning() -> Bool {
|
|
937
|
+
guard !claimingFocus, let coordinator = hostViewController?.transitionCoordinator else { return false }
|
|
938
|
+
if focusAfterTransition { return true }
|
|
939
|
+
focusAfterTransition = true
|
|
940
|
+
coordinator.animate(alongsideTransition: nil) { [weak self] context in
|
|
941
|
+
// Cancelled meanwhile by blur() / Keyboard.dismiss() or a recycle.
|
|
942
|
+
guard let self, self.focusAfterTransition else { return }
|
|
943
|
+
self.focusAfterTransition = false
|
|
944
|
+
guard !context.isCancelled, self.window != nil, self.traits.editable else { return }
|
|
945
|
+
self.claimFirstResponder()
|
|
946
|
+
}
|
|
947
|
+
return true
|
|
948
|
+
}
|
|
949
|
+
|
|
804
950
|
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
|
|
805
951
|
super.traitCollectionDidChange(previousTraitCollection)
|
|
806
952
|
let appearanceChanged = previousTraitCollection.map { traitCollection.hasDifferentColorAppearance(comparedTo: $0) } ?? true
|
|
@@ -845,11 +991,7 @@ final class NitroInputView: UIView {
|
|
|
845
991
|
private func maybeAutoFocus() {
|
|
846
992
|
guard window != nil, traits.autoFocus, traits.editable, !didAutoFocus else { return }
|
|
847
993
|
didAutoFocus = true
|
|
848
|
-
|
|
849
|
-
DispatchQueue.main.async { [weak self] in
|
|
850
|
-
guard let self, self.window != nil else { return }
|
|
851
|
-
self.editor.becomeFirstResponder()
|
|
852
|
-
}
|
|
994
|
+
claimFirstResponder()
|
|
853
995
|
}
|
|
854
996
|
|
|
855
997
|
@objc private func contentSizeCategoryDidChange() {
|
|
@@ -867,6 +1009,10 @@ final class NitroInputView: UIView {
|
|
|
867
1009
|
func resetForRecycle() {
|
|
868
1010
|
worklets = Worklets()
|
|
869
1011
|
if editor.isFirstResponder { editor.resignFirstResponder() }
|
|
1012
|
+
pendingFocusUntil = 0
|
|
1013
|
+
restoreOnReturn = false
|
|
1014
|
+
endedEditingAt = 0
|
|
1015
|
+
focusAfterTransition = false
|
|
870
1016
|
stopDisplayLink()
|
|
871
1017
|
stopBlink()
|
|
872
1018
|
engine.reset()
|
|
@@ -896,13 +1042,32 @@ final class NitroInputView: UIView {
|
|
|
896
1042
|
|
|
897
1043
|
// MARK: - Public API
|
|
898
1044
|
|
|
899
|
-
func focus() {
|
|
1045
|
+
@objc(nitroFocus) func focus() {
|
|
900
1046
|
guard traits.editable else { return }
|
|
901
|
-
|
|
1047
|
+
// Not in a window: nothing can take first responder there. A screen being
|
|
1048
|
+
// popped back to is re-added only as its transition starts, so hold the
|
|
1049
|
+
// request for that moment - claiming it the same turn the view arrives is
|
|
1050
|
+
// what swaps the keyboard in place rather than hiding and showing it.
|
|
1051
|
+
guard window != nil else {
|
|
1052
|
+
pendingFocusUntil = CACurrentMediaTime() + 1
|
|
1053
|
+
return
|
|
1054
|
+
}
|
|
1055
|
+
pendingFocusUntil = 0
|
|
1056
|
+
claimFirstResponder()
|
|
902
1057
|
}
|
|
903
1058
|
|
|
904
|
-
func blur() {
|
|
905
|
-
|
|
1059
|
+
@objc(nitroBlur) func blur() {
|
|
1060
|
+
pendingFocusUntil = 0
|
|
1061
|
+
restoreOnReturn = false
|
|
1062
|
+
endedEditingAt = 0
|
|
1063
|
+
focusAfterTransition = false
|
|
1064
|
+
if editor.isFirstResponder {
|
|
1065
|
+
editor.resignFirstResponder()
|
|
1066
|
+
} else {
|
|
1067
|
+
// `Keyboard.dismiss()` while a field that just left the screen holds the
|
|
1068
|
+
// keyboard for the next one: nothing is focused, so let it go now.
|
|
1069
|
+
KeyboardHandoff.shared.letGo()
|
|
1070
|
+
}
|
|
906
1071
|
}
|
|
907
1072
|
|
|
908
1073
|
/// Moves the caret/selection to `start`..`end`, in code points.
|
|
@@ -997,13 +1162,28 @@ final class NitroInputView: UIView {
|
|
|
997
1162
|
formatter.setFormat(Int32(max(0, format.fractionDigits)), Int32(max(1, format.maxIntegerDigits)),
|
|
998
1163
|
std.string(format.groupingSeparator), std.string(format.decimalSeparator))
|
|
999
1164
|
}
|
|
1000
|
-
|
|
1001
|
-
|
|
1165
|
+
let maskChanged = format.mask != previous.mask || format.maskNotations != previous.maskNotations
|
|
1166
|
+
|| format.maskAffinityFormats != previous.maskAffinityFormats
|
|
1167
|
+
|| format.maskAffinityStrategy != previous.maskAffinityStrategy
|
|
1168
|
+
|| format.maskTextCase != previous.maskTextCase || format.maskCharacterMap != previous.maskCharacterMap
|
|
1169
|
+
|| format.maskSlotPlaceholder != previous.maskSlotPlaceholder
|
|
1170
|
+
if format.mode == .mask, maskChanged || previous.mode != .mask {
|
|
1002
1171
|
maskEngine.clearNotations()
|
|
1003
1172
|
for notation in format.maskNotations {
|
|
1004
1173
|
maskEngine.addNotation(std.string(notation.character), std.string(notation.characterSet),
|
|
1005
1174
|
notation.isOptional)
|
|
1006
1175
|
}
|
|
1176
|
+
maskEngine.clearAffinityFormats()
|
|
1177
|
+
for alternative in format.maskAffinityFormats {
|
|
1178
|
+
maskEngine.addAffinityFormat(std.string(alternative))
|
|
1179
|
+
}
|
|
1180
|
+
maskEngine.setAffinityStrategy(Int32(format.maskAffinityStrategy))
|
|
1181
|
+
maskEngine.setTextCase(Int32(format.maskTextCase))
|
|
1182
|
+
maskEngine.clearCharacterMap()
|
|
1183
|
+
for mapping in format.maskCharacterMap {
|
|
1184
|
+
maskEngine.addCharacterMapping(std.string(mapping.from), std.string(mapping.to))
|
|
1185
|
+
}
|
|
1186
|
+
maskEngine.setSlotPlaceholder(std.string(format.maskSlotPlaceholder))
|
|
1007
1187
|
// A bad pattern leaves the engine inactive; the field then behaves as
|
|
1008
1188
|
// plain text rather than refusing every keystroke.
|
|
1009
1189
|
_ = maskEngine.setFormat(std.string(format.mask))
|
|
@@ -1014,18 +1194,26 @@ final class NitroInputView: UIView {
|
|
|
1014
1194
|
let formatChanged = format.mode != previous.mode || format.fractionDigits != previous.fractionDigits
|
|
1015
1195
|
|| format.maxIntegerDigits != previous.maxIntegerDigits || format.groupingSeparator != previous.groupingSeparator
|
|
1016
1196
|
|| format.decimalSeparator != previous.decimalSeparator || format.maxLength != previous.maxLength
|
|
1017
|
-
||
|
|
1197
|
+
|| maskChanged
|
|
1198
|
+
var reformatted = false
|
|
1018
1199
|
if formatChanged {
|
|
1019
1200
|
let normalizedText = normalized(text)
|
|
1020
1201
|
if normalizedText != text {
|
|
1021
1202
|
isSettingText = true
|
|
1022
1203
|
field.text = normalizedText
|
|
1023
1204
|
isSettingText = false
|
|
1205
|
+
reformatted = true
|
|
1024
1206
|
}
|
|
1025
1207
|
}
|
|
1026
1208
|
applyKeyboardTraitsForMode()
|
|
1027
|
-
|
|
1028
|
-
|
|
1209
|
+
if reformatted {
|
|
1210
|
+
// The text itself changed ("123-456" is "12 34 56" under a new mask), so
|
|
1211
|
+
// it is reported like `setText`, as Android does: without that, JS -
|
|
1212
|
+
// getText(), onChangeText, onChangeMask and a controlled parent - kept
|
|
1213
|
+
// the old text while the field showed the new one.
|
|
1214
|
+
textDidChange(caret: text.unicodeScalars.count, reason: .method)
|
|
1215
|
+
} else if engine.hasText() || traits.plain {
|
|
1216
|
+
// A plain field has nothing in the engine and still has a width to report.
|
|
1029
1217
|
feedEngine(caret: -1)
|
|
1030
1218
|
}
|
|
1031
1219
|
}
|
|
@@ -1122,7 +1310,13 @@ final class NitroInputView: UIView {
|
|
|
1122
1310
|
textView.isUserInteractionEnabled = on && traits.editable
|
|
1123
1311
|
field.isHidden = on
|
|
1124
1312
|
field.isUserInteractionEnabled = !on && traits.editable
|
|
1125
|
-
|
|
1313
|
+
// The hidden twin must not count as an input: keyboard-controller's
|
|
1314
|
+
// next/previous and its toolbar walk every enabled field and editable view.
|
|
1315
|
+
field.isEnabled = !on && traits.editable
|
|
1316
|
+
guard on else {
|
|
1317
|
+
textView.isEditable = false
|
|
1318
|
+
return
|
|
1319
|
+
}
|
|
1126
1320
|
|
|
1127
1321
|
textView.frame = bounds
|
|
1128
1322
|
textView.keyboardType = traits.keyboardType
|
|
@@ -1255,7 +1449,8 @@ final class NitroInputView: UIView {
|
|
|
1255
1449
|
applyMultiline()
|
|
1256
1450
|
field.keyboardType = traits.keyboardType
|
|
1257
1451
|
field.returnKeyType = traits.returnKeyType
|
|
1258
|
-
|
|
1452
|
+
applyReturnAccessory()
|
|
1453
|
+
field.isEnabled = traits.editable && !traits.multiline
|
|
1259
1454
|
field.isUserInteractionEnabled = traits.editable
|
|
1260
1455
|
// `tintColor` is both the caret and the selection on a UITextField. The
|
|
1261
1456
|
// overlay draws its own caret, so only `plain` has a caret colour to honour
|
|
@@ -1332,6 +1527,53 @@ final class NitroInputView: UIView {
|
|
|
1332
1527
|
}
|
|
1333
1528
|
}
|
|
1334
1529
|
|
|
1530
|
+
/// The number pads (number, phone, decimal) have no return key, so a form
|
|
1531
|
+
/// chained with `returnKeyType="next"` would stop at its first numeric field.
|
|
1532
|
+
/// As React Native's `TextInput` does, a field on one of them with a return
|
|
1533
|
+
/// key type set gets a bar above the keyboard with that key: "Next", "Go",
|
|
1534
|
+
/// "Done"... It is the return key, submit behaviour and all.
|
|
1535
|
+
private func applyReturnAccessory() {
|
|
1536
|
+
let pads: [UIKeyboardType] = [.numberPad, .phonePad, .decimalPad, .asciiCapableNumberPad]
|
|
1537
|
+
let returnKey = traits.returnKeyType
|
|
1538
|
+
let wanted = traits.returnKeyBar && pads.contains(traits.keyboardType) && returnKey != .default
|
|
1539
|
+
if !wanted {
|
|
1540
|
+
guard returnAccessoryKey != nil else { return }
|
|
1541
|
+
returnAccessoryKey = nil
|
|
1542
|
+
field.inputAccessoryView = nil
|
|
1543
|
+
} else {
|
|
1544
|
+
guard returnAccessoryKey != returnKey else { return }
|
|
1545
|
+
returnAccessoryKey = returnKey
|
|
1546
|
+
let bar = UIToolbar()
|
|
1547
|
+
bar.sizeToFit()
|
|
1548
|
+
let button = returnKey == .done
|
|
1549
|
+
? UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(returnAccessoryTapped))
|
|
1550
|
+
: UIBarButtonItem(title: Self.returnKeyTitle(returnKey), style: .plain, target: self, action: #selector(returnAccessoryTapped))
|
|
1551
|
+
bar.items = [UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil), button]
|
|
1552
|
+
field.inputAccessoryView = bar
|
|
1553
|
+
}
|
|
1554
|
+
if didBuildTextView { textView.inputAccessoryView = field.inputAccessoryView }
|
|
1555
|
+
if editor.isFirstResponder { editor.reloadInputViews() }
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
@objc private func returnAccessoryTapped() {
|
|
1559
|
+
handleReturnKey()
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
/// The return key's own label, as React Native names it for the same bar.
|
|
1563
|
+
private static func returnKeyTitle(_ key: UIReturnKeyType) -> String {
|
|
1564
|
+
switch key {
|
|
1565
|
+
case .go: return "Go"
|
|
1566
|
+
case .next: return "Next"
|
|
1567
|
+
case .search: return "Search"
|
|
1568
|
+
case .send: return "Send"
|
|
1569
|
+
case .route: return "Route"
|
|
1570
|
+
case .join: return "Join"
|
|
1571
|
+
case .emergencyCall: return "Emergency Call"
|
|
1572
|
+
case .continue: return "Continue"
|
|
1573
|
+
default: return "Done"
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1335
1577
|
private func applyKeyboardTraitsForMode() {
|
|
1336
1578
|
// A mask decides every character, so the keyboard must not second-guess it:
|
|
1337
1579
|
// autocorrect would fight `shouldChangeCharactersIn` over the replacement.
|
|
@@ -1400,6 +1642,7 @@ final class NitroInputView: UIView {
|
|
|
1400
1642
|
// Plain mode has no overlay to feed: the field draws its own text. Its
|
|
1401
1643
|
// width still has to reach React for `autoWidth`, and this is the one
|
|
1402
1644
|
// place every text, font, affix and placeholder change comes through.
|
|
1645
|
+
updateGhostLabel()
|
|
1403
1646
|
guard !traits.plain else { reportIntrinsicSize(); return }
|
|
1404
1647
|
engine.setReduceMotion(UIAccessibility.isReduceMotionEnabled)
|
|
1405
1648
|
engine.beginText()
|
|
@@ -1441,6 +1684,14 @@ final class NitroInputView: UIView {
|
|
|
1441
1684
|
let after = hasSuffix && i == rest.count - 1 ? (fonts.suffixSeam ?? fonts.bodySpacing) : fonts.bodySpacing
|
|
1442
1685
|
engine.addGlyph(scalar.value, Role.body, kind, Double(fonts.width(of: String(scalar), role: .body) + after), showPlaceholder)
|
|
1443
1686
|
}
|
|
1687
|
+
// `keepPlaceholder`: the rest of the mask trails the text as placeholder
|
|
1688
|
+
// glyphs. They come after every typed glyph, so the caret - which counts
|
|
1689
|
+
// body glyphs in order - never lands among them.
|
|
1690
|
+
if !showPlaceholder {
|
|
1691
|
+
for scalar in ghostTail.unicodeScalars {
|
|
1692
|
+
engine.addGlyph(scalar.value, Role.body, Kind.text, Double(fonts.width(of: String(scalar), role: .body) + fonts.bodySpacing), true)
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1444
1695
|
for scalar in format.suffix.unicodeScalars {
|
|
1445
1696
|
engine.addGlyph(scalar.value, Role.suffix, Kind.text, Double(fonts.width(of: String(scalar), role: .suffix) + fonts.suffixLetterSpacing), false)
|
|
1446
1697
|
}
|
|
@@ -1625,11 +1876,49 @@ final class NitroInputView: UIView {
|
|
|
1625
1876
|
/// The width the content asks for: the engine's layout in reflow mode; in
|
|
1626
1877
|
/// plain mode, which never feeds the engine, the text (or the placeholder)
|
|
1627
1878
|
/// and the affixes measured with the same fonts the field draws them in.
|
|
1879
|
+
/// Plain mode has no overlay to draw `keepPlaceholder`'s tail, so it is a
|
|
1880
|
+
/// label pinned inside the field to where the field's own text ends, in the
|
|
1881
|
+
/// same font and kerning and the placeholder colour.
|
|
1882
|
+
private lazy var ghostLabel: UILabel = {
|
|
1883
|
+
let label = UILabel()
|
|
1884
|
+
label.isUserInteractionEnabled = false
|
|
1885
|
+
label.isAccessibilityElement = false
|
|
1886
|
+
label.lineBreakMode = .byClipping
|
|
1887
|
+
return label
|
|
1888
|
+
}()
|
|
1889
|
+
|
|
1890
|
+
private func updateGhostLabel() {
|
|
1891
|
+
let tail = traits.plain ? ghostTail : ""
|
|
1892
|
+
guard !tail.isEmpty, !isRTL else {
|
|
1893
|
+
if ghostLabel.superview != nil { ghostLabel.isHidden = true }
|
|
1894
|
+
return
|
|
1895
|
+
}
|
|
1896
|
+
if ghostLabel.superview !== field { field.addSubview(ghostLabel) }
|
|
1897
|
+
var attributes = field.defaultTextAttributes
|
|
1898
|
+
attributes[.foregroundColor] = typography.placeholderColor.resolvedColor(with: traitCollection)
|
|
1899
|
+
ghostLabel.attributedText = NSAttributedString(string: tail, attributes: attributes)
|
|
1900
|
+
let box = field.textRect(forBounds: field.bounds)
|
|
1901
|
+
let typed = fonts.width(of: text, role: .body) + fonts.bodySpacing * CGFloat(text.unicodeScalars.count)
|
|
1902
|
+
let tailWidth = fonts.width(of: tail, role: .body) + fonts.bodySpacing * CGFloat(tail.unicodeScalars.count)
|
|
1903
|
+
// Where UIKit starts the text: the box's leading edge, or centred / right
|
|
1904
|
+
// aligned without the tail - the tail then trails off the typed text.
|
|
1905
|
+
let start: CGFloat
|
|
1906
|
+
switch field.textAlignment {
|
|
1907
|
+
case .center: start = box.midX - typed / 2
|
|
1908
|
+
case .right: start = box.maxX - typed
|
|
1909
|
+
default: start = box.minX
|
|
1910
|
+
}
|
|
1911
|
+
let end = start + typed
|
|
1912
|
+
// Text wider than the box scrolls inside the field; the tail has no place then.
|
|
1913
|
+
ghostLabel.isHidden = typed > box.width
|
|
1914
|
+
ghostLabel.frame = CGRect(x: end, y: box.minY, width: min(tailWidth + 1, max(0, box.maxX - end)), height: box.height)
|
|
1915
|
+
}
|
|
1916
|
+
|
|
1628
1917
|
private func contentWidth() -> CGFloat {
|
|
1629
1918
|
guard traits.plain else { return CGFloat(engine.targetWidth()) }
|
|
1630
1919
|
let body = text.isEmpty
|
|
1631
1920
|
? effectivePlaceholder
|
|
1632
|
-
: traits.secureTextEntry ? String(repeating: "\u{2022}", count: text.unicodeScalars.count) : text
|
|
1921
|
+
: traits.secureTextEntry ? String(repeating: "\u{2022}", count: text.unicodeScalars.count) : text + ghostTail
|
|
1633
1922
|
// The field's own kerning follows every glyph; the affixes take their room with their seams.
|
|
1634
1923
|
return fonts.affixRoom(format.prefix, role: .prefix)
|
|
1635
1924
|
+ fonts.width(of: body, role: .body) + fonts.bodySpacing * CGFloat(body.unicodeScalars.count)
|
|
@@ -1920,6 +2209,11 @@ final class NitroInputView: UIView {
|
|
|
1920
2209
|
*/
|
|
1921
2210
|
fileprivate final class HiddenTextView: UITextView {
|
|
1922
2211
|
weak var owner: NitroInputView?
|
|
2212
|
+
|
|
2213
|
+
override func becomeFirstResponder() -> Bool {
|
|
2214
|
+
if owner?.deferFocusWhileTransitioning() == true { return false }
|
|
2215
|
+
return super.becomeFirstResponder()
|
|
2216
|
+
}
|
|
1923
2217
|
var placeholder: String = "" { didSet { setNeedsDisplay() } }
|
|
1924
2218
|
var placeholderColor: UIColor = .placeholderText { didSet { setNeedsDisplay() } }
|
|
1925
2219
|
|
|
@@ -1938,7 +2232,10 @@ final class NitroInputView: UIView {
|
|
|
1938
2232
|
override func caretRect(for position: UITextPosition) -> CGRect {
|
|
1939
2233
|
let rect = super.caretRect(for: position)
|
|
1940
2234
|
guard hidesNativeCaret else { return rect }
|
|
1941
|
-
|
|
2235
|
+
// Zero width hides it; the full height is what keyboard-controller
|
|
2236
|
+
// scrolls to (the caret's bottom edge), so the whole line clears the
|
|
2237
|
+
// keyboard rather than only its top.
|
|
2238
|
+
return CGRect(x: rect.minX, y: rect.minY, width: 0, height: rect.height)
|
|
1942
2239
|
}
|
|
1943
2240
|
|
|
1944
2241
|
var hidesNativeCaret = false {
|
|
@@ -1980,6 +2277,11 @@ final class NitroInputView: UIView {
|
|
|
1980
2277
|
|
|
1981
2278
|
fileprivate final class HiddenTextField: UITextField {
|
|
1982
2279
|
weak var owner: NitroInputView?
|
|
2280
|
+
|
|
2281
|
+
override func becomeFirstResponder() -> Bool {
|
|
2282
|
+
if owner?.deferFocusWhileTransitioning() == true { return false }
|
|
2283
|
+
return super.becomeFirstResponder()
|
|
2284
|
+
}
|
|
1983
2285
|
var leftInset: CGFloat = 0
|
|
1984
2286
|
var rightInset: CGFloat = 0
|
|
1985
2287
|
/// Room for a floated label sitting on the top edge.
|
|
@@ -2024,7 +2326,10 @@ final class NitroInputView: UIView {
|
|
|
2024
2326
|
override func caretRect(for position: UITextPosition) -> CGRect {
|
|
2025
2327
|
let rect = super.caretRect(for: position)
|
|
2026
2328
|
guard hidesNativeCaret else { return rect }
|
|
2027
|
-
|
|
2329
|
+
// Zero width hides it; the full height is what keyboard-controller
|
|
2330
|
+
// scrolls to (the caret's bottom edge), so the whole line clears the
|
|
2331
|
+
// keyboard rather than only its top.
|
|
2332
|
+
return CGRect(x: rect.minX, y: rect.minY, width: 0, height: rect.height)
|
|
2028
2333
|
}
|
|
2029
2334
|
|
|
2030
2335
|
override func textRect(forBounds bounds: CGRect) -> CGRect {
|
|
@@ -2113,6 +2418,9 @@ extension NitroInputView: UITextViewDelegate {
|
|
|
2113
2418
|
}
|
|
2114
2419
|
|
|
2115
2420
|
func textViewDidChange(_ textView: UITextView) {
|
|
2421
|
+
// Typing does not go through `text`'s setter, so the drawn placeholder
|
|
2422
|
+
// would stay under the first characters typed.
|
|
2423
|
+
textView.setNeedsDisplay()
|
|
2116
2424
|
// The text view owns the text here. `setText` runs it through the
|
|
2117
2425
|
// formatter, which is single-line by definition, so the multiline path
|
|
2118
2426
|
// reports what the view already holds instead.
|
|
@@ -2246,6 +2554,15 @@ extension NitroInputView: UITextFieldDelegate {
|
|
|
2246
2554
|
}
|
|
2247
2555
|
|
|
2248
2556
|
func textFieldDidBeginEditing(_ textField: UITextField) {
|
|
2557
|
+
// keyboard-controller puts its own delegate in front of ours a turn after
|
|
2558
|
+
// editing begins, and its KeyboardAwareScrollView waits for a selection
|
|
2559
|
+
// event from the newly focused field before it scrolls. A tap or Next
|
|
2560
|
+
// moves the caret and sends one; a field given its focus back with the
|
|
2561
|
+
// caret where it was sends none, and the form never scrolls it into view.
|
|
2562
|
+
// So once that delegate is in place, tell it where the selection is.
|
|
2563
|
+
DispatchQueue.main.async {
|
|
2564
|
+
DispatchQueue.main.async { [weak self] in self?.announceSelectionToDelegate() }
|
|
2565
|
+
}
|
|
2249
2566
|
if traits.clearTextOnFocus {
|
|
2250
2567
|
setText("", reason: .user)
|
|
2251
2568
|
} else if traits.selectTextOnFocus {
|
|
@@ -2265,6 +2582,7 @@ extension NitroInputView: UITextFieldDelegate {
|
|
|
2265
2582
|
}
|
|
2266
2583
|
|
|
2267
2584
|
func textFieldDidEndEditing(_ textField: UITextField) {
|
|
2585
|
+
if window != nil { endedEditingAt = CACurrentMediaTime() }
|
|
2268
2586
|
if contentOverflows { render() }
|
|
2269
2587
|
updateCaret()
|
|
2270
2588
|
if worklets.onFocusChange != 0 {
|
|
@@ -2368,10 +2686,40 @@ extension NitroInputView {
|
|
|
2368
2686
|
/// A label resting inside the field already labels it, so the placeholder
|
|
2369
2687
|
/// waits until the label has floated clear rather than printing over it.
|
|
2370
2688
|
var effectivePlaceholder: String {
|
|
2371
|
-
guard inputFrame.hasLabel, !labelShouldFloat else { return
|
|
2689
|
+
guard inputFrame.hasLabel, !labelShouldFloat else { return basePlaceholder }
|
|
2372
2690
|
return ""
|
|
2373
2691
|
}
|
|
2374
2692
|
|
|
2693
|
+
/// `keepPlaceholder` without a `placeholder` shows the mask itself, empty
|
|
2694
|
+
/// slots as `maskSlotPlaceholder`: "+1 (___) ___-____".
|
|
2695
|
+
private var basePlaceholder: String {
|
|
2696
|
+
if format.placeholder.isEmpty, format.mode == .mask, format.keepPlaceholder, maskEngine.isActive() {
|
|
2697
|
+
return String(maskEngine.apply(std.string(""), 0, true, false, false).tailPlaceholder)
|
|
2698
|
+
}
|
|
2699
|
+
return format.placeholder
|
|
2700
|
+
}
|
|
2701
|
+
|
|
2702
|
+
/// `keepPlaceholder`: what is still to come after the typed text, drawn in
|
|
2703
|
+
/// the placeholder colour. The caller's placeholder from where the text ends
|
|
2704
|
+
/// - "1234 5" leaves "678 9012" of "1234 5678 9012" - or, without one, the
|
|
2705
|
+
/// rest of the mask. The placeholder is written for `mask`, so when one of
|
|
2706
|
+
/// `maskAffinityFormats` has taken over (a 4-6-5 Amex under a 4-4-4-4
|
|
2707
|
+
/// placeholder) its characters no longer line up, and that format's own
|
|
2708
|
+
/// tail is shown instead. A complete text has nothing to come.
|
|
2709
|
+
var ghostTail: String {
|
|
2710
|
+
guard format.mode == .mask, format.keepPlaceholder, !text.isEmpty, !traits.secureTextEntry,
|
|
2711
|
+
maskEngine.isActive() else { return "" }
|
|
2712
|
+
let typed = text.unicodeScalars.count
|
|
2713
|
+
let result = maskEngine.apply(std.string(text), Int32(typed), true, false, false)
|
|
2714
|
+
if result.complete { return "" }
|
|
2715
|
+
let placeholder = Array(format.placeholder.unicodeScalars)
|
|
2716
|
+
if placeholder.isEmpty || result.formatIndex != 0 { return String(result.tailPlaceholder) }
|
|
2717
|
+
guard typed < placeholder.count else { return "" }
|
|
2718
|
+
var rest = String.UnicodeScalarView()
|
|
2719
|
+
rest.append(contentsOf: placeholder[typed...])
|
|
2720
|
+
return String(rest)
|
|
2721
|
+
}
|
|
2722
|
+
|
|
2375
2723
|
/// The label floats once the field is focused or holds text - or always, if
|
|
2376
2724
|
/// the caller asked for that.
|
|
2377
2725
|
private var labelShouldFloat: Bool {
|
|
@@ -2682,3 +3030,109 @@ extension NitroInputView {
|
|
|
2682
3030
|
}
|
|
2683
3031
|
}
|
|
2684
3032
|
|
|
3033
|
+
|
|
3034
|
+
|
|
3035
|
+
/// Keeps the keyboard up while a focused field is taken off screen (its screen
|
|
3036
|
+
/// popped or unmounted), for the next field to take over.
|
|
3037
|
+
///
|
|
3038
|
+
/// UIKit hides the keyboard as soon as nothing is first responder and shows it
|
|
3039
|
+
/// again when a field claims it, even a moment later: a pop from one field's
|
|
3040
|
+
/// screen back to another's drops the keyboard and raises it again. A hidden
|
|
3041
|
+
/// stand-in field configured like the leaving one takes first responder in the
|
|
3042
|
+
/// same turn, so the keyboard stays; the next field to focus takes it from the
|
|
3043
|
+
/// stand-in in place. If none does within the hold, the stand-in lets it go.
|
|
3044
|
+
///
|
|
3045
|
+
/// The stand-in must stay invisible to anything tracking the focused input:
|
|
3046
|
+
/// keyboard-controller reads the first responder's superview `tag` as the
|
|
3047
|
+
/// focused input and swaps its own delegate into that field. So each hold
|
|
3048
|
+
/// builds a fresh field with no delegate inside a 1x1 container tagged `-1`,
|
|
3049
|
+
/// which keyboard-controller reads as "no focused input", and throws both away
|
|
3050
|
+
/// as soon as the stand-in stops being first responder.
|
|
3051
|
+
final class KeyboardHandoff {
|
|
3052
|
+
static let shared = KeyboardHandoff()
|
|
3053
|
+
|
|
3054
|
+
private var container: UIView?
|
|
3055
|
+
private var holder: UITextField?
|
|
3056
|
+
private var release: DispatchWorkItem?
|
|
3057
|
+
private var endObserver: NSObjectProtocol?
|
|
3058
|
+
/// Whether the system keyboard is up or on its way up.
|
|
3059
|
+
private(set) var keyboardVisible = false
|
|
3060
|
+
|
|
3061
|
+
var isHolding: Bool { holder?.isFirstResponder == true }
|
|
3062
|
+
|
|
3063
|
+
private init() {
|
|
3064
|
+
let center = NotificationCenter.default
|
|
3065
|
+
center.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: .main) { [weak self] _ in
|
|
3066
|
+
self?.keyboardVisible = true
|
|
3067
|
+
}
|
|
3068
|
+
center.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: .main) { [weak self] _ in
|
|
3069
|
+
self?.keyboardVisible = false
|
|
3070
|
+
}
|
|
3071
|
+
}
|
|
3072
|
+
|
|
3073
|
+
/// Takes the keyboard from `editor` (the leaving field's system field) for up
|
|
3074
|
+
/// to `milliseconds`.
|
|
3075
|
+
func hold(like editor: UIView, in window: UIWindow, for milliseconds: Double) {
|
|
3076
|
+
letGo()
|
|
3077
|
+
let box = UIView(frame: CGRect(x: -1000, y: -1000, width: 1, height: 1))
|
|
3078
|
+
box.tag = -1
|
|
3079
|
+
box.isUserInteractionEnabled = false
|
|
3080
|
+
box.accessibilityElementsHidden = true
|
|
3081
|
+
let field = UITextField(frame: box.bounds)
|
|
3082
|
+
field.alpha = 0.01
|
|
3083
|
+
field.isAccessibilityElement = false
|
|
3084
|
+
// The leaving field's own keyboard, as configured for its mode (a number
|
|
3085
|
+
// or mask field turns autocorrect and suggestions off): any difference
|
|
3086
|
+
// would re-lay the keyboard, and a suggestion bar that comes or goes
|
|
3087
|
+
// changes its height mid-hold.
|
|
3088
|
+
if let source = editor as? UITextInputTraits {
|
|
3089
|
+
field.keyboardType = source.keyboardType ?? .default
|
|
3090
|
+
field.keyboardAppearance = source.keyboardAppearance ?? .default
|
|
3091
|
+
field.returnKeyType = source.returnKeyType ?? .default
|
|
3092
|
+
field.autocapitalizationType = source.autocapitalizationType ?? .sentences
|
|
3093
|
+
field.autocorrectionType = source.autocorrectionType ?? .default
|
|
3094
|
+
field.spellCheckingType = source.spellCheckingType ?? .default
|
|
3095
|
+
field.smartQuotesType = source.smartQuotesType ?? .default
|
|
3096
|
+
field.smartDashesType = source.smartDashesType ?? .default
|
|
3097
|
+
field.smartInsertDeleteType = source.smartInsertDeleteType ?? .default
|
|
3098
|
+
field.textContentType = source.textContentType ?? nil
|
|
3099
|
+
field.isSecureTextEntry = source.isSecureTextEntry ?? false
|
|
3100
|
+
if #available(iOS 17.0, *) { field.inlinePredictionType = source.inlinePredictionType ?? .default }
|
|
3101
|
+
}
|
|
3102
|
+
field.inputAccessoryView = editor.inputAccessoryView
|
|
3103
|
+
box.addSubview(field)
|
|
3104
|
+
window.addSubview(box)
|
|
3105
|
+
guard field.becomeFirstResponder() else {
|
|
3106
|
+
box.removeFromSuperview()
|
|
3107
|
+
return
|
|
3108
|
+
}
|
|
3109
|
+
container = box
|
|
3110
|
+
holder = field
|
|
3111
|
+
// Another field took the keyboard: the hold is over.
|
|
3112
|
+
endObserver = NotificationCenter.default.addObserver(
|
|
3113
|
+
forName: UITextField.textDidEndEditingNotification, object: field, queue: .main
|
|
3114
|
+
) { [weak self, weak field] _ in
|
|
3115
|
+
guard let self, let field, self.holder === field else { return }
|
|
3116
|
+
self.drop()
|
|
3117
|
+
}
|
|
3118
|
+
let item = DispatchWorkItem { [weak self] in self?.letGo() }
|
|
3119
|
+
release = item
|
|
3120
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + milliseconds / 1000, execute: item)
|
|
3121
|
+
}
|
|
3122
|
+
|
|
3123
|
+
/// Nothing claimed the keyboard in time, or `Keyboard.dismiss()`: let it hide.
|
|
3124
|
+
func letGo() {
|
|
3125
|
+
if let holder, holder.isFirstResponder { holder.resignFirstResponder() }
|
|
3126
|
+
drop()
|
|
3127
|
+
}
|
|
3128
|
+
|
|
3129
|
+
private func drop() {
|
|
3130
|
+
release?.cancel()
|
|
3131
|
+
release = nil
|
|
3132
|
+
if let endObserver { NotificationCenter.default.removeObserver(endObserver) }
|
|
3133
|
+
endObserver = nil
|
|
3134
|
+
holder = nil
|
|
3135
|
+
container?.removeFromSuperview()
|
|
3136
|
+
container = nil
|
|
3137
|
+
}
|
|
3138
|
+
}
|