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) } /// NSTextView's own `mouseMoved` sets the I-beam, so it runs only where the policy gives the I-beam; /// under an overlay it would flip the pointer to the I-beam and back on every move. override func mouseMoved(with event: NSEvent) { guard let point = mouseLocation, isTopmost(atWindowPoint: point) else { return } let shape = pointerShape(isOverText: true) if shape == .iBeam { super.mouseMoved(with: event) } applyPointer(shape, atWindowPoint: point) } /// NSTextView's `mouseEntered` sets the I-beam itself, so it runs only where the policy allows the I-beam. override func mouseEntered(with event: NSEvent) { if allowsIBeamRect { super.mouseEntered(with: event) } guard let window, !PointerPolicy.isSelecting else { return } PointerPolicy.scheduleUpdate(in: window) } /// 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.scheduleUpdate(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 } // NSTextView gives its clip view an I-beam document cursor, whose rect would bypass the policy under an overlay. clip.documentCursor = nil 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 } }