Skip to content

Commit

Permalink
Merge branch 'mlin/PR/IOS-838' into release/5.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
michelle-signal committed Dec 10, 2020
2 parents bdf03e6 + c6004c9 commit ee924d5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ConversationInputTextView: MentionTextView {

var trimmedText: String { text.ows_stripped() }
var untrimmedText: String { text }
private var textIsChanging = false

required init() {
super.init()
Expand Down Expand Up @@ -114,10 +115,22 @@ class ConversationInputTextView: MentionTextView {

override func setContentOffset(_ contentOffset: CGPoint, animated: Bool) {
// When creating new lines, contentOffset is animated, but because because
// we are simultaneously resizing the text view, this can cause the
// text in the textview to be "too high" in the text view.
// Solution is to disable animation for setting content offset.
super.setContentOffset(contentOffset, animated: false)
// we are simultaneously resizing the text view, on pre-iOS 13 this can
// cause the text in the textview to be "too high" in the text view.
// Solution is to disable animation for setting content offset between
// -textViewShouldChange... and -textViewDidChange.
//
// We can't unilaterally disable *all* animated scrolling because that breaks
// manipulation of the cursor in scrollable text. Animation is required to
// slow the text view scrolling down to human scale when the cursor reaches
// the top or bottom edge.
let shouldAnimate: Bool
if #available(iOS 13, *) {
shouldAnimate = animated
} else {
shouldAnimate = animated && !textIsChanging
}
super.setContentOffset(contentOffset, animated: shouldAnimate)
}

override var contentInset: UIEdgeInsets {
Expand Down Expand Up @@ -169,8 +182,14 @@ class ConversationInputTextView: MentionTextView {

// MARK: - UITextViewDelegate

override func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
textIsChanging = true
return super.textView(self, shouldChangeTextIn: range, replacementText: text)
}

override func textViewDidChange(_ textView: UITextView) {
super.textViewDidChange(textView)
textIsChanging = false

updatePlaceholderVisibility()
updateTextContainerInset()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ typedef NS_CLOSED_ENUM(NSUInteger, VoiceMemoRecordingState){

typedef NS_CLOSED_ENUM(NSUInteger, KeyboardType) { KeyboardType_System, KeyboardType_Sticker, KeyboardType_Attachment };

static void *kConversationInputTextViewObservingContext = &kConversationInputTextViewObservingContext;

const CGFloat kMinTextViewHeight = 36;
const CGFloat kMinToolbarItemHeight = 44;
const CGFloat kMaxTextViewHeight = 98;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,36 @@ import Foundation
import UIKit

class AttachmentTextView: MentionTextView {
// When creating new lines, contentOffset is animated, but because
// we are simultaneously resizing the text view, this can cause the
// text in the textview to be "too high" in the text view.
// Solution is to disable animation for setting content offset.

private var textIsChanging = false

override func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
textIsChanging = true
return super.textView(self, shouldChangeTextIn: range, replacementText: text)
}

override func textViewDidChange(_ textView: UITextView) {
super.textViewDidChange(textView)
textIsChanging = false
}

override func setContentOffset(_ contentOffset: CGPoint, animated: Bool) {
super.setContentOffset(contentOffset, animated: false)
// When creating new lines, contentOffset is animated, but because because
// we are simultaneously resizing the text view, on pre-iOS 13 this can
// cause the text in the textview to be "too high" in the text view.
// Solution is to disable animation for setting content offset between
// -textViewShouldChange... and -textViewDidChange.
//
// We can't unilaterally disable *all* animated scrolling because that breaks
// manipulation of the cursor in scrollable text. Animation is required to
// slow the text view scrolling down to human scale when the cursor reaches
// the top or bottom edge.
let shouldAnimate: Bool
if #available(iOS 13, *) {
shouldAnimate = animated
} else {
shouldAnimate = animated && !textIsChanging
}
super.setContentOffset(contentOffset, animated: shouldAnimate)
}
}

0 comments on commit ee924d5

Please sign in to comment.