-
-
Notifications
You must be signed in to change notification settings - Fork 335
fix(hig): show the resize cursor over SwiftUI-hosted split dividers #1908
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
119 changes: 119 additions & 0 deletions
119
TablePro/Views/Components/ResizeCursorSplitViewController.swift
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,119 @@ | ||
| // | ||
| // ResizeCursorSplitViewController.swift | ||
| // TablePro | ||
| // | ||
|
|
||
| import AppKit | ||
|
|
||
| internal enum SplitDividerCursorGeometry { | ||
| static func dividerHitRects( | ||
| subviewFrames: [CGRect], | ||
| collapsed: [Bool], | ||
| isVertical: Bool, | ||
| padding: CGFloat, | ||
| bounds: CGRect | ||
| ) -> [CGRect] { | ||
| guard subviewFrames.count == collapsed.count, subviewFrames.count >= 2 else { return [] } | ||
|
|
||
| var rects: [CGRect] = [] | ||
| for index in 0..<(subviewFrames.count - 1) where !collapsed[index] && !collapsed[index + 1] { | ||
| let first = subviewFrames[index] | ||
| let second = subviewFrames[index + 1] | ||
| if isVertical { | ||
| let (start, end) = gap(first.minX, first.maxX, second.minX, second.maxX) | ||
| rects.append( | ||
| CGRect( | ||
| x: start - padding, | ||
| y: bounds.minY, | ||
| width: end - start + padding * 2, | ||
| height: bounds.height | ||
| ) | ||
| ) | ||
| } else { | ||
| let (start, end) = gap(first.minY, first.maxY, second.minY, second.maxY) | ||
| rects.append( | ||
| CGRect( | ||
| x: bounds.minX, | ||
| y: start - padding, | ||
| width: bounds.width, | ||
| height: end - start + padding * 2 | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
| return rects | ||
| } | ||
|
|
||
| static func isWithinDivider(point: CGPoint, hitRects: [CGRect]) -> Bool { | ||
| hitRects.contains { $0.contains(point) } | ||
| } | ||
|
|
||
| private static func gap( | ||
| _ firstMin: CGFloat, | ||
| _ firstMax: CGFloat, | ||
| _ secondMin: CGFloat, | ||
| _ secondMax: CGFloat | ||
| ) -> (start: CGFloat, end: CGFloat) { | ||
| firstMax <= secondMin ? (firstMax, secondMin) : (secondMax, firstMin) | ||
| } | ||
| } | ||
|
|
||
| @MainActor | ||
| internal class ResizeCursorSplitViewController: NSSplitViewController { | ||
| private static let hitPadding: CGFloat = 3 | ||
|
|
||
| private var isShowingResizeCursor = false | ||
|
|
||
| override func viewDidLoad() { | ||
| super.viewDidLoad() | ||
| let area = NSTrackingArea( | ||
| rect: .zero, | ||
| options: [.activeInKeyWindow, .mouseMoved, .mouseEnteredAndExited, .inVisibleRect], | ||
| owner: self, | ||
| userInfo: nil | ||
| ) | ||
| splitView.addTrackingArea(area) | ||
| } | ||
|
|
||
| override func mouseMoved(with event: NSEvent) { | ||
| let point = splitView.convert(event.locationInWindow, from: nil) | ||
| guard isWithinDivider(point) else { | ||
| restoreCursorIfNeeded() | ||
| super.mouseMoved(with: event) | ||
| return | ||
| } | ||
| resizeCursor.set() | ||
| isShowingResizeCursor = true | ||
| } | ||
|
|
||
| override func mouseExited(with event: NSEvent) { | ||
| restoreCursorIfNeeded() | ||
| super.mouseExited(with: event) | ||
| } | ||
|
|
||
| private func restoreCursorIfNeeded() { | ||
| guard isShowingResizeCursor else { return } | ||
| NSCursor.arrow.set() | ||
| isShowingResizeCursor = false | ||
| } | ||
|
|
||
| private func isWithinDivider(_ point: CGPoint) -> Bool { | ||
| let hitRects = SplitDividerCursorGeometry.dividerHitRects( | ||
| subviewFrames: splitView.subviews.map(\.frame), | ||
| collapsed: splitView.subviews.map { splitView.isSubviewCollapsed($0) }, | ||
| isVertical: splitView.isVertical, | ||
| padding: Self.hitPadding, | ||
| bounds: splitView.bounds | ||
| ) | ||
| return SplitDividerCursorGeometry.isWithinDivider(point: point, hitRects: hitRects) | ||
| } | ||
|
|
||
| private var resizeCursor: NSCursor { | ||
| if #available(macOS 15.0, *) { | ||
| return splitView.isVertical | ||
| ? .columnResize(directions: [.left, .right]) | ||
| : .rowResize(directions: [.up, .down]) | ||
| } | ||
| return splitView.isVertical ? .resizeLeftRight : .resizeUpDown | ||
| } | ||
| } | ||
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
93 changes: 93 additions & 0 deletions
93
TableProTests/Views/Components/SplitDividerCursorGeometryTests.swift
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,93 @@ | ||
| import AppKit | ||
| import Foundation | ||
| @testable import TablePro | ||
| import Testing | ||
|
|
||
| @Suite("Split divider cursor geometry") | ||
| @MainActor | ||
| struct SplitDividerCursorGeometryTests { | ||
| @Test("A vertical split places one padded, full-height rect over the divider") | ||
| func verticalSplitProducesOneRect() { | ||
| let rects = SplitDividerCursorGeometry.dividerHitRects( | ||
| subviewFrames: [ | ||
| CGRect(x: 0, y: 0, width: 150, height: 200), | ||
| CGRect(x: 151, y: 0, width: 149, height: 200) | ||
| ], | ||
| collapsed: [false, false], | ||
| isVertical: true, | ||
| padding: 3, | ||
| bounds: CGRect(x: 0, y: 0, width: 300, height: 200) | ||
| ) | ||
| #expect(rects == [CGRect(x: 147, y: 0, width: 7, height: 200)]) | ||
| } | ||
|
|
||
| @Test("A horizontal split places one padded, full-width rect over the divider") | ||
| func horizontalSplitProducesOneRect() { | ||
| let rects = SplitDividerCursorGeometry.dividerHitRects( | ||
| subviewFrames: [ | ||
| CGRect(x: 0, y: 0, width: 200, height: 150), | ||
| CGRect(x: 0, y: 151, width: 200, height: 149) | ||
| ], | ||
| collapsed: [false, false], | ||
| isVertical: false, | ||
| padding: 3, | ||
| bounds: CGRect(x: 0, y: 0, width: 200, height: 300) | ||
| ) | ||
| #expect(rects == [CGRect(x: 0, y: 147, width: 200, height: 7)]) | ||
| } | ||
|
|
||
| @Test("Three panes produce two dividers") | ||
| func threePanesProduceTwoDividers() { | ||
| let rects = SplitDividerCursorGeometry.dividerHitRects( | ||
| subviewFrames: [ | ||
| CGRect(x: 0, y: 0, width: 150, height: 200), | ||
| CGRect(x: 151, y: 0, width: 150, height: 200), | ||
| CGRect(x: 302, y: 0, width: 159, height: 200) | ||
| ], | ||
| collapsed: [false, false, false], | ||
| isVertical: true, | ||
| padding: 3, | ||
| bounds: CGRect(x: 0, y: 0, width: 461, height: 200) | ||
| ) | ||
| #expect(rects == [ | ||
| CGRect(x: 147, y: 0, width: 7, height: 200), | ||
| CGRect(x: 298, y: 0, width: 7, height: 200) | ||
| ]) | ||
| } | ||
|
|
||
| @Test("A collapsed pane drops the divider next to it") | ||
| func collapsedPaneDropsAdjacentDivider() { | ||
| let rects = SplitDividerCursorGeometry.dividerHitRects( | ||
| subviewFrames: [ | ||
| CGRect(x: 0, y: 0, width: 150, height: 200), | ||
| CGRect(x: 151, y: 0, width: 150, height: 200), | ||
| CGRect(x: 302, y: 0, width: 159, height: 200) | ||
| ], | ||
| collapsed: [false, false, true], | ||
| isVertical: true, | ||
| padding: 3, | ||
| bounds: CGRect(x: 0, y: 0, width: 461, height: 200) | ||
| ) | ||
| #expect(rects == [CGRect(x: 147, y: 0, width: 7, height: 200)]) | ||
| } | ||
|
|
||
| @Test("Fewer than two panes produce no dividers") | ||
| func singlePaneProducesNoDividers() { | ||
| let rects = SplitDividerCursorGeometry.dividerHitRects( | ||
| subviewFrames: [CGRect(x: 0, y: 0, width: 300, height: 200)], | ||
| collapsed: [false], | ||
| isVertical: true, | ||
| padding: 3, | ||
| bounds: CGRect(x: 0, y: 0, width: 300, height: 200) | ||
| ) | ||
| #expect(rects.isEmpty) | ||
| } | ||
|
|
||
| @Test("A point inside the divider is detected, one outside is not") | ||
| func hitTestingMatchesTheDividerRect() { | ||
| let hitRects = [CGRect(x: 147, y: 0, width: 7, height: 200)] | ||
| #expect(SplitDividerCursorGeometry.isWithinDivider(point: CGPoint(x: 150, y: 100), hitRects: hitRects)) | ||
| #expect(!SplitDividerCursorGeometry.isWithinDivider(point: CGPoint(x: 100, y: 100), hitRects: hitRects)) | ||
| #expect(!SplitDividerCursorGeometry.isWithinDivider(point: CGPoint(x: 200, y: 100), hitRects: hitRects)) | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When building with the documented Xcode 15/macOS 14 SDK,
NSCursor.columnResize(directions:)androwResize(directions:)are not declared, and the#available(macOS 15.0, *)runtime check does not prevent the compiler from resolving those symbols. Since README/CONTRIBUTING still advertise Xcode 15+ support, this makes the app fail to compile for a supported build environment; use the existingresizeLeftRight/resizeUpDownAPIs or hide the newer symbols behind an SDK-safe shim.Useful? React with 👍 / 👎.