185 lines
8.0 KiB
Swift
185 lines
8.0 KiB
Swift
import AppKit
|
|
import SwiftUI
|
|
|
|
/// Flat tab navigation shared by the panel and settings, independent of toolbar materials.
|
|
struct NeutralTabPicker<Selection: Hashable>: NSViewRepresentable {
|
|
let title: String
|
|
@Binding var selection: Selection
|
|
let options: [(Selection, String)]
|
|
@Environment(\.locale) private var locale
|
|
|
|
func makeNSView(context: Context) -> FlatTabControl { FlatTabControl() }
|
|
|
|
func updateNSView(_ control: FlatTabControl, context: Context) {
|
|
control.labels = options.map { AppLocalization.string($0.1, locale: locale) }
|
|
control.selectedIndex = options.firstIndex { $0.0 == selection } ?? -1
|
|
control.setAccessibilityLabel(AppLocalization.string(title, locale: locale))
|
|
let binding = $selection, values = options.map(\.0)
|
|
control.onSelect = { index in
|
|
guard values.indices.contains(index) else { return }
|
|
binding.wrappedValue = values[index]
|
|
}
|
|
}
|
|
|
|
func sizeThatFits(_ proposal: ProposedViewSize, nsView: FlatTabControl, context: Context) -> CGSize? {
|
|
CGSize(width: proposal.width ?? nsView.intrinsicContentSize.width,
|
|
height: proposal.height ?? nsView.intrinsicContentSize.height)
|
|
}
|
|
}
|
|
|
|
/// Native buttons retain press/focus behavior; drawing never invokes a system glass bezel.
|
|
@MainActor final class FlatTabControl: NSView {
|
|
var onSelect: ((Int) -> Void)?
|
|
private(set) var buttons: [FlatTabButton] = []
|
|
var labels: [String] = [] {
|
|
didSet {
|
|
guard labels != oldValue else { return }
|
|
if buttons.count != labels.count {
|
|
buttons.forEach { $0.removeFromSuperview() }
|
|
buttons = labels.indices.map { index in
|
|
let button = FlatTabButton()
|
|
button.tag = index
|
|
button.target = self
|
|
button.action = #selector(pressed(_:))
|
|
addSubview(button)
|
|
return button
|
|
}
|
|
}
|
|
for (button, label) in zip(buttons, labels) { button.title = label }
|
|
updateSelection()
|
|
invalidateIntrinsicContentSize()
|
|
needsLayout = true
|
|
}
|
|
}
|
|
var selectedIndex = -1 {
|
|
didSet {
|
|
guard selectedIndex != oldValue else { return }
|
|
updateSelection()
|
|
NSAccessibility.post(element: self, notification: .valueChanged)
|
|
}
|
|
}
|
|
|
|
override init(frame frameRect: NSRect) {
|
|
super.init(frame: frameRect)
|
|
setAccessibilityElement(true)
|
|
setAccessibilityRole(.tabGroup)
|
|
}
|
|
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
|
|
|
override var intrinsicContentSize: NSSize {
|
|
let labelWidth = labels.map { ($0 as NSString).size(withAttributes: [.font: NSFont.systemFont(ofSize: 12, weight: .medium)]).width }.max() ?? 0
|
|
return NSSize(width: CGFloat(labels.count) * (ceil(labelWidth) + 24) + CGFloat(max(0, labels.count - 1)) * 2 + 8, height: 32)
|
|
}
|
|
|
|
override func layout() {
|
|
super.layout()
|
|
guard !buttons.isEmpty else { return }
|
|
let width = max(0, (bounds.width - 8 - CGFloat(buttons.count - 1) * 2) / CGFloat(buttons.count))
|
|
for (index, button) in buttons.enumerated() {
|
|
button.frame = NSRect(x: 4 + CGFloat(index) * (width + 2), y: 4, width: width, height: max(0, bounds.height - 8))
|
|
}
|
|
}
|
|
|
|
override func draw(_ dirtyRect: NSRect) {
|
|
NSColor.labelColor.withAlphaComponent(0.04).setFill()
|
|
NSBezierPath(roundedRect: bounds, xRadius: bounds.height / 2, yRadius: bounds.height / 2).fill()
|
|
}
|
|
|
|
override func accessibilityChildren() -> [Any]? { buttons }
|
|
override func accessibilityTabs() -> [Any]? { buttons }
|
|
override func accessibilitySelectedChildren() -> [Any]? { buttons.filter(\.tabSelected) }
|
|
|
|
private func updateSelection() {
|
|
let hasFocus = buttons.contains { $0 === window?.firstResponder }
|
|
for (index, button) in buttons.enumerated() { button.tabSelected = index == selectedIndex }
|
|
if hasFocus, buttons.indices.contains(selectedIndex) { window?.makeFirstResponder(buttons[selectedIndex]) }
|
|
}
|
|
|
|
@objc private func pressed(_ sender: FlatTabButton) { select(sender.tag) }
|
|
|
|
fileprivate func select(_ index: Int) {
|
|
guard buttons.indices.contains(index), selectedIndex != index else { return }
|
|
selectedIndex = index
|
|
onSelect?(index)
|
|
}
|
|
}
|
|
|
|
@MainActor final class FlatTabButton: NSButton {
|
|
var tabSelected = false {
|
|
didSet { if tabSelected != oldValue { needsDisplay = true } }
|
|
}
|
|
private var hovered = false
|
|
private var hoverTracking: NSTrackingArea?
|
|
|
|
override init(frame frameRect: NSRect) {
|
|
super.init(frame: frameRect)
|
|
setButtonType(.momentaryChange)
|
|
isBordered = false
|
|
focusRingType = .exterior
|
|
setAccessibilityElement(true)
|
|
}
|
|
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
|
|
|
override var acceptsFirstResponder: Bool { isEnabled }
|
|
override var canBecomeKeyView: Bool { isEnabled && tabSelected }
|
|
override func accessibilityRole() -> NSAccessibility.Role? { .radioButton }
|
|
override func accessibilitySubrole() -> NSAccessibility.Subrole? { .tabButtonSubrole }
|
|
override func accessibilityChildren() -> [Any]? { [] }
|
|
override func accessibilityValue() -> Any? { NSNumber(value: tabSelected) }
|
|
override func isAccessibilitySelected() -> Bool { tabSelected }
|
|
override func accessibilityPerformPress() -> Bool {
|
|
guard isEnabled else { return false }
|
|
performClick(nil)
|
|
return true
|
|
}
|
|
|
|
override func draw(_ dirtyRect: NSRect) {
|
|
let pressed = cell?.isHighlighted == true
|
|
NSColor.labelColor.withAlphaComponent(pressed ? 0.16 : (tabSelected ? 0.10 : (hovered ? 0.08 : 0))).setFill()
|
|
NSBezierPath(roundedRect: bounds, xRadius: bounds.height / 2, yRadius: bounds.height / 2).fill()
|
|
let paragraph = NSMutableParagraphStyle()
|
|
paragraph.alignment = .center
|
|
paragraph.lineBreakMode = .byTruncatingTail
|
|
let attributes: [NSAttributedString.Key: Any] = [
|
|
.font: NSFont.systemFont(ofSize: 12, weight: tabSelected ? .medium : .regular),
|
|
.foregroundColor: NSColor.labelColor,
|
|
.paragraphStyle: paragraph
|
|
]
|
|
let height = (title as NSString).size(withAttributes: attributes).height
|
|
(title as NSString).draw(in: NSRect(x: 4, y: (bounds.height - height) / 2, width: max(0, bounds.width - 8), height: height),
|
|
withAttributes: attributes)
|
|
}
|
|
|
|
override func drawFocusRingMask() {
|
|
NSBezierPath(roundedRect: bounds, xRadius: bounds.height / 2, yRadius: bounds.height / 2).fill()
|
|
}
|
|
override var focusRingMaskBounds: NSRect { bounds }
|
|
override func highlight(_ flag: Bool) { super.highlight(flag); needsDisplay = true }
|
|
|
|
override func updateTrackingAreas() {
|
|
super.updateTrackingAreas()
|
|
if let hoverTracking { removeTrackingArea(hoverTracking) }
|
|
let tracking = NSTrackingArea(rect: .zero, options: [.activeAlways, .inVisibleRect, .mouseEnteredAndExited], owner: self)
|
|
addTrackingArea(tracking)
|
|
hoverTracking = tracking
|
|
}
|
|
override func mouseEntered(with event: NSEvent) { hovered = true; needsDisplay = true }
|
|
override func mouseExited(with event: NSEvent) { hovered = false; needsDisplay = true }
|
|
|
|
override func keyDown(with event: NSEvent) {
|
|
guard let tabs = superview as? FlatTabControl,
|
|
event.modifierFlags.intersection([.command, .control, .option]).isEmpty else {
|
|
super.keyDown(with: event); return
|
|
}
|
|
let index: Int
|
|
switch event.keyCode {
|
|
case 123: index = max(0, tabs.selectedIndex - 1)
|
|
case 124: index = min(tabs.buttons.count - 1, tabs.selectedIndex + 1)
|
|
case 115: index = 0
|
|
case 119: index = tabs.buttons.count - 1
|
|
default: super.keyDown(with: event); return
|
|
}
|
|
tabs.select(index)
|
|
}
|
|
}
|