Fix caret not scrolling into view when focusing compose text views

Closes #233
This commit is contained in:
Shadowfacts 2022-11-09 17:18:17 -05:00
parent 01124b76a3
commit 9a43ab5a13
3 changed files with 23 additions and 1 deletions

View File

@ -82,6 +82,7 @@ struct WrappedTextView: UIViewRepresentable {
func updateUIView(_ uiView: UITextView, context: Context) { func updateUIView(_ uiView: UITextView, context: Context) {
uiView.text = text uiView.text = text
context.coordinator.textView = uiView
context.coordinator.text = $text context.coordinator.text = $text
context.coordinator.didChange = textDidChange context.coordinator.didChange = textDidChange
// wait until the next runloop iteration so that SwiftUI view updates have finished and // wait until the next runloop iteration so that SwiftUI view updates have finished and
@ -96,6 +97,7 @@ struct WrappedTextView: UIViewRepresentable {
} }
class Coordinator: NSObject, UITextViewDelegate, ComposeTextViewCaretScrolling { class Coordinator: NSObject, UITextViewDelegate, ComposeTextViewCaretScrolling {
weak var textView: UITextView?
var text: Binding<String> var text: Binding<String>
var didChange: ((UITextView) -> Void)? var didChange: ((UITextView) -> Void)?
var caretScrollPositionAnimator: UIViewPropertyAnimator? var caretScrollPositionAnimator: UIViewPropertyAnimator?
@ -103,6 +105,16 @@ struct WrappedTextView: UIViewRepresentable {
init(text: Binding<String>, didChange: ((UITextView) -> Void)?) { init(text: Binding<String>, didChange: ((UITextView) -> Void)?) {
self.text = text self.text = text
self.didChange = didChange self.didChange = didChange
super.init()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)
}
@objc private func keyboardDidShow() {
guard let textView,
textView.isFirstResponder else { return }
ensureCursorVisible(textView: textView)
} }
func textViewDidChange(_ textView: UITextView) { func textViewDidChange(_ textView: UITextView) {

View File

@ -37,7 +37,7 @@ extension ComposeTextViewCaretScrolling {
rectToMakeVisible.origin.y -= cursorRect.height rectToMakeVisible.origin.y -= cursorRect.height
rectToMakeVisible.size.height *= 3 rectToMakeVisible.size.height *= 3
let animator = UIViewPropertyAnimator(duration: 0.1, curve: .linear) { let animator = UIViewPropertyAnimator(duration: 0.2, curve: .easeInOut) {
scrollView.scrollRectToVisible(rectToMakeVisible, animated: false) scrollView.scrollRectToVisible(rectToMakeVisible, animated: false)
} }
self.caretScrollPositionAnimator = animator self.caretScrollPositionAnimator = animator

View File

@ -168,6 +168,16 @@ struct MainComposeWrappedTextView: UIViewRepresentable {
self.text = text self.text = text
self.didChange = didChange self.didChange = didChange
self.uiState = uiState self.uiState = uiState
super.init()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)
}
@objc private func keyboardDidShow() {
guard let textView,
textView.isFirstResponder else { return }
ensureCursorVisible(textView: textView)
} }
func textViewDidChange(_ textView: UITextView) { func textViewDidChange(_ textView: UITextView) {