summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/Views/MainButtonStyle.swift
blob: f638c87ac2b50e921ba59decf784311d09c6d7b8 (plain)
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
//
//  MainButtonStyle.swift
//  MullvadVPN
//
//  Created by Jon Petersson on 2024-12-05.
//  Copyright © 2024 Mullvad VPN AB. All rights reserved.
//

import SwiftUI

struct MainButtonStyle: ButtonStyle {
    var style: Style
    @State var disabled: Bool

    init(_ style: Style, disabled: Bool = false) {
        self.style = style
        self.disabled = disabled
    }

    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .padding(.horizontal, 8)
            .frame(height: 44)
            .foregroundColor(
                configuration.isPressed
                    ? UIColor.secondaryTextColor.color
                    : disabled
                        ? UIColor.primaryTextColor.withAlphaComponent(0.2).color
                        : UIColor.primaryTextColor.color
            )
            .background(
                disabled
                    ? style.color.darkened(by: 0.6)
                    : style.color
            )
            .font(.body.weight(.semibold))
    }
}

extension MainButtonStyle {
    enum Style {
        case `default`
        case danger
        case success

        var color: Color {
            switch self {
            case .default:
                Color(UIColor.primaryColor)
            case .danger:
                Color(UIColor.dangerColor)
            case .success:
                Color(UIColor.successColor)
            }
        }
    }
}