Revision control
Copy as Markdown
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
import UIKit
import Common
/// Simple address toolbar implementation.
/// +-------------+--------------------------------------------------------+----------+
/// | navigation | [ leading ] indicators url [ trailing ] | browser |
/// | actions | [ page ] [ page ] | actions |
/// | | [ actions ] [ actions ] | |
/// +-------------+--------------------------------------------------------+----------+
public class BrowserAddressToolbar: UIView,
Notifiable,
AddressToolbar,
ThemeApplicable,
ToolbarButtonCaching,
LocationViewDelegate,
UIContextMenuInteractionDelegate,
UIDragInteractionDelegate {
private enum UX {
static let horizontalSpace: CGFloat = 8
static let borderHeight: CGFloat = 1
static let actionSpacing: CGFloat = 0
static let buttonSize = CGSize(width: 44, height: 44)
static let locationHeight: CGFloat = 44
// This could be changed at some point, depending on the a11y UX design.
static let locationMaxHeight: CGFloat = 54
static let toolbarAnimationTime: CGFloat = 0.15
static let iconsAnimationTime: CGFloat = 0.1
static let iconsAnimationDelay: CGFloat = 0.075
}
// Avoids a Swift linker failure where a directly stored @MainActor
// closure emits an unresolved getter on Xcode 27.
private final class LongPressMenuProvider {
private let provider: @MainActor () -> UIMenu?
init(provider: @escaping @MainActor () -> UIMenu?) {
self.provider = provider
}
@MainActor
func makeMenu() -> UIMenu? {
return provider()
}
}
public var notificationCenter: any NotificationProtocol = NotificationCenter.default
/// Removes the trailing margin and button inset of a top toolbar whose trailing edge
/// is already kept clear of system UI, such as iPhone Duo's status bar.
public var alignsTrailingContentToEdge = false {
didSet {
guard oldValue != alignsTrailingContentToEdge else { return }
updateToolbarContainerHorizontalConstraints(for: addressBarPosition)
}
}
private weak var toolbarDelegate: AddressToolbarDelegate?
private var theme: Theme?
private var droppableUrl: URL?
private var longPressMenuProvider: LongPressMenuProvider?
private var addressBarPosition: AddressToolbarPosition = .bottom
private var isContextMenuPresented = false
private var actionsAfterContextMenuDismissal: [() -> Void] = []
var cachedButtonReferences = [String: ToolbarButton]()
private lazy var toolbarContainerView: UIView = .build()
private lazy var navigationActionStack: UIStackView = .build()
private lazy var locationContainer: LocationContainer = .build()
private lazy var locationView: LocationView = .build()
private lazy var locationDividerView: UIView = .build()
private lazy var leadingPageActionStack: UIStackView = .build()
private lazy var trailingPageActionStack: UIStackView = .build { view in
view.spacing = UX.actionSpacing
}
private lazy var browserActionStack: UIStackView = .build()
private lazy var toolbarTopBorderView: UIView = .build()
private lazy var toolbarBottomBorderView: UIView = .build()
private var leadingBrowserActionConstraint: NSLayoutConstraint?
private var leadingLocationContainerConstraint: NSLayoutConstraint?
private var dividerWidthConstraint: NSLayoutConstraint?
private var toolbarBottomConstraint: NSLayoutConstraint?
private var toolbarTopConstraint: NSLayoutConstraint?
private var toolbarTopBorderHeightConstraint: NSLayoutConstraint?
private var toolbarBottomBorderHeightConstraint: NSLayoutConstraint?
private var leadingNavigationActionStackConstraint: NSLayoutConstraint?
private var trailingBrowserActionStackConstraint: NSLayoutConstraint?
private var locationContainerHeightConstraint: NSLayoutConstraint?
private var toolbarContainerSafeAreaLeadingConstraint: NSLayoutConstraint?
private var toolbarContainerSafeAreaTrailingConstraint: NSLayoutConstraint?
private var toolbarContainerWindowControlLeadingConstraint: NSLayoutConstraint?
private var toolbarContainerWindowControlTrailingConstraint: NSLayoutConstraint?
private var toolbarContainerFixedInsetsLeftConstraint: NSLayoutConstraint?
private var toolbarContainerFixedInsetsRightConstraint: NSLayoutConstraint?
/// Horizontal insets used instead of the toolbar's own safe area insets when the toolbar is at the bottom.
/// Translating the toolbar changes its safe area insets, setting stable insets keeps its layout stable(FXIOS-17137)
public var fixedHorizontalSafeAreaInsets: UIEdgeInsets? {
didSet {
guard fixedHorizontalSafeAreaInsets != oldValue else { return }
updateToolbarContainerHorizontalConstraints(for: addressBarPosition)
}
}
// FXIOS-10210 Temporary to support updating the Unified Search feature flag during runtime
private var previousConfiguration: AddressToolbarConfiguration? {
didSet {
// Ensure the theme is reapplied to update colors and other UI elements
// in sync with the new address toolbar configuration.
guard let theme else { return }
applyTheme(theme: theme)
}
}
public var isUnifiedSearchEnabled = false {
didSet {
guard let previousConfiguration, oldValue != isUnifiedSearchEnabled else { return }
locationView.configure(
previousConfiguration.locationViewConfiguration,
delegate: self,
isUnifiedSearchEnabled: isUnifiedSearchEnabled,
uxConfig: previousConfiguration.uxConfiguration,
addressBarPosition: addressBarPosition
)
}
}
override public var transform: CGAffineTransform {
get {
return locationContainer.transform
}
set {
locationContainer.transform = newValue
}
}
override init(frame: CGRect) {
super.init(frame: .zero)
setupLayout()
startObservingNotifications(
withNotificationCenter: notificationCenter,
forObserver: self,
observing: [UIContentSizeCategory.didChangeNotification]
)
adjustHeightConstraintForA11ySizeCategory()
setupDragInteraction()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public func configureNonInteractive(
config: AddressToolbarConfiguration,
leadingSpace: CGFloat,
trailingSpace: CGFloat
) {
previousConfiguration = config
configureUX(config: config.uxConfiguration, toolbarPosition: .bottom)
updateSpacing(leading: leadingSpace, trailing: trailingSpace)
locationView.configureNonInteractive(config.locationViewConfiguration, uxConfig: config.uxConfiguration)
updateActions(config: config, animated: false)
}
public func configure(config: AddressToolbarConfiguration,
toolbarPosition: AddressToolbarPosition,
toolbarDelegate: any AddressToolbarDelegate,
leadingSpace: CGFloat,
trailingSpace: CGFloat,
isUnifiedSearchEnabled: Bool,
animated: Bool) {
[navigationActionStack, leadingPageActionStack, trailingPageActionStack, browserActionStack].forEach {
$0.isHidden = config.uxConfiguration.isAddressBarMinimized
}
if #available(iOS 26.0, *) {
toolbarTopBorderView.isHidden = config.uxConfiguration.isAddressBarMinimized
}
self.toolbarDelegate = toolbarDelegate
self.isUnifiedSearchEnabled = isUnifiedSearchEnabled
addressBarPosition = toolbarPosition
previousConfiguration = config
toolbarTopBorderView.accessibilityIdentifier = config.borderConfiguration.a11yIdentifier
configureUX(config: config.uxConfiguration, toolbarPosition: toolbarPosition)
updateSpacing(leading: leadingSpace, trailing: trailingSpace)
configure(config: config,
isUnifiedSearchEnabled: isUnifiedSearchEnabled,
addressBarPosition: toolbarPosition,
animated: animated)
}
private func configure(
config: AddressToolbarConfiguration,
isUnifiedSearchEnabled: Bool,
addressBarPosition: AddressToolbarPosition,
animated: Bool
) {
updateBorder(borderPosition: config.borderConfiguration.borderPosition)
let isContextMenuEnabled = !config.locationViewConfiguration.isEditing &&
!config.uxConfiguration.isAddressBarMinimized
if isContextMenuEnabled, let provider = config.longPressMenuProvider {
longPressMenuProvider = LongPressMenuProvider(provider: provider)
} else {
longPressMenuProvider = nil
}
locationView.configure(
config.locationViewConfiguration,
delegate: self,
isUnifiedSearchEnabled: isUnifiedSearchEnabled,
uxConfig: config.uxConfiguration,
addressBarPosition: addressBarPosition
)
updateActions(config: config, animated: animated)
droppableUrl = config.locationViewConfiguration.droppableUrl
}
private func configureUX(config: AddressToolbarUXConfiguration,
toolbarPosition: AddressToolbarPosition) {
locationContainer.layer.cornerRadius = config.toolbarCornerRadius
locationContainer.updateShadowOpacityBasedOn(isAddressBarMinimized: config.isAddressBarMinimized)
dividerWidthConstraint?.constant = config.browserActionsAddressBarDividerWidth
let locationViewPaddings = config.locationViewVerticalPaddings(addressBarPosition: toolbarPosition)
toolbarBottomConstraint?.constant = -locationViewPaddings.bottom
toolbarTopConstraint?.constant = locationViewPaddings.top
updateToolbarContainerHorizontalConstraints(for: toolbarPosition)
}
public func setAutocompleteSuggestion(_ suggestion: String?) {
locationView.setAutocompleteSuggestion(suggestion)
}
override public func becomeFirstResponder() -> Bool {
super.becomeFirstResponder()
return locationView.becomeFirstResponder()
}
override public func resignFirstResponder() -> Bool {
super.resignFirstResponder()
return locationView.resignFirstResponder()
}
// MARK: - Private
private func setupLayout() {
addSubview(toolbarContainerView)
addSubview(toolbarTopBorderView)
addSubview(toolbarBottomBorderView)
locationContainer.addSubview(leadingPageActionStack)
locationContainer.addSubview(locationView)
locationContainer.addSubview(locationDividerView)
locationContainer.addSubview(trailingPageActionStack)
locationView.addInteraction(UIContextMenuInteraction(delegate: self))
toolbarContainerView.addSubview(navigationActionStack)
toolbarContainerView.addSubview(locationContainer)
toolbarContainerView.addSubview(browserActionStack)
leadingLocationContainerConstraint = navigationActionStack.trailingAnchor.constraint(
equalTo: locationContainer.leadingAnchor,
constant: -UX.horizontalSpace)
leadingLocationContainerConstraint?.isActive = true
leadingBrowserActionConstraint = browserActionStack.leadingAnchor.constraint(
equalTo: locationContainer.trailingAnchor,
constant: UX.horizontalSpace)
leadingBrowserActionConstraint?.isActive = true
dividerWidthConstraint = locationDividerView.widthAnchor.constraint(equalToConstant: 0)
dividerWidthConstraint?.isActive = true
[navigationActionStack,
leadingPageActionStack,
trailingPageActionStack,
browserActionStack].forEach(setZeroWidthConstraint)
toolbarTopBorderHeightConstraint = toolbarTopBorderView.heightAnchor.constraint(equalToConstant: 0)
toolbarBottomBorderHeightConstraint = toolbarBottomBorderView.heightAnchor.constraint(equalToConstant: 0)
toolbarTopBorderHeightConstraint?.isActive = true
toolbarBottomBorderHeightConstraint?.isActive = true
leadingNavigationActionStackConstraint = navigationActionStack.leadingAnchor.constraint(
equalTo: toolbarContainerView.leadingAnchor)
leadingNavigationActionStackConstraint?.isActive = true
trailingBrowserActionStackConstraint = browserActionStack.trailingAnchor.constraint(
equalTo: toolbarContainerView.trailingAnchor)
trailingBrowserActionStackConstraint?.isActive = true
locationContainerHeightConstraint = locationContainer.heightAnchor.constraint(equalToConstant: UX.locationHeight)
locationContainerHeightConstraint?.isActive = true
toolbarBottomConstraint = toolbarContainerView.bottomAnchor.constraint(
equalTo: toolbarBottomBorderView.bottomAnchor
)
toolbarBottomConstraint?.isActive = true
toolbarTopConstraint = toolbarContainerView.topAnchor.constraint(equalTo: toolbarTopBorderView.topAnchor)
toolbarTopConstraint?.isActive = true
toolbarContainerSafeAreaLeadingConstraint = toolbarContainerView.leadingAnchor.constraint(
equalTo: safeAreaLayoutGuide.leadingAnchor
)
toolbarContainerSafeAreaTrailingConstraint = toolbarContainerView.trailingAnchor.constraint(
equalTo: safeAreaLayoutGuide.trailingAnchor
)
toolbarContainerSafeAreaLeadingConstraint?.isActive = true
toolbarContainerSafeAreaTrailingConstraint?.isActive = true
toolbarContainerFixedInsetsLeftConstraint = toolbarContainerView.leftAnchor.constraint(equalTo: leftAnchor)
toolbarContainerFixedInsetsRightConstraint = toolbarContainerView.rightAnchor.constraint(equalTo: rightAnchor)
if #available(iOS 26.0, *) {
let windowControlLayoutGuide = layoutGuide(for: .margins(cornerAdaptation: .horizontal))
toolbarContainerWindowControlLeadingConstraint = toolbarContainerView.leadingAnchor.constraint(
equalTo: windowControlLayoutGuide.leadingAnchor
)
toolbarContainerWindowControlTrailingConstraint = toolbarContainerView.trailingAnchor.constraint(
equalTo: windowControlLayoutGuide.trailingAnchor
)
}
NSLayoutConstraint.activate([
toolbarTopBorderView.leadingAnchor.constraint(equalTo: leadingAnchor),
toolbarTopBorderView.topAnchor.constraint(equalTo: topAnchor),
toolbarTopBorderView.trailingAnchor.constraint(equalTo: trailingAnchor),
toolbarBottomBorderView.leadingAnchor.constraint(equalTo: leadingAnchor),
toolbarBottomBorderView.trailingAnchor.constraint(equalTo: trailingAnchor),
toolbarBottomBorderView.bottomAnchor.constraint(equalTo: bottomAnchor),
navigationActionStack.topAnchor.constraint(equalTo: toolbarContainerView.topAnchor),
navigationActionStack.bottomAnchor.constraint(equalTo: toolbarContainerView.bottomAnchor),
locationContainer.topAnchor.constraint(equalTo: toolbarContainerView.topAnchor),
locationContainer.bottomAnchor.constraint(equalTo: toolbarContainerView.bottomAnchor),
leadingPageActionStack.leadingAnchor.constraint(equalTo: locationContainer.leadingAnchor),
leadingPageActionStack.topAnchor.constraint(equalTo: locationContainer.topAnchor),
leadingPageActionStack.trailingAnchor.constraint(equalTo: locationView.leadingAnchor),
leadingPageActionStack.bottomAnchor.constraint(equalTo: locationContainer.bottomAnchor),
locationView.topAnchor.constraint(equalTo: locationContainer.topAnchor),
locationView.trailingAnchor.constraint(equalTo: locationDividerView.leadingAnchor),
locationView.bottomAnchor.constraint(equalTo: locationContainer.bottomAnchor),
locationDividerView.topAnchor.constraint(equalTo: locationContainer.topAnchor),
locationDividerView.trailingAnchor.constraint(equalTo: trailingPageActionStack.leadingAnchor),
locationDividerView.bottomAnchor.constraint(equalTo: locationContainer.bottomAnchor),
trailingPageActionStack.topAnchor.constraint(equalTo: locationContainer.topAnchor),
trailingPageActionStack.trailingAnchor.constraint(equalTo: locationContainer.trailingAnchor),
trailingPageActionStack.bottomAnchor.constraint(equalTo: locationContainer.bottomAnchor),
browserActionStack.topAnchor.constraint(equalTo: toolbarContainerView.topAnchor),
browserActionStack.bottomAnchor.constraint(equalTo: toolbarContainerView.bottomAnchor),
])
updateActionSpacing(uxConfig: .default())
setupAccessibility()
}
private func updateToolbarContainerHorizontalConstraints(for toolbarPosition: AddressToolbarPosition) {
let useWindowControlGuide: Bool
if #available(iOS 26.0, *), toolbarPosition == .top {
useWindowControlGuide = true
} else {
useWindowControlGuide = false
}
let fixedInsets = toolbarPosition == .bottom ? fixedHorizontalSafeAreaInsets : nil
// Deactivate all sets first, only one of them can be active at a time.
toolbarContainerWindowControlLeadingConstraint?.isActive = false
toolbarContainerWindowControlTrailingConstraint?.isActive = false
toolbarContainerSafeAreaLeadingConstraint?.isActive = false
toolbarContainerSafeAreaTrailingConstraint?.isActive = false
toolbarContainerFixedInsetsLeftConstraint?.isActive = false
toolbarContainerFixedInsetsRightConstraint?.isActive = false
toolbarContainerSafeAreaTrailingConstraint?.constant = 0
if useWindowControlGuide {
// Top toolbar on iOS 26+: keep clear of the window controls (e.g. iPad windowing).
toolbarContainerWindowControlLeadingConstraint?.isActive = true
if alignsTrailingContentToEdge {
// Extends past the safe area by the last button's inset so its icon sits on the edge.
// The stack view has no trailing padding of its own; the gap is the button's content inset,
// which differs between icon and text buttons, so it is read from the button itself.
let lastButton = browserActionStack.arrangedSubviews.last as? UIButton
let trailingInset = lastButton?.configuration?.contentInsets.trailing ?? 0
toolbarContainerSafeAreaTrailingConstraint?.constant = trailingInset
toolbarContainerSafeAreaTrailingConstraint?.isActive = true
} else {
toolbarContainerWindowControlTrailingConstraint?.isActive = true
}
} else if let fixedInsets {
// Bottom toolbar with `fixedHorizontalSafeAreaInsets` set, used instead of its own safe area
// (e.g. tab swipe next to a vertical system bar on the folded iPhone Duo).
toolbarContainerFixedInsetsLeftConstraint?.constant = fixedInsets.left
toolbarContainerFixedInsetsRightConstraint?.constant = -fixedInsets.right
toolbarContainerFixedInsetsLeftConstraint?.isActive = true
toolbarContainerFixedInsetsRightConstraint?.isActive = true
} else {
// Every other case: keep clear of the toolbar's own safe area.
toolbarContainerSafeAreaLeadingConstraint?.isActive = true
toolbarContainerSafeAreaTrailingConstraint?.isActive = true
}
}
private func setupDragInteraction() {
// Setup UIDragInteraction to handle dragging the location
// bar for dropping its URL into other apps.
let dragInteraction = UIDragInteraction(delegate: self)
dragInteraction.allowsSimultaneousRecognitionDuringLift = true
locationContainer.addInteraction(dragInteraction)
}
// MARK: - Accessibility
private func setupAccessibility() {
addInteraction(UILargeContentViewerInteraction())
}
private func adjustHeightConstraintForA11ySizeCategory() {
let height = min(UIFontMetrics.default.scaledValue(for: UX.locationHeight), UX.locationMaxHeight)
let contentSizeCategory = UIApplication.shared.preferredContentSizeCategory
if contentSizeCategory.isAccessibilityCategory {
locationContainerHeightConstraint?.constant = height
} else {
locationContainerHeightConstraint?.constant = UX.locationHeight
}
}
// MARK: - Toolbar Actions and Layout Updates
internal func updateActions(config: AddressToolbarConfiguration, animated: Bool) {
// Browser actions
updateActionStack(stackView: browserActionStack, toolbarElements: config.browserActions)
updateToolbarContainerHorizontalConstraints(for: addressBarPosition)
// Navigation actions
updateActionStack(stackView: navigationActionStack, toolbarElements: config.navigationActions)
// Page actions
updateActionStack(stackView: leadingPageActionStack, toolbarElements: config.leadingPageActions)
updateActionStack(stackView: trailingPageActionStack, toolbarElements: config.trailingPageActions)
updateActionSpacing(uxConfig: config.uxConfiguration)
updateToolbarLayout(animated: animated)
}
private func updateToolbarLayout(animated: Bool) {
let stacks = browserActionStack.arrangedSubviews +
navigationActionStack.arrangedSubviews +
leadingPageActionStack.arrangedSubviews +
trailingPageActionStack.arrangedSubviews
let areAllStacksAlreadyVisible = stacks.allSatisfy { $0.alpha == 1.0 }
guard !areAllStacksAlreadyVisible else { return }
let isAnimationEnabled = !UIAccessibility.isReduceMotionEnabled && animated
if isAnimationEnabled {
UIView.animate(withDuration: UX.toolbarAnimationTime, delay: 0.0, options: .curveEaseOut) {
self.layoutIfNeeded()
}
UIView.animate(withDuration: UX.iconsAnimationTime, delay: UX.iconsAnimationDelay, options: .curveEaseOut) {
stacks.forEach {
$0.alpha = 1.0
}
}
} else {
stacks.forEach {
$0.alpha = 1.0
}
}
}
public func updateSpacing(leading: CGFloat, trailing: CGFloat) {
leadingNavigationActionStackConstraint?.constant = leading
trailingBrowserActionStackConstraint?.constant = -trailing
}
private func setZeroWidthConstraint(_ stackView: UIStackView) {
let widthAnchor = stackView.widthAnchor.constraint(equalToConstant: 0)
widthAnchor.isActive = true
widthAnchor.priority = .defaultHigh
}
func updateActionStack(stackView: UIStackView, toolbarElements: [ToolbarElement]) {
let buttons = toolbarElements.map { toolbarElement in
let hasCachedButton = hasCachedButton(for: toolbarElement)
let button = getToolbarButton(for: toolbarElement)
button.configure(element: toolbarElement)
if !stackView.arrangedSubviews.contains(button) {
button.alpha = 0
}
if let theme {
// As we recreate the buttons we need to apply the theme for them to be displayed correctly
button.applyTheme(theme: theme)
}
if let contextualHintType = toolbarElement.contextualHintType {
toolbarDelegate?.configureContextualHint(self, for: button, with: contextualHintType)
}
// Only add the constraints to new buttons
if !hasCachedButton {
if button.configuration?.title == nil {
NSLayoutConstraint.activate([
button.widthAnchor.constraint(equalToConstant: UX.buttonSize.width),
button.heightAnchor.constraint(equalToConstant: UX.buttonSize.height),
])
} else {
NSLayoutConstraint.activate([
button.widthAnchor.constraint(greaterThanOrEqualToConstant: UX.buttonSize.width),
button.heightAnchor.constraint(greaterThanOrEqualToConstant: UX.buttonSize.height),
])
button.setContentCompressionResistancePriority(.required, for: .horizontal)
}
}
return button
}
// Buttons are cached and reused, so the stack often ends up with the exact same arrangement.
// Tearing it down and rebuilding it in that case only invalidates layout for no visible change.
guard !stackView.arrangedSubviews.elementsEqual(buttons, by: { $0 === $1 }) else { return }
stackView.removeAllArrangedViews()
buttons.forEach { button in
stackView.addArrangedSubview(button)
}
}
private func updateActionSpacing(uxConfig: AddressToolbarUXConfiguration) {
// Browser action spacing
let hasBrowserActions = !browserActionStack.arrangedSubviews.isEmpty
leadingBrowserActionConstraint?.constant = hasBrowserActions ? UX.horizontalSpace : 0
// Navigation action spacing
let hasNavigationActions = !navigationActionStack.arrangedSubviews.isEmpty
let isRegular = traitCollection.horizontalSizeClass == .regular
leadingLocationContainerConstraint?.constant = hasNavigationActions && isRegular ? -UX.horizontalSpace : 0
// Page action spacing
let hasPageActions = !trailingPageActionStack.arrangedSubviews.isEmpty
dividerWidthConstraint?.constant = hasPageActions ? uxConfig.browserActionsAddressBarDividerWidth : 0
}
private func updateBorder(borderPosition: AddressToolbarBorderPosition?) {
switch borderPosition {
case .top:
toolbarTopBorderHeightConstraint?.constant = UX.borderHeight
toolbarBottomBorderHeightConstraint?.constant = 0
case .bottom:
toolbarTopBorderHeightConstraint?.constant = 0
toolbarBottomBorderHeightConstraint?.constant = UX.borderHeight
default:
toolbarTopBorderHeightConstraint?.constant = 0
toolbarBottomBorderHeightConstraint?.constant = 0
}
}
// MARK: - Notifiable
nonisolated public func handleNotifications(_ notification: Notification) {
switch notification.name {
case UIContentSizeCategory.didChangeNotification:
ensureMainThread {
self.adjustHeightConstraintForA11ySizeCategory()
}
default: break
}
}
// MARK: - LocationViewDelegate
func locationViewDidEnterText(_ text: String) {
toolbarDelegate?.searchSuggestions(searchTerm: text)
}
func locationViewDidClearText() {
toolbarDelegate?.didClearSearch()
}
func locationViewDidBeginEditing(_ text: String, shouldShowSuggestions: Bool) {
toolbarDelegate?.addressToolbarDidBeginEditing(searchTerm: text, shouldShowSuggestions: shouldShowSuggestions)
}
func locationViewDidSubmitText(_ text: String) {
guard !text.isEmpty else { return }
toolbarDelegate?.openBrowser(searchTerm: text)
}
func locationViewDidTapSearchEngine<T: SearchEngineView>(_ searchEngine: T) {
toolbarDelegate?.addressToolbarDidTapSearchEngine(searchEngine)
}
func locationViewAccessibilityActions() -> [UIAccessibilityCustomAction]? {
toolbarDelegate?.addressToolbarAccessibilityActions()
}
func locationTextFieldNeedsSearchReset() {
toolbarDelegate?.addressToolbarNeedsSearchReset()
}
func locationViewDidDisplayEditingAccessoryButton(_ button: UIButton, contextualHintType: String) {
toolbarDelegate?.configureContextualHint(self, for: button, with: contextualHintType)
}
func locationViewDidHideEditingAccessoryButton() {
toolbarDelegate?.addressToolbarDidHideEditingAccessoryButton(self)
}
// MARK: - ThemeApplicable
public func applyTheme(theme: Theme) {
let colors = theme.colors
// Set background color to clear when address bar is positioned at top to avoid
// double opacity effects. The status bar overlay already provides the necessary
// alpha gradient that matches the bottom navigation bar's appearance.
backgroundColor = addressBarPosition == .top ? .clear :
previousConfiguration?.uxConfiguration.addressToolbarBackgroundColor(theme: theme)
locationContainer.backgroundColor = previousConfiguration?.uxConfiguration
.locationContainerBackgroundColor(theme: theme)
locationDividerView.backgroundColor = colors.layer1
toolbarTopBorderView.backgroundColor = colors.borderPrimary
toolbarBottomBorderView.backgroundColor = colors.borderPrimary
locationContainer.applyTheme(theme: theme)
locationView.applyTheme(theme: theme)
self.theme = theme
}
// MARK: - UIContextMenuInteractionDelegate
public func contextMenuInteraction(
_ interaction: UIContextMenuInteraction,
configurationForMenuAtLocation location: CGPoint
) -> UIContextMenuConfiguration? {
guard let menu = longPressMenuProvider?.makeMenu(), !menu.children.isEmpty else { return nil }
let configuration = UIContextMenuConfiguration(actionProvider: { _ in
return menu
})
if #available(iOS 16.0, *) {
configuration.preferredMenuElementOrder = .fixed
}
return configuration
}
public func contextMenuInteraction(
_ interaction: UIContextMenuInteraction,
previewForHighlightingMenuWithConfiguration configuration: UIContextMenuConfiguration
) -> UITargetedPreview? {
return contextMenuPreview()
}
public func contextMenuInteraction(
_ interaction: UIContextMenuInteraction,
previewForDismissingMenuWithConfiguration configuration: UIContextMenuConfiguration
) -> UITargetedPreview? {
return contextMenuPreview()
}
public func performAfterContextMenuDismissal(_ action: @escaping () -> Void) {
guard isContextMenuPresented else {
action()
return
}
actionsAfterContextMenuDismissal.append(action)
}
public func contextMenuInteraction(
_ interaction: UIContextMenuInteraction,
willDisplayMenuFor configuration: UIContextMenuConfiguration,
animator: (any UIContextMenuInteractionAnimating)?
) {
isContextMenuPresented = true
}
public func contextMenuInteraction(
_ interaction: UIContextMenuInteraction,
willEndFor configuration: UIContextMenuConfiguration,
animator: (any UIContextMenuInteractionAnimating)?
) {
guard let animator else {
finishContextMenuDismissal()
return
}
animator.addCompletion { [weak self] in
self?.finishContextMenuDismissal()
}
}
private func finishContextMenuDismissal() {
isContextMenuPresented = false
let actions = actionsAfterContextMenuDismissal
actionsAfterContextMenuDismissal.removeAll()
actions.forEach { $0() }
}
private func contextMenuPreview() -> UITargetedPreview? {
guard let snapshot = locationContainer.snapshotView(afterScreenUpdates: false) else { return nil }
let parameters = UIPreviewParameters()
parameters.backgroundColor = locationContainer.backgroundColor
parameters.visiblePath = UIBezierPath(
roundedRect: locationContainer.bounds,
cornerRadius: locationContainer.layer.cornerRadius
)
let target = UIPreviewTarget(
container: self,
center: locationContainer.convert(
CGPoint(x: locationContainer.bounds.midX, y: locationContainer.bounds.midY),
to: self
)
)
return UITargetedPreview(view: snapshot, parameters: parameters, target: target)
}
// MARK: - UIDragInteractionDelegate
public func dragInteraction(_ interaction: UIDragInteraction,
itemsForBeginning session: UIDragSession) -> [UIDragItem] {
let dragPoint = session.location(in: self)
guard let url = droppableUrl,
let itemProvider = NSItemProvider(contentsOf: url),
locationViewCanBeDrag(dragPoint: dragPoint) else { return [] }
toolbarDelegate?.addressToolbarDidProvideItemsForDragInteraction()
let dragItem = UIDragItem(itemProvider: itemProvider)
return [dragItem]
}
public func dragInteraction(_ interaction: UIDragInteraction, sessionWillBegin session: UIDragSession) {
toolbarDelegate?.addressToolbarDidBeginDragInteraction()
}
// Allow drag only when the touch is within the location view frame.
// This avoids interfering with long-press gestures on address bar buttons
// and disables drag interactions while the location view is minimized.
private func locationViewCanBeDrag(dragPoint: CGPoint) -> Bool {
return locationView.frame.contains(dragPoint) &&
!locationView.isAddressBarMinimized
}
}