This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathFirePopoverViewController.swift
451 lines (375 loc) · 18 KB
/
FirePopoverViewController.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//
// FirePopoverViewController.swift
//
// Copyright © 2021 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Cocoa
import Combine
import Common
import History
import os.log
protocol FirePopoverViewControllerDelegate: AnyObject {
func firePopoverViewControllerDidClear(_ firePopoverViewController: FirePopoverViewController)
func firePopoverViewControllerDidCancel(_ firePopoverViewController: FirePopoverViewController)
}
final class FirePopoverViewController: NSViewController {
struct Constants {
static let maximumContentHeight: CGFloat = 42 + 230 + 32
static let minimumContentHeight: CGFloat = 42
static let headerHeight: CGFloat = 28
static let footerHeight: CGFloat = 8
}
weak var delegate: FirePopoverViewControllerDelegate?
private let fireViewModel: FireViewModel
private var firePopoverViewModel: FirePopoverViewModel
private let historyCoordinating: HistoryCoordinating
@IBOutlet weak var closeTabsLabel: NSTextField!
@IBOutlet weak var openFireWindowsTitleLabel: NSTextField!
@IBOutlet weak var fireWindowDescriptionLabel: NSTextField!
@IBOutlet weak var headerWrapperView: NSView!
@IBOutlet weak var mainButtonsToBurnerWindowContraint: NSLayoutConstraint!
@IBOutlet weak var infoLabel: NSTextField!
@IBOutlet weak var optionsButton: NSPopUpButton!
@IBOutlet weak var optionsButtonWidthConstraint: NSLayoutConstraint!
@IBOutlet weak var openDetailsButton: NSButton!
@IBOutlet weak var openDetailsButtonImageView: NSImageView!
@IBOutlet weak var closeDetailsButton: NSButton!
@IBOutlet weak var detailsWrapperView: NSView!
@IBOutlet weak var contentHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var detailsWrapperViewHeightContraint: NSLayoutConstraint!
@IBOutlet weak var openWrapperView: NSView!
@IBOutlet weak var closeWrapperView: NSView!
@IBOutlet weak var collectionView: NSCollectionView!
@IBOutlet weak var collectionViewBottomConstraint: NSLayoutConstraint!
@IBOutlet weak var warningWrapperView: NSView!
@IBOutlet weak var warningButton: NSButton!
@IBOutlet weak var clearButton: NSButton!
@IBOutlet weak var cancelButton: NSButton!
@IBOutlet weak var closeBurnerWindowButton: NSButton!
private var viewModelCancellable: AnyCancellable?
private var selectedCancellable: AnyCancellable?
required init?(coder: NSCoder) {
fatalError("FirePopoverViewController: Bad initializer")
}
init?(coder: NSCoder,
fireViewModel: FireViewModel,
tabCollectionViewModel: TabCollectionViewModel,
historyCoordinating: HistoryCoordinating = HistoryCoordinator.shared,
fireproofDomains: FireproofDomains = FireproofDomains.shared,
faviconManagement: FaviconManagement = FaviconManager.shared) {
self.fireViewModel = fireViewModel
self.historyCoordinating = historyCoordinating
self.firePopoverViewModel = FirePopoverViewModel(fireViewModel: fireViewModel,
tabCollectionViewModel: tabCollectionViewModel,
historyCoordinating: historyCoordinating,
fireproofDomains: fireproofDomains,
faviconManagement: faviconManagement,
tld: ContentBlocking.shared.tld,
contextualOnboardingStateMachine: Application.appDelegate.onboardingStateMachine)
super.init(coder: coder)
}
override func viewDidLoad() {
super.viewDidLoad()
let nib = NSNib(nibNamed: "FirePopoverCollectionViewItem", bundle: nil)
collectionView.register(nib, forItemWithIdentifier: FirePopoverCollectionViewItem.identifier)
collectionView.delegate = self
collectionView.dataSource = self
if firePopoverViewModel.tabCollectionViewModel?.isBurner ?? false {
adjustViewForBurnerWindow()
return
}
setUpStrings()
updateClearButtonAppearance()
setupOptionsButton()
setupOpenCloseDetailsButton()
updateWarningWrapperView()
subscribeToViewModel()
subscribeToSelected()
}
override func viewWillAppear() {
super.viewWillAppear()
firePopoverViewModel.refreshItems()
}
private func setUpStrings() {
openFireWindowsTitleLabel.stringValue = UserText.fireDialogFireWindowTitle
fireWindowDescriptionLabel.stringValue = UserText.fireDialogFireWindowDescription
closeTabsLabel.stringValue = UserText.fireDialogCloseTabs
closeBurnerWindowButton.title = UserText.fireDialogBurnWindowButton
clearButton.title = UserText.clear
cancelButton.title = UserText.cancel
}
@IBAction func optionsButtonAction(_ sender: NSPopUpButton) {
guard let tag = sender.selectedItem?.tag, let clearingOption = FirePopoverViewModel.ClearingOption(rawValue: tag) else {
assertionFailure("Clearing option for not found for the selected menu item")
return
}
firePopoverViewModel.clearingOption = clearingOption
updateWarningWrapperView()
}
@IBAction func openNewBurnerWindowAction(_ sender: Any) {
NSApp.delegateTyped.newBurnerWindow(self)
}
@IBAction func openDetailsButtonAction(_ sender: Any) {
toggleDetails()
}
@IBAction func closeDetailsButtonAction(_ sender: Any) {
toggleDetails()
}
@IBAction func closeBurnerWindowButtonAction(_ sender: Any) {
let windowControllersManager = WindowControllersManager.shared
guard let tabCollectionViewModel = firePopoverViewModel.tabCollectionViewModel,
let windowController = windowControllersManager.windowController(for: tabCollectionViewModel) else {
assertionFailure("No TabCollectionViewModel or MainWindowController")
return
}
windowController.window?.performClose(self)
}
private func adjustViewForBurnerWindow() {
updateCloseBurnerWindowButtonAppearance()
clearButton.isHidden = true
cancelButton.isHidden = true
closeBurnerWindowButton.isHidden = false
contentHeightConstraint.isActive = false
headerWrapperView.isHidden = true
openWrapperView.isHidden = true
detailsWrapperView.isHidden = true
warningWrapperView.isHidden = true
mainButtonsToBurnerWindowContraint.priority = .required
}
private func updateInfoLabel() {
guard !firePopoverViewModel.selectable.isEmpty else {
infoLabel.stringValue = ""
return
}
guard !firePopoverViewModel.selected.isEmpty else {
infoLabel.stringValue = UserText.selectSiteToClear
return
}
let sites = firePopoverViewModel.selected.count
switch firePopoverViewModel.clearingOption {
case .allData:
let tabs = WindowControllersManager.shared.allTabViewModels.count
infoLabel.stringValue = UserText.activeTabsInfo(tabs: tabs, sites: sites)
case .currentWindow:
let tabs = firePopoverViewModel.tabCollectionViewModel?.tabs.count ?? 0
infoLabel.stringValue = UserText.activeTabsInfo(tabs: tabs, sites: sites)
case .currentTab:
infoLabel.stringValue = UserText.oneTabInfo(sites: sites)
}
}
private func updateClearButtonAppearance() {
setRedTintColor(button: clearButton)
}
private func updateCloseBurnerWindowButtonAppearance() {
setRedTintColor(button: closeBurnerWindowButton)
}
private func setRedTintColor(button: NSButton) {
let attrTitle = NSMutableAttributedString(string: button.title)
let range = NSRange(location: 0, length: button.title.count)
attrTitle.addAttributes([
.foregroundColor: NSColor.redButtonTint,
.font: NSFont.systemFont(ofSize: NSFont.systemFontSize)],
range: range)
button.attributedTitle = attrTitle
}
private func setupOpenCloseDetailsButton() {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.firstLineHeadIndent = 15
let title = NSMutableAttributedString(string: UserText.fireDialogDetails)
title.addAttributes([.paragraphStyle: paragraphStyle], range: NSRange(location: 0, length: title.length))
openDetailsButton.attributedTitle = title
openDetailsButton.alignment = .left
closeDetailsButton.attributedTitle = title
closeDetailsButton.alignment = .left
}
private func updateWarningWrapperView() {
warningWrapperView.isHidden = detailsWrapperView.isHidden
if !warningWrapperView.isHidden {
let title: String
switch firePopoverViewModel.clearingOption {
case .currentTab:
if firePopoverViewModel.tabCollectionViewModel?.selectedTab?.isPinned ?? false {
title = UserText.fireDialogPinnedTabWillReload
} else {
title = UserText.fireDialogTabWillClose
}
case .currentWindow:
title = UserText.fireDialogWindowWillClose
case .allData:
title = UserText.fireDialogAllWindowsWillClose
}
warningButton.title = " \(title)"
}
collectionViewBottomConstraint.constant = warningWrapperView.isHidden ? 0 : 32
}
@IBAction func clearButtonAction(_ sender: Any) {
delegate?.firePopoverViewControllerDidClear(self)
firePopoverViewModel.burn()
}
@IBAction func cancelButtonAction(_ sender: Any) {
delegate?.firePopoverViewControllerDidCancel(self)
}
private func subscribeToViewModel() {
viewModelCancellable = Publishers.Zip(
firePopoverViewModel.$fireproofed,
firePopoverViewModel.$selectable
).receive(on: DispatchQueue.main)
.sink { [weak self] _ in
guard let self = self else { return }
self.collectionView.reloadData()
if self.firePopoverViewModel.selectable.isEmpty && !self.detailsWrapperView.isHidden {
self.toggleDetails()
}
self.updateInfoLabel()
self.adjustContentHeight()
}
}
private func subscribeToSelected() {
selectedCancellable = firePopoverViewModel.$selected
.receive(on: DispatchQueue.main)
.sink { [weak self] selected in
guard let self = self else { return }
let selectionIndexPaths = Set(selected.map {IndexPath(item: $0, section: self.firePopoverViewModel.selectableSectionIndex)})
self.collectionView.selectionIndexPaths = selectionIndexPaths
self.updateInfoLabel()
}
}
private func toggleDetails() {
let showDetails = detailsWrapperView.isHidden
openWrapperView.isHidden = showDetails
detailsWrapperView.isHidden = !showDetails
updateWarningWrapperView()
adjustContentHeight()
}
private func adjustContentHeight() {
NSAnimationContext.runAnimationGroup { [self, contentHeight = contentHeight()] context in
context.duration = 1/3
context.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
self.contentHeightConstraint.animator().constant = contentHeight
if contentHeight != Constants.minimumContentHeight {
self.detailsWrapperViewHeightContraint.animator().constant = contentHeight
}
}
}
private func contentHeight() -> CGFloat {
if detailsWrapperView.isHidden {
return Constants.minimumContentHeight
} else {
if let contentHeight = collectionView.collectionViewLayout?.collectionViewContentSize.height {
let warningWrapperViewHeight = warningWrapperView.isHidden ? 0 : warningWrapperView.frame.height
let height = contentHeight + closeWrapperView.frame.height + warningWrapperViewHeight
return min(Constants.maximumContentHeight, height)
} else {
return Constants.maximumContentHeight
}
}
}
private func setupOptionsButton() {
guard let menu = optionsButton.menu, let font = optionsButton.font else {
Logger.fire.error("FirePopoverViewController: Menu and/or font not present for optionsMenu")
return
}
menu.removeAllItems()
let constraintSize = NSSize(width: .max, height: 0)
let attributes = [NSAttributedString.Key.font: font]
var maxWidth: CGFloat = 0
FirePopoverViewModel.ClearingOption.allCases.forEach { option in
if option == .allData {
menu.addItem(.separator())
}
let item = NSMenuItem(title: option.string)
item.tag = option.rawValue
menu.addItem(item)
let width = (option.string as NSString)
.boundingRect(with: constraintSize, options: .usesDeviceMetrics, attributes: attributes, context: nil)
.width
maxWidth = max(maxWidth, width)
}
optionsButtonWidthConstraint.constant = maxWidth + 32
optionsButton.selectItem(at: optionsButton.numberOfItems - 1)
}
}
extension FirePopoverViewController: NSCollectionViewDataSource {
func numberOfSections(in collectionView: NSCollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
return section == firePopoverViewModel.selectableSectionIndex ? firePopoverViewModel.selectable.count: firePopoverViewModel.fireproofed.count
}
func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
let item = collectionView.makeItem(withIdentifier: FirePopoverCollectionViewItem.identifier, for: indexPath)
guard let firePopoverItem = item as? FirePopoverCollectionViewItem else { return item }
firePopoverItem.delegate = self
let isSelectableSection = indexPath.section == firePopoverViewModel.selectableSectionIndex
let sectionList = isSelectableSection ? firePopoverViewModel.selectable: firePopoverViewModel.fireproofed
let listItem = sectionList[indexPath.item]
firePopoverItem.setItem(listItem, isFireproofed: indexPath.section == firePopoverViewModel.fireproofedSectionIndex)
return firePopoverItem
}
func collectionView(_ collectionView: NSCollectionView,
viewForSupplementaryElementOfKind kind: NSCollectionView.SupplementaryElementKind,
at indexPath: IndexPath) -> NSView {
// swiftlint:disable force_cast
let view = collectionView.makeSupplementaryView(ofKind: NSCollectionView.elementKindSectionHeader,
withIdentifier: FirePopoverCollectionViewHeader.identifier,
for: indexPath) as! FirePopoverCollectionViewHeader
// swiftlint:enable force_cast
if indexPath.section == firePopoverViewModel.selectableSectionIndex {
view.title.stringValue = UserText.fireDialogClearSites
} else {
view.title.stringValue = UserText.fireDialogFireproofSites
}
return view
}
}
extension FirePopoverViewController: NSCollectionViewDelegate {
}
extension FirePopoverViewController: NSCollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: NSCollectionView,
layout collectionViewLayout: NSCollectionViewLayout,
referenceSizeForHeaderInSection section: Int) -> NSSize {
let count: Int
switch section {
case firePopoverViewModel.selectableSectionIndex: count = firePopoverViewModel.selectable.count
case firePopoverViewModel.fireproofedSectionIndex: count = firePopoverViewModel.fireproofed.count
default: count = 0
}
return NSSize(width: collectionView.bounds.width, height: count == 0 ? 0 : Constants.headerHeight)
}
func collectionView(_ collectionView: NSCollectionView,
layout collectionViewLayout: NSCollectionViewLayout,
referenceSizeForFooterInSection section: Int) -> NSSize {
let count: Int
switch section {
case firePopoverViewModel.selectableSectionIndex: count = firePopoverViewModel.selectable.count
case firePopoverViewModel.fireproofedSectionIndex: count = firePopoverViewModel.fireproofed.count
default: count = 0
}
return NSSize(width: collectionView.bounds.width, height: count == 0 ? 0 : Constants.footerHeight)
}
}
extension FirePopoverViewController: FirePopoverCollectionViewItemDelegate {
func firePopoverCollectionViewItemDidToggle(_ firePopoverCollectionViewItem: FirePopoverCollectionViewItem) {
guard let indexPath = collectionView.indexPath(for: firePopoverCollectionViewItem) else {
assertionFailure("No index path for the \(firePopoverCollectionViewItem)")
return
}
if firePopoverCollectionViewItem.isSelected {
firePopoverViewModel.deselect(index: indexPath.item)
} else {
firePopoverViewModel.select(index: indexPath.item)
}
}
}