Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Fix #7642: Move reload long press action to UIContext menu action. #7841

Merged
merged 4 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ extension BrowserViewController {
self.topToolbar.locationView.rewardsButton.iconState = Preferences.Rewards.rewardsToggledOnce.value ? (rewards.isEnabled || rewards.isCreatingWallet ? .enabled : .disabled) : .initial
}

func showRewardsDebugSettings() {
iccub marked this conversation as resolved.
Show resolved Hide resolved
if AppConstants.buildChannel.isPublic { return }

let settings = RewardsDebugSettingsViewController(rewards: rewards)
let container = UINavigationController(rootViewController: settings)
present(container, animated: true)
}

func showBraveRewardsPanel() {
if !Preferences.FullScreenCallout.rewardsCalloutCompleted.value,
Preferences.Onboarding.isNewRetentionUser.value == true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,6 @@ extension BrowserViewController: TopToolbarDelegate {
}
}

func topToolbarDidLongPressReaderMode(_ topToolbar: TopToolbarView) -> Bool {
// Maybe we want to add something here down the road
return false
}

func topToolbarDidPressPlaylistButton(_ urlBar: TopToolbarView) {
guard let tab = tabManager.selectedTab, let playlistItem = tab.playlistItem else { return }
let state = urlBar.locationView.playlistButton.buttonState
Expand Down Expand Up @@ -268,10 +263,6 @@ extension BrowserViewController: TopToolbarDelegate {
}
}

func topToolbarDidLongPressLocation(_ topToolbar: TopToolbarView) {
// The actions are carried to menu actions for Top ToolBar Location View
}

