Tab Bar: Adds tab bar view/view controller
This commit is contained in:
@@ -21,6 +21,14 @@ class BrowserView: UIView
|
||||
didSet { addSubview(toolbarView!) }
|
||||
}
|
||||
|
||||
var tabBarView: TabBarView? {
|
||||
didSet { addSubview(tabBarView!) }
|
||||
}
|
||||
|
||||
var tabBarViewVisible: Bool = true {
|
||||
didSet { setNeedsLayout() }
|
||||
}
|
||||
|
||||
var autocompleteView: UIView? {
|
||||
didSet {
|
||||
addSubview(autocompleteView!)
|
||||
@@ -129,6 +137,25 @@ class BrowserView: UIView
|
||||
}
|
||||
}
|
||||
|
||||
if let tabBarView = tabBarView {
|
||||
bringSubviewToFront(tabBarView)
|
||||
|
||||
if tabBarViewVisible {
|
||||
tabBarView.isHidden = false
|
||||
|
||||
let tabViewSize = tabBarView.sizeThatFits(bounds.size)
|
||||
|
||||
tabBarView.frame = CGRect(
|
||||
x: 0.0, y: webViewContentInset.top,
|
||||
width: bounds.width, height: tabViewSize.height
|
||||
)
|
||||
|
||||
webViewContentInset.top += tabBarView.frame.height
|
||||
} else {
|
||||
tabBarView.isHidden = true
|
||||
}
|
||||
}
|
||||
|
||||
// Fix web view content insets
|
||||
if let webView = webView {
|
||||
webView.scrollView.layer.masksToBounds = true
|
||||
@@ -154,10 +181,15 @@ class BrowserView: UIView
|
||||
autocompleteView.layer.shadowPath = shadowPath.cgPath
|
||||
|
||||
if let toolbarView = toolbarView, let urlBar = toolbarView.urlBar {
|
||||
var yOffset = toolbarView.frame.maxY + 3.0
|
||||
if let tabBarView = tabBarView, tabBarViewVisible {
|
||||
yOffset += tabBarView.frame.height
|
||||
}
|
||||
|
||||
let urlFrame = self.convert(urlBar.frame, from: urlBar.superview)
|
||||
autocompleteView.frame = CGRect(
|
||||
x: urlFrame.minX,
|
||||
y: toolbarView.frame.maxY + 3.0,
|
||||
y: yOffset,
|
||||
width: urlFrame.width,
|
||||
height: bounds.height / 2.5
|
||||
)
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
// Created by James Magahern on 7/21/20.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import UIKit
|
||||
import UniformTypeIdentifiers
|
||||
|
||||
@@ -18,6 +19,7 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
|
||||
var webView: WKWebView { tab.webView }
|
||||
|
||||
private let tabController = TabController()
|
||||
private let tabBarViewController: TabBarViewController
|
||||
private let toolbarController = ToolbarViewController()
|
||||
private let findOnPageController = FindOnPageViewController()
|
||||
|
||||
@@ -32,6 +34,7 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
|
||||
private var loadingObservation: NSKeyValueObservation?
|
||||
private var backButtonObservation: NSKeyValueObservation?
|
||||
private var forwardButtonObservation: NSKeyValueObservation?
|
||||
private var activeTabObservation: AnyCancellable?
|
||||
|
||||
private var loadError: Error?
|
||||
|
||||
@@ -39,10 +42,12 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
|
||||
|
||||
init() {
|
||||
self.tab = tabController.tabs.first!
|
||||
self.tabBarViewController = TabBarViewController(tabController: tabController)
|
||||
super.init(nibName: nil, bundle: nil)
|
||||
|
||||
addChild(toolbarController)
|
||||
addChild(findOnPageController)
|
||||
addChild(tabBarViewController)
|
||||
|
||||
didChangeTab(tab)
|
||||
}
|
||||
@@ -52,6 +57,7 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
|
||||
override func loadView() {
|
||||
browserView.toolbarView = toolbarController.toolbarView
|
||||
browserView.findOnPageView = findOnPageController.findOnPageView
|
||||
browserView.tabBarView = tabBarViewController.tabBarView
|
||||
|
||||
// Refresh button
|
||||
toolbarController.urlBar.refreshButton.addAction(UIAction(handler: { [unowned self] action in
|
||||
@@ -215,6 +221,16 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
|
||||
browserView.setFindOnPageVisible(false, animated: true)
|
||||
}), for: .touchUpInside)
|
||||
|
||||
// Tab controller
|
||||
activeTabObservation = tabController.$activeTabIndex
|
||||
.receive(on: RunLoop.main)
|
||||
.sink(receiveValue: { [unowned self] (activeTab: Int) in
|
||||
let tab = tabController.tabs[activeTab]
|
||||
if self.tab != tab {
|
||||
self.tab = tab
|
||||
}
|
||||
})
|
||||
|
||||
self.view = browserView
|
||||
}
|
||||
|
||||
@@ -236,9 +252,19 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
|
||||
} else {
|
||||
toolbarController.urlBar.textField.text = ""
|
||||
}
|
||||
|
||||
// Figure out which tab this corresponds to
|
||||
let tab = tabController.tabs.first { $0.webView == webView }
|
||||
if let tab = tab, let tabIndex = tabController.tabs.firstIndex(of: tab) {
|
||||
tabBarViewController.tabBarView.reloadTab(atIndex: tabIndex)
|
||||
}
|
||||
}
|
||||
|
||||
private func didChangeTab(_ tab: Tab) {
|
||||
if let activeIndex = tabController.tabs.firstIndex(of: tab) {
|
||||
tabController.activeTabIndex = activeIndex
|
||||
}
|
||||
|
||||
tab.delegate = self
|
||||
|
||||
let webView = tab.webView
|
||||
@@ -253,6 +279,9 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
|
||||
// Autocomplete view
|
||||
browserView.autocompleteView = autocompleteViewController.view
|
||||
|
||||
// Show tab bar view?
|
||||
browserView.tabBarViewVisible = tabController.tabs.count > 1
|
||||
|
||||
// Load progress
|
||||
updateLoadProgress(forWebView: webView)
|
||||
loadingObservation = webView.observe(\.estimatedProgress) { [unowned self] (webView, observedChange) in
|
||||
|
||||
Reference in New Issue
Block a user