From 3e71974fbf3a9c1417985a945be43435ed0d6208 Mon Sep 17 00:00:00 2001 From: Kyle Hickinson Date: Wed, 1 Nov 2023 09:29:51 -0400 Subject: [PATCH] Fix brave/brave-ios#8332: Allow proper caret selection on url bar in overlay mode (brave/brave-ios#8347) This cleans up the duplicate drag interactions as well as fixes the bug where you could no longer move the caret inside of the url bar text field. It also restores a missed delegate method and removes the drop delegate on the active text field so as to not crash --- .../Toolbars/UrlBar/TabLocationView.swift | 26 ------------------- .../Toolbars/UrlBar/TopToolbarView.swift | 23 +++++++++++----- 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/Sources/Brave/Frontend/Browser/Toolbars/UrlBar/TabLocationView.swift b/Sources/Brave/Frontend/Browser/Toolbars/UrlBar/TabLocationView.swift index 37a9d18ecd11..9e6675498edd 100644 --- a/Sources/Brave/Frontend/Browser/Toolbars/UrlBar/TabLocationView.swift +++ b/Sources/Brave/Frontend/Browser/Toolbars/UrlBar/TabLocationView.swift @@ -273,12 +273,6 @@ class TabLocationView: UIView { contentView.snp.makeConstraints { make in make.leading.trailing.top.bottom.equalTo(self) } - - // Setup UIDragInteraction to handle dragging the location - // bar for dropping its URL into other apps. - let dragInteraction = UIDragInteraction(delegate: self) - dragInteraction.allowsSimultaneousRecognitionDuringLift = true - self.addInteraction(dragInteraction) privateModeCancellable = privateBrowsingManager.$isPrivateBrowsing .removeDuplicates() @@ -414,26 +408,6 @@ class TabLocationView: UIView { } } -// MARK: - UIDragInteractionDelegate - -extension TabLocationView: UIDragInteractionDelegate { - func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] { - // Ensure we actually have a URL in the location bar and that the URL is not local. - guard let url = self.url, !InternalURL.isValid(url: url), let itemProvider = NSItemProvider(contentsOf: url), - !reloadButton.isHighlighted - else { - return [] - } - - let dragItem = UIDragItem(itemProvider: itemProvider) - return [dragItem] - } - - func dragInteraction(_ interaction: UIDragInteraction, sessionWillBegin session: UIDragSession) { - delegate?.tabLocationViewDidBeginDragInteraction(self) - } -} - // MARK: - TabEventHandler extension TabLocationView: TabEventHandler { diff --git a/Sources/Brave/Frontend/Browser/Toolbars/UrlBar/TopToolbarView.swift b/Sources/Brave/Frontend/Browser/Toolbars/UrlBar/TopToolbarView.swift index b6c2fbf8258d..08fb51bdfc40 100644 --- a/Sources/Brave/Frontend/Browser/Toolbars/UrlBar/TopToolbarView.swift +++ b/Sources/Brave/Frontend/Browser/Toolbars/UrlBar/TopToolbarView.swift @@ -307,6 +307,10 @@ class TopToolbarView: UIView, ToolbarProtocol { swipeGestureRecognizer.isEnabled = false locationView.addGestureRecognizer(swipeGestureRecognizer) + let dragInteraction = UIDragInteraction(delegate: self) + dragInteraction.allowsSimultaneousRecognitionDuringLift = true + locationView.addInteraction(dragInteraction) + self.displayTabTraySwipeGestureRecognizer = swipeGestureRecognizer qrCodeButton.addTarget(self, action: #selector(topToolbarDidPressQrCodeButton), for: .touchUpInside) @@ -412,11 +416,11 @@ class TopToolbarView: UIView, ToolbarProtocol { $0.attributedPlaceholder = self.locationView.makePlaceholder(colors: .standard) $0.clearButtonMode = .whileEditing $0.rightViewMode = .never + if let dropInteraction = $0.textDropInteraction { + $0.removeInteraction(dropInteraction) + } } - let dragInteraction = UIDragInteraction(delegate: self) - locationTextField.addInteraction(dragInteraction) - if RecentSearchQRCodeScannerController.hasCameraSupport { locationBarOptionsStackView.addArrangedSubview(qrCodeButton) } @@ -807,12 +811,19 @@ extension TopToolbarView: AutocompleteTextFieldDelegate { extension TopToolbarView: UIDragInteractionDelegate { func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] { - guard let text = locationTextField?.text else { + // Ensure we actually have a URL in the location bar and that the URL is not local. + guard let url = self.locationView.url, !InternalURL.isValid(url: url), let itemProvider = NSItemProvider(contentsOf: url), + !locationView.reloadButton.isHighlighted, !inOverlayMode + else { return [] } - - let dragItem = UIDragItem(itemProvider: NSItemProvider(object: text as NSString)) + + let dragItem = UIDragItem(itemProvider: itemProvider) dragItem.localObject = locationTextField return [dragItem] } + + func dragInteraction(_ interaction: UIDragInteraction, sessionWillBegin session: UIDragSession) { + delegate?.topToolbarDidBeginDragInteraction(self) + } }