forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionButtonStyle.swift
More file actions
59 lines (52 loc) · 1.73 KB
/
ActionButtonStyle.swift
File metadata and controls
59 lines (52 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// ActionButtonStyle.swift
// LoopKitUI
//
// Created by Michael Pangburn on 4/15/20.
// Copyright © 2020 LoopKit Authors. All rights reserved.
//
import SwiftUI
public struct ActionButtonStyle: ButtonStyle {
public enum ButtonType {
case primary
case secondary
case destructive
}
private let fontColor: Color
private let backgroundColor: Color
private let edgeColor: Color
private let cornerRadius: CGFloat = 10
private let squidge: CGFloat = 1
@Environment(\.isEnabled) private var isEnabled: Bool
public init(_ style: ButtonType = .primary) {
switch style {
case .primary:
fontColor = .white
backgroundColor = .accentColor
edgeColor = .clear
case .destructive:
fontColor = .white
backgroundColor = .red
edgeColor = .clear
case .secondary:
fontColor = .accentColor
backgroundColor = .clear
edgeColor = .accentColor
}
}
public func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding(configuration.isPressed ? -squidge : 0)
.padding()
.foregroundColor(fontColor)
.font(.headline)
.frame(maxWidth: .infinity)
.background(isEnabled ? backgroundColor : Color(UIColor.lightGray))
.overlay(Color(.secondarySystemBackground).opacity(configuration.isPressed ? 0.35 : 0))
.cornerRadius(cornerRadius)
.padding(configuration.isPressed ? squidge : 0)
.overlay(RoundedRectangle(cornerRadius: cornerRadius)
.stroke(edgeColor))
.contentShape(Rectangle())
}
}