Improve loads of fancy things in relation to the mouse pointer

This commit is contained in:
2026-10-07 13:02:52 +02:00
parent ed5a227c9d
commit a0b3ef9975
25 changed files with 334 additions and 89 deletions
@@ -36,10 +36,20 @@ class PointerBoundTextView: NSTextView, PointerTextTarget {
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 }
super.mouseMoved(with: event)
applyPointer(pointerShape(isOverText: true), atWindowPoint: point)
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.updateCursor(in: window)
}
/// An update meant for another view is handed to the view under the mouse.
@@ -73,6 +83,8 @@ class PointerBoundTextView: NSTextView, PointerTextTarget {
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)
@@ -93,10 +93,13 @@ extension NSView {
hitView(atWindowPoint: point)?.isDescendant(of: self) ?? false
}
/// The visible rect minus the enclosing scroll view's scrollers, which keep the arrow.
/// The visible rect minus the enclosing scroll view's scrollers and line-number ruler, which keep the arrow.
/// The clip view runs under a visible ruler, so the visible rect alone would give the gutter the I-beam.
var visibleRectWithoutScrollers: NSRect {
let scrollers = [enclosingScrollView?.verticalScroller, enclosingScrollView?.horizontalScroller]
return scrollers.compactMap { $0 }.filter { !$0.isHidden && $0.superview != nil }
let scroll = enclosingScrollView
let ruler: NSView? = scroll?.rulersVisible == true ? scroll?.verticalRulerView : nil
let strips: [NSView?] = [scroll?.verticalScroller, scroll?.horizontalScroller, ruler]
return strips.compactMap { $0 }.filter { !$0.isHidden && $0.superview != nil }
.reduce(visibleRect) { $0.removing(convert($1.bounds, from: $1)) }
}
}
@@ -16,7 +16,7 @@ struct CodeEditorConfiguration {
/// Cheap identity of the marks; `makeMarks` runs only when it moves.
var marksKey: String
var makeMarks: () -> [Int: CodeLineMark]
var opensDiffOnClick: Bool
var offersShowDiff: Bool
var knownSymbols: Set<String>
var symbolsVersion: Int
var flash: LineFlash?
@@ -33,9 +33,8 @@ struct CodeEditorActions {
var edited: (String) -> Void = { _ in }
var cursorMoved: (_ line: Int, _ column: Int) -> Void = { _, _ in }
var selectionChanged: (_ lines: ClosedRange<Int>?, _ code: String?) -> Void = { _, _ in }
var markedLineClicked: (Int) -> Void = { _ in }
/// Reference, selected code, and the anchor in top-left window coordinates.
var contextMenu: (AgentReference, String?, CGPoint) -> Void = { _, _, _ in }
/// Reference, selected code, the line that offers Show diff, and the anchor in top-left window coordinates.
var contextMenu: (AgentReference, String?, Int?, CGPoint) -> Void = { _, _, _, _ in }
var symbolClicked: (String) -> Void = { _ in }
/// ⌘→ with a selection; returns false when there was nothing to pass.
var passOnSelection: (CGPoint) -> Bool = { _ in false }
@@ -436,12 +435,17 @@ final class CodeEditorController: NSObject, NSTextViewDelegate, NSLayoutManagerD
return doc.lineIndex.line(containing: characterIndex(atWindowPoint: point))
}
/// A plain click on a marked line in Actual opens the Diff there.
func plainClick(atWindowPoint point: NSPoint) {
guard configuration?.opensDiffOnClick == true,
let line = line(atWindowPoint: point), marks[line] != nil else { return }
let action = actions.markedLineClicked
DispatchQueue.main.async { action(line) }
/// The marked line under the point when the file offers Show diff. The whole row counts,
/// so a blank line and the space after the last glyph hit too.
func showDiffLine(atWindowPoint point: NSPoint) -> Int? {
guard configuration?.offersShowDiff == true else { return nil }
let local = textView.convert(point, from: nil)
var hit: Int?
forEachRow(in: NSRect(x: 0, y: local.y, width: max(1, textView.bounds.width), height: 1)) { row in
if hit == nil, row.rect.minY <= local.y, local.y < row.rect.maxY { hit = row.line }
}
guard let hit, marks[hit] != nil else { return nil }
return hit
}
/// ⌘-click: follows a class name the project declares.
@@ -472,7 +476,7 @@ final class CodeEditorController: NSObject, NSTextViewDelegate, NSLayoutManagerD
let lines = selection.length > 0 ? EditorReference.lines(of: selection, in: doc.lineIndex) : nil
let code = selection.length > 0 ? (doc.storage.string as NSString).substring(with: clamped(selection, in: doc)) : nil
let reference = EditorReference.make(path: configuration.path, selectedLines: lines, clickedLine: clicked)
actions.contextMenu(reference, code, topLeft(event.locationInWindow))
actions.contextMenu(reference, code, showDiffLine(atWindowPoint: event.locationInWindow), topLeft(event.locationInWindow))
}
func passOnSelection() -> Bool {
@@ -4,8 +4,6 @@ import AppKit
final class CodeTextView: PointerBoundTextView {
weak var controller: CodeEditorController?
/// Pointer travel that still counts as a click and not as a text selection.
private static let dragSlop: CGFloat = 4
private var pointerTracking: NSTrackingArea?
override func drawBackground(in rect: NSRect) {
@@ -20,14 +18,7 @@ final class CodeTextView: PointerBoundTextView {
override func mouseDown(with event: NSEvent) {
if event.modifierFlags.contains(.command), controller?.followSymbol(at: event) == true { return }
let hadSelection = selectedRange().length > 0
let down = event.locationInWindow
super.mouseDown(with: event)
guard event.clickCount == 1, !hadSelection, selectedRange().length == 0,
event.modifierFlags.isDisjoint(with: [.shift, .option, .command, .control]),
let up = window?.mouseLocationOutsideOfEventStream,
abs(up.x - down.x) + abs(up.y - down.y) <= Self.dragSlop else { return }
controller?.plainClick(atWindowPoint: down)
}
override func menu(for event: NSEvent) -> NSMenu? {
+4 -4
View File
@@ -56,7 +56,7 @@ struct EditorPane: View {
tabSize: editor.editorConfig.tabSize,
marksKey: marked.map { "actual:" + Self.stamp($0) } ?? "none",
makeMarks: { marked.map { ChangeMarks.actual(DiffViews.actual($0)) } ?? [:] },
opensDiffOnClick: presentation.opensDiffOnClick, knownSymbols: editor.knownSymbols,
offersShowDiff: presentation.offersShowDiff, knownSymbols: editor.knownSymbols,
symbolsVersion: editor.knownSymbolsVersion, flash: editor.flash,
focusRequest: editor.textFocusRequest, selectAllRequest: editor.selectAllRequest, fontScale: fontScale,
isShown: isShown)
@@ -73,9 +73,9 @@ struct EditorPane: View {
edited: { editor.edit($0) },
cursorMoved: { editor.updateCursor(line: $0, column: $1) },
selectionChanged: { editor.updateSelection(lines: $0, code: $1) },
markedLineClicked: { editor.openDiff(atLine: $0) },
contextMenu: { reference, code, anchor in
editor.presentOverlay?(.contextMenu(editor.contextMenuRequest(reference: reference, code: code, anchor: anchor)))
contextMenu: { reference, code, showDiffLine, anchor in
let request = editor.contextMenuRequest(reference: reference, code: code, showDiffLine: showDiffLine, anchor: anchor)
editor.presentOverlay?(.contextMenu(request))
},
symbolClicked: { editor.lookupSymbol($0) },
passOnSelection: { anchor in
@@ -114,6 +114,6 @@ public struct EditorPresentation: Equatable, Sendable {
/// Actual and Code edit the live buffer; Preview is a read-only view.
public var isEditable: Bool { kind == .text && placeholder == nil && (paneMode == .code || paneMode == .actual) }
/// A click on a marked line opens the Diff, except on a new file where every line is marked.
public var opensDiffOnClick: Bool { paneMode == .actual && hasDiff && status != .added }
/// A marked line offers Show diff, except on a new file where every line is marked.
public var offersShowDiff: Bool { paneMode == .actual && hasDiff && status != .added }
}
@@ -111,7 +111,7 @@ struct ContextMenuOverlay: View {
case .copyReference, .copyTerminalSelection, .pasteIntoTerminal: .plain
case .newFile, .newFolder, .stage: .add
case .copyFileName, .unstage, .revealInFinder: .modify
case .passOnReference, .passOnFileName, .openDiff, .rename: .rename
case .passOnReference, .passOnFileName, .bouwen, .openDiff, .showDiff, .rename: .rename
case .discard, .delete: .danger
}
}
@@ -119,13 +119,14 @@ struct ContextMenuOverlay: View {
nonisolated static func symbol(for action: ContextAction) -> String {
switch action {
case .passOnReference, .passOnFileName: ContextMenuRow.spark
case .bouwen: "hammer"
case .copyReference, .copyFileName, .copyTerminalSelection: "doc.on.doc"
case .pasteIntoTerminal: "doc.on.clipboard"
case .newFile: "doc.badge.plus"
case .newFolder: "folder.badge.plus"
case .stage: "plus"
case .unstage: "minus"
case .openDiff: "arrow.left.arrow.right"
case .openDiff, .showDiff: "arrow.left.arrow.right"
case .discard: "arrow.uturn.backward"
case .rename: "pencil"
case .revealInFinder: "folder"
+6 -4
View File
@@ -270,7 +270,7 @@ public final class EditorStore {
track { await $0.load(path, discardEdits: false) }
}
/// A click on a marked line in Actual: the Diff opens on HEAD vs disk, centred on that line.
/// Show diff on a marked line in Actual: the Diff opens on HEAD vs disk, centred on that line.
public func openDiff(atLine line: Int) {
guard let path = activePath, diff(for: path) != nil else { return }
let current = mode(for: path)
@@ -308,9 +308,11 @@ public final class EditorStore {
return PassOnRequest(reference: selection.reference.description, code: code, anchor: anchor)
}
/// The right-click menu for a line or selection in the code: exactly Pass on and Copy reference.
public func contextMenuRequest(reference: AgentReference, code: String?, anchor: CGPoint) -> ContextMenuRequest {
let target = ContextTarget.editor(reference: reference, code: code)
/// The right-click menu for a line or selection in the code: Pass on and Copy reference, plus Show diff
/// when `showDiffLine` names a marked line.
public func contextMenuRequest(reference: AgentReference, code: String?, showDiffLine: Int? = nil,
anchor: CGPoint) -> ContextMenuRequest {
let target = ContextTarget.editor(reference: reference, code: code, showDiffLine: showDiffLine)
return ContextMenuRequest(target: target, header: ContextActions.header(for: target, projectName: PathUtils.basename(root)),
entries: ContextActions.entries(for: target), anchor: anchor)
}
@@ -93,6 +93,8 @@ extension WorkspaceModel {
overlay.present(.passOn(PassOnRequest(reference: reference, code: code, anchor: anchor)))
case let .passOnFileName(name):
overlay.present(.passOn(PassOnRequest(reference: name, anchor: anchor)))
case let .bouwen(name):
passToAgent("/bouwen \(name)", toast: "/bouwen \(name)")
case let .newFile(directory):
overlay.present(.namePrompt(NamePromptRequest(purpose: .newFile(inDirectory: directory), anchor: anchor)))
case let .newFolder(directory):
@@ -102,6 +104,8 @@ extension WorkspaceModel {
initialName: PathUtils.basename(path), anchor: anchor)))
case let .openDiff(path):
open(path, OpenOptions(showDiff: true))
case let .showDiff(path, line):
if editor?.activePath == path { editor?.openDiff(atLine: line) }
case let .copyTerminalSelection(kind):
if let text = terminal.selection(kind) { app.system.copy(text) }
case let .pasteIntoTerminal(kind):