-
-
Notifications
You must be signed in to change notification settings - Fork 143
refactor(datagrid): cell-based sort indicators with native divider behavior #945
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // | ||
| // SortableHeaderCell.swift | ||
| // TablePro | ||
| // | ||
|
|
||
| import AppKit | ||
|
|
||
| @MainActor | ||
| final class SortableHeaderCell: NSTableHeaderCell { | ||
| var sortDirection: SortDirection? | ||
| var sortPriority: Int? | ||
|
|
||
| private static let indicatorPadding: CGFloat = 4 | ||
| private static let indicatorSpacing: CGFloat = 2 | ||
| private static let priorityFontSize: CGFloat = 9 | ||
|
|
||
| override init(textCell string: String) { | ||
| super.init(textCell: string) | ||
| } | ||
|
|
||
| required init(coder: NSCoder) { | ||
| super.init(coder: coder) | ||
| } | ||
|
|
||
| override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) { | ||
| guard let direction = sortDirection else { | ||
| super.drawInterior(withFrame: cellFrame, in: controlView) | ||
| return | ||
| } | ||
|
|
||
| let indicatorImage = Self.indicatorImage(for: direction) | ||
| let indicatorSize = indicatorImage?.size ?? NSSize(width: 9, height: 6) | ||
| let priorityText = priorityNumberString() | ||
| let priorityWidth = priorityText.map { Self.measureWidth(of: $0) } ?? 0 | ||
| let reservedWidth = indicatorSize.width | ||
| + Self.indicatorPadding * 2 | ||
| + (priorityText == nil ? 0 : priorityWidth + Self.indicatorSpacing) | ||
|
|
||
| let titleFrame = NSRect( | ||
| x: cellFrame.minX, | ||
| y: cellFrame.minY, | ||
| width: max(0, cellFrame.width - reservedWidth), | ||
| height: cellFrame.height | ||
| ) | ||
| super.drawInterior(withFrame: titleFrame, in: controlView) | ||
|
|
||
| let indicatorOriginX = cellFrame.maxX - Self.indicatorPadding - indicatorSize.width | ||
| let indicatorOriginY = cellFrame.midY - indicatorSize.height / 2 | ||
| let indicatorRect = NSRect( | ||
| x: indicatorOriginX, | ||
| y: indicatorOriginY, | ||
| width: indicatorSize.width, | ||
| height: indicatorSize.height | ||
| ) | ||
| Self.drawTintedIndicator(image: indicatorImage, in: indicatorRect) | ||
|
|
||
| if let priorityText { | ||
| let textOriginX = indicatorOriginX - Self.indicatorSpacing - priorityWidth | ||
| let textRect = NSRect( | ||
| x: textOriginX, | ||
| y: cellFrame.minY, | ||
| width: priorityWidth, | ||
| height: cellFrame.height | ||
| ) | ||
| Self.drawPriorityText(priorityText, in: textRect) | ||
| } | ||
| } | ||
|
|
||
| override func drawSortIndicator( | ||
| withFrame cellFrame: NSRect, | ||
| in controlView: NSView, | ||
| ascending: Bool, | ||
| priority: Int | ||
| ) {} | ||
|
|
||
| private func priorityNumberString() -> String? { | ||
| guard let sortPriority, sortPriority >= 2 else { return nil } | ||
| return String(sortPriority) | ||
| } | ||
|
|
||
| private static func indicatorImage(for direction: SortDirection) -> NSImage? { | ||
| switch direction { | ||
| case .ascending: | ||
| return NSImage(named: NSImage.Name("NSAscendingSortIndicator")) | ||
| case .descending: | ||
| return NSImage(named: NSImage.Name("NSDescendingSortIndicator")) | ||
| } | ||
| } | ||
|
|
||
| private static func drawTintedIndicator(image: NSImage?, in rect: NSRect) { | ||
| guard let image else { return } | ||
| image.draw( | ||
| in: rect, | ||
| from: .zero, | ||
| operation: .sourceOver, | ||
| fraction: 1.0, | ||
| respectFlipped: true, | ||
| hints: nil | ||
| ) | ||
| } | ||
|
|
||
| private static func drawPriorityText(_ text: String, in rect: NSRect) { | ||
| let attributes = priorityAttributes() | ||
| let textSize = (text as NSString).size(withAttributes: attributes) | ||
| let drawRect = NSRect( | ||
| x: rect.minX, | ||
| y: rect.midY - textSize.height / 2, | ||
| width: rect.width, | ||
| height: textSize.height | ||
| ) | ||
| (text as NSString).draw(in: drawRect, withAttributes: attributes) | ||
| } | ||
|
|
||
| private static func measureWidth(of text: String) -> CGFloat { | ||
| (text as NSString).size(withAttributes: priorityAttributes()).width | ||
| } | ||
|
|
||
| private static func priorityAttributes() -> [NSAttributedString.Key: Any] { | ||
| [ | ||
| .font: NSFont.systemFont(ofSize: priorityFontSize, weight: .medium), | ||
| .foregroundColor: NSColor.secondaryLabelColor | ||
| ] | ||
| } | ||
| } |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
viewDidMoveToWindowunconditionally setswindow?.acceptsMouseMovedEvents = true, but this commit never restores the previous value when the header view is removed or replaced. BecauseacceptsMouseMovedEventsis a window-wide switch (defaultfalse), any window that ever hosts this header will continue dispatching mouse-moved events globally, which can introduce unnecessary event traffic and side effects in other views after the grid is gone.Useful? React with 👍 / 👎.