Skip to content

Commit

Permalink
Apply 10% margin when jump to position
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyzanowskim committed Jan 28, 2023
1 parent f9c8a1b commit 86ccf20
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
8 changes: 8 additions & 0 deletions Sources/STTextView/Extensions/Geometric+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ extension CGRect {
insetBy(dx: 0, dy: dy)
}

func scale(_ scale: CGSize) -> CGRect {
applying(.init(scaleX: scale.width, y: scale.height))
}

func margin(_ margin: CGSize) -> CGRect {
insetBy(dx: -margin.width / 2, dy: -margin.height / 2)
}

func moved(dx: CGFloat = 0, dy: CGFloat = 0) -> CGRect {
applying(.init(translationX: dx, y: dy))
}
Expand Down
1 change: 0 additions & 1 deletion Sources/STTextView/STTextView+Scrolling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,4 @@ extension STTextView {
open override func scrollToEndOfDocument(_ sender: Any?) {
scroll(CGPoint(x: visibleRect.origin.x, y: frame.maxY))
}

}
1 change: 1 addition & 0 deletions Sources/STTextView/STTextView+Select.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Cocoa
extension STTextView {

open override func selectAll(_ sender: Any?) {

if isSelectable {
textLayoutManager.textSelections = [
NSTextSelection(range: textLayoutManager.documentRange, affinity: .downstream, granularity: .line)
Expand Down
6 changes: 3 additions & 3 deletions Sources/STTextView/STTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -718,19 +718,19 @@ open class STTextView: NSView, NSTextInput {

if selectionTextRange.isEmpty {
if let selectionRect = textLayoutManager.textSelectionSegmentFrame(at: selectionTextRange.location, type: .selection) {
scrollToVisible(selectionRect)
scrollToVisible(selectionRect.margin(.init(width: visibleRect.width * 0.1, height: 0)))
}
} else {
switch selection.affinity {
case .upstream:
if let selectionRect = textLayoutManager.textSelectionSegmentFrame(at: selectionTextRange.location, type: .selection) {
scrollToVisible(selectionRect)
scrollToVisible(selectionRect.margin(.init(width: visibleRect.width * 0.1, height: 0)))
}
case .downstream:
if let location = textLayoutManager.location(selectionTextRange.endLocation, offsetBy: -1),
let selectionRect = textLayoutManager.textSelectionSegmentFrame(at: location, type: .selection)
{
scrollToVisible(selectionRect)
scrollToVisible(selectionRect.margin(.init(width: visibleRect.width * 0.1, height: 0)))
}
@unknown default:
break
Expand Down

0 comments on commit 86ccf20

Please sign in to comment.