fixes cursor

This commit is contained in:
2026-10-05 10:32:09 +02:00
parent 336987d821
commit 201c59c659
12 changed files with 460 additions and 46 deletions
@@ -1,22 +1,72 @@
import AppKit
/// An NSTextView that only sets its I-beam while it is the view under the pointer.
/// 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 {
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() }
}
override func mouseMoved(with event: NSEvent) {
guard isUnderPointer else { return }
super.mouseMoved(with: event)
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 isUnderPointer else { return }
super.cursorUpdate(with: event)
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() {
@@ -39,12 +89,7 @@ class PointerBoundTextView: NSTextView {
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)
var mouseLocation: NSPoint? {
window?.mouseLocationOutsideOfEventStream
}
}