// Ported from the r7 mockups (Controls.swift). import SwiftUI enum ControlState: String, CaseIterable { case rest, hover, pressed, focus, disabled, loading } /// The web's button, drawn natively: a face, a full hairline border and a /// short contact shadow that goes away when the control is pressed in. struct EquipoButtonStyle: ButtonStyle { var primary = false var loading = false var forced: ControlState? func makeBody(configuration: Configuration) -> some View { Face(configuration: configuration, primary: primary, loading: loading || forced == .loading, forced: forced) } private struct Face: View { let configuration: ButtonStyleConfiguration let primary: Bool let loading: Bool let forced: ControlState? @Environment(\.isEnabled) private var isEnabled @Environment(\.colorScheme) private var scheme @State private var hovering = false var body: some View { let enabled = isEnabled && forced != .disabled let pressed = configuration.isPressed || forced == .pressed let hover = (hovering || forced == .hover) && enabled let shape = RoundedRectangle(cornerRadius: Metric.control, style: .continuous) ZStack { configuration.label.opacity(loading ? 0 : 1) if loading { ProgressView().controlSize(.small).tint(primary ? Color.white : Palette.secondary).environment(\.colorScheme, primary ? .dark : scheme) } } .font(Typo.button) .foregroundStyle(primary ? Color.white : Palette.controlText) .padding(.horizontal, 16).padding(.vertical, 6) .frame(minHeight: Metric.buttonHeight) .background(shape.fill(fill(pressed: pressed, hover: hover))) .overlay(shape.strokeBorder(primary ? Palette.primaryEdge : Palette.controlBorder, lineWidth: 1)) .shadow(color: Palette.contactShadow.opacity(pressed || !enabled ? 0 : 0.08), radius: 1, y: 1) .overlay { if forced == .focus { shape.inset(by: -3).stroke(Palette.accent.opacity(0.55), lineWidth: 3) } } .opacity(enabled ? 1 : 0.45) .contentShape(shape) .onHover { hovering = $0 } .animation(.easeOut(duration: 0.12), value: pressed) .animation(.easeOut(duration: 0.12), value: hover) } private func fill(pressed: Bool, hover: Bool) -> Color { if primary { return pressed ? Palette.primaryEdge : hover ? Palette.primaryFillHover : Palette.primaryFill } return pressed ? Palette.controlFacePressed : hover ? Palette.controlFaceHover : Palette.controlFace } } } struct EquipoButton: View { let title: String var icon: String? var primary = false var expanded = false var loading = false let action: () -> Void var body: some View { Button(action: action) { HStack(spacing: 7) { if let icon { Image(systemName: icon) } Text(title).multilineTextAlignment(.center) } .frame(maxWidth: expanded ? .infinity : nil) .fixedSize(horizontal: false, vertical: true) } .buttonStyle(EquipoButtonStyle(primary: primary, loading: loading)) } } struct QuietAction: View { let title: String var icon: String? let action: () -> Void @State private var hovering = false var body: some View { Button(action: action) { HStack(spacing: 5) { Text(title).underline(hovering) if let icon { Image(systemName: icon).imageScale(.small) } } .font(Typo.secondary).foregroundStyle(Palette.accent) .frame(minHeight: Metric.target).contentShape(Rectangle()) }.buttonStyle(.plain).onHover { hovering = $0 } } } /// Every state of the two button kinds on one sheet, for review.