Skip to content

Commit

Permalink
Support .cursor attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyzanowskim committed May 18, 2024
1 parent 13d8b8b commit cdc0d3e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let package = Package(
)
],
dependencies: [
.package(url: "/~https://github.com/krzyzanowskim/STTextKitPlus", from: "0.0.2")
.package(url: "/~https://github.com/krzyzanowskim/STTextKitPlus", from: "0.1.1")
],
targets: [
.target(
Expand Down
40 changes: 37 additions & 3 deletions Sources/STTextView/STTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -657,9 +657,43 @@ import AVFoundation
if isSelectable, visibleRect != .zero {
addCursorRect(visibleRect, cursor: .iBeam)

// TODO: NSAttributedString.Key.cursor attribute
// TODO: .pointingHand for .link attribute rectangles
// addCursorRect(visibleRect, cursor: .pointingHand)
// This iteration may be performance intensive. I think it can be debounced without
// affecting the correctness
if let viewportRange = textLayoutManager.textViewportLayoutController.viewportRange,
let viewportAttributedString = textContentManager.attributedString(in: viewportRange)
{
viewportAttributedString.enumerateAttribute(.link, in: viewportAttributedString.range, options: .longestEffectiveRangeNotRequired) { attributeValue, attributeRange, stop in
guard attributeValue != nil else {
return
}

if let startLocation = textLayoutManager.location(viewportRange.location, offsetBy: attributeRange.location),
let endLocation = textLayoutManager.location(startLocation, offsetBy: attributeRange.length),
let linkTextRange = NSTextRange(location: startLocation, end: endLocation),
let linkTypographicBounds = textLayoutManager.typographicBounds(in: linkTextRange)
{
addCursorRect(linkTypographicBounds, cursor: .pointingHand)
} else {
stop.pointee = true
}
}

viewportAttributedString.enumerateAttribute(.cursor, in: viewportAttributedString.range, options: .longestEffectiveRangeNotRequired) { attributeValue, attributeRange, stop in
guard let cursorValue = attributeValue as? NSCursor else {
return
}

if let startLocation = textLayoutManager.location(viewportRange.location, offsetBy: attributeRange.location),
let endLocation = textLayoutManager.location(startLocation, offsetBy: attributeRange.length),
let linkTextRange = NSTextRange(location: startLocation, end: endLocation),
let linkTypographicBounds = textLayoutManager.typographicBounds(in: linkTextRange)
{
addCursorRect(linkTypographicBounds, cursor: cursorValue)
} else {
stop.pointee = true
}
}
}
}
}

Expand Down

0 comments on commit cdc0d3e

Please sign in to comment.