summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/Views/MainButtonStyle.swift
blob: 0ffd2398611e9f99a4f597375bf21876bc8ea3dc (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
//  MainButtonStyle.swift
//  MullvadVPN
//
//  Created by Jon Petersson on 2024-12-05.
//  Copyright © 2025 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
                    ? .mullvadTextPrimary
                    : .mullvadTextPrimaryDisabled
            )
            .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:
                Color.MullvadButton.primary
            case .danger:
                Color.MullvadButton.danger
            case .success:
                Color.MullvadButton.positive
            }
        }

        var pressedColor: Color {
            switch self {
            case .default:
                Color.MullvadButton.primaryPressed
            case .danger:
                Color.MullvadButton.dangerPressed
            case .success:
                Color.MullvadButton.positivePressed
            }
        }

        var disabledColor: Color {
            switch self {
            case .default:
                Color.MullvadButton.primaryDisabled
            case .danger:
                Color.MullvadButton.dangerDisabled
            case .success:
                Color.MullvadButton.positiveDisabled
            }
        }
    }
}