/
BrowserToolbar.swift
217 lines (178 loc) · 10.2 KB
/
BrowserToolbar.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
/* 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 UIKit
import SnapKit
@objc
protocol BrowserToolbarProtocol {
weak var browserToolbarDelegate: BrowserToolbarDelegate? { get set }
var shareButton: UIButton { get }
var bookmarkButton: UIButton { get }
var forwardButton: UIButton { get }
var backButton: UIButton { get }
var stopReloadButton: UIButton { get }
func updateBackStatus(canGoBack: Bool)
func updateForwardStatus(canGoForward: Bool)
func updateBookmarkStatus(isBookmarked: Bool)
func updateReloadStatus(isLoading: Bool)
func updatePageStatus(#isWebPage: Bool)
}
@objc
protocol BrowserToolbarDelegate: class {
func browserToolbarDidPressBack(browserToolbar: BrowserToolbarProtocol, button: UIButton)
func browserToolbarDidPressForward(browserToolbar: BrowserToolbarProtocol, button: UIButton)
func browserToolbarDidLongPressBack(browserToolbar: BrowserToolbarProtocol, button: UIButton)
func browserToolbarDidLongPressForward(browserToolbar: BrowserToolbarProtocol, button: UIButton)
func browserToolbarDidPressReload(browserToolbar: BrowserToolbarProtocol, button: UIButton)
func browserToolbarDidPressStop(browserToolbar: BrowserToolbarProtocol, button: UIButton)
func browserToolbarDidPressBookmark(browserToolbar: BrowserToolbarProtocol, button: UIButton)
func browserToolbarDidLongPressBookmark(browserToolbar: BrowserToolbarProtocol, button: UIButton)
func browserToolbarDidPressShare(browserToolbar: BrowserToolbarProtocol, button: UIButton)
}
@objc
public class BrowserToolbarHelper: NSObject {
let toolbar: BrowserToolbarProtocol
let ImageReload = UIImage(named: "reload")
let ImageReloadPressed = UIImage(named: "reloadPressed")
let ImageStop = UIImage(named: "stop")
let ImageStopPressed = UIImage(named: "stopPressed")
var loading: Bool = false {
didSet {
if loading {
toolbar.stopReloadButton.setImage(ImageStop, forState: .Normal)
toolbar.stopReloadButton.setImage(ImageStopPressed, forState: .Highlighted)
toolbar.stopReloadButton.accessibilityLabel = NSLocalizedString("Stop", comment: "Accessibility Label for the browser toolbar Stop button")
toolbar.stopReloadButton.accessibilityHint = NSLocalizedString("Tap to stop loading the page", comment: "")
} else {
toolbar.stopReloadButton.setImage(ImageReload, forState: .Normal)
toolbar.stopReloadButton.setImage(ImageReloadPressed, forState: .Highlighted)
toolbar.stopReloadButton.accessibilityLabel = NSLocalizedString("Reload", comment: "Accessibility Label for the browser toolbar Reload button")
toolbar.stopReloadButton.accessibilityHint = NSLocalizedString("Tap to reload the page", comment: "")
}
}
}
init(toolbar: BrowserToolbarProtocol) {
self.toolbar = toolbar
super.init()
toolbar.backButton.setImage(UIImage(named: "back"), forState: .Normal)
toolbar.backButton.setImage(UIImage(named: "backPressed"), forState: .Highlighted)
toolbar.backButton.accessibilityLabel = NSLocalizedString("Back", comment: "Accessibility Label for the browser toolbar Back button")
//toolbar.backButton.accessibilityHint = NSLocalizedString("Double tap and hold to open history", comment: "")
var longPressGestureBackButton = UILongPressGestureRecognizer(target: self, action: "SELdidLongPressBack:")
toolbar.backButton.addGestureRecognizer(longPressGestureBackButton)
toolbar.backButton.addTarget(self, action: "SELdidClickBack", forControlEvents: UIControlEvents.TouchUpInside)
toolbar.forwardButton.setImage(UIImage(named: "forward"), forState: .Normal)
toolbar.forwardButton.setImage(UIImage(named: "forwardPressed"), forState: .Highlighted)
toolbar.forwardButton.accessibilityLabel = NSLocalizedString("Forward", comment: "Accessibility Label for the browser toolbar Forward button")
//toolbar.forwardButton.accessibilityHint = NSLocalizedString("Double tap and hold to open history", comment: "")
var longPressGestureForwardButton = UILongPressGestureRecognizer(target: self, action: "SELdidLongPressForward:")
toolbar.forwardButton.addGestureRecognizer(longPressGestureForwardButton)
toolbar.forwardButton.addTarget(self, action: "SELdidClickForward", forControlEvents: UIControlEvents.TouchUpInside)
toolbar.stopReloadButton.setImage(UIImage(named: "reload"), forState: .Normal)
toolbar.stopReloadButton.setImage(UIImage(named: "reloadPressed"), forState: .Highlighted)
toolbar.stopReloadButton.accessibilityLabel = NSLocalizedString("Reload", comment: "Accessibility Label for the browser toolbar Reload button")
toolbar.stopReloadButton.accessibilityHint = NSLocalizedString("Tap to reload the page", comment: "Accessibility hint for the browser toolbar Reload button")
toolbar.stopReloadButton.addTarget(self, action: "SELdidClickStopReload", forControlEvents: UIControlEvents.TouchUpInside)
toolbar.shareButton.setImage(UIImage(named: "send"), forState: .Normal)
toolbar.shareButton.setImage(UIImage(named: "sendPressed"), forState: .Highlighted)
toolbar.shareButton.accessibilityLabel = NSLocalizedString("Share", comment: "Accessibility Label for the browser toolbar Share button")
toolbar.shareButton.addTarget(self, action: "SELdidClickShare", forControlEvents: UIControlEvents.TouchUpInside)
toolbar.bookmarkButton.contentMode = UIViewContentMode.Center
toolbar.bookmarkButton.setImage(UIImage(named: "bookmark"), forState: .Normal)
toolbar.bookmarkButton.setImage(UIImage(named: "bookmarked"), forState: UIControlState.Selected)
toolbar.bookmarkButton.accessibilityLabel = NSLocalizedString("Bookmark", comment: "Accessibility Label for the browser toolbar Bookmark button")
var longPressGestureBookmarkButton = UILongPressGestureRecognizer(target: self, action: "SELdidLongPressBookmark:")
toolbar.bookmarkButton.addGestureRecognizer(longPressGestureBookmarkButton)
toolbar.bookmarkButton.addTarget(self, action: "SELdidClickBookmark", forControlEvents: UIControlEvents.TouchUpInside)
}
func SELdidClickBack() {
toolbar.browserToolbarDelegate?.browserToolbarDidPressBack(toolbar, button: toolbar.backButton)
}
func SELdidLongPressBack(recognizer: UILongPressGestureRecognizer) {
if recognizer.state == UIGestureRecognizerState.Began {
toolbar.browserToolbarDelegate?.browserToolbarDidLongPressBack(toolbar, button: toolbar.backButton)
}
}
func SELdidClickShare() {
toolbar.browserToolbarDelegate?.browserToolbarDidPressShare(toolbar, button: toolbar.shareButton)
}
func SELdidClickForward() {
toolbar.browserToolbarDelegate?.browserToolbarDidPressForward(toolbar, button: toolbar.forwardButton)
}
func SELdidLongPressForward(recognizer: UILongPressGestureRecognizer) {
if recognizer.state == UIGestureRecognizerState.Began {
toolbar.browserToolbarDelegate?.browserToolbarDidLongPressForward(toolbar, button: toolbar.forwardButton)
}
}
func SELdidClickBookmark() {
toolbar.browserToolbarDelegate?.browserToolbarDidPressBookmark(toolbar, button: toolbar.bookmarkButton)
}
func SELdidLongPressBookmark(recognizer: UILongPressGestureRecognizer) {
if recognizer.state == UIGestureRecognizerState.Began {
toolbar.browserToolbarDelegate?.browserToolbarDidLongPressBookmark(toolbar, button: toolbar.bookmarkButton)
}
}
func SELdidClickStopReload() {
if loading {
toolbar.browserToolbarDelegate?.browserToolbarDidPressStop(toolbar, button: toolbar.stopReloadButton)
} else {
toolbar.browserToolbarDelegate?.browserToolbarDidPressReload(toolbar, button: toolbar.stopReloadButton)
}
}
func updateReloadStatus(isLoading: Bool) {
loading = isLoading
}
}
class BrowserToolbar: Toolbar, BrowserToolbarProtocol {
weak var browserToolbarDelegate: BrowserToolbarDelegate?
let shareButton: UIButton
let bookmarkButton: UIButton
let forwardButton: UIButton
let backButton: UIButton
let stopReloadButton: UIButton
var helper: BrowserToolbarHelper?
// This has to be here since init() calls it
private override init(frame: CGRect) {
// And these have to be initialized in here or the compiler will get angry
backButton = UIButton()
forwardButton = UIButton()
stopReloadButton = UIButton()
shareButton = UIButton()
bookmarkButton = UIButton()
super.init(frame: frame)
self.helper = BrowserToolbarHelper(toolbar: self)
addButtons(backButton, forwardButton, stopReloadButton, shareButton, bookmarkButton)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func updateBackStatus(canGoBack: Bool) {
backButton.enabled = canGoBack
}
func updateForwardStatus(canGoForward: Bool) {
forwardButton.enabled = canGoForward
}
func updateBookmarkStatus(isBookmarked: Bool) {
bookmarkButton.selected = isBookmarked
}
func updateReloadStatus(isLoading: Bool) {
helper?.updateReloadStatus(isLoading)
}
func updatePageStatus(#isWebPage: Bool) {
bookmarkButton.enabled = isWebPage
stopReloadButton.enabled = isWebPage
shareButton.enabled = isWebPage
}
override func drawRect(rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
drawLine(context, start: CGPoint(x: 0, y: 0), end: CGPoint(x: frame.width, y: 0))
}
private func drawLine(context: CGContextRef, start: CGPoint, end: CGPoint) {
CGContextSetStrokeColorWithColor(context, UIColor.blackColor().colorWithAlphaComponent(0.05).CGColor)
CGContextSetLineWidth(context, 2)
CGContextMoveToPoint(context, start.x, start.y)
CGContextAddLineToPoint(context, end.x, end.y)
CGContextStrokePath(context)
}
}