ActionButtonStyle.swift 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //
  2. // ActionButtonStyle.swift
  3. // LoopKitUI
  4. //
  5. // Created by Michael Pangburn on 4/15/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. public struct ActionButtonStyle: ButtonStyle {
  10. public enum ButtonType {
  11. case primary
  12. case secondary
  13. case destructive
  14. }
  15. private let fontColor: Color
  16. private let backgroundColor: Color
  17. private let edgeColor: Color
  18. private let cornerRadius: CGFloat = 10
  19. private let squidge: CGFloat = 1
  20. public init(_ style: ButtonType = .primary) {
  21. switch style {
  22. case .primary:
  23. fontColor = .white
  24. backgroundColor = .accentColor
  25. edgeColor = .clear
  26. case .destructive:
  27. fontColor = .white
  28. backgroundColor = .red
  29. edgeColor = .clear
  30. case .secondary:
  31. fontColor = .accentColor
  32. backgroundColor = .clear
  33. edgeColor = .accentColor
  34. }
  35. }
  36. public func makeBody(configuration: Configuration) -> some View {
  37. configuration.label
  38. .padding(configuration.isPressed ? -squidge : 0)
  39. .padding()
  40. .foregroundColor(fontColor)
  41. .font(.headline)
  42. .frame(maxWidth: .infinity)
  43. .background(backgroundColor)
  44. .overlay(Color(.secondarySystemBackground).opacity(configuration.isPressed ? 0.35 : 0))
  45. .cornerRadius(cornerRadius)
  46. .padding(configuration.isPressed ? squidge : 0)
  47. .overlay(RoundedRectangle(cornerRadius: cornerRadius)
  48. .stroke(edgeColor))
  49. .contentShape(Rectangle())
  50. }
  51. }