Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scroll address bar to caret when using arrows #2799

Merged
merged 2 commits into from
May 24, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions DuckDuckGo/NavigationBar/View/AddressBarTextEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,38 +292,44 @@ 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?) {
let selectedRange = self.selectedRange()
guard selectionAffinity == .downstream || selectedRange.length == 0 else {
// current selection is from right to left: reset selection to the upper bound
self.selectedRange = NSRange(location: selectedRange.upperBound, length: 0)
scrollToCaret()
return
}
guard let index = nextWordSelectionIndex(backwards: false) else { return }

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

override func moveWordLeftAndModifySelection(_ sender: Any?) {
let selectedRange = self.selectedRange()
guard selectionAffinity == .upstream || selectedRange.length == 0 else {
// current selection is from left to right: reset selection to the upper bound
self.selectedRange = NSRange(location: selectedRange.lowerBound, length: 0)
scrollToCaret()
return
}
guard let index = nextWordSelectionIndex(backwards: true) else { return }

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

override func deleteForward(_ sender: Any?) {
Expand Down Expand Up @@ -422,6 +428,11 @@ 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)
}
}

final class AddressBarTextFieldCell: NSTextFieldCell {
Expand Down
Loading