-
Notifications
You must be signed in to change notification settings - Fork 405
/
OmniBar.swift
587 lines (471 loc) · 20.7 KB
/
OmniBar.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//
// OmniBar.swift
// DuckDuckGo
//
// Copyright © 2017 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 Common
import UIKit
import Core
import PrivacyDashboard
import DesignResourcesKit
extension OmniBar: NibLoading {}
// swiftlint:disable file_length
// swiftlint:disable type_body_length
class OmniBar: UIView {
public static let didLayoutNotification = Notification.Name("com.duckduckgo.app.OmniBarDidLayout")
@IBOutlet weak var searchLoupe: UIView!
@IBOutlet weak var searchContainer: UIView!
@IBOutlet weak var searchStackContainer: UIStackView!
@IBOutlet weak var searchFieldContainer: SearchFieldContainerView!
@IBOutlet weak var privacyInfoContainer: PrivacyInfoContainerView!
@IBOutlet weak var notificationContainer: OmniBarNotificationContainerView!
@IBOutlet weak var textField: TextFieldWithInsets!
@IBOutlet weak var editingBackground: RoundedRectangleView!
@IBOutlet weak var clearButton: UIButton!
@IBOutlet weak var menuButton: UIButton!
@IBOutlet weak var settingsButton: UIButton!
@IBOutlet weak var separatorView: UIView!
@IBOutlet weak var cancelButton: UIButton!
@IBOutlet weak var refreshButton: UIButton!
@IBOutlet weak var voiceSearchButton: UIButton!
@IBOutlet weak var bookmarksButton: UIButton!
@IBOutlet weak var backButton: UIButton!
@IBOutlet weak var forwardButton: UIButton!
@IBOutlet weak var shareButton: UIButton!
private(set) var menuButtonContent = MenuButton()
// Don't use weak because adding/removing them causes them to go away
@IBOutlet var separatorHeightConstraint: NSLayoutConstraint!
@IBOutlet var leftButtonsSpacingConstraint: NSLayoutConstraint!
@IBOutlet var rightButtonsSpacingConstraint: NSLayoutConstraint!
@IBOutlet var searchContainerCenterConstraint: NSLayoutConstraint!
@IBOutlet var searchContainerMaxWidthConstraint: NSLayoutConstraint!
@IBOutlet var omniBarLeadingConstraint: NSLayoutConstraint!
@IBOutlet var omniBarTrailingConstraint: NSLayoutConstraint!
@IBOutlet var separatorToBottom: NSLayoutConstraint!
weak var omniDelegate: OmniBarDelegate?
fileprivate var state: OmniBarState = SmallOmniBarState.HomeNonEditingState()
private var safeAreaInsetsObservation: NSKeyValueObservation?
private var privacyIconAndTrackersAnimator = PrivacyIconAndTrackersAnimator()
private var notificationAnimator = OmniBarNotificationAnimator()
static func loadFromXib() -> OmniBar {
return OmniBar.load(nibName: "OmniBar")
}
private let appSettings: AppSettings
required init?(coder: NSCoder) {
appSettings = AppDependencyProvider.shared.appSettings
super.init(coder: coder)
}
// Tests require this
override init(frame: CGRect) {
appSettings = AppDependencyProvider.shared.appSettings
super.init(frame: frame)
}
override func awakeFromNib() {
super.awakeFromNib()
configureMenuButton()
configureTextField()
configureSettingsLongPressButton()
configureShareLongPressButton()
registerNotifications()
configureSeparator()
configureEditingMenu()
refreshState(state)
enableInteractionsWithPointer()
observeSafeAreaInsets()
privacyInfoContainer.isHidden = true
}
private func configureSettingsLongPressButton() {
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleSettingsLongPress(_:)))
longPressGesture.minimumPressDuration = 0.7
settingsButton.addGestureRecognizer(longPressGesture)
}
private func configureShareLongPressButton() {
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleShareLongPress(_:)))
longPressGesture.minimumPressDuration = 0.7
shareButton.addGestureRecognizer(longPressGesture)
}
@objc private func handleSettingsLongPress(_ gesture: UILongPressGestureRecognizer) {
if gesture.state == .began {
omniDelegate?.onSettingsLongPressed()
}
}
@objc private func handleShareLongPress(_ gesture: UILongPressGestureRecognizer) {
if gesture.state == .began {
omniDelegate?.onShareLongPressed()
}
}
private func registerNotifications() {
NotificationCenter.default.addObserver(self,
selector: #selector(textDidChange),
name: UITextField.textDidChangeNotification,
object: textField)
NotificationCenter.default.addObserver(self,
selector: #selector(reloadSpeechRecognizerAvailability),
name: .speechRecognizerDidChangeAvailability,
object: nil)
}
private func observeSafeAreaInsets() {
safeAreaInsetsObservation = self.observe(\.safeAreaInsets, options: .new) { [weak self] (_, _) in
self?.updateOmniBarPadding()
}
}
private func enableInteractionsWithPointer() {
backButton.isPointerInteractionEnabled = true
forwardButton.isPointerInteractionEnabled = true
settingsButton.isPointerInteractionEnabled = true
cancelButton.isPointerInteractionEnabled = true
bookmarksButton.isPointerInteractionEnabled = true
shareButton.isPointerInteractionEnabled = true
menuButton.isPointerInteractionEnabled = true
refreshButton.isPointerInteractionEnabled = true
refreshButton.pointerStyleProvider = { button, _, _ -> UIPointerStyle? in
return .init(effect: .lift(.init(view: button)))
}
}
private func configureMenuButton() {
menuButton.addSubview(menuButtonContent)
menuButton.isAccessibilityElement = true
menuButton.accessibilityTraits = .button
}
private func configureTextField() {
let theme = ThemeManager.shared.currentTheme
textField.attributedPlaceholder = NSAttributedString(string: UserText.searchDuckDuckGo,
attributes: [.foregroundColor: theme.searchBarTextPlaceholderColor])
textField.delegate = self
textField.textDragInteraction?.isEnabled = false
textField.onCopyAction = { field in
guard let range = field.selectedTextRange else { return }
UIPasteboard.general.string = field.text(in: range)
}
}
private func configureSeparator() {
separatorHeightConstraint.constant = 1.0 / UIScreen.main.scale
}
private func configureEditingMenu() {
let title = UserText.actionPasteAndGo
UIMenuController.shared.menuItems = [UIMenuItem(title: title, action: #selector(pasteAndGo))]
}
var textFieldBottomSpacing: CGFloat {
return (bounds.size.height - (searchContainer.frame.origin.y + searchContainer.frame.size.height)) / 2.0
}
@objc func textDidChange() {
let newQuery = textField.text ?? ""
omniDelegate?.onOmniQueryUpdated(newQuery)
if newQuery.isEmpty {
refreshState(state.onTextClearedState)
} else {
refreshState(state.onTextEnteredState)
}
}
@objc func pasteAndGo(sender: UIMenuItem) {
guard let pastedText = UIPasteboard.general.string else { return }
textField.text = pastedText
onQuerySubmitted()
}
@objc private func reloadSpeechRecognizerAvailability() {
assert(Thread.isMainThread)
state = state.onReloadState
refreshState(state)
}
func showSeparator() {
separatorView.isHidden = false
}
func hideSeparator() {
separatorView.isHidden = true
}
func moveSeparatorToTop() {
separatorToBottom.constant = frame.height
}
func moveSeparatorToBottom() {
separatorToBottom.constant = 0
}
func startBrowsing() {
refreshState(state.onBrowsingStartedState)
}
func stopBrowsing() {
refreshState(state.onBrowsingStoppedState)
}
func removeTextSelection() {
textField.selectedTextRange = nil
}
public func hidePrivacyIcon() {
privacyInfoContainer.privacyIcon.isHidden = true
}
public func resetPrivacyIcon(for url: URL?) {
cancelAllAnimations()
privacyInfoContainer.privacyIcon.isHidden = false
let icon = PrivacyIconLogic.privacyIcon(for: url)
privacyInfoContainer.privacyIcon.updateIcon(icon)
}
public func updatePrivacyIcon(for privacyInfo: PrivacyInfo?) {
guard let privacyInfo = privacyInfo,
!privacyInfoContainer.isAnimationPlaying,
!privacyIconAndTrackersAnimator.isAnimatingForDaxDialog
else { return }
privacyInfoContainer.privacyIcon.isHidden = false
let icon = PrivacyIconLogic.privacyIcon(for: privacyInfo)
privacyInfoContainer.privacyIcon.updateIcon(icon)
}
public func startTrackersAnimation(_ privacyInfo: PrivacyInfo, forDaxDialog: Bool) {
guard state.allowsTrackersAnimation, !privacyInfoContainer.isAnimationPlaying else { return }
privacyIconAndTrackersAnimator.configure(privacyInfoContainer, with: privacyInfo)
if TrackerAnimationLogic.shouldAnimateTrackers(for: privacyInfo.trackerInfo) {
if forDaxDialog {
privacyIconAndTrackersAnimator.startAnimationForDaxDialog(in: self, with: privacyInfo)
} else {
privacyIconAndTrackersAnimator.startAnimating(in: self, with: privacyInfo)
}
} else {
privacyIconAndTrackersAnimator.completeForNoAnimation()
}
}
public func cancelAllAnimations() {
privacyIconAndTrackersAnimator.cancelAnimations(in: self)
notificationAnimator.cancelAnimations(in: self)
}
public func completeAnimationForDaxDialog() {
privacyIconAndTrackersAnimator.completeAnimationForDaxDialog(in: self)
}
func showOrScheduleCookiesManagedNotification(isCosmetic: Bool) {
let type: OmniBarNotificationType = isCosmetic ? .cookiePopupHidden : .cookiePopupManaged
if privacyIconAndTrackersAnimator.state == .completed {
notificationAnimator.showNotification(type, in: self)
} else {
privacyIconAndTrackersAnimator.onAnimationCompletion = { [weak self] in
guard let self = self else { return }
self.notificationAnimator.showNotification(type, in: self)
}
}
}
func selectTextToEnd(_ offset: Int) {
guard let fromPosition = textField.position(from: textField.beginningOfDocument, offset: offset) else { return }
textField.selectedTextRange = textField.textRange(from: fromPosition, to: textField.endOfDocument)
}
fileprivate func refreshState(_ newState: OmniBarState) {
if state.name != newState.name {
os_log("OmniBar entering %s from %s", log: .generalLog, type: .debug, newState.name, state.name)
if newState.clearTextOnStart {
clear()
}
state = newState
cancelAllAnimations()
}
searchFieldContainer.adjustTextFieldOffset(for: state)
setVisibility(privacyInfoContainer, hidden: !state.showPrivacyIcon)
setVisibility(searchLoupe, hidden: !state.showSearchLoupe)
setVisibility(clearButton, hidden: !state.showClear)
setVisibility(menuButton, hidden: !state.showMenu)
setVisibility(settingsButton, hidden: !state.showSettings)
setVisibility(cancelButton, hidden: !state.showCancel)
setVisibility(refreshButton, hidden: !state.showRefresh)
setVisibility(voiceSearchButton, hidden: !state.showVoiceSearch)
setVisibility(backButton, hidden: !state.showBackButton)
setVisibility(forwardButton, hidden: !state.showForwardButton)
setVisibility(bookmarksButton, hidden: !state.showBookmarksButton)
setVisibility(shareButton, hidden: !state.showShareButton)
searchContainerCenterConstraint.isActive = state.hasLargeWidth
searchContainerMaxWidthConstraint.isActive = state.hasLargeWidth
leftButtonsSpacingConstraint.constant = state.hasLargeWidth ? 24 : 0
rightButtonsSpacingConstraint.constant = state.hasLargeWidth ? 24 : 14
if state.showVoiceSearch && state.showClear {
searchStackContainer.setCustomSpacing(13, after: voiceSearchButton)
}
updateOmniBarPadding()
UIView.animate(withDuration: 0.0) {
self.layoutIfNeeded()
}
}
private func updateOmniBarPadding() {
omniBarLeadingConstraint.constant = (state.hasLargeWidth ? 24 : 8) + safeAreaInsets.left
omniBarTrailingConstraint.constant = (state.hasLargeWidth ? 24 : 14) + safeAreaInsets.right
}
/*
Superfluous check to overcome apple bug in stack view where setting value more than
once causes issues, related to http://www.openradar.me/22819594
Kill this method when radar is fixed - burn it with fire ;-)
*/
private func setVisibility(_ view: UIView, hidden: Bool) {
if view.isHidden != hidden {
view.isHidden = hidden
}
}
@discardableResult override func becomeFirstResponder() -> Bool {
return textField.becomeFirstResponder()
}
@discardableResult override func resignFirstResponder() -> Bool {
return textField.resignFirstResponder()
}
private func clear() {
textField.text = nil
omniDelegate?.onOmniQueryUpdated("")
}
func refreshText(forUrl url: URL?) {
if textField.isEditing {
return
}
guard let url = url else {
textField.text = nil
return
}
if let query = url.searchQuery {
textField.text = query
} else {
textField.attributedText = OmniBar.demphasisePath(forUrl: url)
}
}
public class func demphasisePath(forUrl url: URL) -> NSAttributedString? {
let s = url.absoluteString
let attributedString = NSMutableAttributedString(string: s)
guard let c = URLComponents(url: url, resolvingAgainstBaseURL: true) else {
return attributedString
}
let theme = ThemeManager.shared.currentTheme
if let pathStart = c.rangeOfPath?.lowerBound {
let urlEnd = s.endIndex
let pathRange = NSRange(pathStart ..< urlEnd, in: s)
attributedString.addAttribute(.foregroundColor, value: theme.searchBarTextDeemphasisColor, range: pathRange)
let domainRange = NSRange(s.startIndex ..< pathStart, in: s)
attributedString.addAttribute(.foregroundColor, value: theme.searchBarTextColor, range: domainRange)
} else {
let range = NSRange(s.startIndex ..< s.endIndex, in: s)
attributedString.addAttribute(.foregroundColor, value: theme.searchBarTextColor, range: range)
}
return attributedString
}
@IBAction func onTextEntered(_ sender: Any) {
onQuerySubmitted()
}
func onQuerySubmitted() {
if let suggestion = omniDelegate?.selectedSuggestion() {
omniDelegate?.onOmniSuggestionSelected(suggestion)
} else {
guard let query = textField.text?.trimmingWhitespace(), !query.isEmpty else {
return
}
resignFirstResponder()
if let url = URL(trimmedAddressBarString: query), url.isValid {
omniDelegate?.onOmniQuerySubmitted(url.absoluteString)
} else {
omniDelegate?.onOmniQuerySubmitted(query)
}
}
}
@IBAction func onVoiceSearchButtonPressed(_ sender: UIButton) {
omniDelegate?.onVoiceSearchPressed()
}
@IBAction func onClearButtonPressed(_ sender: Any) {
refreshState(state.onTextClearedState)
}
@IBAction func onPrivacyIconPressed(_ sender: Any) {
omniDelegate?.onPrivacyIconPressed()
}
@IBAction func onMenuButtonPressed(_ sender: UIButton) {
omniDelegate?.onMenuPressed()
}
@IBAction func onTrackersViewPressed(_ sender: Any) {
cancelAllAnimations()
textField.becomeFirstResponder()
}
@IBAction func onSettingsButtonPressed(_ sender: Any) {
Pixel.fire(pixel: .addressBarSettings)
omniDelegate?.onSettingsPressed()
}
@IBAction func onCancelPressed(_ sender: Any) {
omniDelegate?.onCancelPressed()
}
@IBAction func onRefreshPressed(_ sender: Any) {
Pixel.fire(pixel: .refreshPressed)
cancelAllAnimations()
omniDelegate?.onRefreshPressed()
}
@IBAction func onBackPressed(_ sender: Any) {
omniDelegate?.onBackPressed()
}
@IBAction func onForwardPressed(_ sender: Any) {
omniDelegate?.onForwardPressed()
}
@IBAction func onBookmarksPressed(_ sender: Any) {
Pixel.fire(pixel: .bookmarksButtonPressed,
withAdditionalParameters: [PixelParameters.originatedFromMenu: "0"])
omniDelegate?.onBookmarksPressed()
}
@IBAction func onSharePressed(_ sender: Any) {
Pixel.fire(pixel: .addressBarShare)
omniDelegate?.onSharePressed()
}
func enterPhoneState() {
refreshState(state.onEnterPhoneState)
}
func enterPadState() {
refreshState(state.onEnterPadState)
}
override func layoutSubviews() {
super.layoutSubviews()
NotificationCenter.default.post(name: OmniBar.didLayoutNotification, object: self)
}
}
// swiftlint:enable type_body_length
extension OmniBar: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
self.refreshState(self.state.onEditingStartedState)
return true
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
omniDelegate?.onTextFieldWillBeginEditing(self)
return true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
DispatchQueue.main.async {
let highlightText = self.omniDelegate?.onTextFieldDidBeginEditing(self) ?? true
self.refreshState(self.state.onEditingStartedState)
if highlightText {
self.textField.selectAll(nil)
}
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
omniDelegate?.onEnterPressed()
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
omniDelegate?.onDismissed()
refreshState(state.onEditingStoppedState)
}
}
extension OmniBar: Themable {
public func decorate(with theme: Theme) {
backgroundColor = theme.omniBarBackgroundColor
tintColor = theme.barTintColor
configureTextField()
editingBackground?.backgroundColor = theme.searchBarBackgroundColor
editingBackground?.borderColor = theme.searchBarBackgroundColor
privacyInfoContainer.decorate(with: theme)
privacyIconAndTrackersAnimator.resetImageProvider()
searchStackContainer?.tintColor = theme.barTintColor
if let url = textField.text.flatMap({ URL(trimmedAddressBarString: $0.trimmingWhitespace()) }) {
textField.attributedText = OmniBar.demphasisePath(forUrl: url)
}
textField.textColor = theme.searchBarTextColor
textField.tintColor = UIColor(designSystemColor: .accent)
textField.keyboardAppearance = theme.keyboardAppearance
clearButton.tintColor = UIColor(designSystemColor: .icons)
voiceSearchButton.tintColor = UIColor(designSystemColor: .icons)
searchLoupe.tintColor = UIColor(designSystemColor: .icons)
searchLoupe.alpha = 0.5
cancelButton.setTitleColor(theme.barTintColor, for: .normal)
}
}
// swiftlint:enable file_length