Skip to content

Commit 0b88461

Browse files
committed
Add Test
1 parent 7eec4c2 commit 0b88461

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

Sources/SwiftTerm/Mac/MacTerminalView.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,14 +880,19 @@ 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)
892897
let row = Int ((frame.height-point.y) / cellDimension.height) + terminal.buffer.yDisp
893898
if row < 0 {

Tests/SwiftTermTests/SelectionTests.swift

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

4040
}
41+
42+
func testMouseHitCorrectWhenScrolled() {
43+
let view = TerminalView(frame: CGRect(origin: .zero, size: .init(width: 10, height: 10)))
44+
45+
for _ in 0..<100 {
46+
view.terminal.feed (text: "12345")
47+
}
48+
49+
// Scroll all the way down, check the bottom-left corner
50+
view.scrollTo(row: 100)
51+
XCTAssertEqual(view.calculateMouseHit(at: CGPoint(x: 0, y: 0)).grid.row, 100)
52+
53+
// Scroll all the way back up, check the top-left corner
54+
view.scrollTo(row: 1)
55+
XCTAssertEqual(view.calculateMouseHit(at: CGPoint(x: 0, y: 10)).grid.row, 1)
56+
}
4157
}

0 commit comments

Comments
 (0)