Skip to content

Commit

Permalink
Scroll address bar to caret when using arrows (#2799)
Browse files Browse the repository at this point in the history
Task/Issue URL:
https://app.asana.com/0/1177771139624306/1207043866291151/f

**Description**: When using arrows in the address bar it will scroll to
the caret position
  • Loading branch information
SabrinaTardio authored May 24, 2024
1 parent 89627db commit 36da959
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions DuckDuckGo/NavigationBar/View/AddressBarTextEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,14 @@ final class AddressBarTextEditor: NSTextView {
guard let index = nextWordSelectionIndex(backwards: false) else { return }

self.selectedRange = NSRange(location: index, length: 0)
scrollToCaret()
}

override func moveWordLeft(_ sender: Any?) {
guard let index = nextWordSelectionIndex(backwards: true) else { return }

self.selectedRange = NSRange(location: index, length: 0)
scrollToCaret()
}

override func moveWordRightAndModifySelection(_ sender: Any?) {
Expand All @@ -311,6 +313,7 @@ final class AddressBarTextEditor: NSTextView {

let range = NSRange(location: selectedRange.location, length: index - selectedRange.location)
self.setSelectedRange(range, affinity: .downstream, stillSelecting: false)
self.scrollToSelectionEnd()
}

override func moveWordLeftAndModifySelection(_ sender: Any?) {
Expand All @@ -324,6 +327,7 @@ final class AddressBarTextEditor: NSTextView {

let range = NSRange(location: index, length: selectedRange.upperBound - index)
self.setSelectedRange(range, affinity: .upstream, stillSelecting: false)
self.scrollToSelectionStart()
}

override func deleteForward(_ sender: Any?) {
Expand Down Expand Up @@ -422,6 +426,22 @@ final class AddressBarTextEditor: NSTextView {
breakUndoCoalescing()
}

private func scrollToCaret() {
guard let layoutManager = layoutManager, let textContainer = textContainer else { return }
let caretRect = layoutManager.boundingRect(forGlyphRange: selectedRange(), in: textContainer)
scrollToVisible(caretRect)
}

private func scrollToSelectionStart() {
let startRange = NSRange(location: selectedRange().location, length: 0)
self.scrollRangeToVisible(startRange)
}

private func scrollToSelectionEnd() {
let endRange = NSRange(location: selectedRange.location + selectedRange.length, length: 0)
self.scrollRangeToVisible(endRange)
}

}

final class AddressBarTextFieldCell: NSTextFieldCell {
Expand Down

0 comments on commit 36da959

Please sign in to comment.