react-native-nitro-input 0.3.2 → 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.
Files changed (30) hide show
  1. package/CHANGELOG.md +83 -0
  2. package/README.md +45 -0
  3. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +25 -4
  4. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +170 -2
  5. package/android/src/main/java/com/margelo/nitro/nitroinput/UnmountHandoffs.kt +57 -0
  6. package/ios/HybridNitroInputView.swift +19 -4
  7. package/ios/NitroInputCommands.mm +10 -0
  8. package/ios/NitroInputView.swift +349 -13
  9. package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +2 -0
  10. package/lib/commonjs/src/NitroInput.js +103 -15
  11. package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +2 -0
  12. package/lib/module/src/NitroInput.js +104 -16
  13. package/lib/typescript/src/NitroInput.d.ts +16 -0
  14. package/lib/typescript/src/specs/NitroInput.nitro.d.ts +22 -0
  15. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +22 -0
  16. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +5 -0
  17. package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +10 -0
  18. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +16 -0
  19. package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +18 -0
  20. package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +12 -0
  21. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +3 -0
  22. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +33 -0
  23. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +5 -0
  24. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +5 -0
  25. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +4 -0
  26. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +6 -0
  27. package/nitrogen/generated/shared/json/NitroInputViewConfig.json +2 -0
  28. package/package.json +1 -1
  29. package/src/NitroInput.tsx +117 -11
  30. package/src/specs/NitroInput.nitro.ts +22 -0
