-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6653c21
commit 86b9804
Showing
5 changed files
with
84 additions
and
69 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
Sources/CodeEditTextView/TextLayoutManager/TextLayoutManager+Invalidation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// TextLayoutManager+Invalidation.swift | ||
// CodeEditTextView | ||
// | ||
// Created by Khan Winter on 2/24/24. | ||
// | ||
|
||
import Foundation | ||
|
||
extension TextLayoutManager { | ||
/// Invalidates layout for the given rect. | ||
/// - Parameter rect: The rect to invalidate. | ||
public func invalidateLayoutForRect(_ rect: NSRect) { | ||
for linePosition in lineStorage.linesStartingAt(rect.minY, until: rect.maxY) { | ||
linePosition.data.setNeedsLayout() | ||
} | ||
layoutLines() | ||
} | ||
|
||
/// Invalidates layout for the given range of text. | ||
/// - Parameter range: The range of text to invalidate. | ||
public func invalidateLayoutForRange(_ range: NSRange) { | ||
for linePosition in lineStorage.linesInRange(range) { | ||
linePosition.data.setNeedsLayout() | ||
} | ||
|
||
layoutLines() | ||
} | ||
|
||
public func setNeedsLayout() { | ||
needsLayout = true | ||
visibleLineIds.removeAll(keepingCapacity: true) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
Sources/CodeEditTextView/TextLayoutManager/TextLayoutManager+Transaction.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// TextLayoutManager+Transaction.swift | ||
// CodeEditTextView | ||
// | ||
// Created by Khan Winter on 2/24/24. | ||
// | ||
|
||
import Foundation | ||
|
||
extension TextLayoutManager { | ||
/// Begins a transaction, preventing the layout manager from performing layout until the `endTransaction` is called. | ||
/// Useful for grouping attribute modifications into one layout pass rather than laying out every update. | ||
/// | ||
/// You can nest transaction start/end calls, the layout manager will not cause layout until the last transaction | ||
/// group is ended. | ||
/// | ||
/// Ensure there is a balanced number of begin/end calls. If there is a missing endTranscaction call, the layout | ||
/// manager will never lay out text. If there is a end call without matching a start call an assertionFailure | ||
/// will occur. | ||
public func beginTransaction() { | ||
transactionCounter += 1 | ||
} | ||
|
||
/// Ends a transaction. When called, the layout manager will layout any necessary lines. | ||
public func endTransaction(forceLayout: Bool = false) { | ||
transactionCounter -= 1 | ||
if transactionCounter == 0 { | ||
if forceLayout { | ||
setNeedsLayout() | ||
} | ||
layoutLines() | ||
} else if transactionCounter < 0 { | ||
assertionFailure( | ||
"TextLayoutManager.endTransaction called without a matching TextLayoutManager.beginTransaction call" | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters