Skip to content

Commit

Permalink
Fix brave#3735: Add search button doesn't resize correctly when movin…
Browse files Browse the repository at this point in the history
…g between text fields
  • Loading branch information
soner-yuksel authored and KacperWybranski committed Sep 4, 2021
1 parent 7730910 commit 5950a6b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
14 changes: 8 additions & 6 deletions Client/Extensions/UIViewExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,18 @@ extension UIView {
*/
static func findSubViewWithFirstResponder(_ view: UIView) -> UIView? {
let subviews = view.subviews
if subviews.isEmpty {

guard !subviews.isEmpty else {
return nil
}

guard let firstSubview = subviews.first else { return nil }
if firstSubview.isFirstResponder {
return firstSubview
} else {
return findSubViewWithFirstResponder(firstSubview)
if let firstResponderSubview = subviews.first(where: { $0.isFirstResponder }) {
return firstResponderSubview
}

guard let firstSubview = subviews.first( where: { !($0 is UIRefreshControl) }) else { return nil }

return findSubViewWithFirstResponder(firstSubview)
}

/// Creates empty view with specified height or width parameter.
Expand Down
8 changes: 5 additions & 3 deletions Client/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,16 @@ class BrowserViewController: UIViewController {

/// Custom Search Engine
var openSearchEngine: OpenSearchReference?
var openSearchTextFieldInputAssistantBarButtonGroup = [UIBarButtonItemGroup]()

lazy var customSearchEngineButton = OpenSearchEngineButton(hidesWhenDisabled: false).then {
$0.addTarget(self, action: #selector(addCustomSearchEngineForFocusedElement), for: .touchUpInside)
$0.accessibilityIdentifier = "BrowserViewController.customSearchEngineButton"
$0.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
$0.setContentHuggingPriority(.defaultHigh, for: .horizontal)
}
var customSearchBarButton: UIBarButtonItem?


var customSearchBarButtonItemGroup: UIBarButtonItemGroup?

// popover rotation handling
var displayedPopoverController: UIViewController?
var updateDisplayedPopoverProperties: (() -> Void)?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,19 @@ extension BrowserViewController {
*/
return supportsAutoAdd
}

if openSearchTextFieldInputAssistantBarButtonGroup.isEmpty {
openSearchTextFieldInputAssistantBarButtonGroup = webContentView.inputAssistantItem.trailingBarButtonGroups
}


if UIDevice.isIpad {
webContentView.inputAssistantItem.trailingBarButtonGroups = openSearchTextFieldInputAssistantBarButtonGroup +
[UIBarButtonItemGroup(barButtonItems: [UIBarButtonItem(customView: customSearchEngineButton)], representativeItem: nil)]
if customSearchBarButtonItemGroup == nil {
customSearchBarButtonItemGroup = UIBarButtonItemGroup(
barButtonItems: [UIBarButtonItem(customView: customSearchEngineButton)], representativeItem: nil)
} else {
webContentView.inputAssistantItem.trailingBarButtonGroups.removeAll(
where: { $0.barButtonItems.contains(where: { $0.customView != nil })})
}

if let barButtonItemGroup = customSearchBarButtonItemGroup {
webContentView.inputAssistantItem.trailingBarButtonGroups.append(barButtonItemGroup)
}
} else {
let argumentNextItem: [Any] = ["_n", "extI", "tem"]
let argumentView: [Any] = ["v", "ie", "w"]
Expand Down Expand Up @@ -241,15 +246,12 @@ extension BrowserViewController: KeyboardHelperDelegate {
func keyboardHelper(_ keyboardHelper: KeyboardHelper, keyboardWillHideWithState state: KeyboardState) {
keyboardState = nil
updateViewConstraints()
// If the searchEngineButton exists remove it form the keyboard
if let buttonGroup = customSearchBarButton?.buttonGroup {
buttonGroup.barButtonItems = buttonGroup.barButtonItems.filter { $0 != customSearchBarButton }
customSearchBarButton = nil
}

if self.customSearchEngineButton.superview != nil {
self.customSearchEngineButton.removeFromSuperview()
openSearchTextFieldInputAssistantBarButtonGroup.removeAll()
customSearchBarButtonItemGroup?.barButtonItems.removeAll()
customSearchBarButtonItemGroup = nil

if customSearchEngineButton.superview != nil {
customSearchEngineButton.removeFromSuperview()
}

UIViewPropertyAnimator(duration: state.animationDuration, curve: state.animationCurve) {
Expand Down

0 comments on commit 5950a6b

Please sign in to comment.