diff --git a/Sources/STTextView/Extensions/Geometric+Helpers.swift b/Sources/STTextView/Extensions/Geometric+Helpers.swift index 0108585..84b4a97 100644 --- a/Sources/STTextView/Extensions/Geometric+Helpers.swift +++ b/Sources/STTextView/Extensions/Geometric+Helpers.swift @@ -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)) } diff --git a/Sources/STTextView/STTextView+Scrolling.swift b/Sources/STTextView/STTextView+Scrolling.swift index 4db3598..592020a 100644 --- a/Sources/STTextView/STTextView+Scrolling.swift +++ b/Sources/STTextView/STTextView+Scrolling.swift @@ -46,5 +46,4 @@ extension STTextView { open override func scrollToEndOfDocument(_ sender: Any?) { scroll(CGPoint(x: visibleRect.origin.x, y: frame.maxY)) } - } diff --git a/Sources/STTextView/STTextView+Select.swift b/Sources/STTextView/STTextView+Select.swift index 3048014..ffddcb2 100644 --- a/Sources/STTextView/STTextView+Select.swift +++ b/Sources/STTextView/STTextView+Select.swift @@ -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) diff --git a/Sources/STTextView/STTextView.swift b/Sources/STTextView/STTextView.swift index 60b19ff..320a09b 100644 --- a/Sources/STTextView/STTextView.swift +++ b/Sources/STTextView/STTextView.swift @@ -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