import SwiftUI /// The context-menu surface: 320pt, `panel`, hairline border, radius 4, menu shadow, 8pt vertical padding. /// Fill it with a `ContextMenuHeader`, then `ContextMenuRow`s and `ContextMenuSeparator`s. public struct ContextMenuPanel: View { var content: Content public init(@ViewBuilder content: () -> Content) { self.content = content() } public var body: some View { VStack(alignment: .leading, spacing: 0) { content } .padding(.vertical, 8) .frame(width: Metrics.contextMenuWidth, alignment: .leading) .background(Palette.panel, in: RoundedRectangle(cornerRadius: Metrics.radiusMedium)) .overlay(RoundedRectangle(cornerRadius: Metrics.radiusMedium).strokeBorder(Palette.raised, lineWidth: Metrics.hairline)) .shadow(color: Palette.menuShadow, radius: Metrics.menuShadowRadius / 2, y: Metrics.menuShadowY) } } /// The path line at the top of a context menu: mono 11, text-4, hairline below. public struct ContextMenuHeader: View { var text: String public init(_ text: String) { self.text = text } public var body: some View { Text(text) .typeRole(Typography.meta) .foregroundStyle(Palette.text4) .lineLimit(1) .truncationMode(.middle) .padding(.horizontal, 14) .padding(.bottom, 8) .frame(maxWidth: .infinity, alignment: .leading) .hairline(.bottom) .padding(.bottom, 8) } } /// One 30pt menu item. Highlighted: `raised` fill, 2pt amber left rule, text-1 500 label, amber icon. /// A tinted icon keeps its tint when highlighted; destructive items stay amber-deep in every state. public struct ContextMenuRow: View { var label: String var systemImage: String? var iconTint: Color? var shortcut: String? var isHighlighted: Bool var isDestructive: Bool public init(_ label: String, systemImage: String? = nil, iconTint: Color? = nil, shortcut: String? = nil, isHighlighted: Bool, isDestructive: Bool = false) { self.label = label self.systemImage = systemImage self.iconTint = iconTint self.shortcut = shortcut self.isHighlighted = isHighlighted self.isDestructive = isDestructive } /// Pass as `systemImage` for the pass-on spark, which has no SF Symbol. nonisolated public static let spark = "helder.spark" public var body: some View { HStack(spacing: 10) { if systemImage == Self.spark { SparkGlyph() .stroke(iconColor, style: StrokeStyle(lineWidth: 1.1 * 13 / 16, lineJoin: .round)) .frame(width: 13, height: 13) .frame(width: 15) } else if let systemImage { Image(systemName: systemImage) .font(.system(size: 12, weight: .light)) .foregroundStyle(iconColor) .frame(width: 15) } Text(label).typeRole(isHighlighted ? Typography.menuItemHighlighted : Typography.menuItem) .foregroundStyle(labelColor) Spacer(minLength: 8) if let shortcut { Text(shortcut).typeRole(Typography.chip).foregroundStyle(Palette.text4) } } .padding(.leading, 16) .padding(.trailing, 14) .frame(height: Metrics.contextMenuItem) .background(isHighlighted ? Palette.raised : .clear) .overlay(alignment: .leading) { Rectangle().fill(isHighlighted ? Palette.amber : .clear).frame(width: Metrics.accentRule) } .contentShape(Rectangle()) } private var labelColor: Color { if isDestructive { return Palette.removedText } return isHighlighted ? Palette.text1 : Palette.text3 } private var iconColor: Color { if isDestructive { return Palette.removedText } if let iconTint { return iconTint } return isHighlighted ? Palette.amber : Palette.text4 } } /// The 1pt rule between menu groups, with 6pt above and below. public struct ContextMenuSeparator: View { public init() {} public var body: some View { Hairline().padding(.vertical, 6) } }