Files
multi-simadmin/apps/ios/Sources/MultiSimAdmin/Theme/AIButtonStyle.swift
T

73 lines
2.5 KiB
Swift

import SwiftUI
public struct AIButtonStyle: ButtonStyle {
public enum Kind: Hashable {
case primary
case secondary
case danger
var foregroundColor: Color { foregroundColor(isEnabled: true) }
var backgroundColor: Color { backgroundColor(isEnabled: true) }
func foregroundColor(isEnabled: Bool) -> Color {
switch self {
case .primary, .danger: return .white.opacity(isEnabled ? 1 : 0.72)
case .secondary: return isEnabled ? AITheme.ink : AITheme.muted
}
}
func backgroundColor(isEnabled: Bool) -> Color {
switch self {
case .primary: return isEnabled ? AITheme.primary : AITheme.primary.opacity(0.35)
case .secondary: return isEnabled ? AITheme.surface : AITheme.canvas
case .danger: return isEnabled ? AITheme.danger : AITheme.danger.opacity(0.30)
}
}
}
public enum Size: Hashable {
case compact
case regular
case large
}
private let kind: Kind
private let size: Size
@Environment(\.isEnabled) private var isEnabled
public init(kind: Kind = .secondary, size: Size = .regular) {
self.kind = kind
self.size = size
}
public func makeBody(configuration: Configuration) -> some View {
let height: CGFloat = switch size {
case .compact: AITheme.compactButtonHeight
case .large: AITheme.largeButtonHeight
case .regular: AITheme.buttonHeight
}
configuration.label
.font(.system(size: 16, weight: .semibold))
.foregroundStyle(kind.foregroundColor(isEnabled: isEnabled))
.frame(minWidth: minimumWidth, minHeight: height)
.padding(.horizontal, kind == .secondary ? 12 : 14)
.background(kind.backgroundColor(isEnabled: isEnabled))
.clipShape(RoundedRectangle(cornerRadius: AITheme.buttonRadius))
.overlay {
RoundedRectangle(cornerRadius: AITheme.buttonRadius)
.strokeBorder(kind == .secondary || !isEnabled ? AITheme.line : .clear)
}
.opacity(configuration.isPressed ? 0.78 : 1)
}
private var minimumWidth: CGFloat {
switch size {
case .compact: return AITheme.compactButtonMinWidth
case .regular: return AITheme.regularButtonMinWidth
case .large: return 92
}
}
}