-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathReaderModeStyleViewController.swift
378 lines (306 loc) · 15 KB
/
ReaderModeStyleViewController.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
/* 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 UIKit
import Shared
private struct ReaderModeStyleViewControllerUX {
static let RowHeight = 50
static let Width = 270
static let Height = 4 * RowHeight
static let FontTypeRowBackground = UIColor(rgb: 0xfbfbfb)
static let FontTypeTitleSelectedColor = UIColor(rgb: 0x333333)
static let FontTypeTitleNormalColor = UIColor.lightGray // TODO THis needs to be 44% of 0x333333
static let FontSizeRowBackground = UIColor(rgb: 0xf4f4f4)
static let FontSizeLabelColor = UIColor(rgb: 0x333333)
static let FontSizeButtonTextColorEnabled = UIColor(rgb: 0x333333)
static let FontSizeButtonTextColorDisabled = UIColor.lightGray // TODO THis needs to be 44% of 0x333333
static let ThemeRowBackgroundColor = UIColor.white
static let ThemeTitleColorLight = UIColor(rgb: 0x333333)
static let ThemeTitleColorDark = UIColor.white
static let ThemeTitleColorSepia = UIColor(rgb: 0x333333)
static let ThemeBackgroundColorLight = UIColor.white
static let ThemeBackgroundColorDark = UIColor(rgb: 0x333333)
static let ThemeBackgroundColorSepia = UIColor(rgb: 0xF0E6DC)
static let BrightnessRowBackground = UIColor(rgb: 0xf4f4f4)
static let BrightnessSliderTintColor = UIColor(rgb: 0xe66000)
static let BrightnessSliderWidth = 140
static let BrightnessIconOffset = 10
}
// MARK: -
protocol ReaderModeStyleViewControllerDelegate {
func readerModeStyleViewController(_ readerModeStyleViewController: ReaderModeStyleViewController, didConfigureStyle style: ReaderModeStyle)
}
// MARK: -
class ReaderModeStyleViewController: UIViewController {
var delegate: ReaderModeStyleViewControllerDelegate?
var readerModeStyle: ReaderModeStyle = DefaultReaderModeStyle
fileprivate var fontTypeButtons: [FontTypeButton]!
fileprivate var fontSizeLabel: FontSizeLabel!
fileprivate var fontSizeButtons: [FontSizeButton]!
fileprivate var themeButtons: [ThemeButton]!
override func viewDidLoad() {
// Our preferred content size has a fixed width and height based on the rows + padding
preferredContentSize = CGSize(width: ReaderModeStyleViewControllerUX.Width, height: ReaderModeStyleViewControllerUX.Height)
popoverPresentationController?.backgroundColor = ReaderModeStyleViewControllerUX.FontTypeRowBackground
// Font type row
let fontTypeRow = UIView()
view.addSubview(fontTypeRow)
fontTypeRow.backgroundColor = ReaderModeStyleViewControllerUX.FontTypeRowBackground
fontTypeRow.snp.makeConstraints { (make) -> Void in
make.top.equalTo(self.view)
make.left.right.equalTo(self.view)
make.height.equalTo(ReaderModeStyleViewControllerUX.RowHeight)
}
fontTypeButtons = [
FontTypeButton(fontType: ReaderModeFontType.sansSerif),
FontTypeButton(fontType: ReaderModeFontType.serif)
]
setupButtons(fontTypeButtons, inRow: fontTypeRow, action: #selector(changeFontType))
// Font size row
let fontSizeRow = UIView()
view.addSubview(fontSizeRow)
fontSizeRow.backgroundColor = ReaderModeStyleViewControllerUX.FontSizeRowBackground
fontSizeRow.snp.makeConstraints { (make) -> Void in
make.top.equalTo(fontTypeRow.snp.bottom)
make.left.right.equalTo(self.view)
make.height.equalTo(ReaderModeStyleViewControllerUX.RowHeight)
}
fontSizeLabel = FontSizeLabel()
fontSizeRow.addSubview(fontSizeLabel)
fontSizeLabel.snp.makeConstraints { (make) -> Void in
make.center.equalTo(fontSizeRow)
return
}
fontSizeButtons = [
FontSizeButton(fontSizeAction: FontSizeAction.smaller),
FontSizeButton(fontSizeAction: FontSizeAction.reset),
FontSizeButton(fontSizeAction: FontSizeAction.bigger)
]
setupButtons(fontSizeButtons, inRow: fontSizeRow, action: #selector(changeFontSize))
// Theme row
let themeRow = UIView()
view.addSubview(themeRow)
themeRow.snp.makeConstraints { (make) -> Void in
make.top.equalTo(fontSizeRow.snp.bottom)
make.left.right.equalTo(self.view)
make.height.equalTo(ReaderModeStyleViewControllerUX.RowHeight)
}
themeButtons = [
ThemeButton(theme: ReaderModeTheme.light),
ThemeButton(theme: ReaderModeTheme.dark),
ThemeButton(theme: ReaderModeTheme.sepia)
]
setupButtons(themeButtons, inRow: themeRow, action: #selector(changeTheme))
// Brightness row
let brightnessRow = UIView()
view.addSubview(brightnessRow)
brightnessRow.backgroundColor = ReaderModeStyleViewControllerUX.BrightnessRowBackground
brightnessRow.snp.makeConstraints { (make) -> Void in
make.top.equalTo(themeRow.snp.bottom)
make.left.right.equalTo(self.view)
make.height.equalTo(ReaderModeStyleViewControllerUX.RowHeight)
}
let slider = UISlider()
brightnessRow.addSubview(slider)
slider.accessibilityLabel = NSLocalizedString("Brightness", comment: "Accessibility label for brightness adjustment slider in Reader Mode display settings")
slider.tintColor = ReaderModeStyleViewControllerUX.BrightnessSliderTintColor
slider.addTarget(self, action: #selector(changeBrightness), for: .valueChanged)
slider.snp.makeConstraints { make in
make.center.equalTo(brightnessRow)
make.width.equalTo(ReaderModeStyleViewControllerUX.BrightnessSliderWidth)
}
let brightnessMinImageView = UIImageView(image: UIImage(named: "brightnessMin"))
brightnessRow.addSubview(brightnessMinImageView)
brightnessMinImageView.snp.makeConstraints { (make) -> Void in
make.centerY.equalTo(slider)
make.right.equalTo(slider.snp.left).offset(-ReaderModeStyleViewControllerUX.BrightnessIconOffset)
}
let brightnessMaxImageView = UIImageView(image: UIImage(named: "brightnessMax"))
brightnessRow.addSubview(brightnessMaxImageView)
brightnessMaxImageView.snp.makeConstraints { (make) -> Void in
make.centerY.equalTo(slider)
make.left.equalTo(slider.snp.right).offset(ReaderModeStyleViewControllerUX.BrightnessIconOffset)
}
selectFontType(readerModeStyle.fontType)
updateFontSizeButtons()
selectTheme(readerModeStyle.theme)
slider.value = Float(UIScreen.main.brightness)
}
/// Setup constraints for a row of buttons. Left to right. They are all given the same width.
fileprivate func setupButtons(_ buttons: [UIButton], inRow row: UIView, action: Selector) {
for (idx, button) in buttons.enumerated() {
row.addSubview(button)
button.addTarget(self, action: action, for: .touchUpInside)
button.snp.makeConstraints { make in
make.top.equalTo(row.snp.top)
if idx == 0 {
make.left.equalTo(row.snp.left)
} else {
make.left.equalTo(buttons[idx - 1].snp.right)
}
make.bottom.equalTo(row.snp.bottom)
make.width.equalTo(self.preferredContentSize.width / CGFloat(buttons.count))
}
}
}
@objc func changeFontType(_ button: FontTypeButton) {
selectFontType(button.fontType)
delegate?.readerModeStyleViewController(self, didConfigureStyle: readerModeStyle)
}
fileprivate func selectFontType(_ fontType: ReaderModeFontType) {
readerModeStyle.fontType = fontType
for button in fontTypeButtons {
button.isSelected = (button.fontType == fontType)
}
for button in themeButtons {
button.fontType = fontType
}
fontSizeLabel.fontType = fontType
}
@objc func changeFontSize(_ button: FontSizeButton) {
switch button.fontSizeAction {
case .smaller:
readerModeStyle.fontSize = readerModeStyle.fontSize.smaller()
case .bigger:
readerModeStyle.fontSize = readerModeStyle.fontSize.bigger()
case .reset:
readerModeStyle.fontSize = ReaderModeFontSize.defaultSize
}
updateFontSizeButtons()
delegate?.readerModeStyleViewController(self, didConfigureStyle: readerModeStyle)
}
fileprivate func updateFontSizeButtons() {
for button in fontSizeButtons {
switch button.fontSizeAction {
case .bigger:
button.isEnabled = !readerModeStyle.fontSize.isLargest()
break
case .smaller:
button.isEnabled = !readerModeStyle.fontSize.isSmallest()
break
case .reset:
break
}
}
}
@objc func changeTheme(_ button: ThemeButton) {
selectTheme(button.theme)
delegate?.readerModeStyleViewController(self, didConfigureStyle: readerModeStyle)
}
fileprivate func selectTheme(_ theme: ReaderModeTheme) {
readerModeStyle.theme = theme
}
@objc func changeBrightness(_ slider: UISlider) {
UIScreen.main.brightness = CGFloat(slider.value)
}
}
// MARK: -
class FontTypeButton: UIButton {
var fontType: ReaderModeFontType = .sansSerif
convenience init(fontType: ReaderModeFontType) {
self.init(frame: .zero)
self.fontType = fontType
setTitleColor(ReaderModeStyleViewControllerUX.FontTypeTitleSelectedColor, for: .selected)
setTitleColor(ReaderModeStyleViewControllerUX.FontTypeTitleNormalColor, for: [])
backgroundColor = ReaderModeStyleViewControllerUX.FontTypeRowBackground
accessibilityHint = NSLocalizedString("Changes font type.", comment: "Accessibility hint for the font type buttons in reader mode display settings")
switch fontType {
case .sansSerif:
setTitle(NSLocalizedString("Sans-serif", comment: "Font type setting in the reading view settings"), for: [])
let f = UIFont(name: "FiraSans-Book", size: DynamicFontHelper.defaultHelper.ReaderStandardFontSize)
titleLabel?.font = f
case .serif:
setTitle(NSLocalizedString("Serif", comment: "Font type setting in the reading view settings"), for: [])
let f = UIFont(name: "Charis SIL", size: DynamicFontHelper.defaultHelper.ReaderStandardFontSize)
titleLabel?.font = f
}
}
}
// MARK: -
enum FontSizeAction {
case smaller
case reset
case bigger
}
class FontSizeButton: UIButton {
var fontSizeAction: FontSizeAction = .bigger
convenience init(fontSizeAction: FontSizeAction) {
self.init(frame: .zero)
self.fontSizeAction = fontSizeAction
setTitleColor(ReaderModeStyleViewControllerUX.FontSizeButtonTextColorEnabled, for: .normal)
setTitleColor(ReaderModeStyleViewControllerUX.FontSizeButtonTextColorDisabled, for: .disabled)
switch fontSizeAction {
case .smaller:
let smallerFontLabel = NSLocalizedString("-", comment: "Button for smaller reader font size. Keep this extremely short! This is shown in the reader mode toolbar.")
let smallerFontAccessibilityLabel = NSLocalizedString("Decrease text size", comment: "Accessibility label for button decreasing font size in display settings of reader mode")
setTitle(smallerFontLabel, for: [])
accessibilityLabel = smallerFontAccessibilityLabel
case .bigger:
let largerFontLabel = NSLocalizedString("+", comment: "Button for larger reader font size. Keep this extremely short! This is shown in the reader mode toolbar.")
let largerFontAccessibilityLabel = NSLocalizedString("Increase text size", comment: "Accessibility label for button increasing font size in display settings of reader mode")
setTitle(largerFontLabel, for: [])
accessibilityLabel = largerFontAccessibilityLabel
case .reset:
accessibilityLabel = Strings.ReaderModeResetFontSizeAccessibilityLabel
}
// TODO Does this need to change with the selected font type? Not sure if makes sense for just +/-
titleLabel?.font = UIFont(name: "FiraSans-Light", size: DynamicFontHelper.defaultHelper.ReaderBigFontSize)
}
}
// MARK: -
class FontSizeLabel: UILabel {
override init(frame: CGRect) {
super.init(frame: frame)
let fontSizeLabel = NSLocalizedString("Aa", comment: "Button for reader mode font size. Keep this extremely short! This is shown in the reader mode toolbar.")
text = fontSizeLabel
isAccessibilityElement = false
}
required init?(coder aDecoder: NSCoder) {
// TODO
fatalError("init(coder:) has not been implemented")
}
var fontType: ReaderModeFontType = .sansSerif {
didSet {
switch fontType {
case .sansSerif:
font = UIFont(name: "FiraSans-Book", size: DynamicFontHelper.defaultHelper.ReaderBigFontSize)
case .serif:
font = UIFont(name: "Charis SIL", size: DynamicFontHelper.defaultHelper.ReaderBigFontSize)
}
}
}
}
// MARK: -
class ThemeButton: UIButton {
var theme: ReaderModeTheme!
convenience init(theme: ReaderModeTheme) {
self.init(frame: .zero)
self.theme = theme
setTitle(theme.rawValue, for: [])
accessibilityHint = NSLocalizedString("Changes color theme.", comment: "Accessibility hint for the color theme setting buttons in reader mode display settings")
switch theme {
case .light:
setTitle(NSLocalizedString("Light", comment: "Light theme setting in Reading View settings"), for: [])
setTitleColor(ReaderModeStyleViewControllerUX.ThemeTitleColorLight, for: .normal)
backgroundColor = ReaderModeStyleViewControllerUX.ThemeBackgroundColorLight
case .dark:
setTitle(NSLocalizedString("Dark", comment: "Dark theme setting in Reading View settings"), for: [])
setTitleColor(ReaderModeStyleViewControllerUX.ThemeTitleColorDark, for: [])
backgroundColor = ReaderModeStyleViewControllerUX.ThemeBackgroundColorDark
case .sepia:
setTitle(NSLocalizedString("Sepia", comment: "Sepia theme setting in Reading View settings"), for: [])
setTitleColor(ReaderModeStyleViewControllerUX.ThemeTitleColorSepia, for: .normal)
backgroundColor = ReaderModeStyleViewControllerUX.ThemeBackgroundColorSepia
}
}
var fontType: ReaderModeFontType = .sansSerif {
didSet {
switch fontType {
case .sansSerif:
titleLabel?.font = UIFont(name: "FiraSans-Book", size: DynamicFontHelper.defaultHelper.ReaderStandardFontSize)
case .serif:
titleLabel?.font = UIFont(name: "Charis SIL", size: DynamicFontHelper.defaultHelper.ReaderStandardFontSize)
}
}
}
}