-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathTabsButton.swift
289 lines (244 loc) · 10.8 KB
/
TabsButton.swift
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import SnapKit
import Shared
import XCGLogger
private let log = Logger.browserLogger
struct TabsButtonUX {
static let TitleColor: UIColor = UIColor.black
static let TitleBackgroundColor: UIColor = UIColor.white
static let CornerRadius: CGFloat = 2
static let TitleFont: UIFont = UIConstants.DefaultChromeSmallFontBold
static let BorderStrokeWidth: CGFloat = 1
static let BorderColor: UIColor = UIColor.clear
static let TitleInsets = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12)
static let Themes: [String: Theme] = {
var themes = [String: Theme]()
var theme = Theme()
theme.borderColor = UIConstants.PrivateModePurple
theme.borderWidth = BorderStrokeWidth
theme.font = UIConstants.DefaultChromeBoldFont
theme.backgroundColor = UIConstants.AppBackgroundColor
theme.textColor = UIConstants.PrivateModePurple
theme.insets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
theme.highlightButtonColor = UIConstants.PrivateModePurple
theme.highlightTextColor = TabsButtonUX.TitleColor
theme.highlightBorderColor = UIConstants.PrivateModePurple
themes[Theme.PrivateMode] = theme
theme = Theme()
theme.borderColor = BorderColor
theme.borderWidth = BorderStrokeWidth
theme.font = TitleFont
theme.backgroundColor = TitleBackgroundColor
theme.textColor = TitleColor
theme.insets = TitleInsets
theme.highlightButtonColor = TabsButtonUX.TitleColor
theme.highlightTextColor = TabsButtonUX.TitleBackgroundColor
theme.highlightBorderColor = TabsButtonUX.TitleBackgroundColor
themes[Theme.NormalMode] = theme
return themes
}()
}
class TabsButton: UIControl {
fileprivate var theme: Theme = TabsButtonUX.Themes[Theme.NormalMode]!
override var isHighlighted: Bool {
didSet {
if isHighlighted {
borderColor = theme.highlightBorderColor!
titleBackgroundColor = theme.highlightButtonColor
textColor = theme.highlightTextColor
} else {
borderColor = theme.borderColor!
titleBackgroundColor = theme.backgroundColor
textColor = theme.textColor
}
}
}
override var transform: CGAffineTransform {
didSet {
clonedTabsButton?.transform = transform
}
}
lazy var titleLabel: UILabel = {
let label = UILabel()
label.font = TabsButtonUX.TitleFont
label.layer.cornerRadius = TabsButtonUX.CornerRadius
label.textAlignment = NSTextAlignment.center
label.isUserInteractionEnabled = false
return label
}()
lazy var insideButton: UIView = {
let view = UIView()
view.clipsToBounds = false
view.isUserInteractionEnabled = false
return view
}()
fileprivate lazy var labelBackground: UIView = {
let background = UIView()
background.layer.cornerRadius = TabsButtonUX.CornerRadius
background.isUserInteractionEnabled = false
return background
}()
fileprivate lazy var borderView: InnerStrokedView = {
let border = InnerStrokedView()
border.strokeWidth = TabsButtonUX.BorderStrokeWidth
border.cornerRadius = TabsButtonUX.CornerRadius
border.isUserInteractionEnabled = false
return border
}()
fileprivate var buttonInsets: UIEdgeInsets = TabsButtonUX.TitleInsets
// Used to temporarily store the cloned button so we can respond to layout changes during animation
fileprivate weak var clonedTabsButton: TabsButton?
override init(frame: CGRect) {
super.init(frame: frame)
insideButton.addSubview(labelBackground)
insideButton.addSubview(borderView)
insideButton.addSubview(titleLabel)
addSubview(insideButton)
isAccessibilityElement = true
accessibilityTraits |= UIAccessibilityTraitButton
}
override func updateConstraints() {
super.updateConstraints()
labelBackground.snp.remakeConstraints { (make) -> Void in
make.edges.equalTo(insideButton)
}
borderView.snp.remakeConstraints { (make) -> Void in
make.edges.equalTo(insideButton)
}
titleLabel.snp.remakeConstraints { (make) -> Void in
make.edges.equalTo(insideButton)
}
insideButton.snp.remakeConstraints { (make) -> Void in
make.edges.equalTo(self).inset(insets)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func clone() -> UIView {
let button = TabsButton()
button.accessibilityLabel = accessibilityLabel
button.titleLabel.text = titleLabel.text
button.theme = theme
// Copy all of the styable properties over to the new TabsButton
button.titleLabel.font = titleLabel.font
button.titleLabel.textColor = titleLabel.textColor
button.titleLabel.layer.cornerRadius = titleLabel.layer.cornerRadius
button.labelBackground.backgroundColor = labelBackground.backgroundColor
button.labelBackground.layer.cornerRadius = labelBackground.layer.cornerRadius
button.borderView.strokeWidth = borderView.strokeWidth
button.borderView.color = borderView.color
button.borderView.cornerRadius = borderView.cornerRadius
return button
}
func updateTabCount(_ count: Int, animated: Bool = true) {
let currentCount = self.titleLabel.text
let infinity = "\u{221E}"
let countToBe = (count < 100) ? count.description : infinity
// only animate a tab count change if the tab count has actually changed
if currentCount != count.description || (clonedTabsButton?.titleLabel.text ?? count.description) != count.description {
if let _ = self.clonedTabsButton {
self.clonedTabsButton?.layer.removeAllAnimations()
self.clonedTabsButton?.removeFromSuperview()
insideButton.layer.removeAllAnimations()
}
// make a 'clone' of the tabs button
let newTabsButton = clone() as! TabsButton
self.clonedTabsButton = newTabsButton
newTabsButton.addTarget(self, action: #selector(TabsButton.cloneDidClickTabs), for: UIControlEvents.touchUpInside)
newTabsButton.titleLabel.text = countToBe
newTabsButton.accessibilityValue = countToBe
superview?.addSubview(newTabsButton)
newTabsButton.snp.makeConstraints { make in
make.centerY.equalTo(self)
make.trailing.equalTo(self)
make.size.equalTo(UIConstants.ToolbarHeight)
}
newTabsButton.frame = self.frame
newTabsButton.insets = insets
// Instead of changing the anchorPoint of the CALayer, lets alter the rotation matrix math to be
// a rotation around a non-origin point
let frame = self.insideButton.frame
let halfTitleHeight = frame.height / 2
var newFlipTransform = CATransform3DIdentity
newFlipTransform = CATransform3DTranslate(newFlipTransform, 0, halfTitleHeight, 0)
newFlipTransform.m34 = -1.0 / 200.0 // add some perspective
newFlipTransform = CATransform3DRotate(newFlipTransform, CGFloat(-(Double.pi / 2)), 1.0, 0.0, 0.0)
newTabsButton.insideButton.layer.transform = newFlipTransform
var oldFlipTransform = CATransform3DIdentity
oldFlipTransform = CATransform3DTranslate(oldFlipTransform, 0, halfTitleHeight, 0)
oldFlipTransform.m34 = -1.0 / 200.0 // add some perspective
oldFlipTransform = CATransform3DRotate(oldFlipTransform, CGFloat(-(Double.pi / 2)), 1.0, 0.0, 0.0)
let animate = {
newTabsButton.insideButton.layer.transform = CATransform3DIdentity
self.insideButton.layer.transform = oldFlipTransform
self.insideButton.layer.opacity = 0
}
let completion: (Bool) -> Void = { _ in
// Remove the clone and setup the actual tab button
newTabsButton.removeFromSuperview()
self.insideButton.layer.opacity = 1
self.insideButton.layer.transform = CATransform3DIdentity
self.accessibilityLabel = NSLocalizedString("Show Tabs", comment: "Accessibility label for the tabs button in the (top) tab toolbar")
self.titleLabel.text = countToBe
self.accessibilityValue = countToBe
}
if animated {
UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options: UIViewAnimationOptions(), animations: animate, completion: completion)
} else {
completion(true)
}
}
}
func cloneDidClickTabs() {
sendActions(for: UIControlEvents.touchUpInside)
}
}
extension TabsButton: Themeable {
func applyTheme(_ themeName: String) {
guard let theme = TabsButtonUX.Themes[themeName] else {
log.error("Unable to apply unknown theme \(themeName)")
return
}
borderColor = theme.borderColor!
borderWidth = theme.borderWidth!
titleFont = theme.font
titleBackgroundColor = theme.backgroundColor
textColor = theme.textColor
insets = theme.insets!
self.theme = theme
}
}
// MARK: UIAppearance
extension TabsButton {
dynamic var borderColor: UIColor {
get { return borderView.color }
set { borderView.color = newValue }
}
dynamic var borderWidth: CGFloat {
get { return borderView.strokeWidth }
set { borderView.strokeWidth = newValue }
}
dynamic var textColor: UIColor? {
get { return titleLabel.textColor }
set { titleLabel.textColor = newValue }
}
dynamic var titleFont: UIFont? {
get { return titleLabel.font }
set { titleLabel.font = newValue }
}
dynamic var titleBackgroundColor: UIColor? {
get { return labelBackground.backgroundColor }
set { labelBackground.backgroundColor = newValue }
}
dynamic var insets: UIEdgeInsets {
get { return buttonInsets }
set {
buttonInsets = newValue
setNeedsUpdateConstraints()
}
}
}