Skip to content

Commit

Permalink
Using text change notification instead of observing content size chan…
Browse files Browse the repository at this point in the history
…ges to recalculateSize so that the addition of newlines affect scrollEnabled as soon as aplicable
  • Loading branch information
rajdeep committed Aug 21, 2024
1 parent 37f992c commit 869a13e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions Proton/Sources/Swift/Base/AutogrowingTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class AutogrowingTextView: UITextView {
}
//TODO: enable only when line numbering is turned on
contentMode = .redraw

NotificationCenter.default.addObserver(
self,
selector: #selector(textViewTextDidChange(_:)),
name: UITextView.textDidChangeNotification,
object: self
)
}

required init?(coder: NSCoder) {
Expand All @@ -63,16 +70,11 @@ class AutogrowingTextView: UITextView {
}
}

override var contentSize: CGSize {
didSet {
guard oldValue != .zero,
contentSize != .zero,
oldValue != contentSize else { return }
// Entering a newline char may not always cause layout(super.layoutSubviews) to take place
// This code is required to be run so that the isScrollEnabled state can be correctly calculated based
// on the content size.
recalculateHeightIfRequired()
}
@objc func textViewTextDidChange(_ notification: Notification) {
// Entering a newline char may not always cause layout(super.layoutSubviews) to take place
// This code is required to be run so that the isScrollEnabled state can be correctly calculated based
// on the content size.
setNeedsLayout()
}

// Expose this method to the Objective-C runtime so clients
Expand Down Expand Up @@ -136,6 +138,14 @@ class AutogrowingTextView: UITextView {
self.becomeFirstResponder()
}

deinit {
NotificationCenter.default.removeObserver(
self,
name: UITextView.textDidChangeNotification,
object: self
)
}

private func calculatedSize(attributedText: NSAttributedString, frame: CGSize, textContainerInset: UIEdgeInsets) -> CGSize {
DispatchQueue.global(qos: .userInteractive).sync { [lineFragmentPadding = textContainer.lineFragmentPadding ] in
// Adjust for horizontal paddings in textview to exclude from overall available width for attachment
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 869a13e

Please sign in to comment.