Files
helder/Sources/HelderUI/Components/PointerBoundTextView.swift
T
2026-10-05 10:32:09 +02:00

96 lines
3.7 KiB
Swift

import AppKit
/// An NSTextView that takes its pointer from `PointerPolicy`, and only 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, PointerTextTarget {
var pointerLayer = PointerLayer.workspace
/// False while the view stays alive but is not shown, so its text takes no I-beam.
var isPointerActive = true {
didSet { if isPointerActive != oldValue { PointerPolicy.setNeedsRefresh(in: window) } }
}
/// 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() }
}
var acceptsPointerText: Bool { isPointerActive && isSelectable }
func isPointerOverText(atWindowPoint point: NSPoint) -> Bool {
isTopmost(atWindowPoint: point)
}
func updatePointer(atWindowPoint point: NSPoint) {
applyPointer(pointerShape(atWindowPoint: point), atWindowPoint: point)
}
/// Sets the cursor for a shape the policy picked; subclasses add their extras here.
func applyPointer(_ shape: PointerShape, atWindowPoint point: NSPoint) {
shape.cursor.set()
}
/// The first click in an inactive window already places the caret or starts a selection.
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
override func resetCursorRects() {
addPointerCursorRect(visibleRectWithoutScrollers)
}
override func mouseMoved(with event: NSEvent) {
guard let point = mouseLocation, isTopmost(atWindowPoint: point) else { return }
super.mouseMoved(with: event)
applyPointer(pointerShape(isOverText: true), atWindowPoint: point)
}
/// An update meant for another view is handed to the view under the mouse.
override func cursorUpdate(with event: NSEvent) {
guard let window, let point = mouseLocation else { return }
if PointerPolicy.isSelecting || isTopmost(atWindowPoint: point) {
updatePointer(atWindowPoint: point)
} else {
PointerPolicy.updateCursor(in: window)
}
}
/// NSTextView tracks the whole drag inside `mouseDown`, so the selection ends when it returns.
override func mouseDown(with event: NSEvent) {
PointerPolicy.beginSelection()
super.mouseDown(with: event)
PointerPolicy.endSelection(in: window)
}
override func viewWillMove(toWindow newWindow: NSWindow?) {
if newWindow == nil { PointerPolicy.setNeedsRefresh(in: window) }
super.viewWillMove(toWindow: newWindow)
}
override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
PointerPolicy.setNeedsRefresh(in: window)
}
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()
}
var mouseLocation: NSPoint? {
window?.mouseLocationOutsideOfEventStream
}
}