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
@@ -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 }
}