import AppKit import ParcelConfig @MainActor final class MenuStatusView: NSView { private let label = NSTextField(wrappingLabelWithString: "") private let minimap = NSImageView() private let heading = NSTextField(labelWithString: UIStrings.text("Parcel")) let automaticSwitch = NSSwitch() private var automaticItem: CommandMenuItem? private var dismiss: (@MainActor () -> Void)? init(title: String, image: NSImage?) { super.init(frame: .zero) label.font = .menuFont(ofSize: 0) label.textColor = .secondaryLabelColor label.setAccessibilityElement(false) minimap.setAccessibilityElement(false) heading.font = .boldSystemFont(ofSize: NSFont.systemFontSize) heading.setAccessibilityElement(false) addSubview(label) addSubview(heading) addSubview(minimap) automaticSwitch.isHidden = true automaticSwitch.target = self automaticSwitch.action = #selector(toggleAutomatic) addSubview(automaticSwitch) setAccessibilityElement(true) setAccessibilityRole(.staticText) update(title: title, image: image) } required init?(coder: NSCoder) { fatalError("init(coder:) is unavailable") } func update(title: String, image: NSImage?) { label.stringValue = title minimap.image = image setAccessibilityLabel(title) let imageWidth: CGFloat = image == nil ? 0 : 30 let textWidth = min(480, max(210, label.attributedStringValue.size().width)) label.preferredMaxLayoutWidth = textWidth let height = label.fittingSize.height frame.size = CGSize(width: textWidth + imageWidth + 28, height: max(48, height + 34)) needsLayout = true } func update(from other: MenuStatusView) { update(title: other.label.stringValue, image: other.minimap.image) if let item = other.automaticItem, let dismiss = other.dismiss { configureAutomatic(item, dismiss: dismiss) } } func configureAutomatic(_ item: CommandMenuItem, dismiss: @escaping @MainActor () -> Void) { automaticItem = item self.dismiss = dismiss automaticSwitch.isHidden = false automaticSwitch.state = item.state automaticSwitch.isEnabled = item.isEnabled automaticSwitch.setAccessibilityLabel(item.title) automaticSwitch.toolTip = item.title setAccessibilityRole(.group) } @objc private func toggleAutomatic() { guard let automaticItem, automaticItem.isEnabled else { return } dismiss?() automaticItem.invoke(automaticItem.command) } override func layout() { super.layout() let imageWidth: CGFloat = minimap.image == nil ? 0 : 30 let bottom: CGFloat = 5 automaticSwitch.frame = CGRect(x: bounds.maxX - 54, y: bounds.height - 27, width: 40, height: 24) heading.frame = CGRect(x: 14, y: bounds.height - 25, width: bounds.width - 82, height: 20) label.frame = CGRect( x: 14, y: bottom, width: bounds.width - 28 - imageWidth, height: bounds.height - bottom - 29) minimap.frame = CGRect(x: bounds.maxX - 32, y: bottom, width: 18, height: 18) } } @MainActor final class MenuDimSlider: NSView { private let label = NSTextField(labelWithString: MenuText.text("Dimming level")) let slider = NSSlider( value: FocusModeConfiguration().noPeekLevel, minValue: FocusModeConfiguration.noPeekLevels.lowerBound, maxValue: FocusModeConfiguration.noPeekLevels.upperBound, target: nil, action: nil) private var change: @MainActor (Double) -> Void init(value: Double, change: @escaping @MainActor (Double) -> Void) { self.change = change super.init(frame: CGRect(x: 0, y: 0, width: 310, height: 58)) label.font = .menuFont(ofSize: 0) slider.isContinuous = true slider.target = self slider.action = #selector(changed) slider.setAccessibilityLabel(MenuText.text("Dimming level")) addSubview(label) addSubview(slider) setValue(value) } required init?(coder: NSCoder) { fatalError("init(coder:) is unavailable") } func update(from other: MenuDimSlider) { setValue(other.slider.doubleValue) change = other.change } override func layout() { super.layout() label.frame = CGRect(x: 14, y: 32, width: bounds.width - 28, height: 20) slider.frame = CGRect(x: 14, y: 5, width: bounds.width - 28, height: 24) } private func setValue(_ value: Double) { slider.doubleValue = value slider.setAccessibilityValueDescription(slider.doubleValue.formatted(.percent)) } @objc private func changed() { setValue(slider.doubleValue) change(slider.doubleValue) } }