Skip to content

Commit

Permalink
make annotations public
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyzanowskim committed May 21, 2022
1 parent 7f5149e commit 194e06a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
11 changes: 4 additions & 7 deletions Sources/STTextView/STTextView+Annotations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@ public class STLineAnnotation: NSObject {
extension STTextView {

public func addAnnotation(_ annotations: STLineAnnotation...) {
lineAnnotations.append(contentsOf: annotations)
updateLineAnnotations()
self.annotations.append(contentsOf: annotations)
}

public func removeAnnotation(_ annotations: STLineAnnotation...) {
lineAnnotations.removeAll(where: { annotations.contains($0) })
updateLineAnnotations()
self.annotations.removeAll(where: { annotations.contains($0) })
}

public func removeAllAnnotations() {
lineAnnotations.removeAll(keepingCapacity: true)
updateLineAnnotations()
annotations.removeAll(keepingCapacity: true)
}

internal func updateLineAnnotations() {
subviews = lineAnnotations.compactMap { lineAnnotation -> NSView? in
subviews = annotations.compactMap { lineAnnotation -> NSView? in
if let textLineFragment = textLayoutManager.textLineFragment(at: lineAnnotation.location) {
return delegate?.textView?(self, viewForLineAnnotation: lineAnnotation, textLineFragment: textLineFragment)
}
Expand Down
8 changes: 7 additions & 1 deletion Sources/STTextView/STTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,13 @@ open class STTextView: NSView, CALayerDelegate, NSTextInput {
internal let selectionLayer: STCATiledLayer
internal var backingScaleFactor: CGFloat { window?.backingScaleFactor ?? 1 }
internal var fragmentLayerMap: NSMapTable<NSTextLayoutFragment, STCALayer>
internal var lineAnnotations: [STLineAnnotation] = []

/// Text line annotation views
public var annotations: [STLineAnnotation] = [] {
didSet {
updateLineAnnotations()
}
}

public let textFinder: NSTextFinder
internal let textFinderClient: STTextFinderClient
Expand Down

0 comments on commit 194e06a

Please sign in to comment.