Liquid Glass (`NSGlassEffectView`) on macOS 26, and an `NSVisualEffectView` before it. React children mount in `contentView`.
| 4 | /// Liquid Glass (`NSGlassEffectView`) on macOS 26, and an |
| 5 | /// `NSVisualEffectView` before it. React children mount in `contentView`. |
| 6 | final class EffectUIGlassView: NSView, EffectUIHostedView { |
| 7 | private let effect: NSView |
| 8 | private let content = EffectUIFlippedView() |
| 9 | |
| 10 | init() { |
| 11 | #if compiler(>=6.2) |
| 12 | if #available(macOS 26.0, *) { |
| 13 | let glass = NSGlassEffectView() |
| 14 | glass.contentView = content |
| 15 | effect = glass |
| 16 | } else { |
| 17 | effect = Self.visualEffect(content) |
| 18 | } |
| 19 | #else |
| 20 | effect = Self.visualEffect(content) |
| 21 | #endif |
| 22 | super.init(frame: .zero) |
| 23 | content.autoresizingMask = [.width, .height] |
| 24 | effect.autoresizingMask = [.width, .height] |
| 25 | addSubview(effect) |
| 26 | } |
| 27 | |
| 28 | @available(*, unavailable) |
| 29 | required init?(coder: NSCoder) { |
| 30 | nil |
| 31 | } |
| 32 | |
| 33 | private static func visualEffect(_ content: NSView) -> NSVisualEffectView { |
| 34 | let view = NSVisualEffectView() |
| 35 | view.material = .sidebar |
| 36 | view.blendingMode = .behindWindow |
| 37 | view.state = .followsWindowActiveState |
| 38 | view.wantsLayer = true |
| 39 | view.layer?.masksToBounds = true |
| 40 | view.addSubview(content) |
| 41 | return view |
| 42 | } |
| 43 | |
| 44 | var contentView: EffectUIPlatformView? { |
| 45 | content |
| 46 | } |
| 47 | |
| 48 | func update(_ changed: [String: Any]) { |
| 49 | let radius = changed.keys.contains("cornerRadius") ? kitNumber(changed["cornerRadius"]) ?? 0 : nil |
| 50 | #if compiler(>=6.2) |
| 51 | if #available(macOS 26.0, *), let glass = effect as? NSGlassEffectView { |
| 52 | if let radius { glass.cornerRadius = radius } |
| 53 | if changed.keys.contains("tintColor") { glass.tintColor = changed["tintColor"] as? NSColor } |
| 54 | return |
| 55 | } |
| 56 | #endif |
| 57 | guard let view = effect as? NSVisualEffectView else { return } |
| 58 | if let radius { view.layer?.cornerRadius = radius } |
| 59 | if changed.keys.contains("material") { |
| 60 | view.material = Self.material(changed["material"] as? String) |
| 61 | } |
| 62 | } |
| 63 |
no test coverage detected