wake-ios/wake/View/Components/Button.swift
2025-08-14 19:49:54 +08:00

115 lines
3.0 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import SwiftUI
//
enum ButtonType {
case primary //
case secondary //
case danger //
}
//
enum ButtonSize {
case small
case medium
case large
}
struct CustomButton: View {
// MARK: -
let text: String
let type: ButtonType
let size: ButtonSize
let fullWidth: Bool //
let isLoading: Bool //
let action: () -> Void //
// MARK: -
var body: some View {
Button(action: {
if !isLoading { //
action()
}
}) {
if isLoading {
//
HStack {
ProgressView() // iOS
.progressViewStyle(CircularProgressViewStyle(tint: .white))
Text("加载中...")
}
} else {
Text(text)
.fontWeight(.medium)
}
}
.font(font(for: size))
.padding(padding(for: size))
.frame(maxWidth: fullWidth ? .infinity : (width(for: size)))
.background(backgroundColor)
.foregroundColor(.white)
.cornerRadius(8)
.overlay(
//
RoundedRectangle(cornerRadius: 8)
.stroke(borderColor, lineWidth: 2)
.opacity(type == .secondary ? 1 : 0)
)
.disabled(isLoading) //
.opacity(isLoading ? 0.8 : 1.0)
}
// MARK: -
private var backgroundColor: Color {
switch type {
case .primary:
return .blue
case .secondary:
return .clear
case .danger:
return .red
}
}
// MARK: -
private var borderColor: Color {
switch type {
case .secondary:
return .gray
default:
return .clear
}
}
// MARK: -
private func font(for size: ButtonSize) -> Font {
switch size {
case .small:
return .caption
case .medium:
return .subheadline
case .large:
return .headline
}
}
// MARK: -
private func padding(for size: ButtonSize) -> EdgeInsets {
let horizontal = CGFloat(16)
let vertical: CGFloat
switch size {
case .small:
vertical = 6
case .medium:
vertical = 10
case .large:
vertical = 14
}
return EdgeInsets(top: vertical, leading: horizontal, bottom: vertical, trailing: horizontal)
}
// MARK: -
private func width(for size: ButtonSize) -> CGFloat? {
return nil //
}
}