Skip to content

Commit

Permalink
Allow changing allowAutogrowing after init
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdeep committed Sep 23, 2024
1 parent 46db750 commit 5eb9d29
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
33 changes: 24 additions & 9 deletions Proton/Sources/Swift/Base/AutogrowingTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,15 @@ class AutogrowingTextView: UITextView {
private var allowAutogrowing: Bool
weak var boundsObserver: BoundsObserving?
private var maxHeightConstraint: NSLayoutConstraint!
private var heightAnchorConstraint: NSLayoutConstraint!
private var heightAnchorConstraint: NSLayoutConstraint?
private var isSizeRecalculationRequired = true

init(frame: CGRect = .zero, textContainer: NSTextContainer? = nil, allowAutogrowing: Bool = false) {
self.allowAutogrowing = allowAutogrowing
super.init(frame: frame, textContainer: textContainer)
isScrollEnabled = false
setAutogrowing(allowAutogrowing)

if allowAutogrowing {
heightAnchorConstraint = heightAnchor.constraint(greaterThanOrEqualToConstant: contentSize.height)
heightAnchorConstraint.priority = .defaultHigh

NSLayoutConstraint.activate([
heightAnchorConstraint
])
}
//TODO: enable only when line numbering is turned on
contentMode = .redraw
}
Expand All @@ -51,6 +44,28 @@ class AutogrowingTextView: UITextView {
fatalError("init(coder:) has not been implemented")
}

func setAutogrowing(_ isAutogrowing: Bool) {
allowAutogrowing = isAutogrowing

if allowAutogrowing {
if heightAnchorConstraint == nil {
let heightConstraint = heightAnchor.constraint(greaterThanOrEqualToConstant: contentSize.height)
heightAnchorConstraint = heightConstraint
heightAnchorConstraint?.priority = .defaultHigh

NSLayoutConstraint.activate([
heightConstraint
])
}
} else {
isScrollEnabled = false
if let heightAnchorConstraint {
NSLayoutConstraint.deactivate([heightAnchorConstraint])
}
heightAnchorConstraint = nil
}
}

override func layoutSubviews() {
super.layoutSubviews()
guard allowAutogrowing, maxHeight != .greatestFiniteMagnitude else { return }
Expand Down
9 changes: 9 additions & 0 deletions Proton/Sources/Swift/Editor/EditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,15 @@ open class EditorView: UIView {
richTextView.recalculateHeight(size: size)
}

/// Set the behavior for how Editor size would be updated based on content
/// - Parameter isAutogrowing: When `true`, uses custom calculation and constrains to size editor based on content. This is typically the case where
/// Editor is scrollable and needs to be confined to certain size using applied constraints. Use `false` in case Editor is itself non-scrollable but is hosted within
/// another scroll container. This will use iOS's internal logic for sizing the Editor based on the height of the content and is generally better performing.
public func setAutogrowing(_ isAutogrowing: Bool) {
richTextView.setAutogrowing(isAutogrowing)
}


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

0 comments on commit 5eb9d29

Please sign in to comment.