From a1cd9d7d3dc41b21d4f19da8061b8e923d5976f5 Mon Sep 17 00:00:00 2001 From: Sabrina Tardio Date: Thu, 8 Feb 2024 11:59:49 +0100 Subject: [PATCH] implement show bookmark name in tab feature --- .../Common/Utilities/UserDefaultsWrapper.swift | 1 + .../Model/AppearancePreferences.swift | 11 +++++++++++ .../View/PreferencesAppearanceView.swift | 5 ++++- DuckDuckGo/Tab/Model/Tab.swift | 16 ++++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift b/DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift index 286d5842fa..8fb76073e1 100644 --- a/DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift +++ b/DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift @@ -81,6 +81,7 @@ public struct UserDefaultsWrapper { case customHomePageURL = "preferences.startup.customHomePageURL" case currentThemeName = "com.duckduckgo.macos.currentThemeNameKey" case showFullURL = "preferences.appearance.show-full-url" + case showBookmarkTitleInTab = "preferences.appearance.show-bookmark-title-in-tab" case showAutocompleteSuggestions = "preferences.appearance.show-autocomplete-suggestions" case defaultPageZoom = "preferences.appearance.default-page-zoom" case bookmarksBarAppearance = "preferences.appearance.bookmarks-bar" diff --git a/DuckDuckGo/Preferences/Model/AppearancePreferences.swift b/DuckDuckGo/Preferences/Model/AppearancePreferences.swift index ced7a60730..ad363e5c24 100644 --- a/DuckDuckGo/Preferences/Model/AppearancePreferences.swift +++ b/DuckDuckGo/Preferences/Model/AppearancePreferences.swift @@ -23,6 +23,7 @@ import Common protocol AppearancePreferencesPersistor { var showFullURL: Bool { get set } + var showBookmarkTitleInTab: Bool { get set } var showAutocompleteSuggestions: Bool { get set } var currentThemeName: String { get set } var defaultPageZoom: CGFloat { get set } @@ -39,6 +40,9 @@ struct AppearancePreferencesUserDefaultsPersistor: AppearancePreferencesPersisto @UserDefaultsWrapper(key: .showFullURL, defaultValue: false) var showFullURL: Bool + @UserDefaultsWrapper(key: .showBookmarkTitleInTab, defaultValue: true) + var showBookmarkTitleInTab: Bool + @UserDefaultsWrapper(key: .showAutocompleteSuggestions, defaultValue: true) var showAutocompleteSuggestions: Bool @@ -181,6 +185,12 @@ final class AppearancePreferences: ObservableObject { } } + @Published var showBookmarkTitleInTab: Bool { + didSet { + persistor.showBookmarkTitleInTab = showBookmarkTitleInTab + } + } + @Published var showAutocompleteSuggestions: Bool { didSet { persistor.showAutocompleteSuggestions = showAutocompleteSuggestions @@ -269,6 +279,7 @@ final class AppearancePreferences: ObservableObject { showBookmarksBar = persistor.showBookmarksBar bookmarksBarAppearance = persistor.bookmarksBarAppearance homeButtonPosition = persistor.homeButtonPosition + showBookmarkTitleInTab = persistor.showBookmarkTitleInTab } private var persistor: AppearancePreferencesPersistor diff --git a/DuckDuckGo/Preferences/View/PreferencesAppearanceView.swift b/DuckDuckGo/Preferences/View/PreferencesAppearanceView.swift index 5ebc18ddc5..3e5c4ff3e5 100644 --- a/DuckDuckGo/Preferences/View/PreferencesAppearanceView.swift +++ b/DuckDuckGo/Preferences/View/PreferencesAppearanceView.swift @@ -90,11 +90,14 @@ extension Preferences { // SECTION 1: Theme PreferencePaneSection(UserText.theme) { - ThemePicker() .environmentObject(model) } + PreferencePaneSection("Tabs") { + ToggleMenuItem("Show Bookmark Titles in Tabs", isOn: $model.showBookmarkTitleInTab) + } + // SECTION 2: Address Bar PreferencePaneSection(UserText.addressBar) { ToggleMenuItem(UserText.showFullWebsiteAddress, isOn: $model.showFullURL) diff --git a/DuckDuckGo/Tab/Model/Tab.swift b/DuckDuckGo/Tab/Model/Tab.swift index 04e7edc570..2a39b68336 100644 --- a/DuckDuckGo/Tab/Model/Tab.swift +++ b/DuckDuckGo/Tab/Model/Tab.swift @@ -265,6 +265,8 @@ protocol NewWindowPolicyDecisionMaker { private let statisticsLoader: StatisticsLoader? private let internalUserDecider: InternalUserDecider? + private let bookmarksManager: BookmarkManager = LocalBookmarkManager.shared + private let appearancePreferences = AppearancePreferences.shared let pinnedTabsManager: PinnedTabsManager #if NETWORK_PROTECTION @@ -450,6 +452,14 @@ protocol NewWindowPolicyDecisionMaker { handleFavicon() } + bookmarksManager.listPublisher.receive(on: RunLoop.main).sink { [weak self] _ in + self?.updateTitle() + }.store(in: &bookmarkManagerCancellables) + + appearancePreferences.$showBookmarkTitleInTab.sink { [weak self] _ in + self?.updateTitle() + }.store(in: &bookmarkManagerCancellables) + emailDidSignOutCancellable = NotificationCenter.default.publisher(for: .emailDidSignOut) .receive(on: DispatchQueue.main) .sink { [weak self] notification in @@ -665,6 +675,11 @@ protocol NewWindowPolicyDecisionMaker { if title != self.title { self.title = title } + + guard appearancePreferences.showBookmarkTitleInTab else { return } + if let url = webView.url, let bookmark = bookmarksManager.getBookmark(for: url) { + self.title = bookmark.title + } } @PublishedAfter var error: WKError? { @@ -939,6 +954,7 @@ protocol NewWindowPolicyDecisionMaker { _ = FireproofDomains.shared.toggle(domain: host) } + private var bookmarkManagerCancellables = Set() private var webViewCancellables = Set() private var emailDidSignOutCancellable: AnyCancellable?