51 lines
2.1 KiB
Swift
51 lines
2.1 KiB
Swift
import AppKit
|
|
|
|
/// An NSTextView that only sets its I-beam while it is the view under the pointer.
|
|
/// On macOS 26 SwiftTerm makes the window send every mouse move to the first responder, and NSTextView
|
|
/// sets the I-beam on each one, which fights the arrow over the other columns and a splitter's resize cursor.
|
|
class PointerBoundTextView: NSTextView {
|
|
/// Grows the view to at least the clip view's height, so the empty area below the text still takes the I-beam and clicks.
|
|
var fillsClipView = false {
|
|
didSet { fitClipView() }
|
|
}
|
|
|
|
override func mouseMoved(with event: NSEvent) {
|
|
guard isUnderPointer else { return }
|
|
super.mouseMoved(with: event)
|
|
}
|
|
|
|
override func cursorUpdate(with event: NSEvent) {
|
|
guard isUnderPointer else { return }
|
|
super.cursorUpdate(with: event)
|
|
}
|
|
|
|
override func viewDidMoveToSuperview() {
|
|
super.viewDidMoveToSuperview()
|
|
NotificationCenter.default.removeObserver(self, name: NSView.frameDidChangeNotification, object: nil)
|
|
guard let clip = superview as? NSClipView else { return }
|
|
clip.postsFrameChangedNotifications = true
|
|
NotificationCenter.default.addObserver(self, selector: #selector(clipFrameChanged), name: NSView.frameDidChangeNotification,
|
|
object: clip)
|
|
fitClipView()
|
|
}
|
|
|
|
@objc private func clipFrameChanged() {
|
|
fitClipView()
|
|
}
|
|
|
|
private func fitClipView() {
|
|
guard fillsClipView, let clip = superview as? NSClipView else { return }
|
|
minSize.height = clip.bounds.height
|
|
sizeToFit()
|
|
}
|
|
|
|
/// A splitter's hit area lies on top of the text view's edge, so the visible rect alone is not enough.
|
|
private var isUnderPointer: Bool {
|
|
guard let window, let content = window.contentView else { return false }
|
|
let location = window.mouseLocationOutsideOfEventStream
|
|
guard visibleRect.contains(convert(location, from: nil)),
|
|
let hit = content.hitTest(content.superview?.convert(location, from: nil) ?? location) else { return false }
|
|
return hit.isDescendant(of: self)
|
|
}
|
|
}
|