import SwiftUI /// One button in a `ButtonGroup`. A toggle carries a state dot; an action has none. public struct GroupItem { public enum Kind: Equatable { case action case toggle(isOn: Bool) } var label: String var keys: String? var kind: Kind var tip: Text var action: () -> Void public init(_ label: String, keys: String? = nil, kind: Kind, tip: Text, action: @escaping () -> Void) { self.label = label self.keys = keys self.kind = kind self.tip = tip self.action = action } } /// The title bar's shared button treatment: buttons edge to edge in one hairline box, a hairline between each. public struct ButtonGroup: View { var items: [GroupItem] var tipEdge: HorizontalAlignment public init(_ items: [GroupItem], tipEdge: HorizontalAlignment = .leading) { self.items = items self.tipEdge = tipEdge } public var body: some View { HStack(spacing: 0) { ForEach(Array(items.enumerated()), id: \.element.label) { index, item in GroupButton(item: item, isFirst: index == 0, isLast: index == items.count - 1) .tip(item.tip, edge: tipEdge) } } .fixedSize() .overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(Palette.raised, lineWidth: Metrics.hairline)) } } /// The fill follows the group's rounded ends; the group is not clipped, so the tooltips can hang below it. private struct GroupButton: View { var item: GroupItem var isFirst: Bool var isLast: Bool @State private var isHovered = false var body: some View { Button(action: item.action) { HStack(spacing: Metrics.groupButtonGap) { if case .toggle = item.kind { Circle().fill(dotColor).frame(width: Metrics.groupButtonDot, height: Metrics.groupButtonDot) } Text(item.label).typeRole(Typography.button).foregroundStyle(labelColor) if let keys = item.keys { Text(keys).typeRole(Typography.keycapSmall).foregroundStyle(isOff ? Palette.text4 : Palette.amber) } } .lineLimit(1) .fixedSize() .padding(Metrics.groupButtonPadding) .background(fill, in: UnevenRoundedRectangle(cornerRadii: corners)) .overlay(alignment: .leading) { if !isFirst { Rectangle().fill(Palette.raised).frame(width: Metrics.hairline) } } .contentShape(Rectangle()) } .buttonStyle(.plain) .onHover { isHovered = $0 } .accessibilityAddTraits(isOn ? .isSelected : []) .animation(Metrics.smallControlTransition, value: isHovered) .animation(Metrics.smallControlTransition, value: item.kind) } private var isOn: Bool { item.kind == .toggle(isOn: true) } private var isOff: Bool { item.kind == .toggle(isOn: false) } private var labelColor: Color { isOn ? Palette.text1 : isOff ? Palette.text4 : Palette.text3 } private var dotColor: Color { isOn ? Palette.amber : Palette.text5 } private var fill: Color { isOn ? Palette.amberTint : isHovered ? Palette.hover : .clear } private var corners: RectangleCornerRadii { let r = Metrics.radiusSmall return RectangleCornerRadii(topLeading: isFirst ? r : 0, bottomLeading: isFirst ? r : 0, bottomTrailing: isLast ? r : 0, topTrailing: isLast ? r : 0) } } /// The icon-only title-bar control (the `?`): a 26pt square with a hairline border. public struct ToolbarIconButton: View { var glyph: String var action: () -> Void @State private var isHovered = false public init(_ glyph: String, action: @escaping () -> Void) { self.glyph = glyph self.action = action } public var body: some View { Button(action: action) { Text(glyph) .typeRole(Typography.keycap) .foregroundStyle(isHovered ? Palette.text1 : Palette.text4) .frame(width: Metrics.toolbarIconButton, height: Metrics.toolbarIconButton) .background(isHovered ? Palette.hover : .clear, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall)) .overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(Palette.raised, lineWidth: Metrics.hairline)) .contentShape(Rectangle()) } .buttonStyle(.plain) .onHover { isHovered = $0 } .animation(Metrics.smallControlTransition, value: isHovered) } }