-
-
Notifications
You must be signed in to change notification settings - Fork 8
Fix external monitor geometry conversion #206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import CoreGraphics | ||
|
|
||
| /// Describes one physical display in both coordinate systems Tabby has to bridge. | ||
| /// | ||
| /// Accessibility and CoreGraphics APIs report rectangles in a top-left-origin display space. | ||
| /// AppKit windows use a bottom-left-origin screen space. Keeping both frames together lets the | ||
| /// conversion flip Y inside the display that actually owns the rectangle instead of using a | ||
| /// fragile union of every connected monitor. | ||
| struct DisplayGeometry: Equatable { | ||
| let appKitFrame: CGRect | ||
| let visibleFrame: CGRect | ||
| let coreGraphicsBounds: CGRect | ||
| let backingScaleFactor: CGFloat | ||
| } | ||
|
|
||
| /// Pure display-coordinate conversion shared by AX geometry and tests. | ||
| /// | ||
| /// This type intentionally knows nothing about `NSScreen`; callers pass snapshots of display | ||
| /// geometry in. That keeps the math testable for external-monitor arrangements that are awkward | ||
| /// to reproduce in CI, such as a secondary display above the primary display. | ||
| enum DisplayCoordinateConverter { | ||
| static func appKitRect( | ||
| fromCoreGraphicsRect rect: CGRect, | ||
| displays: [DisplayGeometry] | ||
| ) -> CGRect? { | ||
| guard let display = bestDisplay( | ||
| for: rect, | ||
| displays: displays, | ||
| keyPath: \.coreGraphicsBounds | ||
| ) else { | ||
| return nil | ||
| } | ||
|
|
||
| return appKitRect(fromCoreGraphicsRect: rect, on: display) | ||
| } | ||
|
|
||
| static func appKitRectsFromPixelRect( | ||
| _ rect: CGRect, | ||
| displays: [DisplayGeometry] | ||
| ) -> [CGRect] { | ||
| displays.compactMap { display in | ||
| guard display.backingScaleFactor > 0 else { return nil } | ||
|
|
||
| let pixelBounds = CGRect( | ||
| x: display.coreGraphicsBounds.minX * display.backingScaleFactor, | ||
| y: display.coreGraphicsBounds.minY * display.backingScaleFactor, | ||
| width: display.coreGraphicsBounds.width * display.backingScaleFactor, | ||
| height: display.coreGraphicsBounds.height * display.backingScaleFactor | ||
| ) | ||
|
|
||
| let midpoint = CGPoint(x: rect.midX, y: rect.midY) | ||
| guard pixelBounds.intersects(rect) || pixelBounds.contains(midpoint) else { | ||
| return nil | ||
| } | ||
|
|
||
| let pointRect = CGRect( | ||
| x: display.coreGraphicsBounds.minX | ||
| + (rect.minX - pixelBounds.minX) / display.backingScaleFactor, | ||
| y: display.coreGraphicsBounds.minY | ||
| + (rect.minY - pixelBounds.minY) / display.backingScaleFactor, | ||
| width: rect.width / display.backingScaleFactor, | ||
| height: rect.height / display.backingScaleFactor | ||
| ) | ||
|
|
||
| return appKitRect(fromCoreGraphicsRect: pointRect, on: display) | ||
| } | ||
| } | ||
|
|
||
| private static func appKitRect( | ||
| fromCoreGraphicsRect rect: CGRect, | ||
| on display: DisplayGeometry | ||
| ) -> CGRect { | ||
| let localX = rect.minX - display.coreGraphicsBounds.minX | ||
| let localY = rect.minY - display.coreGraphicsBounds.minY | ||
|
|
||
| return CGRect( | ||
| x: display.appKitFrame.minX + localX, | ||
| y: display.appKitFrame.maxY - localY - rect.height, | ||
| width: rect.width, | ||
| height: rect.height | ||
| ) | ||
| } | ||
|
|
||
| private static func bestDisplay( | ||
| for rect: CGRect, | ||
| displays: [DisplayGeometry], | ||
| keyPath: KeyPath<DisplayGeometry, CGRect> | ||
| ) -> DisplayGeometry? { | ||
| let midpoint = CGPoint(x: rect.midX, y: rect.midY) | ||
|
|
||
| if let containingDisplay = displays.first(where: { | ||
| $0[keyPath: keyPath].contains(midpoint) | ||
| }) { | ||
| return containingDisplay | ||
| } | ||
|
|
||
| return displays | ||
| .filter { $0[keyPath: keyPath].intersects(rect) } | ||
| .max { lhs, rhs in | ||
| intersectionArea(lhs[keyPath: keyPath], rect) | ||
| < intersectionArea(rhs[keyPath: keyPath], rect) | ||
| } | ||
| } | ||
|
|
||
| private static func intersectionArea(_ lhs: CGRect, _ rhs: CGRect) -> CGFloat { | ||
| let intersection = lhs.intersection(rhs) | ||
| guard !intersection.isNull else { return 0 } | ||
| return intersection.width * intersection.height | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import CoreGraphics | ||
| import XCTest | ||
| @testable import tabby | ||
|
|
||
| final class DisplayCoordinateConverterTests: XCTestCase { | ||
| func test_appKitRect_flipsWithinOwningDisplayAbovePrimary() { | ||
| let primary = DisplayGeometry( | ||
| appKitFrame: CGRect(x: 0, y: 0, width: 1440, height: 900), | ||
| visibleFrame: CGRect(x: 0, y: 0, width: 1440, height: 875), | ||
| coreGraphicsBounds: CGRect(x: 0, y: 0, width: 1440, height: 900), | ||
| backingScaleFactor: 2 | ||
| ) | ||
| let displayAbove = DisplayGeometry( | ||
| appKitFrame: CGRect(x: 0, y: 900, width: 1920, height: 1080), | ||
| visibleFrame: CGRect(x: 0, y: 900, width: 1920, height: 1055), | ||
| coreGraphicsBounds: CGRect(x: 0, y: -1080, width: 1920, height: 1080), | ||
| backingScaleFactor: 1 | ||
| ) | ||
|
|
||
| let rect = DisplayCoordinateConverter.appKitRect( | ||
| fromCoreGraphicsRect: CGRect(x: 120, y: -1000, width: 300, height: 20), | ||
| displays: [primary, displayAbove] | ||
| ) | ||
|
|
||
| XCTAssertEqual(rect, CGRect(x: 120, y: 1880, width: 300, height: 20)) | ||
| } | ||
|
|
||
| func test_appKitRect_preservesNegativeXWhenRectCrossesDisplayBoundary() { | ||
| let left = DisplayGeometry( | ||
| appKitFrame: CGRect(x: -1280, y: 0, width: 1280, height: 720), | ||
| visibleFrame: CGRect(x: -1280, y: 0, width: 1280, height: 700), | ||
| coreGraphicsBounds: CGRect(x: -1280, y: 0, width: 1280, height: 720), | ||
| backingScaleFactor: 1 | ||
| ) | ||
| let primary = DisplayGeometry( | ||
| appKitFrame: CGRect(x: 0, y: 0, width: 1440, height: 900), | ||
| visibleFrame: CGRect(x: 0, y: 0, width: 1440, height: 875), | ||
| coreGraphicsBounds: CGRect(x: 0, y: 0, width: 1440, height: 900), | ||
| backingScaleFactor: 2 | ||
| ) | ||
|
|
||
| let rect = DisplayCoordinateConverter.appKitRect( | ||
| fromCoreGraphicsRect: CGRect(x: -20, y: 100, width: 80, height: 20), | ||
| displays: [left, primary] | ||
| ) | ||
|
|
||
| XCTAssertEqual(rect, CGRect(x: -20, y: 780, width: 80, height: 20)) | ||
| } | ||
|
|
||
| func test_appKitRectsFromPixelRect_scalesRelativeToDisplayOrigin() { | ||
| let primary = DisplayGeometry( | ||
| appKitFrame: CGRect(x: 0, y: 0, width: 1440, height: 900), | ||
| visibleFrame: CGRect(x: 0, y: 0, width: 1440, height: 875), | ||
| coreGraphicsBounds: CGRect(x: 0, y: 0, width: 1440, height: 900), | ||
| backingScaleFactor: 2 | ||
| ) | ||
| let rightRetina = DisplayGeometry( | ||
| appKitFrame: CGRect(x: 1440, y: 0, width: 1512, height: 982), | ||
| visibleFrame: CGRect(x: 1440, y: 0, width: 1512, height: 957), | ||
| coreGraphicsBounds: CGRect(x: 1440, y: 0, width: 1512, height: 982), | ||
| backingScaleFactor: 2 | ||
| ) | ||
|
|
||
| let rects = DisplayCoordinateConverter.appKitRectsFromPixelRect( | ||
| CGRect(x: 1440 * 2 + 200, y: 120, width: 80, height: 40), | ||
| displays: [primary, rightRetina] | ||
| ) | ||
|
|
||
| XCTAssertEqual(rects, [CGRect(x: 1540, y: 902, width: 40, height: 20)]) | ||
| } | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.