-
Notifications
You must be signed in to change notification settings - Fork 18
Add Text Insets #65
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
thecoolwinter
merged 3 commits into
CodeEditApp:main
from
thecoolwinter:feat/text-insets
Jan 16, 2025
Merged
Add Text Insets #65
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
97 changes: 97 additions & 0 deletions
97
Sources/CodeEditTextView/TextSelectionManager/TextSelectionManager+Draw.swift
This file contains hidden or 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,97 @@ | ||
// | ||
// File.swift | ||
// CodeEditTextView | ||
// | ||
// Created by Khan Winter on 1/12/25. | ||
// | ||
|
||
import AppKit | ||
|
||
extension TextSelectionManager { | ||
/// Draws line backgrounds and selection rects for each selection in the given rect. | ||
/// - Parameter rect: The rect to draw in. | ||
func drawSelections(in rect: NSRect) { | ||
guard let context = NSGraphicsContext.current?.cgContext else { return } | ||
context.saveGState() | ||
var highlightedLines: Set<UUID> = [] | ||
// For each selection in the rect | ||
for textSelection in textSelections { | ||
if textSelection.range.isEmpty { | ||
drawHighlightedLine( | ||
in: rect, | ||
for: textSelection, | ||
context: context, | ||
highlightedLines: &highlightedLines | ||
) | ||
} else { | ||
drawSelectedRange(in: rect, for: textSelection, context: context) | ||
} | ||
} | ||
context.restoreGState() | ||
} | ||
|
||
/// Draws a highlighted line in the given rect. | ||
/// - Parameters: | ||
/// - rect: The rect to draw in. | ||
/// - textSelection: The selection to draw. | ||
/// - context: The context to draw in. | ||
/// - highlightedLines: The set of all lines that have already been highlighted, used to avoid highlighting lines | ||
/// twice and updated if this function comes across a new line id. | ||
private func drawHighlightedLine( | ||
in rect: NSRect, | ||
for textSelection: TextSelection, | ||
context: CGContext, | ||
highlightedLines: inout Set<UUID> | ||
) { | ||
guard let linePosition = layoutManager?.textLineForOffset(textSelection.range.location), | ||
!highlightedLines.contains(linePosition.data.id) else { | ||
return | ||
} | ||
highlightedLines.insert(linePosition.data.id) | ||
context.saveGState() | ||
|
||
let insetXPos = max(rect.minX, edgeInsets.left) | ||
let maxWidth = (textView?.frame.width ?? 0) - insetXPos - edgeInsets.right | ||
|
||
let selectionRect = CGRect( | ||
x: insetXPos, | ||
y: linePosition.yPos, | ||
width: min(rect.width, maxWidth), | ||
height: linePosition.height | ||
).pixelAligned | ||
|
||
if selectionRect.intersects(rect) { | ||
context.setFillColor(selectedLineBackgroundColor.cgColor) | ||
context.fill(selectionRect) | ||
} | ||
context.restoreGState() | ||
} | ||
|
||
/// Draws a selected range in the given context. | ||
/// - Parameters: | ||
/// - rect: The rect to draw in. | ||
/// - range: The range to highlight. | ||
/// - context: The context to draw in. | ||
private func drawSelectedRange(in rect: NSRect, for textSelection: TextSelection, context: CGContext) { | ||
context.saveGState() | ||
|
||
let fillColor = (textView?.isFirstResponder ?? false) | ||
? selectionBackgroundColor.cgColor | ||
: selectionBackgroundColor.grayscale.cgColor | ||
|
||
context.setFillColor(fillColor) | ||
|
||
let fillRects = getFillRects(in: rect, for: textSelection) | ||
|
||
let minX = fillRects.min(by: { $0.origin.x < $1.origin.x })?.origin.x ?? 0 | ||
let minY = fillRects.min(by: { $0.origin.y < $1.origin.y })?.origin.y ?? 0 | ||
let max = fillRects.max(by: { $0.maxY < $1.maxY }) ?? .zero | ||
let origin = CGPoint(x: minX, y: minY) | ||
let size = CGSize(width: max.maxX - minX, height: max.maxY - minY) | ||
textSelection.boundingRect = CGRect(origin: origin, size: size) | ||
|
||
context.fill(fillRects) | ||
context.restoreGState() | ||
} | ||
|
||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,40 @@ | ||
// | ||
// TextView+SetText.swift | ||
// CodeEditTextView | ||
// | ||
// Created by Khan Winter on 1/12/25. | ||
// | ||
|
||
import AppKit | ||
|
||
extension TextView { | ||
/// Sets the text view's text to a new value. | ||
/// - Parameter text: The new contents of the text view. | ||
public func setText(_ text: String) { | ||
let newStorage = NSTextStorage(string: text) | ||
self.setTextStorage(newStorage) | ||
} | ||
|
||
/// Set a new text storage object for the view. | ||
/// - Parameter textStorage: The new text storage to use. | ||
public func setTextStorage(_ textStorage: NSTextStorage) { | ||
self.textStorage = textStorage | ||
|
||
subviews.forEach { view in | ||
view.removeFromSuperview() | ||
} | ||
|
||
textStorage.addAttributes(typingAttributes, range: documentRange) | ||
layoutManager.textStorage = textStorage | ||
layoutManager.reset() | ||
|
||
selectionManager.textStorage = textStorage | ||
selectionManager.setSelectedRanges(selectionManager.textSelections.map { $0.range }) | ||
|
||
_undoManager?.clearStack() | ||
|
||
textStorage.delegate = storageDelegate | ||
needsDisplay = true | ||
needsLayout = true | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.