From 56d9f65daadef8bba80c8ea237763ac32a193b91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mariusz=20=C5=9Apiewak?= Date: Mon, 22 Jan 2024 11:23:28 +0100 Subject: [PATCH] Prevent selection animation glitch when editing --- DuckDuckGo/AddressDisplayHelper.swift | 4 ++-- DuckDuckGo/MainViewController.swift | 7 +++++-- DuckDuckGo/OmniBar.swift | 6 +++--- DuckDuckGoTests/AddressDisplayHelperTests.swift | 10 +++++----- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/DuckDuckGo/AddressDisplayHelper.swift b/DuckDuckGo/AddressDisplayHelper.swift index c034353f5d..0016af4702 100644 --- a/DuckDuckGo/AddressDisplayHelper.swift +++ b/DuckDuckGo/AddressDisplayHelper.swift @@ -23,9 +23,9 @@ extension OmniBar { struct AddressDisplayHelper { - static func addressForDisplay(url: URL, isEditing: Bool) -> NSAttributedString { + static func addressForDisplay(url: URL, showsFullURL: Bool) -> NSAttributedString { - if !isEditing, let shortAddress = shortURLString(url) { + if !showsFullURL, let shortAddress = shortURLString(url) { return NSAttributedString( string: shortAddress, attributes: [.foregroundColor: ThemeManager.shared.currentTheme.searchBarTextColor]) diff --git a/DuckDuckGo/MainViewController.swift b/DuckDuckGo/MainViewController.swift index 260656cb91..f26553402f 100644 --- a/DuckDuckGo/MainViewController.swift +++ b/DuckDuckGo/MainViewController.swift @@ -1543,6 +1543,10 @@ extension MainViewController: OmniBarDelegate { } func onTextFieldWillBeginEditing(_ omniBar: OmniBar) { + if let currentTab { + viewCoordinator.omniBar.refreshText(forUrl: currentTab.url, forceFullURL: true) + } + guard homeController == nil else { return } if !skipSERPFlow, isSERPPresented, let query = omniBar.textField.text { @@ -1553,8 +1557,7 @@ extension MainViewController: OmniBarDelegate { } func onTextFieldDidBeginEditing(_ omniBar: OmniBar) -> Bool { - refreshOmniBar() - + let selectQueryText = !(isSERPPresented && !skipSERPFlow) skipSERPFlow = false diff --git a/DuckDuckGo/OmniBar.swift b/DuckDuckGo/OmniBar.swift index 5c7099a9f7..ec5969f657 100644 --- a/DuckDuckGo/OmniBar.swift +++ b/DuckDuckGo/OmniBar.swift @@ -384,7 +384,7 @@ class OmniBar: UIView { omniDelegate?.onOmniQueryUpdated("") } - func refreshText(forUrl url: URL?) { + func refreshText(forUrl url: URL?, forceFullURL: Bool = false) { guard let url = url else { textField.text = nil @@ -394,7 +394,7 @@ class OmniBar: UIView { if let query = url.searchQuery { textField.text = query } else { - textField.attributedText = AddressDisplayHelper.addressForDisplay(url: url, isEditing: textField.isEditing) + textField.attributedText = AddressDisplayHelper.addressForDisplay(url: url, showsFullURL: textField.isEditing || forceFullURL) } } @@ -540,7 +540,7 @@ extension OmniBar: Themable { searchStackContainer?.tintColor = theme.barTintColor if let url = textField.text.flatMap({ URL(trimmedAddressBarString: $0.trimmingWhitespace()) }) { - textField.attributedText = AddressDisplayHelper.addressForDisplay(url: url, isEditing: textField.isEditing) + textField.attributedText = AddressDisplayHelper.addressForDisplay(url: url, showsFullURL: textField.isEditing) } textField.textColor = theme.searchBarTextColor textField.tintColor = UIColor(designSystemColor: .accent) diff --git a/DuckDuckGoTests/AddressDisplayHelperTests.swift b/DuckDuckGoTests/AddressDisplayHelperTests.swift index ce2c3be79d..7894c758f0 100644 --- a/DuckDuckGoTests/AddressDisplayHelperTests.swift +++ b/DuckDuckGoTests/AddressDisplayHelperTests.swift @@ -79,20 +79,20 @@ class AddressDisplayHelperTests: XCTestCase { XCTAssertNil(AddressHelper.shortURLString(URL(string: "data:text/plain;charset=UTF-8;page=21,the%20data:12345")!)) } - func testShortensURLWhenNotEditing() { - let addressForDisplay = AddressHelper.addressForDisplay(url: URL(string: "http://some.domain.eu/with/path")!, isEditing: false) + func testShortensURLWhenShortVersionExpected() { + let addressForDisplay = AddressHelper.addressForDisplay(url: URL(string: "http://some.domain.eu/with/path")!, showsFullURL: false) XCTAssertEqual(addressForDisplay.string, "some.domain.eu") } - func testDoesNotShortenURLWhenEditing() { - let addressForDisplay = AddressHelper.addressForDisplay(url: URL(string: "http://some.domain.eu/with/path")!, isEditing: true) + func testDoesNotShortenURLWhenFullVersionExpected() { + let addressForDisplay = AddressHelper.addressForDisplay(url: URL(string: "http://some.domain.eu/with/path")!, showsFullURL: true) XCTAssertEqual(addressForDisplay.string, "http://some.domain.eu/with/path") } func testFallsBackToLongURLWhenCannotProduceShortURL() { - let addressForDisplay = AddressHelper.addressForDisplay(url: URL(string: "file:///some/path")!, isEditing: false) + let addressForDisplay = AddressHelper.addressForDisplay(url: URL(string: "file:///some/path")!, showsFullURL: false) XCTAssertEqual(addressForDisplay.string, "file:///some/path") }