func topToolbarDidPressScrollToTop(_ topToolbar: TopToolbarView) {
if let selectedTab = tabManager.selectedTab, favoritesController == nil {
// Only scroll to top if we are not showing the home view controller
Expand Down Expand Up @@ -532,10 +523,6 @@ extension BrowserViewController: TopToolbarDelegate {
showBraveRewardsPanel()
}

func topToolbarDidLongPressBraveRewardsButton(_ topToolbar: TopToolbarView) {
showRewardsDebugSettings()
}

func topToolbarDidTapMenuButton(_ topToolbar: TopToolbarView) {
tabToolbarDidPressMenu(topToolbar)
}
Expand Down Expand Up @@ -926,10 +913,27 @@ extension BrowserViewController: ToolbarDelegate {

extension BrowserViewController: UIContextMenuInteractionDelegate {
public func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { [unowned self] _ in
var actionMenu: [UIMenu] = []

let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { [unowned self] _ in
var actionMenus: [UIMenu?] = []
var pasteMenuChildren: [UIAction] = []

let tab = tabManager.selectedTab
var reloadMenu: UIMenu?

if let url = tab?.url, url.isWebPage() {
let reloadTitle = tab?.isDesktopSite == true ? Strings.appMenuViewMobileSiteTitleString : Strings.appMenuViewDesktopSiteTitleString
let reloadIcon = tab?.isDesktopSite == true ? "leo.smartphone" : "leo.monitor"
let reloadAction = UIAction(
title: reloadTitle,
image: UIImage(braveSystemNamed: reloadIcon),
handler: UIAction.deferredActionHandler { [weak tab] _ in
tab?.switchUserAgent()
})

reloadMenu = UIMenu(options: .displayInline, children: [reloadAction])
}

let pasteGoAction = UIAction(
identifier: .pasteAndGo,
handler: UIAction.deferredActionHandler { _ in
Expand All @@ -942,7 +946,6 @@ extension BrowserViewController: UIContextMenuInteractionDelegate {
identifier: .paste,
handler: UIAction.deferredActionHandler { _ in
if let pasteboardContents = UIPasteboard.general.string {
// Enter overlay mode and make the search controller appear.
self.topToolbar.enterOverlayMode(pasteboardContents, pasted: true, search: true)
}
})
Expand All @@ -953,6 +956,8 @@ extension BrowserViewController: UIContextMenuInteractionDelegate {
pasteMenuChildren.reverse()
}

var copyMenu: UIMenu?
iccub marked this conversation as resolved.
Show resolved Hide resolved

let copyAction = UIAction(
title: Strings.copyAddressTitle,
image: UIImage(systemName: "doc.on.doc"),
Expand All @@ -962,23 +967,20 @@ extension BrowserViewController: UIContextMenuInteractionDelegate {
}
})

let copyMenu = UIMenu(title: "", options: .displayInline, children: [copyAction])
copyMenu = UIMenu(options: .displayInline, children: [copyAction])

if UIPasteboard.general.hasStrings || UIPasteboard.general.hasURLs {
let pasteMenu = UIMenu(title: "", options: .displayInline, children: pasteMenuChildren)

actionMenu = [pasteMenu, copyMenu]

if #unavailable(iOS 16.0), isUsingBottomBar {
actionMenu.reverse()
}
let pasteMenu = UIMenu(options: .displayInline, children: pasteMenuChildren)
actionMenus.append(contentsOf: [pasteMenu, copyMenu])
} else {
actionMenu = [copyMenu]
actionMenus.append(copyMenu)
}

return UIMenu(title: "", identifier: nil, children: actionMenu)
}

actionMenus.append(reloadMenu)

return UIMenu(children: actionMenus.compactMap { $0 })
}

if #available(iOS 16.0, *) {
configuration.preferredMenuElementOrder = .priority
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,17 @@ import DesignSystem

protocol TabLocationViewDelegate {
func tabLocationViewDidTapLocation(_ tabLocationView: TabLocationView)
func tabLocationViewDidLongPressLocation(_ tabLocationView: TabLocationView)
func tabLocationViewDidTapReaderMode(_ tabLocationView: TabLocationView)
func tabLocationViewDidBeginDragInteraction(_ tabLocationView: TabLocationView)
func tabLocationViewDidTapPlaylist(_ tabLocationView: TabLocationView)
func tabLocationViewDidTapPlaylistMenuAction(_ tabLocationView: TabLocationView, action: PlaylistURLBarButton.MenuAction)
func tabLocationViewDidTapLockImageView(_ tabLocationView: TabLocationView)
func tabLocationViewDidTapReload(_ tabLocationView: TabLocationView)
func tabLocationViewDidLongPressReload(_ tabLocationView: TabLocationView, from button: UIButton)
func tabLocationViewDidTapStop(_ tabLocationView: TabLocationView)
func tabLocationViewDidTapVoiceSearch(_ tabLocationView: TabLocationView)
func tabLocationViewDidTapShieldsButton(_ urlBar: TabLocationView)
func tabLocationViewDidTapRewardsButton(_ urlBar: TabLocationView)
func tabLocationViewDidLongPressRewardsButton(_ urlBar: TabLocationView)
func tabLocationViewDidTapWalletButton(_ urlBar: TabLocationView)

/// - returns: whether the long-press was handled by the delegate; i.e. return `false` when the conditions for even starting handling long-press were not satisfied
@discardableResult func tabLocationViewDidLongPressReaderMode(_ tabLocationView: TabLocationView) -> Bool
}

private struct TabLocationViewUX {
Expand All @@ -41,8 +35,6 @@ private struct TabLocationViewUX {

class TabLocationView: UIView {
var delegate: TabLocationViewDelegate?
var longPressRecognizer: UILongPressGestureRecognizer!
var tapRecognizer: UITapGestureRecognizer!
var contentView: UIStackView!
private var tabObservers: TabObservers!
private var privateModeCancellable: AnyCancellable?
Expand Down Expand Up @@ -169,13 +161,11 @@ class TabLocationView: UIView {
private(set) lazy var readerModeButton: ReaderModeButton = {
let readerModeButton = ReaderModeButton(frame: .zero)
readerModeButton.addTarget(self, action: #selector(didTapReaderModeButton), for: .touchUpInside)
readerModeButton.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(didLongPressReaderModeButton)))
readerModeButton.isAccessibilityElement = true
readerModeButton.isHidden = true
readerModeButton.imageView?.contentMode = .scaleAspectFit
readerModeButton.accessibilityLabel = Strings.tabToolbarReaderViewButtonAccessibilityLabel
readerModeButton.accessibilityIdentifier = "TabLocationView.readerModeButton"
readerModeButton.accessibilityCustomActions = [UIAccessibilityCustomAction(name: Strings.tabToolbarReaderViewButtonTitle, target: self, selector: #selector(didLongPressReaderModeCustomAction))]
readerModeButton.unselectedTintColor = .braveLabel
readerModeButton.selectedTintColor = .braveBlurpleTint
return readerModeButton
Expand All @@ -202,8 +192,6 @@ class TabLocationView: UIView {
$0.accessibilityLabel = Strings.tabToolbarReloadButtonAccessibilityLabel
$0.setImage(UIImage(braveSystemNamed: "leo.browser.refresh", compatibleWith: nil), for: .normal)
$0.tintColor = .braveLabel
let longPressGestureStopReloadButton = UILongPressGestureRecognizer(target: self, action: #selector(didLongPressStopReloadButton(_:)))
$0.addGestureRecognizer(longPressGestureStopReloadButton)
$0.addTarget(self, action: #selector(didTapStopReloadButton), for: .touchUpInside)
}

Expand Down Expand Up @@ -231,8 +219,6 @@ class TabLocationView: UIView {
lazy var rewardsButton: RewardsButton = {
let button = RewardsButton()
button.addTarget(self, action: #selector(didTapBraveRewardsButton), for: .touchUpInside)
let longPressGestureRewardsButton = UILongPressGestureRecognizer(target: self, action: #selector(didLongPressRewardsButton(_:)))
button.addGestureRecognizer(longPressGestureRewardsButton)
return button
}()

Expand Down Expand Up @@ -260,14 +246,7 @@ class TabLocationView: UIView {

tabObservers = registerFor(.didChangeContentBlocking, .didGainFocus, queue: .main)

longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didLongPressLocationBar))
longPressRecognizer.delegate = self

tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTapLocationBar))
tapRecognizer.delegate = self

addGestureRecognizer(longPressRecognizer)
addGestureRecognizer(tapRecognizer)
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTapLocationBar)))

var optionSubviews: [UIView] = [readerModeButton, walletButton, playlistButton]
if isVoiceSearchAvailable {
Expand Down Expand Up @@ -387,36 +366,6 @@ class TabLocationView: UIView {
reloadButton.isHidden = url == nil
voiceSearchButton.isHidden = (url != nil) || !isVoiceSearchAvailable
}

// MARK: Long Press Actions

@objc func didLongPressReaderModeButton(_ recognizer: UILongPressGestureRecognizer) {
if recognizer.state == .began {
delegate?.tabLocationViewDidLongPressReaderMode(self)
}
}

@objc func didLongPressLocationBar(_ recognizer: UITapGestureRecognizer) {
if recognizer.state == .began {
delegate?.tabLocationViewDidLongPressLocation(self)
}
}

@objc func didLongPressStopReloadButton(_ recognizer: UILongPressGestureRecognizer) {
if recognizer.state == .began && !loading {
delegate?.tabLocationViewDidLongPressReload(self, from: reloadButton)
}
}

@objc func didLongPressReaderModeCustomAction() -> Bool {
return delegate?.tabLocationViewDidLongPressReaderMode(self) ?? false
}

@objc func didLongPressRewardsButton(_ gesture: UILongPressGestureRecognizer) {
if gesture.state == .began {
delegate?.tabLocationViewDidLongPressRewardsButton(self)
}
}

// MARK: Tap Actions

Expand Down Expand Up @@ -463,27 +412,6 @@ class TabLocationView: UIView {
}
}

// MARK: - UIGestureRecognizerDelegate

extension TabLocationView: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// When long pressing a button make sure the textfield's long press gesture is not triggered
return !(otherGestureRecognizer.view is UIButton)
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// If the longPressRecognizer is active, fail the tap recognizer to avoid conflicts.
return gestureRecognizer == longPressRecognizer && otherGestureRecognizer == tapRecognizer
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if gestureRecognizer == tapRecognizer && touch.view == tabOptionsStackView {
return false
}
return true
}
}

// MARK: - UIDragInteractionDelegate

extension TabLocationView: UIDragInteractionDelegate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ import BraveCore
protocol TopToolbarDelegate: AnyObject {
func topToolbarDidPressTabs(_ topToolbar: TopToolbarView)
func topToolbarDidPressReaderMode(_ topToolbar: TopToolbarView)
// Returns whether the long-press was handled by the delegate; i.e. return `false` when the conditions for even starting handling long-press were not satisfied
func topToolbarDidLongPressReaderMode(_ topToolbar: TopToolbarView) -> Bool
func topToolbarDidPressPlaylistButton(_ urlBar: TopToolbarView)
func topToolbarDidPressPlaylistMenuAction(_ urlBar: TopToolbarView, action: PlaylistURLBarButton.MenuAction)
func topToolbarDidEnterOverlayMode(_ topToolbar: TopToolbarView)
func topToolbarDidLeaveOverlayMode(_ topToolbar: TopToolbarView)
func topToolbarDidLongPressLocation(_ topToolbar: TopToolbarView)
func topToolbarDidPressScrollToTop(_ topToolbar: TopToolbarView)
func topToolbar(_ topToolbar: TopToolbarView, didEnterText text: String)
func topToolbar(_ topToolbar: TopToolbarView, didSubmitText text: String)
Expand All @@ -31,9 +28,7 @@ protocol TopToolbarDelegate: AnyObject {
func topToolbarDidTapBookmarkButton(_ topToolbar: TopToolbarView)
func topToolbarDidTapBraveShieldsButton(_ topToolbar: TopToolbarView)
func topToolbarDidTapBraveRewardsButton(_ topToolbar: TopToolbarView)
func topToolbarDidLongPressBraveRewardsButton(_ topToolbar: TopToolbarView)
func topToolbarDidTapMenuButton(_ topToolbar: TopToolbarView)
func topToolbarDidLongPressReloadButton(_ urlBar: TopToolbarView, from button: UIButton)
func topToolbarDidPressVoiceSearchButton(_ urlBar: TopToolbarView)
func topToolbarDidPressStop(_ urlBar: TopToolbarView)
func topToolbarDidPressReload(_ urlBar: TopToolbarView)
Expand Down Expand Up @@ -725,14 +720,6 @@ extension TopToolbarView: TabLocationViewDelegate {
delegate?.topToolbarDidTapBraveRewardsButton(self)
}

func tabLocationViewDidLongPressRewardsButton(_ urlBar: TabLocationView) {
delegate?.topToolbarDidLongPressBraveRewardsButton(self)
}

func tabLocationViewDidLongPressReaderMode(_ tabLocationView: TabLocationView) -> Bool {
return delegate?.topToolbarDidLongPressReaderMode(self) ?? false
}

func tabLocationViewDidTapLocation(_ tabLocationView: TabLocationView) {
guard let (locationText, isSearchQuery) = delegate?.topToolbarDisplayTextForURL(locationView.url as URL?) else { return }

Expand All @@ -745,10 +732,6 @@ extension TopToolbarView: TabLocationViewDelegate {
enterOverlayMode(overlayText, pasted: false, search: isSearchQuery)
}

func tabLocationViewDidLongPressLocation(_ tabLocationView: TabLocationView) {
delegate?.topToolbarDidLongPressLocation(self)
}

func tabLocationViewDidTapLockImageView(_ tabLocationView: TabLocationView) {
delegate?.topToolbarDidPressLockImageView(self)
}
Expand All @@ -760,10 +743,6 @@ extension TopToolbarView: TabLocationViewDelegate {
func tabLocationViewDidTapStop(_ tabLocationView: TabLocationView) {
delegate?.topToolbarDidPressStop(self)
}

func tabLocationViewDidLongPressReload(_ tabLocationView: TabLocationView, from button: UIButton) {
delegate?.topToolbarDidLongPressReloadButton(self, from: button)
}

func tabLocationViewDidTapVoiceSearch(_ tabLocationView: TabLocationView) {
delegate?.topToolbarDidPressVoiceSearchButton(self)
Expand Down