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
@@ -3,7 +3,7 @@ import HelderCore
import SwiftTerm
/// One pane's terminal: a SwiftTerm view bound to a PTY child, reporting to the `TerminalStore`.
final class HelderTerminalView: LocalProcessTerminalView, TerminalSessionHandle {
final class HelderTerminalView: LocalProcessTerminalView, TerminalSessionHandle, PointerTextTarget {
let kind: TerminalKind
private weak var store: TerminalStore?
private(set) var hasStarted = false
@@ -18,7 +18,7 @@ final class HelderTerminalView: LocalProcessTerminalView, TerminalSessionHandle
super.init(frame: NSRect(x: 0, y: 0, width: 480, height: 320),
font: TerminalAppearance.font(store.config.terminal),
options: TerminalAppearance.options(store.config.terminal))
Self.arrowCursorUpdate
Self.policyCursorUpdate
events.owner = self
processDelegate = events
appliedSettings = store.config.terminal
@@ -104,23 +104,68 @@ final class HelderTerminalView: LocalProcessTerminalView, TerminalSessionHandle
// MARK: Pointer
/// The terminal shows the arrow, so SwiftTerm's I-beam rect is dropped; its `resetCursorRects` is not open.
override func addCursorRect(_ rect: NSRect, cursor object: NSCursor) {
guard object != .iBeam else { return }
super.addCursorRect(rect, cursor: object)
let pointerLayer = PointerLayer.workspace
var acceptsPointerText: Bool { true }
/// The grid takes the I-beam: its output selects and its prompt takes input. SwiftTerm's scroller keeps the arrow.
func isPointerOverText(atWindowPoint point: NSPoint) -> Bool {
guard let hit = hitView(atWindowPoint: point) else { return false }
return hit.isDescendant(of: self) && !(hit is NSScroller)
}
/// Gives this class its own `cursorUpdate:` that sets the arrow; Swift cannot override SwiftTerm's I-beam one,
/// The first click in an inactive window already places the selection or reaches the program.
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
/// SwiftTerm's `resetCursorRects` is not open, so its I-beam rect is filtered here and leaves out the scroller.
override func addCursorRect(_ rect: NSRect, cursor object: NSCursor) {
guard object == .iBeam else { return super.addCursorRect(rect, cursor: object) }
guard allowsIBeamRect else { return }
let scroller = subviews.first { $0 is NSScroller && !$0.isHidden }
super.addCursorRect(scroller.map { rect.removing($0.frame) } ?? rect, cursor: object)
}
/// Gives this class its own `cursorUpdate:` that asks the policy; Swift cannot override SwiftTerm's I-beam one,
/// which runs when an update reaches the pane through a subview or a neighbour's tracking area.
private static let arrowCursorUpdate: Void = {
let block: @convention(block) (AnyObject, NSEvent) -> Void = { _, _ in NSCursor.arrow.set() }
private static let policyCursorUpdate: Void = {
let block: @convention(block) (AnyObject, NSEvent) -> Void = { view, _ in
MainActor.assumeIsolated {
guard let view = view as? HelderTerminalView, let window = view.window else { return }
let point = window.mouseLocationOutsideOfEventStream
if PointerPolicy.isSelecting || view.isTopmost(atWindowPoint: point) {
view.updatePointer(atWindowPoint: point)
} else {
PointerPolicy.updateCursor(in: window)
}
}
}
let selector = #selector(NSResponder.cursorUpdate(with:))
let types = method_getTypeEncoding(class_getInstanceMethod(NSView.self, selector)!)
class_addMethod(HelderTerminalView.self, selector, imp_implementationWithBlock(block), types)
}()
/// A drag that starts on the grid selects text, so it keeps the I-beam until mouse-up wherever it goes.
override func mouseDown(with event: NSEvent) {
PointerPolicy.beginSelection()
super.mouseDown(with: event)
}
override func mouseDragged(with event: NSEvent) {
super.mouseDragged(with: event)
if PointerPolicy.isSelecting { NSCursor.iBeam.set() }
}
override func mouseUp(with event: NSEvent) {
super.mouseUp(with: event)
PointerPolicy.endSelection(in: window)
}
// MARK: Lifetime
override func viewWillMove(toWindow newWindow: NSWindow?) {
PointerPolicy.endSelection(in: nil)
super.viewWillMove(toWindow: newWindow)
}
/// Closing the window kills the child; moving the view between containers does not.
override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()