Skip to content

Commit

Permalink
Rework annotation views hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyzanowskim committed Dec 30, 2023
1 parent 94b6c97 commit c3b837a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
26 changes: 26 additions & 0 deletions Sources/AnnotationsPlugin/AnnotationsContentView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import AppKit

final class AnnotationsContentView: NSView {

override var canBecomeKeyView: Bool {
false
}

override var acceptsFirstResponder: Bool {
false
}

override var isFlipped: Bool {
true
}

override dynamic func hitTest(_ point: NSPoint) -> NSView? {
let result = super.hitTest(point)

if let view = result, view != self, view.isDescendant(of: self) {
return view
}

return nil
}
}
25 changes: 13 additions & 12 deletions Sources/AnnotationsPlugin/AnnotationsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@ public class AnnotationsPlugin: STPlugin {
private weak var dataSource: AnnotationsDataSource?
private var reloadDataHandler: (() -> Void)?

// Annotations surface view. Add annotations to this view.
private var annotationsContentView: AnnotationsContentView!

public init(dataSource: AnnotationsDataSource) {
self.dataSource = dataSource
}

public func setUp(context: any Context) {
self.annotationsContentView = AnnotationsContentView(frame: context.textView.frame)
annotationsContentView.wantsLayer = true
annotationsContentView.autoresizingMask = [.height, .width]
context.textView.addSubview(annotationsContentView)

context.events.onDidChangeText { [weak self] affectedRange, replacementString in
self?.didChangeText(context: context, in: affectedRange, replacementString: replacementString)
}

context.events.onDidLayoutViewport { [weak self] range in
self?.annotationsContentView.frame = context.textView.frame
self?.didLayoutViewport(context: context, range)
}

Expand Down Expand Up @@ -47,7 +56,6 @@ public class AnnotationsPlugin: STPlugin {
// we need to reposition all views that may be
// affected by the text change
context.coordinator.layoutAnnotationViewsIfNeeded()

}

private func didLayoutViewport(context: any Context, _ range: NSTextRange?) {
Expand All @@ -69,7 +77,6 @@ public class AnnotationsPlugin: STPlugin {
extension AnnotationsPlugin {

public class Coordinator {
private var annotationsViews: [NSView] = []
private let parent: AnnotationsPlugin
private let context: CoordinatorContext

Expand All @@ -84,29 +91,23 @@ extension AnnotationsPlugin {
return
}

// Remove all views
for view in annotationsViews {
view.removeFromSuperview()
}
annotationsViews.removeAll()

// Add views for annotations
var annotationViews: [NSView] = []
let textLayoutManager = context.textView.textLayoutManager
for annotation in dataSource.textViewAnnotations() {
textLayoutManager.ensureLayout(for: NSTextRange(location: annotation.location))
if let textLineFragment = textLayoutManager.textLineFragment(at: annotation.location) {
if let annotationView = dataSource.textView(context.textView, viewForLineAnnotation: annotation, textLineFragment: textLineFragment) {
// Set or Update view
context.textView.addSubview(annotationView)
annotationsViews.append(annotationView)
annotationViews.append(annotationView)
} else {
assertionFailure()
}
}

}
}

parent.annotationsContentView.subviews = annotationViews
}
}

}

0 comments on commit c3b837a

Please sign in to comment.