Skip to content

Change scroll enabled #347

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

Merged
merged 2 commits into from
Nov 11, 2024
Merged
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
39 changes: 30 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
addHeightConstraint()

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,34 @@ class AutogrowingTextView: UITextView {
fatalError("init(coder:) has not been implemented")
}

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

if allowAutogrowing {
if heightAnchorConstraint == nil {
addHeightConstraint()
recalculateHeight()
}
} else {
isScrollEnabled = false
if let heightAnchorConstraint {
NSLayoutConstraint.deactivate([heightAnchorConstraint])
}
heightAnchorConstraint = nil
}
}

private func addHeightConstraint() {
guard allowAutogrowing else { return }
let heightConstraint = heightAnchor.constraint(greaterThanOrEqualToConstant: contentSize.height)
heightAnchorConstraint = heightConstraint
heightAnchorConstraint?.priority = .defaultHigh

NSLayoutConstraint.activate([
heightConstraint
])
}

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
Loading