From d6a9bde2badce97606d5c9ae633c3cf5ee9525f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Buczek?= Date: Wed, 9 Aug 2023 13:13:58 +0200 Subject: [PATCH] Single context menu --- .../Browser/BrowserViewController.swift | 2 - ...rowserViewController+ToolbarDelegate.swift | 81 +++++++------------ .../Toolbars/UrlBar/TabLocationView.swift | 4 +- 3 files changed, 31 insertions(+), 56 deletions(-) diff --git a/Sources/Brave/Frontend/Browser/BrowserViewController.swift b/Sources/Brave/Frontend/Browser/BrowserViewController.swift index 67c288117e6..6c2c84b9982 100644 --- a/Sources/Brave/Frontend/Browser/BrowserViewController.swift +++ b/Sources/Brave/Frontend/Browser/BrowserViewController.swift @@ -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 }() diff --git a/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController+ToolbarDelegate.swift b/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController+ToolbarDelegate.swift index 7c82b684ebd..5e4fe220b0d 100644 --- a/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController+ToolbarDelegate.swift +++ b/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController+ToolbarDelegate.swift @@ -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, @@ -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) } }) @@ -980,6 +956,8 @@ extension BrowserViewController: UIContextMenuInteractionDelegate { pasteMenuChildren.reverse() } + var copyMenu: UIMenu? + let copyAction = UIAction( title: Strings.copyAddressTitle, image: UIImage(systemName: "doc.on.doc"), @@ -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 diff --git a/Sources/Brave/Frontend/Browser/Toolbars/UrlBar/TabLocationView.swift b/Sources/Brave/Frontend/Browser/Toolbars/UrlBar/TabLocationView.swift index 33cee9ba07b..96ea8391a40 100644 --- a/Sources/Brave/Frontend/Browser/Toolbars/UrlBar/TabLocationView.swift +++ b/Sources/Brave/Frontend/Browser/Toolbars/UrlBar/TabLocationView.swift @@ -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)