@@ -185,6 +185,11 @@ final class NitroInputView: UIView {
185
185
  var showSoftInputOnFocus: Bool = true
186
186
  var selectTextOnFocus: Bool = false
187
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
188
193
  var contextMenuHidden: Bool = false
189
194
  var spellCheck: Bool = true
190
195
  /// `testID` and `accessibilityLabel`, forwarded from JS so the hidden
@@ -599,6 +604,19 @@ final class NitroInputView: UIView {
599
604
  /// A narrower settled size waiting for the current reflow to finish before it is reported.
600
605
  private var pendingSizeReport = false
601
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
602
620
  /// Set while this view writes the field's text itself, so the change handler
603
621
  /// can tell a programmatic set from a user edit.
604
622
  private var isSettingText = false
@@ -620,6 +638,8 @@ final class NitroInputView: UIView {
620
638
  }()
621
639
  /// True once `textView` has been built; reading the lazy var would build it.
622
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?
623
643
  /// The width the multiline height was last measured against.
624
644
  private var lastMeasuredWidth: CGFloat = 0
625
645
  /// Scaled and aligned container for the content (font space).
@@ -657,6 +677,8 @@ final class NitroInputView: UIView {
657
677
 
658
678
  override init(frame: CGRect) {
659
679
  super.init(frame: frame)
680
+ // Starts it tracking the keyboard before any field needs to ask.
681
+ _ = KeyboardHandoff.shared
660
682
  isOpaque = false
661
683
  backgroundColor = .clear
662
684
  clipsToBounds = false
@@ -816,9 +838,115 @@ final class NitroInputView: UIView {
816
838
  // The fonts were built against whatever traits the view had when it was
817
839
  // made; the window's are the ones that count.
818
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
+ }
819
865
  maybeAutoFocus()
820
866
  }
821
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
+
822
950
  override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
823
951
  super.traitCollectionDidChange(previousTraitCollection)
824
952
  let appearanceChanged = previousTraitCollection.map { traitCollection.hasDifferentColorAppearance(comparedTo: $0) } ?? true
@@ -863,11 +991,7 @@ final class NitroInputView: UIView {
863
991
  private func maybeAutoFocus() {
864
992
  guard window != nil, traits.autoFocus, traits.editable, !didAutoFocus else { return }
865
993
  didAutoFocus = true
866
- if editor.becomeFirstResponder() { return }
867
- DispatchQueue.main.async { [weak self] in
868
- guard let self, self.window != nil else { return }
869
- self.editor.becomeFirstResponder()
870
- }
994
+ claimFirstResponder()
871
995
  }
872
996
 
873
997
  @objc private func contentSizeCategoryDidChange() {
@@ -885,6 +1009,10 @@ final class NitroInputView: UIView {
885
1009
  func resetForRecycle() {
886
1010
  worklets = Worklets()
887
1011
  if editor.isFirstResponder { editor.resignFirstResponder() }
1012
+ pendingFocusUntil = 0
1013
+ restoreOnReturn = false
1014
+ endedEditingAt = 0
1015
+ focusAfterTransition = false
888
1016
  stopDisplayLink()
889
1017
  stopBlink()
890
1018
  engine.reset()
@@ -914,13 +1042,32 @@ final class NitroInputView: UIView {
914
1042
 
915
1043
  // MARK: - Public API
916
1044
 
917
- func focus() {
1045
+ @objc(nitroFocus) func focus() {
918
1046
  guard traits.editable else { return }
919
- editor.becomeFirstResponder()
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()
920
1057
  }
921
1058
 
922
- func blur() {
923
- editor.resignFirstResponder()
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
+ }
924
1071
  }
925
1072
 
926
1073
  /// Moves the caret/selection to `start`..`end`, in code points.
@@ -1163,7 +1310,13 @@ final class NitroInputView: UIView {
1163
1310
  textView.isUserInteractionEnabled = on && traits.editable
1164
1311
  field.isHidden = on
1165
1312
  field.isUserInteractionEnabled = !on && traits.editable
1166
- guard on else { return }
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
+ }
1167
1320
 
1168
1321
  textView.frame = bounds
1169
1322
  textView.keyboardType = traits.keyboardType
@@ -1296,7 +1449,8 @@ final class NitroInputView: UIView {
1296
1449
  applyMultiline()
1297
1450
  field.keyboardType = traits.keyboardType
1298
1451
  field.returnKeyType = traits.returnKeyType
1299
- field.isEnabled = traits.editable
1452
+ applyReturnAccessory()
1453
+ field.isEnabled = traits.editable && !traits.multiline
1300
1454
  field.isUserInteractionEnabled = traits.editable
1301
1455
  // `tintColor` is both the caret and the selection on a UITextField. The
1302
1456
  // overlay draws its own caret, so only `plain` has a caret colour to honour
@@ -1373,6 +1527,53 @@ final class NitroInputView: UIView {
1373
1527
  }
1374
1528
  }
1375
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
+
1376
1577
  private func applyKeyboardTraitsForMode() {
1377
1578
  // A mask decides every character, so the keyboard must not second-guess it:
1378
1579
  // autocorrect would fight `shouldChangeCharactersIn` over the replacement.
@@ -2008,6 +2209,11 @@ final class NitroInputView: UIView {
2008
2209
  */
2009
2210
  fileprivate final class HiddenTextView: UITextView {
2010
2211
  weak var owner: NitroInputView?
2212
+
2213
+ override func becomeFirstResponder() -> Bool {
2214
+ if owner?.deferFocusWhileTransitioning() == true { return false }
2215
+ return super.becomeFirstResponder()
2216
+ }
2011
2217
  var placeholder: String = "" { didSet { setNeedsDisplay() } }
2012
2218
  var placeholderColor: UIColor = .placeholderText { didSet { setNeedsDisplay() } }
2013
2219
 
@@ -2026,7 +2232,10 @@ final class NitroInputView: UIView {
2026
2232
  override func caretRect(for position: UITextPosition) -> CGRect {
2027
2233
  let rect = super.caretRect(for: position)
2028
2234
  guard hidesNativeCaret else { return rect }
2029
- return CGRect(origin: rect.origin, size: .zero)
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)
2030
2239
  }
2031
2240
 
2032
2241
  var hidesNativeCaret = false {
@@ -2068,6 +2277,11 @@ final class NitroInputView: UIView {
2068
2277
 
2069
2278
  fileprivate final class HiddenTextField: UITextField {
2070
2279
  weak var owner: NitroInputView?
2280
+
2281
+ override func becomeFirstResponder() -> Bool {
2282
+ if owner?.deferFocusWhileTransitioning() == true { return false }
2283
+ return super.becomeFirstResponder()
2284
+ }
2071
2285
  var leftInset: CGFloat = 0
2072
2286
  var rightInset: CGFloat = 0
2073
2287
  /// Room for a floated label sitting on the top edge.
@@ -2112,7 +2326,10 @@ final class NitroInputView: UIView {
2112
2326
  override func caretRect(for position: UITextPosition) -> CGRect {
2113
2327
  let rect = super.caretRect(for: position)
2114
2328
  guard hidesNativeCaret else { return rect }
2115
- return CGRect(origin: rect.origin, size: .zero)
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)
2116
2333
  }
2117
2334
 
2118
2335
  override func textRect(forBounds bounds: CGRect) -> CGRect {
@@ -2201,6 +2418,9 @@ extension NitroInputView: UITextViewDelegate {
2201
2418
  }
2202
2419
 
2203
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()
2204
2424
  // The text view owns the text here. `setText` runs it through the
2205
2425
  // formatter, which is single-line by definition, so the multiline path
2206
2426
  // reports what the view already holds instead.
@@ -2334,6 +2554,15 @@ extension NitroInputView: UITextFieldDelegate {
2334
2554
  }
2335
2555
 
2336
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
+ }
2337
2566
  if traits.clearTextOnFocus {
2338
2567
  setText("", reason: .user)
2339
2568
  } else if traits.selectTextOnFocus {
@@ -2353,6 +2582,7 @@ extension NitroInputView: UITextFieldDelegate {
2353
2582
  }
2354
2583
 
2355
2584
  func textFieldDidEndEditing(_ textField: UITextField) {
2585
+ if window != nil { endedEditingAt = CACurrentMediaTime() }
2356
2586
  if contentOverflows { render() }
2357
2587
  updateCaret()
2358
2588
  if worklets.onFocusChange != 0 {
@@ -2800,3 +3030,109 @@ extension NitroInputView {
2800
3030
  }
2801
3031
  }
2802
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
+ }
@@ -85,6 +85,8 @@
85
85
  "showSoftInputOnFocus": true,
86
86
  "selectTextOnFocus": true,
87
87
  "clearTextOnFocus": true,
88
+ "keyboardHandoffMs": true,
89
+ "returnKeyBar": true,
88
90
  "contextMenuHidden": true,
89
91
  "spellCheck": true,
90
92
  "selectionStart": true,