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 all commits
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
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
Loading