2020-12-19 20:19:32 +00:00
|
|
|
//
|
|
|
|
// ToolbarView.swift
|
|
|
|
// Gemini-iOS
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 12/19/20.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import BrowserCore
|
|
|
|
import Combine
|
|
|
|
|
|
|
|
class ToolbarView: UIView {
|
|
|
|
|
|
|
|
let navigator: NavigationManager
|
|
|
|
|
2020-12-20 18:45:22 +00:00
|
|
|
var showTableOfContents: (() -> Void)?
|
2020-12-19 20:19:32 +00:00
|
|
|
var showShareSheet: ((UIView) -> Void)?
|
|
|
|
var showPreferences: (() -> Void)?
|
|
|
|
|
|
|
|
private var border: UIView!
|
|
|
|
private var backButton: UIButton!
|
|
|
|
private var forwardsButton: UIButton!
|
|
|
|
private var reloadButton: UIButton!
|
2020-12-20 18:45:22 +00:00
|
|
|
private var tableOfContentsButton: UIButton!
|
2020-12-19 20:19:32 +00:00
|
|
|
private var shareButton: UIButton!
|
|
|
|
private var prefsButton: UIButton!
|
|
|
|
|
|
|
|
private var cancellables = [AnyCancellable]()
|
|
|
|
|
|
|
|
init(navigator: NavigationManager) {
|
|
|
|
self.navigator = navigator
|
|
|
|
|
|
|
|
super.init(frame: .zero)
|
|
|
|
|
|
|
|
backgroundColor = .systemBackground
|
|
|
|
|
|
|
|
border = UIView()
|
|
|
|
border.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
border.backgroundColor = UIColor(white: traitCollection.userInterfaceStyle == .dark ? 0.25 : 0.75, alpha: 1)
|
|
|
|
addSubview(border)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
border.leadingAnchor.constraint(equalTo: leadingAnchor),
|
|
|
|
border.trailingAnchor.constraint(equalTo: trailingAnchor),
|
|
|
|
border.topAnchor.constraint(equalTo: topAnchor),
|
|
|
|
border.heightAnchor.constraint(equalToConstant: 1),
|
|
|
|
])
|
|
|
|
|
|
|
|
let symbolConfig = UIImage.SymbolConfiguration(pointSize: 24)
|
|
|
|
|
|
|
|
backButton = UIButton()
|
|
|
|
backButton.addTarget(navigator, action: #selector(NavigationManager.goBack), for: .touchUpInside)
|
|
|
|
backButton.isEnabled = navigator.backStack.count > 0
|
|
|
|
backButton.setImage(UIImage(systemName: "arrow.left", withConfiguration: symbolConfig), for: .normal)
|
|
|
|
backButton.accessibilityLabel = "Back"
|
|
|
|
backButton.isPointerInteractionEnabled = true
|
2020-12-20 19:03:38 +00:00
|
|
|
// fallback for when UIButton.menu isn't available
|
|
|
|
if #available(iOS 14.0, *) {
|
|
|
|
} else {
|
|
|
|
backButton.addInteraction(UIContextMenuInteraction(delegate: self))
|
|
|
|
}
|
2020-12-19 20:19:32 +00:00
|
|
|
|
|
|
|
forwardsButton = UIButton()
|
|
|
|
forwardsButton.addTarget(navigator, action: #selector(NavigationManager.goForward), for: .touchUpInside)
|
|
|
|
forwardsButton.isEnabled = navigator.forwardStack.count > 0
|
|
|
|
forwardsButton.setImage(UIImage(systemName: "arrow.right", withConfiguration: symbolConfig), for: .normal)
|
|
|
|
forwardsButton.accessibilityLabel = "Forward"
|
|
|
|
forwardsButton.isPointerInteractionEnabled = true
|
2020-12-20 19:03:38 +00:00
|
|
|
if #available(iOS 14.0, *) {
|
|
|
|
} else {
|
|
|
|
forwardsButton.addInteraction(UIContextMenuInteraction(delegate: self))
|
|
|
|
}
|
2020-12-19 20:19:32 +00:00
|
|
|
|
|
|
|
reloadButton = UIButton()
|
|
|
|
reloadButton.addTarget(navigator, action: #selector(NavigationManager.reload), for: .touchUpInside)
|
|
|
|
reloadButton.setImage(UIImage(systemName: "arrow.clockwise", withConfiguration: symbolConfig), for: .normal)
|
|
|
|
reloadButton.accessibilityLabel = "Reload"
|
|
|
|
reloadButton.isPointerInteractionEnabled = true
|
|
|
|
|
2020-12-20 18:45:22 +00:00
|
|
|
tableOfContentsButton = UIButton()
|
|
|
|
tableOfContentsButton.addTarget(self, action: #selector(tableOfContentsPressed), for: .touchUpInside)
|
|
|
|
tableOfContentsButton.setImage(UIImage(systemName: "list.bullet.indent", withConfiguration: symbolConfig), for: .normal)
|
|
|
|
tableOfContentsButton.accessibilityLabel = "Table of Contents"
|
|
|
|
tableOfContentsButton.isPointerInteractionEnabled = true
|
|
|
|
|
2020-12-19 20:19:32 +00:00
|
|
|
shareButton = UIButton()
|
|
|
|
shareButton.addTarget(self, action: #selector(sharePressed), for: .touchUpInside)
|
|
|
|
shareButton.setImage(UIImage(systemName: "square.and.arrow.up", withConfiguration: symbolConfig), for: .normal)
|
|
|
|
shareButton.accessibilityLabel = "Share"
|
|
|
|
shareButton.isPointerInteractionEnabled = true
|
|
|
|
|
|
|
|
prefsButton = UIButton()
|
|
|
|
prefsButton.addTarget(self, action: #selector(prefsPressed), for: .touchUpInside)
|
|
|
|
prefsButton.setImage(UIImage(systemName: "gear", withConfiguration: symbolConfig), for: .normal)
|
|
|
|
prefsButton.accessibilityLabel = "Preferences"
|
|
|
|
prefsButton.isPointerInteractionEnabled = true
|
|
|
|
|
|
|
|
let stack = UIStackView(arrangedSubviews: [
|
|
|
|
backButton,
|
|
|
|
forwardsButton,
|
|
|
|
reloadButton,
|
2020-12-20 18:45:22 +00:00
|
|
|
tableOfContentsButton,
|
2020-12-19 20:19:32 +00:00
|
|
|
shareButton,
|
|
|
|
prefsButton,
|
|
|
|
])
|
|
|
|
stack.axis = .horizontal
|
|
|
|
stack.distribution = .fillEqually
|
|
|
|
stack.alignment = .fill
|
|
|
|
stack.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
addSubview(stack)
|
2020-12-20 19:37:31 +00:00
|
|
|
let safeAreaConstraint = stack.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor)
|
|
|
|
safeAreaConstraint.priority = .defaultHigh
|
2020-12-19 20:19:32 +00:00
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
stack.leadingAnchor.constraint(equalTo: leadingAnchor),
|
|
|
|
stack.trailingAnchor.constraint(equalTo: trailingAnchor),
|
|
|
|
stack.topAnchor.constraint(equalTo: topAnchor, constant: 5),
|
2020-12-20 19:37:31 +00:00
|
|
|
safeAreaConstraint,
|
|
|
|
stack.bottomAnchor.constraint(lessThanOrEqualTo: bottomAnchor, constant: -8)
|
2020-12-19 20:19:32 +00:00
|
|
|
])
|
|
|
|
|
2020-12-20 19:03:38 +00:00
|
|
|
updateNavigationButtons()
|
|
|
|
|
2020-12-20 18:47:49 +00:00
|
|
|
navigator.navigationOperation
|
2020-12-19 20:19:32 +00:00
|
|
|
.sink { (_) in
|
2020-12-20 19:03:38 +00:00
|
|
|
self.updateNavigationButtons()
|
2020-12-19 20:19:32 +00:00
|
|
|
}
|
|
|
|
.store(in: &cancellables)
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
|
|
|
|
super.traitCollectionDidChange(previousTraitCollection)
|
|
|
|
|
|
|
|
border.backgroundColor = UIColor(white: traitCollection.userInterfaceStyle == .dark ? 0.25 : 0.75, alpha: 1)
|
|
|
|
}
|
|
|
|
|
2020-12-20 19:03:38 +00:00
|
|
|
private func updateNavigationButtons() {
|
|
|
|
backButton.isEnabled = navigator.backStack.count > 0
|
|
|
|
forwardsButton.isEnabled = navigator.forwardStack.count > 0
|
|
|
|
|
|
|
|
if #available(iOS 14.0, *) {
|
2021-09-29 02:13:28 +00:00
|
|
|
let back = navigator.backStack.suffix(5).enumerated().reversed().map { (index, entry) -> UIAction in
|
2020-12-20 19:03:38 +00:00
|
|
|
let backCount = min(5, navigator.backStack.count) - index
|
2021-09-29 02:13:28 +00:00
|
|
|
if #available(iOS 15.0, *),
|
|
|
|
let title = entry.title {
|
2021-09-30 15:05:35 +00:00
|
|
|
return UIAction(title: title, subtitle: BrowserHelper.urlForDisplay(entry.url)) { [unowned self] (_) in
|
2021-09-29 02:13:28 +00:00
|
|
|
self.navigator.back(count: backCount)
|
|
|
|
}
|
|
|
|
} else {
|
2021-09-30 15:05:35 +00:00
|
|
|
return UIAction(title: BrowserHelper.urlForDisplay(entry.url)) { [unowned self] (_) in
|
2021-09-29 02:13:28 +00:00
|
|
|
self.navigator.back(count: backCount)
|
|
|
|
}
|
2020-12-20 19:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
backButton.menu = UIMenu(children: back)
|
|
|
|
|
2021-09-29 02:13:28 +00:00
|
|
|
let forward = navigator.forwardStack.prefix(5).enumerated().map { (index, entry) -> UIAction in
|
2020-12-20 19:03:38 +00:00
|
|
|
let forwardCount = index + 1
|
2021-09-29 02:13:28 +00:00
|
|
|
if #available(iOS 15.0, *),
|
|
|
|
let title = entry.title {
|
2021-09-30 15:05:35 +00:00
|
|
|
return UIAction(title: title, subtitle: BrowserHelper.urlForDisplay(entry.url)) { [unowned self] (_) in
|
2021-09-29 02:13:28 +00:00
|
|
|
self.navigator.forward(count: forwardCount)
|
|
|
|
}
|
|
|
|
} else {
|
2021-09-30 15:05:35 +00:00
|
|
|
return UIAction(title: BrowserHelper.urlForDisplay(entry.url)) { [unowned self] (_) in
|
2021-09-29 02:13:28 +00:00
|
|
|
self.navigator.forward(count: forwardCount)
|
|
|
|
}
|
2020-12-20 19:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
forwardsButton.menu = UIMenu(children: forward)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-20 18:45:22 +00:00
|
|
|
@objc private func tableOfContentsPressed() {
|
|
|
|
showTableOfContents?()
|
|
|
|
}
|
|
|
|
|
2020-12-19 20:19:32 +00:00
|
|
|
@objc private func sharePressed() {
|
|
|
|
showShareSheet?(shareButton)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func prefsPressed() {
|
|
|
|
showPreferences?()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-12-20 19:03:38 +00:00
|
|
|
|
|
|
|
extension ToolbarView: UIContextMenuInteractionDelegate {
|
|
|
|
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
|
2021-09-29 02:13:28 +00:00
|
|
|
// this path is only used on <iOS 14, on >=iOS 14, we don't create a UIContextMenuInteraction
|
2020-12-20 19:03:38 +00:00
|
|
|
if interaction.view == backButton {
|
|
|
|
return UIContextMenuConfiguration(identifier: nil, previewProvider: { nil }) { (_) -> UIMenu? in
|
2021-09-29 02:13:28 +00:00
|
|
|
let children = self.navigator.backStack.suffix(5).enumerated().map { (index, entry) in
|
2021-09-30 15:05:35 +00:00
|
|
|
UIAction(title: BrowserHelper.urlForDisplay(entry.url)) { (_) in
|
2020-12-20 19:03:38 +00:00
|
|
|
self.navigator.back(count: min(5, self.navigator.backStack.count) - index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return UIMenu(title: "", image: nil, identifier: nil, options: [], children: children)
|
|
|
|
}
|
|
|
|
} else if interaction.view == forwardsButton {
|
|
|
|
return UIContextMenuConfiguration(identifier: nil, previewProvider: { nil }) { (_) -> UIMenu? in
|
2021-09-29 02:13:28 +00:00
|
|
|
let children = self.navigator.forwardStack.prefix(5).enumerated().map { (index, entry) -> UIAction in
|
2020-12-20 19:03:38 +00:00
|
|
|
let forwardCount = index + 1
|
2021-09-30 15:05:35 +00:00
|
|
|
return UIAction(title: BrowserHelper.urlForDisplay(entry.url)) { (_) in
|
2020-12-20 19:03:38 +00:00
|
|
|
self.navigator.forward(count: forwardCount)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return UIMenu(title: "", image: nil, identifier: nil, options: [], children: children)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|