diff --git a/DuckDuckGo/HomePage/Model/HomePageAddressBarModel.swift b/DuckDuckGo/HomePage/Model/HomePageAddressBarModel.swift index 038ce2eca3..a945c8a700 100644 --- a/DuckDuckGo/HomePage/Model/HomePageAddressBarModel.swift +++ b/DuckDuckGo/HomePage/Model/HomePageAddressBarModel.swift @@ -44,7 +44,20 @@ extension HomePage.Models { } } } - @Published var value: AddressBarTextField.Value = .text("", userTyped: false) + @Published var value: AddressBarTextField.Value = .text("", userTyped: false) { + didSet { + if shouldDisplayInitialPlaceholder && !value.string.isEmpty { + shouldDisplayInitialPlaceholder = false + } + } + } + + /** + * This property is a workaround for placeholder not being displayed on the address bar in SwiftUI until focused. + * + * It's cleared after first edit on the field. + */ + @Published var shouldDisplayInitialPlaceholder = true var addressBarTextField: AddressBarTextField? { guard shouldShowAddressBar else { diff --git a/DuckDuckGo/HomePage/View/AddressBarTextFieldView.swift b/DuckDuckGo/HomePage/View/AddressBarTextFieldView.swift index 0a6ceaf39e..b07133181c 100644 --- a/DuckDuckGo/HomePage/View/AddressBarTextFieldView.swift +++ b/DuckDuckGo/HomePage/View/AddressBarTextFieldView.swift @@ -72,6 +72,9 @@ struct BigSearchBox: View { let isCompact: Bool let supportsFixedColorScheme: Bool + @EnvironmentObject var addressBarModel: HomePage.Models.AddressBarModel + @Environment(\.colorScheme) private var colorScheme + init(isCompact: Bool, supportsFixedColorScheme: Bool = true) { self.isCompact = isCompact self.supportsFixedColorScheme = supportsFixedColorScheme @@ -106,9 +109,22 @@ struct BigSearchBox: View { @ViewBuilder func searchField() -> some View { - AddressBarTextFieldView(supportsFixedColorScheme: supportsFixedColorScheme) - .frame(height: Const.searchBoxHeight) - .shadow(color: .black.opacity(0.2), radius: 4, x: 0, y: 3) - .shadow(color: .black.opacity(0.15), radius: 0, x: 0, y: 0) + ZStack { + AddressBarTextFieldView(supportsFixedColorScheme: supportsFixedColorScheme) + .shadow(color: .black.opacity(0.2), radius: 4, x: 0, y: 3) + .shadow(color: .black.opacity(0.15), radius: 0, x: 0, y: 0) + if #available(macOS 12.0, *), addressBarModel.shouldDisplayInitialPlaceholder { + HStack { + Text(UserText.addressBarPlaceholder) + .foregroundColor(Color(nsColor: NSColor.placeholderTextColor)) + .background(Color.homePageAddressBarBackground) + .font(.system(size: 15)) + .padding(.leading, 38) + .animation(.none, value: colorScheme) // don't animate color scheme change because NSTextField doesn't + Spacer() + } + } + } + .frame(height: Const.searchBoxHeight) } } diff --git a/DuckDuckGo/HomePage/View/HomePageViewController.swift b/DuckDuckGo/HomePage/View/HomePageViewController.swift index 2f063bc939..9a0b903e6b 100644 --- a/DuckDuckGo/HomePage/View/HomePageViewController.swift +++ b/DuckDuckGo/HomePage/View/HomePageViewController.swift @@ -126,7 +126,6 @@ final class HomePageViewController: NSViewController { override func viewDidAppear() { super.viewDidAppear() refreshModels() - addressBarModel.addressBarTextField?.makeMeFirstResponder() showSettingsOnboardingIfNeeded() } @@ -276,7 +275,7 @@ final class HomePageViewController: NSViewController { } private func showSettingsOnboardingIfNeeded() { - if !settingsVisibilityModel.didShowSettingsOnboarding { + if addressBarModel.shouldShowAddressBar && !settingsVisibilityModel.didShowSettingsOnboarding { // async dispatch in order to get the final value for self.view.bounds DispatchQueue.main.async { guard let superview = self.view.superview else { diff --git a/DuckDuckGo/MainWindow/MainViewController.swift b/DuckDuckGo/MainWindow/MainViewController.swift index 42bfe7ba16..997c390578 100644 --- a/DuckDuckGo/MainWindow/MainViewController.swift +++ b/DuckDuckGo/MainWindow/MainViewController.swift @@ -476,14 +476,7 @@ final class MainViewController: NSViewController { let tabContent = tabContent ?? selectedTabViewModel.tab.content if case .newtab = tabContent { - let homeAddressBarTextField = browserTabViewController.homePageViewController?.addressBarModel?.addressBarTextField - let isHomeAddressBarVisible = browserTabViewController.homePageViewController?.appearancePreferences.isSearchBarVisible == true - if isHomeAddressBarVisible, let homeAddressBarTextField { - homeAddressBarTextField.makeMeFirstResponder() - } else { - navigationBarViewController.addressBarViewController?.addressBarTextField.makeMeFirstResponder() - } - + navigationBarViewController.addressBarViewController?.addressBarTextField.makeMeFirstResponder() } else { // ignore published tab switch: BrowserTabViewController // adjusts first responder itself