Skip to content

Commit

Permalink
Add STTextFinderBarContainer to handle text finder bar
Browse files Browse the repository at this point in the history
Move responsibility for managing the text finder bar from STTextView to a
new STTextFinderBarContainer class that implements NSTextFinderBarContainer.
This allows properly positioning the gutter view below the find bar when it
is shown/hidden.

Update STTextView to:
- Create the text finder client, bar container and finder in init
- Connect the text finder client and bar container
- Remove code to set up the text finder when moving to a window
- Scope some enclosing scroll view accesses
  • Loading branch information
krzyzanowskim committed Dec 15, 2024
1 parent 00a4241 commit 828dd6f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
43 changes: 43 additions & 0 deletions Sources/STTextViewAppKit/STTextFinderBarContainer.swift
Original file line number Diff line number Diff line change
@@ -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()
}
}
4 changes: 2 additions & 2 deletions Sources/STTextViewAppKit/STTextView+Gutter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 16 additions & 12 deletions Sources/STTextViewAppKit/STTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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? {
Expand Down

0 comments on commit 828dd6f

Please sign in to comment.