-
Notifications
You must be signed in to change notification settings - Fork 3k
/
TabCell.swift
273 lines (224 loc) · 11.1 KB
/
TabCell.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
// 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 Common
import Foundation
import UIKit
import Shared
import SiteImageView
// MARK: - Tab Tray Cell Protocol
protocol TabTrayCell where Self: UICollectionViewCell {
/// True when the tab is the selected tab in the tray
var isSelectedTab: Bool { get }
/// Configure a tab cell using a Tab object, setting it's selected state at the same time
func configureWith(tab: Tab, isSelected selected: Bool, theme: Theme)
}
protocol TabCellDelegate: AnyObject {
func tabCellDidClose(_ cell: TabCell)
}
// MARK: - Tab Cell
class TabCell: UICollectionViewCell,
TabTrayCell,
ReusableCell,
ThemeApplicable {
// MARK: - Constants
enum Style {
case light
case dark
}
static let borderWidth: CGFloat = 3
// MARK: - UI Vars
lazy var backgroundHolder: UIView = .build { view in
view.layer.cornerRadius = GridTabViewController.UX.cornerRadius + TabCell.borderWidth
view.clipsToBounds = true
}
lazy private var faviconBG: UIView = .build { view in
view.layer.cornerRadius = HomepageViewModel.UX.generalCornerRadius
view.layer.borderWidth = HomepageViewModel.UX.generalBorderWidth
view.layer.shadowOffset = HomepageViewModel.UX.shadowOffset
view.layer.shadowRadius = HomepageViewModel.UX.shadowRadius
}
lazy var screenshotView: UIImageView = .build { view in
view.contentMode = .scaleAspectFill
view.clipsToBounds = true
view.isUserInteractionEnabled = false
}
lazy var titleText: UILabel = .build { label in
label.isUserInteractionEnabled = false
label.numberOfLines = 1
label.font = DynamicFontHelper.defaultHelper.DefaultSmallFontBold
}
lazy var smallFaviconView: FaviconImageView = .build { _ in }
lazy var favicon: FaviconImageView = .build { _ in }
lazy var closeButton: UIButton = .build { button in
button.setImage(UIImage.templateImageNamed(StandardImageIdentifiers.Large.cross), for: [])
button.imageView?.contentMode = .scaleAspectFit
button.contentMode = .center
button.imageEdgeInsets = UIEdgeInsets(equalInset: GridTabViewController.UX.closeButtonEdgeInset)
}
// TODO: Handle visual effects theming FXIOS-5064
var title = UIVisualEffectView(effect: UIBlurEffect(style: UIColor.legacyTheme.tabTray.tabTitleBlur))
var animator: SwipeAnimator?
var isSelectedTab = false
weak var delegate: TabCellDelegate?
// Changes depending on whether we're full-screen or not.
var margin = CGFloat(0)
// MARK: - Initializer
override init(frame: CGRect) {
super.init(frame: frame)
self.animator = SwipeAnimator(animatingView: self)
self.closeButton.addTarget(self, action: #selector(close), for: .touchUpInside)
contentView.addSubview(backgroundHolder)
faviconBG.addSubview(smallFaviconView)
backgroundHolder.addSubviews(screenshotView, faviconBG)
self.accessibilityCustomActions = [
UIAccessibilityCustomAction(name: .TabTrayCloseAccessibilityCustomAction, target: self.animator, selector: #selector(SwipeAnimator.closeWithoutGesture))
]
backgroundHolder.addSubview(title)
title.translatesAutoresizingMaskIntoConstraints = false
title.contentView.addSubview(self.closeButton)
title.contentView.addSubview(self.titleText)
title.contentView.addSubview(self.favicon)
setupConstraint()
}
func setupConstraint() {
NSLayoutConstraint.activate([
backgroundHolder.topAnchor.constraint(equalTo: contentView.topAnchor),
backgroundHolder.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
backgroundHolder.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
backgroundHolder.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
title.topAnchor.constraint(equalTo: backgroundHolder.topAnchor),
title.leftAnchor.constraint(equalTo: backgroundHolder.leftAnchor),
title.rightAnchor.constraint(equalTo: backgroundHolder.rightAnchor),
title.heightAnchor.constraint(equalToConstant: GridTabViewController.UX.textBoxHeight),
favicon.leadingAnchor.constraint(equalTo: title.leadingAnchor, constant: 6),
favicon.topAnchor.constraint(equalTo: title.topAnchor, constant: (GridTabViewController.UX.textBoxHeight - GridTabViewController.UX.faviconSize) / 2),
favicon.heightAnchor.constraint(equalToConstant: GridTabViewController.UX.faviconSize),
favicon.widthAnchor.constraint(equalToConstant: GridTabViewController.UX.faviconSize),
closeButton.heightAnchor.constraint(equalToConstant: GridTabViewController.UX.closeButtonSize),
closeButton.widthAnchor.constraint(equalToConstant: GridTabViewController.UX.closeButtonSize),
closeButton.centerYAnchor.constraint(equalTo: title.contentView.centerYAnchor),
closeButton.trailingAnchor.constraint(equalTo: title.trailingAnchor),
titleText.leadingAnchor.constraint(equalTo: favicon.trailingAnchor, constant: 6),
titleText.trailingAnchor.constraint(equalTo: closeButton.leadingAnchor, constant: 6),
titleText.centerYAnchor.constraint(equalTo: title.contentView.centerYAnchor),
screenshotView.topAnchor.constraint(equalTo: topAnchor),
screenshotView.leftAnchor.constraint(equalTo: backgroundHolder.leftAnchor),
screenshotView.rightAnchor.constraint(equalTo: backgroundHolder.rightAnchor),
screenshotView.bottomAnchor.constraint(equalTo: backgroundHolder.bottomAnchor),
faviconBG.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 10),
faviconBG.centerXAnchor.constraint(equalTo: centerXAnchor),
faviconBG.heightAnchor.constraint(equalToConstant: TopSiteItemCell.UX.imageBackgroundSize.height),
faviconBG.widthAnchor.constraint(equalToConstant: TopSiteItemCell.UX.imageBackgroundSize.width),
smallFaviconView.heightAnchor.constraint(equalToConstant: TopSiteItemCell.UX.iconSize.height),
smallFaviconView.widthAnchor.constraint(equalToConstant: TopSiteItemCell.UX.iconSize.width),
smallFaviconView.centerYAnchor.constraint(equalTo: faviconBG.centerYAnchor),
smallFaviconView.centerXAnchor.constraint(equalTo: faviconBG.centerXAnchor),
])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
}
// MARK: - Configure tab cell with a Tab
func configureWith(tab: Tab, isSelected selected: Bool, theme: Theme) {
isSelectedTab = selected
applyTheme(theme: theme)
titleText.text = tab.getTabTrayTitle()
accessibilityLabel = getA11yTitleLabel(tab: tab)
isAccessibilityElement = true
accessibilityHint = .TabTraySwipeToCloseAccessibilityHint
favicon.image = UIImage(named: StandardImageIdentifiers.Large.globe)
if !tab.isFxHomeTab, let tabURL = tab.url?.absoluteString {
favicon.setFavicon(FaviconImageViewModel(siteURLString: tabURL))
}
if selected {
setTabSelected(tab.isPrivate, theme: theme)
} else {
layoutMargins = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layer.borderColor = UIColor.clear.cgColor
layer.borderWidth = 0
layer.cornerRadius = GridTabViewController.UX.cornerRadius + TabCell.borderWidth
}
faviconBG.isHidden = true
// Regular screenshot for home or internal url when tab has home screenshot
if let url = tab.url, let tabScreenshot = tab.screenshot, (url.absoluteString.starts(with: "internal") &&
tab.hasHomeScreenshot) {
screenshotView.image = tabScreenshot
// Favicon or letter image when home screenshot is present for a regular (non-internal) url
} else if let url = tab.url, (!url.absoluteString.starts(with: "internal") &&
tab.hasHomeScreenshot) {
smallFaviconView.image = UIImage(named: StandardImageIdentifiers.Large.globe)
faviconBG.isHidden = false
screenshotView.image = nil
// Tab screenshot when available
} else if let tabScreenshot = tab.screenshot {
screenshotView.image = tabScreenshot
// Favicon or letter image when tab screenshot isn't available
} else {
faviconBG.isHidden = false
screenshotView.image = nil
if let tabURL = tab.url?.absoluteString {
smallFaviconView.setFavicon(FaviconImageViewModel(siteURLString: tabURL))
}
}
}
func applyTheme(theme: Theme) {
backgroundHolder.backgroundColor = theme.colors.layer1
closeButton.tintColor = theme.colors.indicatorActive
titleText.textColor = theme.colors.textPrimary
screenshotView.backgroundColor = theme.colors.layer1
favicon.tintColor = theme.colors.textPrimary
smallFaviconView.tintColor = theme.colors.textPrimary
}
override func prepareForReuse() {
// Reset any close animations.
super.prepareForReuse()
screenshotView.image = nil
backgroundHolder.transform = .identity
backgroundHolder.alpha = 1
self.titleText.font = DynamicFontHelper.defaultHelper.DefaultSmallFontBold
layer.shadowOffset = .zero
layer.shadowPath = nil
layer.shadowOpacity = 0
isHidden = false
}
override func accessibilityScroll(_ direction: UIAccessibilityScrollDirection) -> Bool {
var right: Bool
switch direction {
case .left:
right = false
case .right:
right = true
default:
return false
}
animator?.close(right: right)
return true
}
@objc
func close() {
delegate?.tabCellDidClose(self)
}
private func setTabSelected(_ isPrivate: Bool, theme: Theme) {
layoutMargins = UIEdgeInsets(top: TabCell.borderWidth, left: TabCell.borderWidth, bottom: TabCell.borderWidth, right: TabCell.borderWidth)
layer.borderColor = (isPrivate ? theme.colors.borderAccentPrivate : theme.colors.borderAccent).cgColor
layer.borderWidth = TabCell.borderWidth
layer.cornerRadius = GridTabViewController.UX.cornerRadius + TabCell.borderWidth
}
}
// MARK: - Extension Tab Tray Cell protocol
extension TabTrayCell {
func getA11yTitleLabel(tab: Tab) -> String? {
let baseName = tab.getTabTrayTitle()
if isSelectedTab, let baseName = baseName, !baseName.isEmpty {
return baseName + ". " + String.TabTrayCurrentlySelectedTabAccessibilityLabel
} else if isSelectedTab {
return String.TabTrayCurrentlySelectedTabAccessibilityLabel
} else {
return baseName
}
}
}