2020-12-20 00:40:16 +00:00
|
|
|
//
|
|
|
|
// NavigationBarView.swift
|
|
|
|
// Gemini-iOS
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 12/19/20.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2020-12-20 03:43:43 +00:00
|
|
|
import BrowserCore
|
|
|
|
import Combine
|
2020-12-20 00:40:16 +00:00
|
|
|
|
|
|
|
class NavigationBarView: UIView {
|
|
|
|
|
2020-12-20 03:43:43 +00:00
|
|
|
let navigator: NavigationManager
|
|
|
|
|
|
|
|
private var border: UIView!
|
2020-12-20 20:24:36 +00:00
|
|
|
private(set) var textField: UITextField!
|
2020-12-20 03:43:43 +00:00
|
|
|
|
|
|
|
private var cancellables = [AnyCancellable]()
|
|
|
|
|
|
|
|
init(navigator: NavigationManager) {
|
|
|
|
self.navigator = navigator
|
|
|
|
|
|
|
|
super.init(frame: .zero)
|
|
|
|
|
|
|
|
backgroundColor = .systemBackground
|
|
|
|
|
|
|
|
border = UIView()
|
|
|
|
border.backgroundColor = UIColor(white: traitCollection.userInterfaceStyle == .dark ? 0.25 : 0.75, alpha: 1)
|
|
|
|
border.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
addSubview(border)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
border.leadingAnchor.constraint(equalTo: leadingAnchor),
|
|
|
|
border.trailingAnchor.constraint(equalTo: trailingAnchor),
|
|
|
|
border.bottomAnchor.constraint(equalTo: bottomAnchor),
|
|
|
|
border.heightAnchor.constraint(equalToConstant: 1),
|
|
|
|
])
|
|
|
|
|
|
|
|
textField = UITextField()
|
|
|
|
textField.text = navigator.displayURL
|
|
|
|
textField.borderStyle = .roundedRect
|
|
|
|
textField.keyboardType = .URL
|
2020-12-20 20:24:36 +00:00
|
|
|
textField.returnKeyType = .go
|
2020-12-20 03:43:43 +00:00
|
|
|
textField.autocapitalizationType = .none
|
|
|
|
textField.autocorrectionType = .no
|
2020-12-20 20:24:36 +00:00
|
|
|
textField.addTarget(self, action: #selector(commitURL), for: .primaryActionTriggered)
|
2020-12-20 03:43:43 +00:00
|
|
|
textField.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
addSubview(textField)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
textField.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8),
|
|
|
|
textField.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -8),
|
|
|
|
textField.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor),
|
|
|
|
textField.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -9),
|
|
|
|
])
|
|
|
|
|
|
|
|
navigator.$currentURL
|
|
|
|
.sink { (newURL) in
|
|
|
|
// can't use navigator.displayURL because the publisher fires before the underlying value is updated, so the displayURL getter returns the old value
|
|
|
|
var components = URLComponents(url: newURL, resolvingAgainstBaseURL: false)!
|
|
|
|
if components.port == 1965 {
|
|
|
|
components.port = nil
|
|
|
|
}
|
|
|
|
self.textField.text = components.string!
|
|
|
|
}
|
|
|
|
.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 commitURL() {
|
2020-12-20 20:24:36 +00:00
|
|
|
textField.resignFirstResponder()
|
2021-06-16 03:33:29 +00:00
|
|
|
if let text = textField.text,
|
|
|
|
!text.isEmpty,
|
|
|
|
var components = URLComponents(string: text) {
|
2020-12-20 20:32:36 +00:00
|
|
|
if components.scheme == nil {
|
|
|
|
components.scheme = "gemini"
|
|
|
|
}
|
|
|
|
navigator.changeURL(components.url!)
|
2020-12-20 03:43:43 +00:00
|
|
|
} else {
|
|
|
|
textField.text = navigator.displayURL
|
|
|
|
}
|
2020-12-20 00:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|