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

Commit

Permalink
Single context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
iccub committed Aug 9, 2023
1 parent f5045a2 commit d6a9bde
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 56 deletions.
2 changes: 0 additions & 2 deletions Sources/Brave/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ public class BrowserViewController: UIViewController {

let toolBarInteraction = UIContextMenuInteraction(delegate: self)
topToolbar.locationView.addInteraction(toolBarInteraction)
let reloadInteraction = UIContextMenuInteraction(delegate: self)
topToolbar.locationView.reloadButton.addInteraction(reloadInteraction)
return topToolbar
}()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -914,48 +914,25 @@ extension BrowserViewController: ToolbarDelegate {
extension BrowserViewController: UIContextMenuInteractionDelegate {
public func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {

var configuration: UIContextMenuConfiguration?

switch interaction.view?.accessibilityIdentifier {
case topToolbar.locationView.reloadButtonAccessibilityIdentifier:
configuration = reloadButtonMenuConfiguration
default:
configuration = locationViewMenuConfiguration
}

if #available(iOS 16.0, *) {
configuration?.preferredMenuElementOrder = .priority
}

return configuration
}

private var reloadButtonMenuConfiguration: UIContextMenuConfiguration {
let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { [unowned self] _ in
let tab = tabManager.selectedTab

let title = tab?.isDesktopSite == true ? Strings.appMenuViewMobileSiteTitleString : Strings.appMenuViewDesktopSiteTitleString
let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { [unowned self] _ in
var actionMenus: [UIMenu?] = []
var pasteMenuChildren: [UIAction] = []

let icon = tab?.isDesktopSite == true ? "leo.smartphone" : "leo.monitor"
let tab = tabManager.selectedTab
var reloadMenu: UIMenu?

let copyAction = UIAction(
title: title,
image: UIImage(braveSystemNamed: icon),
handler: UIAction.deferredActionHandler { [weak tab] _ in
tab?.switchUserAgent()
})

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

return configuration
}

private var locationViewMenuConfiguration: UIContextMenuConfiguration {
let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { [unowned self] _ in
var actionMenu: [UIMenu] = []
var pasteMenuChildren: [UIAction] = []
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,
Expand All @@ -969,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 @@ -980,6 +956,8 @@ extension BrowserViewController: UIContextMenuInteractionDelegate {
pasteMenuChildren.reverse()
}

var copyMenu: UIMenu?

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

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

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

actionMenu = [pasteMenu, copyMenu]

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

return UIMenu(children: actionMenu)

actionMenus.append(reloadMenu)

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

if #available(iOS 16.0, *) {
configuration.preferredMenuElementOrder = .priority
}

return configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,8 @@ class TabLocationView: UIView {
$0.addTarget(self, action: #selector(didTapWalletButton), for: .touchUpInside)
}

let reloadButtonAccessibilityIdentifier = "TabToolbar.stopReloadButton"

lazy var reloadButton = ToolbarButton().then {
$0.accessibilityIdentifier = reloadButtonAccessibilityIdentifier
$0.accessibilityIdentifier = "TabToolbar.stopReloadButton"
$0.isAccessibilityElement = true
$0.accessibilityLabel = Strings.tabToolbarReloadButtonAccessibilityLabel
$0.setImage(UIImage(braveSystemNamed: "leo.browser.refresh", compatibleWith: nil), for: .normal)
Expand Down

0 comments on commit d6a9bde

Please sign in to comment.