Skip to content

Commit 5acd0b9

Browse files
Merge pull request migueldeicaza#381 from thecoolwinter/fix/selection-ignores-display-offset
Consider Buffer Y Offset In Mouse Hit
2 parents f55b5b7 + 3f2a901 commit 5acd0b9

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

Sources/SwiftTerm/Mac/MacTerminalView.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -880,20 +880,25 @@ open class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations,
880880
}
881881

882882
func calculateMouseHit (with event: NSEvent) -> (grid: Position, pixels: Position)
883+
{
884+
let point = convert(event.locationInWindow, from: nil)
885+
return calculateMouseHit(at: point)
886+
}
887+
888+
func calculateMouseHit (at point: CGPoint) -> (grid: Position, pixels: Position)
883889
{
884890
func toInt (_ p: NSPoint) -> Position {
885-
891+
886892
let x = min (max (p.x, 0), bounds.width)
887893
let y = min (max (p.y, 0), bounds.height)
888894
return Position (col: Int (x), row: Int (bounds.height-y))
889895
}
890-
let point = convert(event.locationInWindow, from: nil)
891896
let col = Int (point.x / cellDimension.width)
892-
let row = Int ((frame.height-point.y) / cellDimension.height)
897+
let row = Int ((frame.height-point.y) / cellDimension.height) + terminal.buffer.yDisp
893898
if row < 0 {
894899
return (Position(col: 0, row: 0), toInt (point))
895900
}
896-
return (Position(col: min (max (0, col), terminal.cols-1), row: min (row, terminal.rows-1)), toInt (point))
901+
return (Position(col: min (max (0, col), terminal.cols-1), row: row), toInt (point))
897902
}
898903

899904
private func sharedMouseEvent (with event: NSEvent)

Tests/SwiftTermTests/SelectionTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,24 @@ final class SelectionTests: XCTestCase, TerminalDelegate {
3838
selection.selectWordOrExpression(at: Position (col: 0, row: -1), in: terminal.buffer)
3939

4040
}
41+
42+
#if os(macOS)
43+
// Test only on macOS due to differences in how frames are handled on mac and iOS
44+
func testMouseHitCorrectWhenScrolled()
45+
{
46+
let view = TerminalView(frame: CGRect(origin: .zero, size: .init(width: 10, height: 10)))
47+
48+
for _ in 0..<100 {
49+
view.terminal.feed (text: "12345")
50+
}
51+
52+
// Scroll all the way down, check the bottom-left corner
53+
view.scrollTo(row: 100)
54+
XCTAssertEqual(view.calculateMouseHit(at: CGPoint(x: 0, y: 0)).grid.row, 100)
55+
56+
// Scroll all the way back up, check the top-left corner
57+
view.scrollTo(row: 1)
58+
XCTAssertEqual(view.calculateMouseHit(at: CGPoint(x: 0, y: 10)).grid.row, 1)
59+
}
60+
#endif
4161
}

0 commit comments

Comments
 (0)