ActionButton.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // ActionButton.swift
  3. // LoopKitUI
  4. //
  5. // Created by Pete Schwamb on 2020-03-04.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. // TODO: Migrate use sites to ActionButtonStyle
  10. public struct ActionButton: ViewModifier {
  11. private let fontColor: Color
  12. private let backgroundColor: Color
  13. private let edgeColor: Color
  14. private let cornerRadius: CGFloat = 10
  15. public enum ButtonType {
  16. case primary
  17. case secondary
  18. case destructive
  19. case deactivated
  20. }
  21. init(_ style: ButtonType = .primary) {
  22. switch style {
  23. case .primary:
  24. fontColor = .white
  25. backgroundColor = .accentColor
  26. edgeColor = .clear
  27. case .destructive:
  28. fontColor = .white
  29. backgroundColor = .red
  30. edgeColor = .clear
  31. case .secondary:
  32. fontColor = .accentColor
  33. backgroundColor = .clear
  34. edgeColor = .accentColor
  35. case .deactivated:
  36. fontColor = .white
  37. backgroundColor = .gray
  38. edgeColor = .clear
  39. }
  40. }
  41. public func body(content: Content) -> some View {
  42. content
  43. .padding(.all)
  44. .foregroundColor(fontColor)
  45. .font(.headline)
  46. .frame(maxWidth: .infinity)
  47. .background(backgroundColor)
  48. .cornerRadius(cornerRadius)
  49. .overlay(RoundedRectangle(cornerRadius: cornerRadius)
  50. .stroke(edgeColor))
  51. }
  52. }
  53. public extension View {
  54. func actionButtonStyle(_ style: ActionButton.ButtonType = .primary) -> some View {
  55. ModifiedContent(content: self, modifier: ActionButton(style))
  56. }
  57. }