Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: key commands for external keyboards #243

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/InputBarAccessoryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ open class InputBarAccessoryView: UIView {
// MARK: - Auto-Layout Constraint Sets

private var middleContentViewLayoutSet: NSLayoutConstraintSet?
private var textViewHeightAnchor: NSLayoutConstraint?
open var textViewHeightAnchor: NSLayoutConstraint?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unintentional change as well?

private var topStackViewLayoutSet: NSLayoutConstraintSet?
private var leftStackViewLayoutSet: NSLayoutConstraintSet?
private var rightStackViewLayoutSet: NSLayoutConstraintSet?
Expand Down
136 changes: 68 additions & 68 deletions Sources/KeyboardManager/KeyboardManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,84 +158,84 @@ open class KeyboardManager: NSObject, UIGestureRecognizerDelegate {
///
/// - Parameter inputAccessoryView: The view to bind to the top of the keyboard but within its superview
/// - Returns: Self
@discardableResult
open func bind(inputAccessoryView: UIView, withAdditionalBottomSpace additionalBottomSpace: (() -> CGFloat)? = .none) -> Self {

guard let superview = inputAccessoryView.superview else {
fatalError("`inputAccessoryView` must have a superview")
}
self.inputAccessoryView = inputAccessoryView
self.additionalBottomSpace = additionalBottomSpace
inputAccessoryView.translatesAutoresizingMaskIntoConstraints = false
constraints = NSLayoutConstraintSet(
bottom: inputAccessoryView.bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: additionalInputViewBottomConstraintConstant()),
left: inputAccessoryView.leftAnchor.constraint(equalTo: superview.leftAnchor),
right: inputAccessoryView.rightAnchor.constraint(equalTo: superview.rightAnchor)
).activate()

callbacks[.willShow] = { [weak self] (notification) in
guard
self?.isKeyboardHidden == false,
self?.constraints?.bottom?.constant == self?.additionalInputViewBottomConstraintConstant(),
notification.isForCurrentApp
else { return }
@discardableResult
open func bind(inputAccessoryView: UIView, withAdditionalBottomSpace additionalBottomSpace: (() -> CGFloat)? = .none) -> Self {

let keyboardHeight = notification.endFrame.height
let animateAlongside = {
self?.animateAlongside(notification) {
self?.constraints?.bottom?.constant = min(0, -keyboardHeight + (self?.bottomGap ?? 0)) - (additionalBottomSpace?() ?? 0)
self?.inputAccessoryView?.superview?.layoutIfNeeded()
}
guard let superview = inputAccessoryView.superview else {
fatalError("`inputAccessoryView` must have a superview")
}
animateAlongside()

// Trigger a new animation if gap changed, this typically happens when using pagesheet on portrait iPad
let initialBottomGap = self?.bottomGap ?? 0
DispatchQueue.main.async {
let newBottomGap = self?.bottomGap ?? 0
if newBottomGap != 0 && newBottomGap != initialBottomGap {
animateAlongside()
self.inputAccessoryView = inputAccessoryView
self.additionalBottomSpace = additionalBottomSpace
inputAccessoryView.translatesAutoresizingMaskIntoConstraints = false
constraints = NSLayoutConstraintSet(
bottom: inputAccessoryView.bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: additionalInputViewBottomConstraintConstant()),
left: inputAccessoryView.leftAnchor.constraint(equalTo: superview.leftAnchor),
right: inputAccessoryView.rightAnchor.constraint(equalTo: superview.rightAnchor)
).activate()

callbacks[.willShow] = { [weak self] (notification) in
guard
self?.isKeyboardHidden == false,
self?.constraints?.bottom?.constant == self?.additionalInputViewBottomConstraintConstant(),
notification.isForCurrentApp
else { return }

let keyboardHeight = notification.endFrame.height
let animateAlongside = {
self?.animateAlongside(notification) {
self?.constraints?.bottom?.constant = min(0, -keyboardHeight + (self?.bottomGap ?? 0)) - (additionalBottomSpace?() ?? 0)
self?.inputAccessoryView?.superview?.layoutIfNeeded()
}
}
animateAlongside()

// Trigger a new animation if gap changed, this typically happens when using pagesheet on portrait iPad
let initialBottomGap = self?.bottomGap ?? 0
DispatchQueue.main.async {
let newBottomGap = self?.bottomGap ?? 0
if newBottomGap != 0 && newBottomGap != initialBottomGap {
animateAlongside()
}
}
}
}
callbacks[.willChangeFrame] = { [weak self] (notification) in
let keyboardHeight = notification.endFrame.height
guard
self?.isKeyboardHidden == false,
notification.isForCurrentApp
else {
return
callbacks[.willChangeFrame] = { [weak self] (notification) in
let keyboardHeight = notification.endFrame.height
guard
self?.isKeyboardHidden == false,
notification.isForCurrentApp
else {
return
}
let animateAlongside = {
self?.animateAlongside(notification) {
self?.constraints?.bottom?.constant = min(0, -keyboardHeight + (self?.bottomGap ?? 0)) - (additionalBottomSpace?() ?? 0)
self?.inputAccessoryView?.superview?.layoutIfNeeded()
}
}
animateAlongside()

// Trigger a new animation if gap changed, this typically happens when using pagesheet on portrait iPad
let initialBottomGap = self?.bottomGap ?? 0
DispatchQueue.main.async {
let newBottomGap = self?.bottomGap ?? 0
if newBottomGap != 0 && newBottomGap != initialBottomGap && !(self?.justDidWillHide ?? false) {
animateAlongside()
}
}
}
let animateAlongside = {
self?.animateAlongside(notification) {
self?.constraints?.bottom?.constant = min(0, -keyboardHeight + (self?.bottomGap ?? 0)) - (additionalBottomSpace?() ?? 0)
callbacks[.willHide] = { [weak self] (notification) in
guard notification.isForCurrentApp else { return }
self?.justDidWillHide = true
self?.animateAlongside(notification) { [weak self] in
self?.constraints?.bottom?.constant = self?.additionalInputViewBottomConstraintConstant() ?? 0
self?.inputAccessoryView?.superview?.layoutIfNeeded()
}
}
animateAlongside()

// Trigger a new animation if gap changed, this typically happens when using pagesheet on portrait iPad
let initialBottomGap = self?.bottomGap ?? 0
DispatchQueue.main.async {
let newBottomGap = self?.bottomGap ?? 0
if newBottomGap != 0 && newBottomGap != initialBottomGap && !(self?.justDidWillHide ?? false) {
animateAlongside()
DispatchQueue.main.async {
self?.justDidWillHide = false
}
}
return self
}
callbacks[.willHide] = { [weak self] (notification) in
guard notification.isForCurrentApp else { return }
self?.justDidWillHide = true
self?.animateAlongside(notification) { [weak self] in
self?.constraints?.bottom?.constant = self?.additionalInputViewBottomConstraintConstant() ?? 0
self?.inputAccessoryView?.superview?.layoutIfNeeded()
}
DispatchQueue.main.async {
self?.justDidWillHide = false
}
}
return self
}

/// Adds a `UIPanGestureRecognizer` to the `scrollView` to enable interactive dismissal`
///
Expand Down
31 changes: 31 additions & 0 deletions Sources/Views/InputTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ open class InputTextView: UITextView {
}
}

open var registeredCommands: [UIKeyCommand] = []
open var registeredKeyCallbacks: [String: () -> Void] = [:]

/// A weak reference to the InputBarAccessoryView that the InputTextView is contained within
open weak var inputBarAccessoryView: InputBarAccessoryView?

Expand Down Expand Up @@ -231,6 +234,34 @@ open class InputTextView: UITextView {
}
}

// MARK: - Key Command

open override var keyCommands: [UIKeyCommand]? {
return registeredCommands
}

open func observeKeyInput(_ input: String, modifiers: UIKeyModifierFlags, discoverabilityTitle: String?, callback: @escaping (() -> Void)) {
if input == "" {
return
}
let keyCommand = UIKeyCommand(input: input, modifierFlags: modifiers, action: #selector(didDetectKeyCommand(_:)))
keyCommand.discoverabilityTitle = discoverabilityTitle
if #available(iOS 15.0, *) {
keyCommand.wantsPriorityOverSystemBehavior = true
}

let key = "\(input)_\(modifiers)"
registeredCommands.append(keyCommand)
registeredKeyCallbacks[key] = callback
}

@objc private func didDetectKeyCommand(_ keyCommand: UIKeyCommand) {
if keyCommand.input == nil { return }
let key = "\(String(describing: keyCommand.input!))_\(keyCommand.modifierFlags)"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big fan of this force unwrap - can we figure out better way for the key?

let callback = registeredKeyCallbacks[key]
callback?()
}

// MARK: - Image Paste Support

open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
Expand Down