SPB Git

Repository Public License

Components.swift 13.1 KB 360 lines swift

1// ORIX Arcade2// File: DesignSystem/Components.swift3// Description: Reusable SwiftUI components — buttons, glass panels, chips, stat tiles, counters, stars, progress bars, segmented control and setting rows.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import SwiftUI89// MARK: - Buttons1011struct OrixPrimaryButtonStyle: ButtonStyle {12    var theme: GameTheme = .orix13    var expand = true1415    func makeBody(configuration: Configuration) -> some View {16        configuration.label17            .font(OrixFont.button)18            .foregroundStyle(OrixColor.ink)19            .padding(.horizontal, OrixSpace.lg)20            .frame(minHeight: 54)21            .frame(maxWidth: expand ? .infinity : nil)22            .background(theme.gradient, in: Capsule())23            .overlay(24                Capsule().strokeBorder(25                    LinearGradient(colors: [.white.opacity(0.7), .white.opacity(0.05)], startPoint: .top, endPoint: .bottom),26                    lineWidth: 1.2)27            )28            .shadow(color: theme.primary.opacity(configuration.isPressed ? 0.2 : 0.45), radius: configuration.isPressed ? 6 : 16, y: 6)29            .scaleEffect(configuration.isPressed ? 0.96 : 1)30            .animation(OrixMotion.press, value: configuration.isPressed)31    }32}3334struct OrixSecondaryButtonStyle: ButtonStyle {35    var tint: Color = OrixColor.textPrimary36    var expand = true3738    func makeBody(configuration: Configuration) -> some View {39        configuration.label40            .font(OrixFont.button)41            .foregroundStyle(tint)42            .padding(.horizontal, OrixSpace.lg)43            .frame(minHeight: 54)44            .frame(maxWidth: expand ? .infinity : nil)45            .background(Color.white.opacity(configuration.isPressed ? 0.16 : 0.08), in: Capsule())46            .overlay(Capsule().strokeBorder(OrixColor.stroke, lineWidth: 1))47            .scaleEffect(configuration.isPressed ? 0.97 : 1)48            .animation(OrixMotion.press, value: configuration.isPressed)49    }50}5152/// A button that also plays the standard tap sound and haptic.53struct OrixButton: View {54    enum Kind { case primary, secondary, destructive }5556    let title: String57    var systemImage: String?58    var kind: Kind = .primary59    var theme: GameTheme = .orix60    var expand = true61    let action: () -> Void62    @Environment(ArcadeServices.self) private var services6364    var body: some View {65        let button = Button {66            services.feedbackTap()67            action()68        } label: {69            HStack(spacing: OrixSpace.xs) {70                if let systemImage { Image(systemName: systemImage).font(.system(size: 17, weight: .heavy)) }71                Text(title).lineLimit(1).minimumScaleFactor(0.7)72            }73        }74        switch kind {75        case .primary: button.buttonStyle(OrixPrimaryButtonStyle(theme: theme, expand: expand))76        case .secondary: button.buttonStyle(OrixSecondaryButtonStyle(expand: expand))77        case .destructive: button.buttonStyle(OrixSecondaryButtonStyle(tint: OrixColor.danger, expand: expand))78        }79    }80}8182/// Round icon-only button (48 pt target) used for pause, close and HUD actions.83struct OrixIconButton: View {84    let systemImage: String85    let label: String86    var tint: Color = OrixColor.textPrimary87    let action: () -> Void88    @Environment(ArcadeServices.self) private var services8990    var body: some View {91        Button {92            services.feedbackTap()93            action()94        } label: {95            Image(systemName: systemImage)96                .font(.system(size: 17, weight: .heavy))97                .foregroundStyle(tint)98                .frame(width: OrixSpace.hit, height: OrixSpace.hit)99                .background(.ultraThinMaterial, in: Circle())100                .overlay(Circle().strokeBorder(OrixColor.stroke, lineWidth: 1))101        }102        .buttonStyle(OrixPressStyle())103        .accessibilityLabel(label)104    }105}106107struct OrixPressStyle: ButtonStyle {108    func makeBody(configuration: Configuration) -> some View {109        configuration.label110            .scaleEffect(configuration.isPressed ? 0.94 : 1)111            .opacity(configuration.isPressed ? 0.85 : 1)112            .animation(OrixMotion.press, value: configuration.isPressed)113    }114}115116// MARK: - Surfaces117118struct GlassPanel: ViewModifier {119    var radius: CGFloat = OrixRadius.lg120    var tint: Color = .white121    var padding: CGFloat = OrixSpace.md122123    func body(content: Content) -> some View {124        content125            .padding(padding)126            .background(127                RoundedRectangle(cornerRadius: radius, style: .continuous)128                    .fill(OrixColor.surface.opacity(0.82))129                    .overlay(130                        RoundedRectangle(cornerRadius: radius, style: .continuous)131                            .fill(LinearGradient(colors: [tint.opacity(0.10), .clear], startPoint: .top, endPoint: .center))132                    )133            )134            .overlay(RoundedRectangle(cornerRadius: radius, style: .continuous).strokeBorder(OrixColor.stroke, lineWidth: 1))135    }136}137138extension View {139    func glassPanel(radius: CGFloat = OrixRadius.lg, tint: Color = .white, padding: CGFloat = OrixSpace.md) -> some View {140        modifier(GlassPanel(radius: radius, tint: tint, padding: padding))141    }142}143144struct OrixChip: View {145    let text: String146    var systemImage: String?147    var tint: Color = OrixColor.textPrimary148    var filled = false149150    var body: some View {151        HStack(spacing: 5) {152            if let systemImage { Image(systemName: systemImage).font(.system(size: 10, weight: .black)) }153            Text(text).font(OrixFont.overline).tracking(0.6)154        }155        .foregroundStyle(filled ? OrixColor.ink : tint)156        .padding(.horizontal, 10)157        .padding(.vertical, 6)158        .background(filled ? AnyShapeStyle(tint) : AnyShapeStyle(Color.black.opacity(0.42)), in: Capsule())159        .overlay(Capsule().strokeBorder(tint.opacity(filled ? 0 : 0.35), lineWidth: 1))160    }161}162163struct SectionHeader: View {164    let title: String165    var trailing: String?166167    var body: some View {168        HStack(alignment: .firstTextBaseline) {169            Text(title.uppercased()).font(OrixFont.overline).tracking(1.6).foregroundStyle(OrixColor.textTertiary)170            Spacer()171            if let trailing { Text(trailing).font(OrixFont.caption).foregroundStyle(OrixColor.textSecondary) }172        }173        .accessibilityAddTraits(.isHeader)174    }175}176177struct StatTile: View {178    let value: String179    let label: String180    let systemImage: String181    var tint: Color = OrixColor.iris182183    var body: some View {184        VStack(alignment: .leading, spacing: 6) {185            Image(systemName: systemImage).font(.system(size: 15, weight: .heavy)).foregroundStyle(tint)186            Text(value)187                .font(.system(.title3, design: .rounded, weight: .heavy).monospacedDigit())188                .foregroundStyle(OrixColor.textPrimary)189                .lineLimit(1).minimumScaleFactor(0.6)190                .contentTransition(.numericText())191            Text(label).font(OrixFont.caption).foregroundStyle(OrixColor.textSecondary).lineLimit(1).minimumScaleFactor(0.7)192        }193        .frame(maxWidth: .infinity, alignment: .leading)194        .glassPanel(radius: OrixRadius.md, tint: tint, padding: OrixSpace.sm)195        .accessibilityElement(children: .ignore)196        .accessibilityLabel("\(label): \(value)")197    }198}199200// MARK: - Counters, stars, progress201202/// Number that counts up smoothly to its target (score recaps).203struct CountingNumber: View, Animatable {204    var value: Double205    var font: Font = OrixFont.hud(44)206    var color: Color = OrixColor.textPrimary207208    var animatableData: Double {209        get { value }210        set { value = newValue }211    }212213    var body: some View {214        Text(Int(value.rounded()).formatted(.number.grouping(.automatic)))215            .font(font)216            .foregroundStyle(color)217    }218}219220struct StarRow: View {221    let earned: Int222    var total = 3223    var size: CGFloat = 34224    var animated = true225    @State private var shown = 0226227    var body: some View {228        HStack(spacing: size * 0.18) {229            ForEach(0..<total, id: \.self) { index in230                let lit = index < (animated ? shown : earned)231                Image(systemName: "star.fill")232                    .font(.system(size: size, weight: .black))233                    .foregroundStyle(lit ? AnyShapeStyle(LinearGradient(colors: [Color(hex: 0xFFE08A), OrixColor.gold], startPoint: .top, endPoint: .bottom))234                                         : AnyShapeStyle(Color.white.opacity(0.12)))235                    .shadow(color: lit ? OrixColor.gold.opacity(0.6) : .clear, radius: 8)236                    .scaleEffect(lit ? 1 : 0.82)237                    .rotationEffect(.degrees(index == 0 ? -8 : index == total - 1 ? 8 : 0))238                    .offset(y: index == total / 2 ? -size * 0.14 : 0)239            }240        }241        .accessibilityElement(children: .ignore)242        .accessibilityLabel("\(earned) of \(total) stars")243        .task(id: earned) {244            guard animated else { return }245            shown = 0246            if OrixAppearance.reduceMotion { shown = earned; return }247            for index in 0..<earned {248                try? await Task.sleep(for: .milliseconds(index == 0 ? 250 : 280))249                withAnimation(OrixMotion.bouncy) { shown = index + 1 }250            }251        }252    }253}254255struct OrixProgressBar: View {256    let fraction: Double257    var theme: GameTheme = .orix258    var height: CGFloat = 6259260    var body: some View {261        GeometryReader { geo in262            ZStack(alignment: .leading) {263                Capsule().fill(Color.white.opacity(0.12))264                Capsule().fill(theme.gradient)265                    .frame(width: max(height, geo.size.width * min(max(fraction, 0), 1)))266                    .opacity(fraction > 0 ? 1 : 0)267            }268        }269        .frame(height: height)270        .accessibilityElement()271        .accessibilityLabel("Progress")272        .accessibilityValue("\(Int(min(max(fraction, 0), 1) * 100)) percent")273    }274}275276// MARK: - Segmented control277278struct OrixSegmented<Value: Hashable>: View {279    let options: [(value: Value, label: String)]280    @Binding var selection: Value281    var theme: GameTheme = .orix282    @Environment(ArcadeServices.self) private var services283    @Namespace private var namespace284285    var body: some View {286        HStack(spacing: 4) {287            ForEach(Array(options.enumerated()), id: \.offset) { _, option in288                let selected = option.value == selection289                Button {290                    services.feedbackTap()291                    withAnimation(OrixMotion.snappy) { selection = option.value }292                } label: {293                    Text(option.label)294                        .font(OrixFont.caption)295                        .foregroundStyle(selected ? OrixColor.ink : OrixColor.textSecondary)296                        .lineLimit(1).minimumScaleFactor(0.7)297                        .frame(maxWidth: .infinity, minHeight: 40)298                        .background {299                            if selected {300                                Capsule().fill(theme.gradient).matchedGeometryEffect(id: "seg", in: namespace)301                            }302                        }303                        .contentShape(Capsule())304                }305                .buttonStyle(.plain)306                .accessibilityAddTraits(selected ? .isSelected : [])307            }308        }309        .padding(4)310        .background(Color.white.opacity(0.07), in: Capsule())311        .overlay(Capsule().strokeBorder(OrixColor.stroke, lineWidth: 1))312    }313}314315// MARK: - Setting rows316317struct OrixToggleRow: View {318    let title: String319    var detail: String?320    let systemImage: String321    @Binding var isOn: Bool322    var tint: Color = OrixColor.iris323324    var body: some View {325        Toggle(isOn: $isOn) {326            HStack(spacing: OrixSpace.sm) {327                Image(systemName: systemImage).font(.system(size: 16, weight: .bold)).foregroundStyle(tint).frame(width: 26)328                VStack(alignment: .leading, spacing: 2) {329                    Text(title).font(OrixFont.body).foregroundStyle(OrixColor.textPrimary)330                    if let detail { Text(detail).font(OrixFont.caption).foregroundStyle(OrixColor.textSecondary) }331                }332            }333        }334        .tint(tint)335        .frame(minHeight: OrixSpace.hit)336    }337}338339struct OrixSliderRow: View {340    let title: String341    let systemImage: String342    @Binding var value: Double343    var tint: Color = OrixColor.iris344345    var body: some View {346        VStack(alignment: .leading, spacing: 4) {347            HStack(spacing: OrixSpace.sm) {348                Image(systemName: systemImage).font(.system(size: 16, weight: .bold)).foregroundStyle(tint).frame(width: 26)349                Text(title).font(OrixFont.body).foregroundStyle(OrixColor.textPrimary)350                Spacer()351                Text("\(Int(value * 100))%").font(OrixFont.caption.monospacedDigit()).foregroundStyle(OrixColor.textSecondary)352            }353            Slider(value: $value, in: 0...1).tint(tint)354                .accessibilityLabel(title)355                .accessibilityValue("\(Int(value * 100)) percent")356        }357        .frame(minHeight: OrixSpace.hit)358    }359}360
Files main
Menu

Theme