Implemented find on page UI

This commit is contained in:
James Magahern
2020-09-30 18:06:47 -07:00
parent 1d27674d7d
commit 3769f5bbbf
12 changed files with 372 additions and 9 deletions

View File

@@ -13,6 +13,10 @@ class BrowserView: UIView
{
let titlebarView = TitlebarView()
var findOnPageView: FindOnPageView? {
didSet { addSubview(findOnPageView!) }
}
var toolbarView: ToolbarView? {
didSet { addSubview(toolbarView!) }
}
@@ -38,6 +42,18 @@ class BrowserView: UIView
}
}
var findOnPageVisible: Bool = false {
didSet { layoutSubviews() }
}
func setFindOnPageVisible(_ visible: Bool, animated: Bool) {
if animated {
UIView.animate(withDuration: 0.33, animations: { self.findOnPageVisible = visible })
} else {
findOnPageVisible = visible
}
}
var keyboardWillShowObserver: AnyCancellable?
var keyboardWillHideObserver: AnyCancellable?
var keyboardLayoutOffset: CGFloat = 0 { didSet { setNeedsLayout() } }
@@ -101,6 +117,11 @@ class BrowserView: UIView
toolbarView.bounds = CGRect(origin: .zero, size: toolbarSize)
toolbarView.center = CGPoint(x: bounds.center.x, y: bounds.maxY - (toolbarView.bounds.height / 2) - bottomOffset)
webViewContentInset.bottom += toolbarView.frame.height
if findOnPageVisible {
// Hide off the bottom
toolbarView.center = CGPoint(x: toolbarView.center.x, y: toolbarView.center.y + toolbarView.frame.height)
}
} else {
// Regular: toolbar is at the top
toolbarView.frame = CGRect(origin: CGPoint(x: 0.0, y: titlebarView.frame.maxY), size: toolbarSize)
@@ -139,5 +160,25 @@ class BrowserView: UIView
}
}
}
// Find on page view
if let findOnPageView = findOnPageView {
var bottomOffset: CGFloat = 0.0
var findOnPageSize = CGSize(width: bounds.width, height: 54.0)
if keyboardLayoutOffset == 0 {
findOnPageSize.height += safeAreaInsets.bottom
} else if findOnPageView.textField.isFirstResponder {
bottomOffset = keyboardLayoutOffset
}
findOnPageView.bounds = CGRect(origin: .zero, size: findOnPageSize)
findOnPageView.center = CGPoint(x: bounds.center.x, y: bounds.maxY - (findOnPageView.bounds.height / 2) - bottomOffset)
bringSubviewToFront(findOnPageView)
if !findOnPageVisible {
// Hide off the bottom
findOnPageView.center = CGPoint(x: findOnPageView.center.x, y: findOnPageView.center.y + findOnPageView.frame.height)
}
}
}
}

View File

@@ -19,6 +19,7 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
private let tabController = TabController()
private let toolbarController = ToolbarViewController()
private let findOnPageController = FindOnPageViewController()
private let autocompleteViewController = AutocompleteViewController()
private let redirectRules = PersonalRedirectRules()
@@ -41,6 +42,8 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
super.init(nibName: nil, bundle: nil)
addChild(toolbarController)
addChild(findOnPageController)
didChangeTab(tab)
}
@@ -48,6 +51,7 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
override func loadView() {
browserView.toolbarView = toolbarController.toolbarView
browserView.findOnPageView = findOnPageController.findOnPageView
// Refresh button
toolbarController.urlBar.refreshButton.addAction(UIAction(handler: { [unowned self] action in
@@ -198,9 +202,19 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
label.text = numberFormatter.string(for: tab.webView._viewScale)
}), for: .touchUpInside)
documentControls.findOnPageControlView.addAction(UIAction(handler: { [unowned self] _ in
documentControls.dismiss(animated: true, completion: nil)
browserView.setFindOnPageVisible(true, animated: true)
}), for: .touchUpInside)
present(documentControls, animated: true, completion: nil)
}), for: .touchUpInside)
// Find on page dismiss
findOnPageController.findOnPageView.doneButton.addAction(UIAction(handler: { [unowned self] _ in
browserView.setFindOnPageVisible(false, animated: true)
}), for: .touchUpInside)
self.view = browserView
}
@@ -234,6 +248,7 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
// Change webView
browserView.webView = webView
findOnPageController.webView = webView
// Autocomplete view
browserView.autocompleteView = autocompleteViewController.view
@@ -552,4 +567,9 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
}
}
}
func findOnPage(_ sender: Any?) {
browserView.setFindOnPageVisible(true, animated: true)
findOnPageController.findOnPageView.textField.becomeFirstResponder()
}
}