An `NSSegmentedControl` that fills the view's width, centred in its height. On macOS 26 it sits on capsule glass, which `tintColor` tints.
| 14 | /// An `NSSegmentedControl` that fills the view's width, centred in its |
| 15 | /// height. On macOS 26 it sits on capsule glass, which `tintColor` tints. |
| 16 | final class EffectUISegmentedView: NSView, EffectUIHostedView { |
| 17 | private let context: EffectUIContext |
| 18 | private let control = NSSegmentedControl() |
| 19 | private var container: NSView |
| 20 | private var selected: Int? |
| 21 | private var momentary: Bool { |
| 22 | control.trackingMode == .momentary |
| 23 | } |
| 24 | |
| 25 | init(_ context: EffectUIContext) { |
| 26 | self.context = context |
| 27 | container = control |
| 28 | super.init(frame: .zero) |
| 29 | control.segmentDistribution = .fill |
| 30 | control.target = self |
| 31 | control.action = #selector(changed) |
| 32 | #if compiler(>=6.2) |
| 33 | if #available(macOS 26.0, *) { |
| 34 | control.borderShape = .capsule |
| 35 | let glass = NSGlassEffectView() |
| 36 | glass.contentView = control |
| 37 | container = glass |
| 38 | } |
| 39 | #endif |
| 40 | addSubview(container) |
| 41 | } |
| 42 | |
| 43 | @available(*, unavailable) |
| 44 | required init?(coder: NSCoder) { |
| 45 | nil |
| 46 | } |
| 47 | |
| 48 | func update(_ changed: [String: Any]) { |
| 49 | if changed.keys.contains("momentary") { |
| 50 | control.trackingMode = kitBool(changed["momentary"]) ? .momentary : .selectOne |
| 51 | } |
| 52 | if let segments = changed["segments"] as? [Any] { |
| 53 | control.segmentCount = segments.count |
| 54 | for (index, item) in segments.enumerated() { |
| 55 | let (label, symbol) = segmentParts(item) |
| 56 | let image = symbol.flatMap { NSImage(systemSymbolName: $0, accessibilityDescription: label) } |
| 57 | control.setImage(image, forSegment: index) |
| 58 | control.setLabel(image == nil ? label : "", forSegment: index) |
| 59 | control.setToolTip(image != nil && !label.isEmpty ? label : nil, forSegment: index) |
| 60 | } |
| 61 | } |
| 62 | if changed.keys.contains("size") { control.controlSize = kitControlSize(changed["size"]) } |
| 63 | if changed.keys.contains("disabled") { control.isEnabled = !kitBool(changed["disabled"]) } |
| 64 | #if compiler(>=6.2) |
| 65 | if #available(macOS 26.0, *), changed.keys.contains("tintColor"), let glass = container as? NSGlassEffectView { |
| 66 | glass.tintColor = changed["tintColor"] as? NSColor |
| 67 | } |
| 68 | #endif |
| 69 | if changed.keys.contains("selected") { selected = (changed["selected"] as? NSNumber)?.intValue } |
| 70 | showSelected() |
| 71 | place() |
| 72 | } |
| 73 |