| 1 | // Ported from the r7 mockups (Controls.swift). |
| 2 | import SwiftUI |
| 3 | |
| 4 | enum ControlState: String, CaseIterable { case rest, hover, pressed, focus, disabled, loading } |
| 5 | |
| 6 | /// The web's button, drawn natively: a face, a full hairline border and a |
| 7 | /// short contact shadow that goes away when the control is pressed in. |
| 8 | struct EquipoButtonStyle: ButtonStyle { |
| 9 | var primary = false |
| 10 | var loading = false |
| 11 | var forced: ControlState? |
| 12 | func makeBody(configuration: Configuration) -> some View { |
| 13 | Face(configuration: configuration, primary: primary, loading: loading || forced == .loading, forced: forced) |
| 14 | } |
| 15 | private struct Face: View { |
| 16 | let configuration: ButtonStyleConfiguration |
| 17 | let primary: Bool |
| 18 | let loading: Bool |
| 19 | let forced: ControlState? |
| 20 | @Environment(\.isEnabled) private var isEnabled |
| 21 | @Environment(\.colorScheme) private var scheme |
| 22 | @State private var hovering = false |
| 23 | var body: some View { |
| 24 | let enabled = isEnabled && forced != .disabled |
| 25 | let pressed = configuration.isPressed || forced == .pressed |
| 26 | let hover = (hovering || forced == .hover) && enabled |
| 27 | let shape = RoundedRectangle(cornerRadius: Metric.control, style: .continuous) |
| 28 | ZStack { |
| 29 | configuration.label.opacity(loading ? 0 : 1) |
| 30 | if loading { ProgressView().controlSize(.small).tint(primary ? Color.white : Palette.secondary).environment(\.colorScheme, primary ? .dark : scheme) } |
| 31 | } |
| 32 | .font(Typo.button) |
| 33 | .foregroundStyle(primary ? Color.white : Palette.controlText) |
| 34 | .padding(.horizontal, 16).padding(.vertical, 6) |
| 35 | .frame(minHeight: Metric.buttonHeight) |
| 36 | .background(shape.fill(fill(pressed: pressed, hover: hover))) |
| 37 | .overlay(shape.strokeBorder(primary ? Palette.primaryEdge : Palette.controlBorder, lineWidth: 1)) |
| 38 | .shadow(color: Palette.contactShadow.opacity(pressed || !enabled ? 0 : 0.08), radius: 1, y: 1) |
| 39 | .overlay { if forced == .focus { shape.inset(by: -3).stroke(Palette.accent.opacity(0.55), lineWidth: 3) } } |
| 40 | .opacity(enabled ? 1 : 0.45) |
| 41 | .contentShape(shape) |
| 42 | .onHover { hovering = $0 } |
| 43 | .animation(.easeOut(duration: 0.12), value: pressed) |
| 44 | .animation(.easeOut(duration: 0.12), value: hover) |
| 45 | } |
| 46 | private func fill(pressed: Bool, hover: Bool) -> Color { |
| 47 | if primary { return pressed ? Palette.primaryEdge : hover ? Palette.primaryFillHover : Palette.primaryFill } |
| 48 | return pressed ? Palette.controlFacePressed : hover ? Palette.controlFaceHover : Palette.controlFace |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | struct EquipoButton: View { |
| 53 | let title: String |
| 54 | var icon: String? |
| 55 | var primary = false |
| 56 | var expanded = false |
| 57 | var loading = false |
| 58 | let action: () -> Void |
| 59 | var body: some View { |
| 60 | Button(action: action) { |
| 61 | HStack(spacing: 7) { |
| 62 | if let icon { Image(systemName: icon) } |
| 63 | Text(title).multilineTextAlignment(.center) |
| 64 | } |
| 65 | .frame(maxWidth: expanded ? .infinity : nil) |
| 66 | .fixedSize(horizontal: false, vertical: true) |
| 67 | } |
| 68 | .buttonStyle(EquipoButtonStyle(primary: primary, loading: loading)) |
| 69 | } |
| 70 | } |
| 71 | struct QuietAction: View { |
| 72 | let title: String |
| 73 | var icon: String? |
| 74 | let action: () -> Void |
| 75 | @State private var hovering = false |
| 76 | var body: some View { |
| 77 | Button(action: action) { |
| 78 | HStack(spacing: 5) { |
| 79 | Text(title).underline(hovering) |
| 80 | if let icon { Image(systemName: icon).imageScale(.small) } |
| 81 | } |
| 82 | .font(Typo.secondary).foregroundStyle(Palette.accent) |
| 83 | .frame(minHeight: Metric.target).contentShape(Rectangle()) |
| 84 | }.buttonStyle(.plain).onHover { hovering = $0 } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /// Every state of the two button kinds on one sheet, for review. |
| 89 | |