Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Foundation/NSGeometry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ public func NSPointInRect(_ aPoint: NSPoint, _ aRect: NSRect) -> Bool {

public func NSMouseInRect(_ aPoint: NSPoint, _ aRect: NSRect, _ flipped: Bool) -> Bool {
if flipped {
return aPoint.x >= aRect.minX && aPoint.y >= aRect.minX && aPoint.x < aRect.maxX && aPoint.y < aRect.maxY
return aPoint.x >= aRect.minX && aPoint.y >= aRect.minY && aPoint.x < aRect.maxX && aPoint.y < aRect.maxY
}
return aPoint.x >= aRect.minX && aPoint.y > aRect.minY && aPoint.x < aRect.maxX && aPoint.y <= aRect.maxY
}
Expand Down
17 changes: 17 additions & 0 deletions Tests/Foundation/TestNSGeometry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,23 @@ class TestNSGeometry : XCTestCase {
XCTAssertTrue(NSMouseInRect(p3, r1, true))
}

func test_NSMouseInRect_FlippedUsesMinY() {
// Regression: in flipped coordinates the Y checks must use minY/maxY, not minX.
let r = NSMakeRect(CGFloat(10.0), CGFloat(1.0), CGFloat(2.0), CGFloat(4.0)) // y ∈ [1,5)

let topEdge = NSMakePoint(CGFloat(10.0), CGFloat(1.0))
XCTAssertTrue(NSMouseInRect(topEdge, r, true))

let bottomEdge = NSMakePoint(CGFloat(10.0), CGFloat(5.0))
XCTAssertFalse(NSMouseInRect(bottomEdge, r, true))

let inside = NSMakePoint(CGFloat(11.0), CGFloat(3.0))
XCTAssertTrue(NSMouseInRect(inside, r, true))

let outsideX = NSMakePoint(CGFloat(13.0), CGFloat(3.0))
XCTAssertFalse(NSMouseInRect(outsideX, r, true))
}

func test_NSContainsRect() {
let r1 = NSMakeRect(CGFloat(1.2), CGFloat(3.1), CGFloat(10.0), CGFloat(10.0))
let r2 = NSMakeRect(CGFloat(-2.3), CGFloat(-1.5), CGFloat(1.0), CGFloat(1.0))
Expand Down