@onekeyfe/react-native-auto-size-input 3.0.32 → 3.0.33

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.
@@ -5,6 +5,7 @@ import android.graphics.Paint
5
5
  import android.graphics.Rect
6
6
  import android.graphics.Typeface
7
7
  import android.graphics.drawable.GradientDrawable
8
+ import android.os.Looper
8
9
  import android.text.Editable
9
10
  import android.text.InputType
10
11
  import android.view.inputmethod.EditorInfo
@@ -753,16 +754,36 @@ class HybridAutoSizeInput(val context: ThemedReactContext) : HybridAutoSizeInput
753
754
 
754
755
  // MARK: - Methods
755
756
 
757
+ // Nitro hybrid view methods may be invoked off the UI thread. Android view
758
+ // operations (`requestFocus` / `clearFocus`) and `InputMethodManager` calls
759
+ // must run on the view's looper — otherwise we hit
760
+ // `CalledFromWrongThreadException`. `View.post` is thread-safe and queues
761
+ // onto the main looper.
762
+ private fun runOnViewThread(action: () -> Unit) {
763
+ if (Looper.myLooper() == Looper.getMainLooper()) {
764
+ action()
765
+ } else {
766
+ inputView.post {
767
+ if (isDisposed) return@post
768
+ action()
769
+ }
770
+ }
771
+ }
772
+
756
773
  override fun focus() {
757
774
  if (isDisposed) return
758
- requestInputFocus()
775
+ runOnViewThread {
776
+ requestInputFocus()
777
+ }
759
778
  }
760
779
 
761
780
  override fun blur() {
762
781
  if (isDisposed) return
763
- inputView.clearFocus()
764
- val imm = context.getSystemService(android.content.Context.INPUT_METHOD_SERVICE) as? android.view.inputmethod.InputMethodManager
765
- imm?.hideSoftInputFromWindow(inputView.windowToken, 0)
782
+ runOnViewThread {
783
+ inputView.clearFocus()
784
+ val imm = context.getSystemService(android.content.Context.INPUT_METHOD_SERVICE) as? android.view.inputmethod.InputMethodManager
785
+ imm?.hideSoftInputFromWindow(inputView.windowToken, 0)
786
+ }
766
787
  }
767
788
 
768
789
  // MARK: - Helpers
@@ -509,19 +509,37 @@ class HybridAutoSizeInput: HybridAutoSizeInputSpec {
509
509
 
510
510
  // MARK: - Methods
511
511
 
512
- func focus() throws {
513
- if multiline == true {
514
- multiLineInput.becomeFirstResponder()
512
+ // Nitro hybrid view methods are invoked on the JS thread. UIKit responder
513
+ // changes (becomeFirstResponder / resignFirstResponder) must run on the
514
+ // main thread — otherwise FrontBoardServices' `assertBarrierOnQueue`
515
+ // trips and the process is killed with SIGTRAP.
516
+ private func runOnMain(_ block: @escaping () -> Void) {
517
+ if Thread.isMainThread {
518
+ block()
515
519
  } else {
516
- singleLineInput.becomeFirstResponder()
520
+ DispatchQueue.main.async(execute: block)
521
+ }
522
+ }
523
+
524
+ func focus() throws {
525
+ runOnMain { [weak self] in
526
+ guard let self else { return }
527
+ if self.multiline == true {
528
+ self.multiLineInput.becomeFirstResponder()
529
+ } else {
530
+ self.singleLineInput.becomeFirstResponder()
531
+ }
517
532
  }
518
533
  }
519
534
 
520
535
  func blur() throws {
521
- if multiline == true {
522
- multiLineInput.resignFirstResponder()
523
- } else {
524
- singleLineInput.resignFirstResponder()
536
+ runOnMain { [weak self] in
537
+ guard let self else { return }
538
+ if self.multiline == true {
539
+ self.multiLineInput.resignFirstResponder()
540
+ } else {
541
+ self.singleLineInput.resignFirstResponder()
542
+ }
525
543
  }
526
544
  }
527
545
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-auto-size-input",
3
- "version": "3.0.32",
3
+ "version": "3.0.33",
4
4
  "description": "Auto-sizing text input with font scaling, prefix and suffix support",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",