Skip to content

Commit

Permalink
Move All Delegate Calls Outside Layout Pass (#57)
Browse files Browse the repository at this point in the history
- Moves a few more delegate calls or delegate triggering calls outside the protected area in `layoutLines` to prevent crashes. These are safe to call outside the intended protected area.
- Documents an important layout method.
- Fixes a flaking test by sorting selection ranges after deleting.
  • Loading branch information
thecoolwinter authored Nov 18, 2024
1 parent 013566b commit 509d7b2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ public class TextLayoutManager: NSObject {
// Update the visible lines with the new set.
visibleLineIds = newVisibleLines

#if DEBUG
isInLayout = false
#endif

// These are fine to update outside of `isInLayout` as our internal data structures are finalized at this point
// so laying out again won't break our line storage or visible line.

if maxFoundLineWidth > maxLineWidth {
maxLineWidth = maxFoundLineWidth
}
Expand All @@ -274,15 +281,11 @@ public class TextLayoutManager: NSObject {
delegate?.layoutManagerYAdjustment(yContentAdjustment)
}

#if DEBUG
isInLayout = false
#endif
needsLayout = false

// This needs to happen after ``needsLayout`` is toggled. Things can be triggered by frame changes.
if originalHeight != lineStorage.height || layoutView?.frame.size.height != lineStorage.height {
delegate?.layoutManagerHeightDidUpdate(newHeight: lineStorage.height)
}

needsLayout = false
}

/// Lays out a single text line.
Expand Down
1 change: 1 addition & 0 deletions Sources/CodeEditTextView/TextView/TextView+Delete.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ extension TextView {
guard extendedRange.location >= 0 else { continue }
textSelection.range.formUnion(extendedRange)
}
selectionManager.textSelections.sort(by: { $0.range.location < $1.range.location })
KillRing.shared.kill(
strings: selectionManager.textSelections.map(\.range).compactMap({ textStorage.substring(from: $0) })
)
Expand Down
2 changes: 2 additions & 0 deletions Sources/CodeEditTextView/TextView/TextView+Layout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ extension TextView {
inputContext?.invalidateCharacterCoordinates()
}

/// Updates the view's frame if needed depending on wrapping lines, a new maximum width, or changed available size.
/// - Returns: Whether or not the view was updated.
@discardableResult
public func updateFrameIfNeeded() -> Bool {
var availableSize = scrollView?.contentSize ?? .zero
Expand Down

0 comments on commit 509d7b2

Please sign in to comment.