Skip to content

Commit

Permalink
Line annotations frame alignment and visibility fixes
Browse files Browse the repository at this point in the history
- Fixed line annotation view frames to be offset based on the textView's
  contentFrame origin
- Enabled line highlighting and line number display in the demo apps
- Added an assertion when no text layout fragment is found for an annotation
  location
  • Loading branch information
krzyzanowskim committed Dec 15, 2024
1 parent bf49f6f commit 2736aa2
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DemoApp.AppKit/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@
<windowCollectionBehavior key="collectionBehavior" fullScreenNone="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="335" y="390" width="480" height="360"/>
<rect key="screenRect" x="0.0" y="0.0" width="1440" height="898"/>
<rect key="screenRect" x="0.0" y="0.0" width="2560" height="1415"/>
<view key="contentView" id="EiT-Mj-1SZ">
<rect key="frame" x="0.0" y="0.0" width="480" height="360"/>
<autoresizingMask key="autoresizingMask"/>
Expand Down
3 changes: 2 additions & 1 deletion DemoApp.AppKit/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ final class ViewController: NSViewController, STAnnotationsDataSource {
defaultParagraphStyle.lineHeightMultiple = 1.2

textView.defaultParagraphStyle = defaultParagraphStyle
textView.highlightSelectedLine = false
textView.highlightSelectedLine = true
textView.showsLineNumbers = true

textView.text = """
Expand Down
4 changes: 2 additions & 2 deletions DemoApp.UIKit/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class ViewController: UIViewController, STAnnotationsDataSource {
defaultParagraphStyle.lineHeightMultiple = 1.2

textView.defaultParagraphStyle = defaultParagraphStyle
textView.highlightSelectedLine = false

textView.font = UIFont.monospacedSystemFont(ofSize: 14, weight: .regular)
textView.showsLineNumbers = false
textView.highlightSelectedLine = true
textView.showsLineNumbers = true

textView.text = """
Expand Down
7 changes: 5 additions & 2 deletions Sources/STAnnotationsPluginAppKit/STAnnotationsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ extension STAnnotationsPlugin {
let textLayoutManager = context.textView.textLayoutManager
for annotation in dataSource.textViewAnnotations {
textLayoutManager.ensureLayout(for: NSTextRange(location: annotation.location))
// textLayoutFragment is in the textView.contentFrame coordinates
if let textLayoutFragment = textLayoutManager.textLayoutFragment(for: annotation.location),
let textLineFragment = textLayoutFragment.textLineFragment(at: annotation.location) {

Expand All @@ -107,15 +108,17 @@ extension STAnnotationsPlugin {
// Calculate proposed annotation view frame
let segmentFrame = context.textView.textLayoutManager.textSegmentFrame(at: annotation.location, type: .standard, options: [.upstreamAffinity])!
let proposedFrame = CGRect(
x: lineFragmentFrame.maxX + 2,
y: lineFragmentFrame.origin.y + textLineFragment.typographicBounds.minY,
x: context.textView.contentFrame.origin.x + lineFragmentFrame.maxX + 2,
y: context.textView.contentFrame.origin.y + lineFragmentFrame.origin.y + textLineFragment.typographicBounds.minY,
width: context.textView.visibleRect.maxX - segmentFrame.maxX,
height: lineFragmentFrame.height
).pixelAligned

if let annotationView = dataSource.textView(context.textView, viewForLineAnnotation: annotation, textLineFragment: textLineFragment, proposedViewFrame: proposedFrame) {
annotationViews.append(annotationView)
}
} else {
assertionFailure("No fragment for location \(annotation.location)?")
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/STAnnotationsPluginUIKit/STAnnotationsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ extension STAnnotationsPlugin {
// Calculate proposed annotation view frame
let segmentFrame = context.textView.textLayoutManager.textSegmentFrame(at: annotation.location, type: .standard, options: [.upstreamAffinity])!
let proposedFrame = CGRect(
x: lineFragmentFrame.maxX + 2,
y: lineFragmentFrame.origin.y + textLineFragment.typographicBounds.minY,
x: context.textView.contentFrame.origin.x + lineFragmentFrame.maxX + 2,
y: context.textView.contentFrame.origin.y + lineFragmentFrame.origin.y + textLineFragment.typographicBounds.minY,
width: context.textView.textInputView.frame.maxX - segmentFrame.maxX,
height: lineFragmentFrame.height
).pixelAligned
Expand Down

0 comments on commit 2736aa2

Please sign in to comment.