Gemini/Gemini-iOS/ToolbarView.swift

126 lines
4.8 KiB
Swift

//
// ToolbarView.swift
// Gemini-iOS
//
// Created by Shadowfacts on 12/19/20.
//
import UIKit
import BrowserCore
import Combine
class ToolbarView: UIView {
let navigator: NavigationManager
var showShareSheet: ((UIView) -> Void)?
var showPreferences: (() -> Void)?
private var border: UIView!
private var backButton: UIButton!
private var forwardsButton: UIButton!
private var reloadButton: UIButton!
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
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
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
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,
shareButton,
prefsButton,
])
stack.axis = .horizontal
stack.distribution = .fillEqually
stack.alignment = .fill
stack.translatesAutoresizingMaskIntoConstraints = false
addSubview(stack)
NSLayoutConstraint.activate([
stack.leadingAnchor.constraint(equalTo: leadingAnchor),
stack.trailingAnchor.constraint(equalTo: trailingAnchor),
stack.topAnchor.constraint(equalTo: topAnchor, constant: 5),
stack.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor),
])
navigator.$currentURL
.sink { (_) in
self.backButton.isEnabled = navigator.backStack.count > 0
self.forwardsButton.isEnabled = navigator.forwardStack.count > 0
}
.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)
}
@objc private func sharePressed() {
showShareSheet?(shareButton)
}
@objc private func prefsPressed() {
showPreferences?()
}
}