import AppKit import ParcelConfig /// Pointer shortcuts supplement the native rows, which retain arrow-key navigation and key equivalents. @MainActor final class MenuQuickControlsView: NSView { private let stack = NSStackView() private(set) var buttons: [NSButton] = [] private var items: [CommandMenuItem] = [] private let dismiss: @MainActor () -> Void init(snapshot: LayoutSnapshot, scope: String, items: [CommandMenuItem], dismiss: @escaping @MainActor () -> Void) { self.dismiss = dismiss super.init(frame: .zero) stack.orientation = .vertical stack.alignment = .leading stack.spacing = 10 stack.translatesAutoresizingMaskIntoConstraints = false addSubview(stack) NSLayoutConstraint.activate([ stack.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 14), stack.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -14), stack.topAnchor.constraint(equalTo: topAnchor, constant: 10), stack.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -10), ]) stack.widthAnchor.constraint(greaterThanOrEqualToConstant: 332).isActive = true let heading = NSTextField(wrappingLabelWithString: scope + " ยท " + snapshot.layout.uiTitle) heading.preferredMaxLayoutWidth = 332 heading.font = .boldSystemFont(ofSize: NSFont.systemFontSize) stack.addArrangedSubview(heading) let preview = MenuArrangementView(snapshot: snapshot) stack.addArrangedSubview(preview) preview.widthAnchor.constraint(equalTo: stack.widthAnchor).isActive = true preview.heightAnchor.constraint(equalToConstant: 90).isActive = true let layoutCommands: [ConfigCommand] = [.layoutGrid, .layoutColumns, .layoutRows, .layoutSide] addRow(items.filter { layoutCommands.contains($0.command) }, images: true) let tiles = NSTextField(labelWithString: MenuText.text("Tile count")) tiles.font = .systemFont(ofSize: NSFont.smallSystemFontSize, weight: .medium) stack.addArrangedSubview(tiles) addRow( items.filter { if case .setWindowCap(let count) = $0.command { return (1...4).contains(count) }; return false }, images: true) let reasons = items.filter { if case .setWindowCap(let count) = $0.command { return (1...4).contains(count) && !$0.isEnabled } return false }.compactMap(\.toolTip) if let reason = reasons.first { let help = NSTextField(wrappingLabelWithString: reason) help.font = .systemFont(ofSize: NSFont.smallSystemFontSize) help.textColor = .secondaryLabelColor help.preferredMaxLayoutWidth = 332 stack.addArrangedSubview(help) } let quickCommands: [ConfigCommand] = [.retile, .toggleBorders, .toggleHoverFocus] addRow(quickCommands.compactMap { command in items.first { $0.command == command } }, images: false) let width = max(360, stack.fittingSize.width + 28) frame.size = NSSize(width: width, height: stack.fittingSize.height + 20) } required init?(coder: NSCoder) { fatalError("init(coder:) is unavailable") } private func addRow(_ choices: [CommandMenuItem], images: Bool) { let row = NSStackView() row.orientation = .horizontal row.distribution = .fillEqually row.spacing = 6 for item in choices { let button = NSButton(title: item.title, target: self, action: #selector(activate(_:))) button.bezelStyle = .regularSquare button.setButtonType(item.command == .retile ? .momentaryPushIn : .pushOnPushOff) button.state = item.state button.isEnabled = item.isEnabled button.font = .menuFont(ofSize: 0) let symbol: String? = switch item.command { case .retile: "arrow.clockwise" case .toggleBorders: "rectangle.inset.filled" case .toggleHoverFocus: "cursorarrow.rays" default: nil } button.image = images ? item.image : symbol.flatMap { NSImage(systemSymbolName: $0, accessibilityDescription: nil) } button.imagePosition = images ? .imageAbove : .imageLeading button.toolTip = item.toolTip ?? item.title button.setAccessibilityLabel(item.title) button.setAccessibilityHelp(item.toolTip) button.identifier = item.identifier button.tag = self.items.count self.items.append(item) buttons.append(button) row.addArrangedSubview(button) button.heightAnchor.constraint(greaterThanOrEqualToConstant: images ? 48 : 32).isActive = true } stack.addArrangedSubview(row) row.widthAnchor.constraint(equalTo: stack.widthAnchor).isActive = true } @objc private func activate(_ sender: NSButton) { guard items.indices.contains(sender.tag) else { return } let item = items[sender.tag] guard item.isEnabled else { return } // Use the same validated, captured workspace as the native menu command. dismiss() item.invoke(item.command) } }