From bb1e8b76dbf6fff1b7b4e2345e48be5bacb03fad Mon Sep 17 00:00:00 2001 From: Brandon T Date: Fri, 13 Oct 2023 10:14:52 -0400 Subject: [PATCH] Fix #8241: Fix Menu not updating when the private-mode changes (#8242) --- .../Tabs/TabBar/TabsBarViewController.swift | 71 +++++++++++-------- 1 file changed, 40 insertions(+), 31 deletions(-) diff --git a/Sources/Brave/Frontend/Browser/Tabs/TabBar/TabsBarViewController.swift b/Sources/Brave/Frontend/Browser/Tabs/TabBar/TabsBarViewController.swift index dd8f413f619..56e01b9264e 100644 --- a/Sources/Brave/Frontend/Browser/Tabs/TabBar/TabsBarViewController.swift +++ b/Sources/Brave/Frontend/Browser/Tabs/TabBar/TabsBarViewController.swift @@ -101,43 +101,17 @@ class TabsBarViewController: UIViewController { make.right.equalTo(view).inset(UX.TabsBar.buttonWidth) } - var newTabMenu: [UIAction] = [] let isPrivateBrowsing = tabManager?.privateBrowsingManager.isPrivateBrowsing == true - - if !isPrivateBrowsing { - let openNewPrivateTab = UIAction( - title: Strings.Hotkey.newPrivateTabTitle, - image: UIImage(systemName: "plus.square.fill.on.square.fill"), - handler: UIAction.deferredActionHandler { [unowned self] _ in - self.delegate?.tabsBarDidSelectAddNewTab(true) - }) - - newTabMenu.append(openNewPrivateTab) - } - - let openNewTab = UIAction( - title: isPrivateBrowsing ? Strings.Hotkey.newPrivateTabTitle : Strings.Hotkey.newTabTitle, - image: isPrivateBrowsing ? UIImage(systemName: "plus.square.fill.on.square.fill") : UIImage(systemName: "plus.square.on.square"), - handler: UIAction.deferredActionHandler { [unowned self] _ in - self.delegate?.tabsBarDidSelectAddNewTab(isPrivateBrowsing) - }) - - newTabMenu.append(openNewTab) + updatePlusButtonMenu() + updateColors() - newTabMenu.append(UIAction(title: Strings.newWindowTitle, image: UIImage(braveSystemNamed: "leo.window"), handler: UIAction.deferredActionHandler { [unowned self] _ in - self.delegate?.tabsBarDidSelectAddNewWindow(false) - })) - - newTabMenu.append(UIAction(title: Strings.newPrivateWindowTitle, image: UIImage(braveSystemNamed: "leo.window.tab-private"), handler: UIAction.deferredActionHandler { [unowned self] _ in - self.delegate?.tabsBarDidSelectAddNewWindow(true) - })) - - plusButton.menu = UIMenu(title: "", identifier: nil, children: newTabMenu) privateModeCancellable = tabManager?.privateBrowsingManager .$isPrivateBrowsing .removeDuplicates() .sink(receiveValue: { [weak self] isPrivateBrowsing in - self?.updateColors(isPrivateBrowsing) + guard let self = self else { return } + self.updatePlusButtonMenu() + self.updateColors(isPrivateBrowsing) }) Preferences.General.nightModeEnabled.objectWillChange @@ -218,6 +192,41 @@ class TabsBarViewController: UIViewController { delegate?.tabsBarDidLongPressAddTab(self, button: plusButton) } } + + func updatePlusButtonMenu() { + var newTabMenu: [UIAction] = [] + let isPrivateBrowsing = tabManager?.privateBrowsingManager.isPrivateBrowsing == true + + let openNewTab = UIAction( + title: isPrivateBrowsing ? Strings.Hotkey.newPrivateTabTitle : Strings.Hotkey.newTabTitle, + image: isPrivateBrowsing ? UIImage(systemName: "plus.square.fill.on.square.fill") : UIImage(systemName: "plus.square.on.square"), + handler: UIAction.deferredActionHandler { [unowned self] _ in + self.delegate?.tabsBarDidSelectAddNewTab(isPrivateBrowsing) + }) + + newTabMenu.append(openNewTab) + + if !isPrivateBrowsing { + let openNewPrivateTab = UIAction( + title: Strings.Hotkey.newPrivateTabTitle, + image: UIImage(systemName: "plus.square.fill.on.square.fill"), + handler: UIAction.deferredActionHandler { [unowned self] _ in + self.delegate?.tabsBarDidSelectAddNewTab(true) + }) + + newTabMenu.append(openNewPrivateTab) + } + + newTabMenu.append(UIAction(title: Strings.newWindowTitle, image: UIImage(braveSystemNamed: "leo.window"), handler: UIAction.deferredActionHandler { [unowned self] _ in + self.delegate?.tabsBarDidSelectAddNewWindow(false) + })) + + newTabMenu.append(UIAction(title: Strings.newPrivateWindowTitle, image: UIImage(braveSystemNamed: "leo.window.tab-private"), handler: UIAction.deferredActionHandler { [unowned self] _ in + self.delegate?.tabsBarDidSelectAddNewWindow(true) + })) + + plusButton.menu = UIMenu(title: "", identifier: nil, children: newTabMenu) + } func updateData() { // Don't waste time/resources updating data when we're in the middle of a restore or bulk delete