ActionButton.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. case plain
  21. }
  22. init(_ style: ButtonType = .primary) {
  23. switch style {
  24. case .primary:
  25. fontColor = .white
  26. backgroundColor = .accentColor
  27. edgeColor = .clear
  28. case .destructive:
  29. fontColor = .white
  30. backgroundColor = .red
  31. edgeColor = .clear
  32. case .secondary:
  33. fontColor = .accentColor
  34. backgroundColor = .clear
  35. edgeColor = .accentColor
  36. case .deactivated:
  37. fontColor = .white
  38. backgroundColor = .gray
  39. edgeColor = .clear
  40. case .plain:
  41. fontColor = .accentColor
  42. backgroundColor = .clear
  43. edgeColor = .clear
  44. }
  45. }
  46. public func body(content: Content) -> some View {
  47. content
  48. .padding(.all)
  49. .foregroundColor(fontColor)
  50. .font(.headline)
  51. .frame(maxWidth: .infinity)
  52. .background(backgroundColor)
  53. .cornerRadius(cornerRadius)
  54. .overlay(RoundedRectangle(cornerRadius: cornerRadius)
  55. .stroke(edgeColor))
  56. }
  57. }
  58. public extension View {
  59. func actionButtonStyle(_ style: ActionButton.ButtonType = .primary) -> some View {
  60. ModifiedContent(content: self, modifier: ActionButton(style))
  61. }
  62. }