diff --git a/Sources/STTextViewAppKit/STTextFinderBarContainer.swift b/Sources/STTextViewAppKit/STTextFinderBarContainer.swift new file mode 100644 index 0000000..f22f88c --- /dev/null +++ b/Sources/STTextViewAppKit/STTextFinderBarContainer.swift @@ -0,0 +1,43 @@ +import AppKit + +final class STTextFinderBarContainer: NSObject, NSTextFinderBarContainer { + + // Forward NSTextFinderBarContainer to enclosing NSScrollView (for now at least) + weak var client: STTextView? + + var findBarView: NSView? { + get { + client?.scrollView?.findBarView + } + + set { + client?.scrollView?.findBarView = newValue + + // Rearrange gutter view position in NSScrollView hierarchy + Task { @MainActor in + if let scrollView = client?.scrollView, let gutterView = client?.gutterView { + gutterView.removeFromSuperviewWithoutNeedingDisplay() + scrollView.addSubview(gutterView, positioned: .below, relativeTo: scrollView.findBarView) + } + } + } + } + + var isFindBarVisible: Bool { + get { + client?.scrollView?.isFindBarVisible ?? false + } + + set { + client?.scrollView?.isFindBarVisible = newValue + } + } + + func contentView() -> NSView? { + client?.contentView + } + + func findBarViewDidChangeHeight() { + client?.scrollView?.findBarViewDidChangeHeight() + } +} diff --git a/Sources/STTextViewAppKit/STTextView+Gutter.swift b/Sources/STTextViewAppKit/STTextView+Gutter.swift index b050e44..f5ffcd5 100644 --- a/Sources/STTextViewAppKit/STTextView+Gutter.swift +++ b/Sources/STTextViewAppKit/STTextView+Gutter.swift @@ -21,8 +21,8 @@ extension STTextView { gutterView.selectedLineTextColor = textColor gutterView.highlightSelectedLine = highlightSelectedLine gutterView.selectedLineHighlightColor = selectedLineHighlightColor - if let scrollView = enclosingScrollView { - scrollView.addSubview(gutterView, positioned: .below, relativeTo: scrollView.findBarView) + if let scrollView { + scrollView.addSubview(gutterView) } self.gutterView = gutterView needsLayout = true diff --git a/Sources/STTextViewAppKit/STTextView.swift b/Sources/STTextViewAppKit/STTextView.swift index dbce6f2..0439c0c 100644 --- a/Sources/STTextViewAppKit/STTextView.swift +++ b/Sources/STTextViewAppKit/STTextView.swift @@ -457,6 +457,8 @@ import AVFoundation /// NSTextFinderClient internal let textFinderClient: STTextFinderClient + internal let textFinderBarContainer: STTextFinderBarContainer + internal var textCheckingController: NSTextCheckingController! /// A Boolean value that indicates whether the receiver has continuous spell checking enabled. @@ -592,9 +594,10 @@ import AVFoundation allowsUndo = true _undoManager = CoalescingUndoManager() - - textFinder = NSTextFinder() textFinderClient = STTextFinderClient() + textFinderBarContainer = STTextFinderBarContainer() + textFinder = NSTextFinder() + textFinder.client = textFinderClient _defaultTypingAttributes = [ .paragraphStyle: NSParagraphStyle.default, @@ -606,6 +609,9 @@ import AVFoundation super.init(frame: frameRect) + textFinderBarContainer.client = self + textFinder.findBarContainer = textFinderBarContainer + setSelectedTextRange(NSTextRange(location: textLayoutManager.documentRange.location), updateLayout: false) textLayoutManager.delegate = self @@ -724,25 +730,23 @@ import AVFoundation open override func viewDidMoveToSuperview() { super.viewDidMoveToSuperview() - NotificationCenter.default.addObserver( - self, - selector: #selector(enclosingClipViewBoundsDidChange(_:)), - name: NSClipView.boundsDidChangeNotification, - object: scrollView?.contentView - ) + if let scrollView { + NotificationCenter.default.addObserver( + self, + selector: #selector(enclosingClipViewBoundsDidChange(_:)), + name: NSClipView.boundsDidChangeNotification, + object: scrollView.contentView + ) + } } open override func viewDidMoveToWindow() { super.viewDidMoveToWindow() if self.window != nil { - textFinder.client = textFinderClient - textFinder.findBarContainer = enclosingScrollView - // setup registerd plugins setupPlugins() } - } open override func hitTest(_ point: NSPoint) -> NSView? {