Skip to content

Commit

Permalink
Prevent selection animation glitch when editing
Browse files Browse the repository at this point in the history
  • Loading branch information
dus7 committed Jan 22, 2024
1 parent 3ed2865 commit 56d9f65
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions DuckDuckGo/AddressDisplayHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
7 changes: 5 additions & 2 deletions DuckDuckGo/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -1553,8 +1557,7 @@ extension MainViewController: OmniBarDelegate {
}

func onTextFieldDidBeginEditing(_ omniBar: OmniBar) -> Bool {
refreshOmniBar()


let selectQueryText = !(isSERPPresented && !skipSERPFlow)
skipSERPFlow = false

Expand Down
6 changes: 3 additions & 3 deletions DuckDuckGo/OmniBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions DuckDuckGoTests/AddressDisplayHelperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit 56d9f65

Please sign in to comment.