summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/Views/MainButtonStyle.swift
blob: 40f834658437a96a76385e95b7c184e574652200 (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
58
59
60
61
62
63
//
//  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
    @Environment(\.isEnabled) private var isEnabled: Bool

    init(_ style: Style) {
        self.style = style
    }

    func makeBody(configuration: Configuration) -> some View {
        return configuration.label
            .frame(minHeight: 44)
            .foregroundColor(
                isEnabled
                    ? UIColor.primaryTextColor.color
                    : UIColor.primaryTextColor.withAlphaComponent(0.2).color
            )
            .background(
                isEnabled
                    ? configuration.isPressed
                        ? style.pressedColor
                        : style.color
                    : style.disabledColor
            )
            .font(.body.weight(.semibold))
    }
}

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

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

        var pressedColor: Color {
            color.darkened(by: 0.4)!
        }

        var disabledColor: Color {
            color.darkened(by: 0.6)!
        }
    }
}