Skip to content

Commit

Permalink
fix: intercept responder events instead of keyboard events
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillzyusko committed Jan 15, 2025
1 parent 6da1bb4 commit 046db2c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ios/extensions/Notification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ extension Notification {

extension Notification.Name {
static let shouldIgnoreKeyboardEvents = Notification.Name("shouldIgnoreKeyboardEvents")
static let didBecomeFirstResponder = Notification.Name("didBecomeFirstResponder")
static let didResignFirstResponder = Notification.Name("didResignFirstResponder")
}
4 changes: 2 additions & 2 deletions ios/observers/FocusedInputObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ public class FocusedInputObserver: NSObject {
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillShow),
name: UIResponder.keyboardWillShowNotification,
name: .didBecomeFirstResponder,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillHide),
name: UIResponder.keyboardWillHideNotification,
name: .didResignFirstResponder,
object: nil
)
}
Expand Down
39 changes: 39 additions & 0 deletions ios/swizzling/UIResponderSwizzle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation
import UIKit

private var originalResignFirstResponder: IMP?
private var originalBecomeFirstResponder: IMP?

@objc
extension UIResponder {
Expand Down Expand Up @@ -55,6 +56,44 @@ extension UIResponder {
typealias Function = @convention(c) (AnyObject, Selector) -> Bool
let castOriginalResignFirstResponder = unsafeBitCast(originalResignFirstResponder, to: Function.self)
let result = castOriginalResignFirstResponder(self, selector)

Check failure on line 59 in ios/swizzling/UIResponderSwizzle.swift

View workflow job for this annotation

GitHub Actions / 🔎 Swift Lint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
if result {
NotificationCenter.default.post(name: .didResignFirstResponder, object: self)
}

Check failure on line 63 in ios/swizzling/UIResponderSwizzle.swift

View workflow job for this annotation

GitHub Actions / 🔎 Swift Lint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
return result
}

Check failure on line 66 in ios/swizzling/UIResponderSwizzle.swift

View workflow job for this annotation

GitHub Actions / 🔎 Swift Lint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
public static func swizzleBecomeFirstResponder() {
let originalSelector = #selector(becomeFirstResponder)

Check failure on line 69 in ios/swizzling/UIResponderSwizzle.swift

View workflow job for this annotation

GitHub Actions / 🔎 Swift Lint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
guard let originalMethod = class_getInstanceMethod(UIResponder.self, originalSelector) else {
return
}

originalBecomeFirstResponder = method_getImplementation(originalMethod)

let swizzledImplementation: @convention(block) (UIResponder) -> Bool = { (self) in
// Call the original implementation
let didBecomeFirstResponder = self.callOriginalBecomeFirstResponder(originalSelector)

Check failure on line 79 in ios/swizzling/UIResponderSwizzle.swift

View workflow job for this annotation

GitHub Actions / 🔎 Swift Lint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
if didBecomeFirstResponder {
// Post a notification when the responder becomes first responder
NotificationCenter.default.post(name: .didBecomeFirstResponder, object: self)
}

Check failure on line 84 in ios/swizzling/UIResponderSwizzle.swift

View workflow job for this annotation

GitHub Actions / 🔎 Swift Lint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
return didBecomeFirstResponder
}

let implementation = imp_implementationWithBlock(swizzledImplementation)
method_setImplementation(originalMethod, implementation)
}

private func callOriginalBecomeFirstResponder(_ selector: Selector) -> Bool {
guard let originalBecomeFirstResponder = originalBecomeFirstResponder else { return false }
typealias Function = @convention(c) (AnyObject, Selector) -> Bool
let castOriginalBecomeFirstResponder = unsafeBitCast(originalBecomeFirstResponder, to: Function.self)
let result = castOriginalBecomeFirstResponder(self, selector)
return result
}
}

0 comments on commit 046db2c

Please sign in to comment.