Skip to content
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

General Performance Improvements #24

Merged
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
2 changes: 2 additions & 0 deletions Sources/CodeEditTextView/Cursors/CursorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ open class CursorView: NSView {
true
}

override open func hitTest(_ point: NSPoint) -> NSView? { nil }

/// Create a cursor view.
/// - Parameters:
/// - blinkDuration: The duration to blink, leave as nil to never blink.
Expand Down
24 changes: 19 additions & 5 deletions Sources/CodeEditTextView/TextLayoutManager/TextLayoutManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ public class TextLayoutManager: NSObject {
}
}

/// The amount of extra vertical padding used to lay out lines in before they come into view.
///
/// This solves a small problem with layout performance, if you're seeing layout lagging behind while scrolling,
/// adjusting this value higher may help fix that.
/// Defaults to `350`.
public var verticalLayoutPadding: CGFloat = 350 {
didSet {
setNeedsLayout()
}
}

// MARK: - Internal

weak var textStorage: NSTextStorage?
Expand Down Expand Up @@ -215,10 +226,12 @@ public class TextLayoutManager: NSObject {
}

/// Ends a transaction. When called, the layout manager will layout any necessary lines.
public func endTransaction() {
public func endTransaction(forceLayout: Bool = false) {
transactionCounter -= 1
if transactionCounter == 0 {
setNeedsLayout()
if forceLayout {
setNeedsLayout()
}
layoutLines()
} else if transactionCounter < 0 {
// swiftlint:disable:next line_length
Expand All @@ -232,8 +245,8 @@ public class TextLayoutManager: NSObject {
func layoutLines() { // swiftlint:disable:this function_body_length
guard let visibleRect = delegate?.visibleRect, !isInTransaction, let textStorage else { return }
CATransaction.begin()
let minY = max(visibleRect.minY, 0)
let maxY = max(visibleRect.maxY, 0)
let minY = max(visibleRect.minY - verticalLayoutPadding, 0)
let maxY = max(visibleRect.maxY + verticalLayoutPadding, 0)
let originalHeight = lineStorage.height
var usedFragmentIDs = Set<UUID>()
var forceLayout: Bool = needsLayout
Expand Down Expand Up @@ -278,6 +291,8 @@ public class TextLayoutManager: NSObject {
newVisibleLines.insert(linePosition.data.id)
}

CATransaction.commit()

// Enqueue any lines not used in this layout pass.
viewReuseQueue.enqueueViews(notInSet: usedFragmentIDs)

Expand All @@ -297,7 +312,6 @@ public class TextLayoutManager: NSObject {
}

needsLayout = false
CATransaction.commit()
}

/// Lays out a single text line.
Expand Down
2 changes: 2 additions & 0 deletions Sources/CodeEditTextView/TextLine/LineFragmentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ final class LineFragmentView: NSView {
false
}

override func hitTest(_ point: NSPoint) -> NSView? { nil }

/// Prepare the view for reuse, clears the line fragment reference.
override func prepareForReuse() {
super.prepareForReuse()
Expand Down
10 changes: 3 additions & 7 deletions Sources/CodeEditTextView/TextView/TextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,11 @@ public class TextView: NSView, NSTextContent {
/// - Parameter point: The point to find.
/// - Returns: A view at the given point, if any.
override public func hitTest(_ point: NSPoint) -> NSView? {
// For our purposes, cursor and line fragment views should be transparent from the point of view of
// all other views. So, if the normal hitTest returns one of them, we return `self` instead.
let hitView = super.hitTest(point)

if let hitView, hitView != self,
type(of: hitView) == CursorView.self || type(of: hitView) == LineFragmentView.self {
if visibleRect.contains(point) {
return self
} else {
return super.hitTest(point)
}
return hitView
}

// MARK: - Key Down
Expand Down
2 changes: 1 addition & 1 deletion Sources/CodeEditTextView/Utils/ViewReuseQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class ViewReuseQueue<View: NSView, Key: Hashable> {
/// Enqueues all views not in the given set.
/// - Parameter outsideSet: The keys who's views should not be enqueued for reuse.
public func enqueueViews(notInSet keys: Set<Key>) {
// Get all keys that are in "use" but not in the given set.
// Get all keys that are currently in "use" but not in the given set, and enqueue them for reuse.
for key in Set(usedViews.keys).subtracting(keys) {
enqueueView(forKey: key)
}
Expand Down
Loading