From 2d823d8fc063b23dbbc9c9d088caad6eda359c01 Mon Sep 17 00:00:00 2001 From: AZero13 Date: Sat, 6 Dec 2025 11:15:50 -0500 Subject: [PATCH] Typo: NSMouseInRect compares .y to minx instead of minY This causes points to be misclassified, --- Sources/Foundation/NSGeometry.swift | 2 +- Tests/Foundation/TestNSGeometry.swift | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Sources/Foundation/NSGeometry.swift b/Sources/Foundation/NSGeometry.swift index 3dc58a2e48..92b4258ab7 100644 --- a/Sources/Foundation/NSGeometry.swift +++ b/Sources/Foundation/NSGeometry.swift @@ -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 } diff --git a/Tests/Foundation/TestNSGeometry.swift b/Tests/Foundation/TestNSGeometry.swift index a9b921c1a3..a3d08a84a5 100644 --- a/Tests/Foundation/TestNSGeometry.swift +++ b/Tests/Foundation/TestNSGeometry.swift @@ -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))