blob: be2a809b09ccdf0fae5100f5789e66d592702270 (
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
//
// AppButton.swift
// MullvadVPN
//
// Created by pronebird on 23/05/2019.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import UIKit
/// A subclass that implements action buttons used across the app
class AppButton: CustomButton {
/// Default content insets based on current trait collection.
var defaultContentInsets: NSDirectionalEdgeInsets {
switch traitCollection.userInterfaceIdiom {
case .phone:
return NSDirectionalEdgeInsets(top: 12, leading: 8, bottom: 12, trailing: 8)
case .pad:
return NSDirectionalEdgeInsets(top: 16, leading: 16, bottom: 16, trailing: 16)
default:
return .zero
}
}
enum Style: Int {
/// Default blue appearance.
case `default`
/// Destructive appearance.
case danger
/// Positive appearance suitable for actions that provide security.
case success
/// Translucent destructive appearance.
case translucentDanger
/// Translucent neutral appearance.
case translucentNeutral
/// Translucent destructive appearance for the left-hand side of a two component split button.
case translucentDangerSplitLeft
/// Translucent destructive appearance for the right-hand side of a two component split button.
case translucentDangerSplitRight
/// Default blue rounded button suitable for presentation in table view using `.insetGrouped` style.
case tableInsetGroupedDefault
/// Positive appearance for presentation in table view using `.insetGrouped` style.
case tableInsetGroupedSuccess
/// Destructive style suitable for presentation in table view using `.insetGrouped` style.
case tableInsetGroupedDanger
/// Returns a background image for the button.
var backgroundImage: UIImage {
switch self {
case .default:
UIImage(resource: .defaultButton)
case .danger:
UIImage(resource: .dangerButton)
case .success:
UIImage(resource: .successButton)
case .translucentDanger:
UIImage(resource: .translucentDangerButton)
case .translucentNeutral:
UIImage(resource: .translucentNeutralButton)
case .translucentDangerSplitLeft:
UIImage(resource: .translucentDangerSplitLeftButton).imageFlippedForRightToLeftLayoutDirection()
case .translucentDangerSplitRight:
UIImage(resource: .translucentDangerSplitRightButton).imageFlippedForRightToLeftLayoutDirection()
case .tableInsetGroupedDefault:
UIImage(resource: .defaultButton)
case .tableInsetGroupedSuccess:
UIImage(resource: .successButton)
case .tableInsetGroupedDanger:
UIImage(resource: .dangerButton)
}
}
}
/// Button style.
var style: Style {
didSet {
updateButtonBackground()
}
}
init(style: Style) {
self.style = style
super.init(frame: .zero)
commonInit()
}
override init(frame: CGRect) {
self.style = .default
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func commonInit() {
imageAlignment = .trailing
titleAlignment = .leading
var config = super.configuration ?? .plain()
config.title = title(for: state)
config.contentInsets = defaultContentInsets
config.background.image = style.backgroundImage
config.background.imageContentMode = .scaleAspectFill
config.titleTextAttributesTransformer =
UIConfigurationTextAttributesTransformer { [weak self] attributeContainer in
var updatedAttributeContainer = attributeContainer
updatedAttributeContainer.font = .mullvadSmallSemiBold
updatedAttributeContainer.foregroundColor = self?.state.customButtonTitleColor
return updatedAttributeContainer
}
let configurationHandler: UIButton.ConfigurationUpdateHandler = { [weak self] _ in
guard let self else { return }
updateButtonBackground()
}
configuration = config
configurationUpdateHandler = configurationHandler
}
/// Set background image based on current style.
private func updateButtonBackground() {
if isEnabled {
// Load the normal image and set it as the background
configuration?.background.image = style.backgroundImage
} else {
// Adjust the image for the disabled state
configuration?.background.image = style.backgroundImage.withAlpha(0.5)
}
}
}
|