2020-07-24 19:26:35 -07:00
|
|
|
//
|
|
|
|
|
// ToolbarViewController.swift
|
|
|
|
|
// SBrowser
|
|
|
|
|
//
|
|
|
|
|
// Created by James Magahern on 7/23/20.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
2021-02-15 22:47:02 -08:00
|
|
|
public struct DarkModeControls
|
|
|
|
|
{
|
|
|
|
|
public static func buttonImage(forDarkModeState enabled: Bool) -> UIImage {
|
|
|
|
|
if enabled {
|
|
|
|
|
return UIImage(systemName: "moon.circle.fill")!
|
|
|
|
|
} else {
|
|
|
|
|
return UIImage(systemName: "moon.circle")!
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-24 19:26:35 -07:00
|
|
|
class ToolbarViewController: UIViewController
|
|
|
|
|
{
|
|
|
|
|
let urlBar = URLBar()
|
|
|
|
|
let toolbarView = ToolbarView()
|
|
|
|
|
let scriptControllerIconView = ScriptControllerIconView()
|
2020-08-14 15:55:08 -07:00
|
|
|
|
|
|
|
|
let shareButton = ReliefButton()
|
|
|
|
|
let darkModeButton = ReliefButton()
|
|
|
|
|
let windowButton = ReliefButton()
|
|
|
|
|
let newTabButton = ReliefButton()
|
|
|
|
|
|
|
|
|
|
let backButton = ReliefButton()
|
|
|
|
|
let forwardButton = ReliefButton()
|
|
|
|
|
let navigationSegmentedButton = SegmentedReliefButton(children: [])
|
2020-07-24 19:26:35 -07:00
|
|
|
|
2020-07-29 18:17:22 -07:00
|
|
|
var darkModeEnabled: Bool = false {
|
|
|
|
|
didSet {
|
2021-02-15 22:47:02 -08:00
|
|
|
darkModeButton.setImage(DarkModeControls.buttonImage(forDarkModeState: darkModeEnabled), for: .normal)
|
2020-07-29 18:17:22 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-24 19:26:35 -07:00
|
|
|
init() {
|
|
|
|
|
super.init(nibName: nil, bundle: nil)
|
|
|
|
|
|
|
|
|
|
toolbarView.urlBar = urlBar
|
|
|
|
|
|
2020-07-29 19:24:05 -07:00
|
|
|
// Dark mode button
|
2021-02-15 22:47:02 -08:00
|
|
|
darkModeButton.setImage(DarkModeControls.buttonImage(forDarkModeState: darkModeEnabled), for: .normal)
|
2020-07-29 17:46:53 -07:00
|
|
|
|
2020-07-29 19:24:05 -07:00
|
|
|
// Share button
|
2020-07-24 19:26:35 -07:00
|
|
|
shareButton.setImage(UIImage(systemName: "square.and.arrow.up"), for: .normal)
|
2020-07-29 19:24:05 -07:00
|
|
|
|
|
|
|
|
// Window button
|
|
|
|
|
windowButton.setImage(UIImage(systemName: "rectangle.on.rectangle"), for: .normal)
|
2020-07-29 17:46:53 -07:00
|
|
|
|
2020-07-31 14:08:10 -07:00
|
|
|
// Back button
|
|
|
|
|
backButton.setImage(UIImage(systemName: "chevron.left"), for: .normal)
|
|
|
|
|
|
|
|
|
|
// Forward button
|
|
|
|
|
forwardButton.setImage(UIImage(systemName: "chevron.right"), for: .normal)
|
|
|
|
|
|
2020-08-14 15:55:08 -07:00
|
|
|
// Navigation control
|
|
|
|
|
navigationSegmentedButton.children = [ backButton, forwardButton ]
|
|
|
|
|
|
2020-07-31 16:36:10 -07:00
|
|
|
// New tab button
|
|
|
|
|
newTabButton.setImage(UIImage(systemName: "plus"), for: .normal)
|
|
|
|
|
|
2020-07-29 17:46:53 -07:00
|
|
|
let toolbarAnimationDuration: TimeInterval = 0.3
|
2020-07-31 14:23:00 -07:00
|
|
|
urlBar.textField.addAction(.init(handler: { [traitCollection, toolbarView, urlBar] _ in
|
|
|
|
|
if traitCollection.horizontalSizeClass == .compact {
|
|
|
|
|
UIView.animate(withDuration: toolbarAnimationDuration) {
|
|
|
|
|
toolbarView.cancelButtonVisible = urlBar.textField.isFirstResponder
|
|
|
|
|
}
|
2020-07-29 17:46:53 -07:00
|
|
|
}
|
|
|
|
|
}), for: [ .editingDidBegin, .editingDidEnd ])
|
|
|
|
|
|
2020-07-31 14:08:10 -07:00
|
|
|
toolbarView.cancelButton.addAction(.init(handler: { [urlBar] action in
|
|
|
|
|
urlBar.textField.resignFirstResponder()
|
2020-07-29 17:46:53 -07:00
|
|
|
}), for: .touchUpInside)
|
2020-07-29 19:24:05 -07:00
|
|
|
|
|
|
|
|
traitCollectionDidChange(nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
|
|
|
|
|
super.traitCollectionDidChange(previousTraitCollection)
|
|
|
|
|
|
2020-07-31 14:08:10 -07:00
|
|
|
toolbarView.leadingButtonsView.removeAllButtonViews()
|
|
|
|
|
toolbarView.trailingButtonsView.removeAllButtonViews()
|
2020-07-29 19:24:05 -07:00
|
|
|
|
2020-08-14 15:55:08 -07:00
|
|
|
let spacerView = { () -> SpacerView in
|
2022-02-21 15:52:18 -08:00
|
|
|
SpacerView(space: 20.0)
|
2020-08-14 15:55:08 -07:00
|
|
|
}
|
|
|
|
|
|
2020-07-29 19:24:05 -07:00
|
|
|
// Setup toolbar based on trait collection
|
|
|
|
|
if traitCollection.horizontalSizeClass == .compact {
|
2020-07-31 14:08:10 -07:00
|
|
|
toolbarView.trailingButtonsView.addButtonView(scriptControllerIconView)
|
2021-02-15 22:48:18 -08:00
|
|
|
toolbarView.trailingButtonsView.addButtonView(newTabButton)
|
2020-07-31 14:08:10 -07:00
|
|
|
toolbarView.trailingButtonsView.addButtonView(windowButton)
|
2020-07-29 19:24:05 -07:00
|
|
|
} else {
|
2020-08-14 15:55:08 -07:00
|
|
|
toolbarView.leadingButtonsView.addButtonView(navigationSegmentedButton)
|
|
|
|
|
toolbarView.leadingButtonsView.addButtonView(spacerView())
|
2020-07-31 14:08:10 -07:00
|
|
|
|
|
|
|
|
toolbarView.trailingButtonsView.addButtonView(shareButton)
|
2020-08-14 15:55:08 -07:00
|
|
|
toolbarView.trailingButtonsView.addButtonView(spacerView())
|
|
|
|
|
|
|
|
|
|
toolbarView.trailingButtonsView.addButtonView(darkModeButton)
|
2020-07-31 14:08:10 -07:00
|
|
|
toolbarView.trailingButtonsView.addButtonView(scriptControllerIconView)
|
2020-08-14 15:55:08 -07:00
|
|
|
|
|
|
|
|
toolbarView.trailingButtonsView.addButtonView(spacerView())
|
|
|
|
|
|
2020-07-31 16:36:10 -07:00
|
|
|
toolbarView.trailingButtonsView.addButtonView(newTabButton)
|
2020-07-31 14:08:10 -07:00
|
|
|
toolbarView.trailingButtonsView.addButtonView(windowButton)
|
2020-07-29 19:24:05 -07:00
|
|
|
}
|
2020-07-24 19:26:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func loadView() {
|
|
|
|
|
self.view = toolbarView
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-14 15:55:08 -07:00
|
|
|
|
|
|
|
|
class SpacerView: UIView {
|
|
|
|
|
internal var space: CGFloat = 0
|
|
|
|
|
|
|
|
|
|
convenience init(space: CGFloat) {
|
|
|
|
|
self.init(frame: .zero)
|
|
|
|
|
self.space = space
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func sizeThatFits(_ size: CGSize) -> CGSize {
|
|
|
|
|
CGSize(width: space, height: size.height)
|
|
|
|
|
}
|
|
|
|
|
}
|