From 3fe867045b6dd356106340d9afc7a96b1812f912 Mon Sep 17 00:00:00 2001 From: Khan Winter <35942988+thecoolwinter@users.noreply.github.com> Date: Thu, 13 Jun 2024 14:54:07 -0500 Subject: [PATCH] Start Page Up/Down Methods --- .../TextLayoutManager/TextLayoutManager.swift | 4 ++-- .../SelectionManipulation+Horizontal.swift | 4 +++- .../SelectionManipulation+Vertical.swift | 8 ++++---- .../TextSelectionManager.swift | 3 +-- .../TextView/TextView+Move.swift | 20 +++++++++++++++++++ 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/Sources/CodeEditTextView/TextLayoutManager/TextLayoutManager.swift b/Sources/CodeEditTextView/TextLayoutManager/TextLayoutManager.swift index d6e9583d..d713620a 100644 --- a/Sources/CodeEditTextView/TextLayoutManager/TextLayoutManager.swift +++ b/Sources/CodeEditTextView/TextLayoutManager/TextLayoutManager.swift @@ -299,8 +299,8 @@ public class TextLayoutManager: NSObject { var height: CGFloat = 0 var width: CGFloat = 0 - var relativeMinY = max(layoutData.minY - position.yPos, 0) - var relativeMaxY = max(layoutData.maxY - position.yPos, relativeMinY) + let relativeMinY = max(layoutData.minY - position.yPos, 0) + let relativeMaxY = max(layoutData.maxY - position.yPos, relativeMinY) for lineFragmentPosition in line.typesetter.lineFragments.linesStartingAt( relativeMinY, diff --git a/Sources/CodeEditTextView/TextSelectionManager/SelectionManipulation/SelectionManipulation+Horizontal.swift b/Sources/CodeEditTextView/TextSelectionManager/SelectionManipulation/SelectionManipulation+Horizontal.swift index c1165d28..73269247 100644 --- a/Sources/CodeEditTextView/TextSelectionManager/SelectionManipulation/SelectionManipulation+Horizontal.swift +++ b/Sources/CodeEditTextView/TextSelectionManager/SelectionManipulation/SelectionManipulation+Horizontal.swift @@ -36,7 +36,7 @@ package extension TextSelectionManager { ) case .word: return extendSelectionWord(string: string, from: offset, delta: delta) - case .line, .container: + case .line: return extendSelectionLine(string: string, from: offset, delta: delta) case .visualLine: return extendSelectionVisualLine(string: string, from: offset, delta: delta) @@ -46,6 +46,8 @@ package extension TextSelectionManager { } else { return NSRange(location: 0, length: offset) } + case .page: // Not a valid destination horizontally. + return NSRange(location: offset, length: 0) } } diff --git a/Sources/CodeEditTextView/TextSelectionManager/SelectionManipulation/SelectionManipulation+Vertical.swift b/Sources/CodeEditTextView/TextSelectionManager/SelectionManipulation/SelectionManipulation+Vertical.swift index ea8417bf..5c4c54e9 100644 --- a/Sources/CodeEditTextView/TextSelectionManager/SelectionManipulation/SelectionManipulation+Vertical.swift +++ b/Sources/CodeEditTextView/TextSelectionManager/SelectionManipulation/SelectionManipulation+Vertical.swift @@ -36,8 +36,8 @@ package extension TextSelectionManager { return extendSelectionVerticalCharacter(from: offset, up: up, suggestedXPos: suggestedXPos) case .word, .line, .visualLine: return extendSelectionVerticalLine(from: offset, up: up) - case .container: - return extendSelectionContainer(from: offset, delta: up ? 1 : -1) + case .page: + return extendSelectionPage(from: offset, delta: up ? 1 : -1) case .document: if up { return NSRange(location: 0, length: offset) @@ -115,12 +115,12 @@ package extension TextSelectionManager { } } - /// Extends a selection one "container" long. + /// Extends a selection one "page" long. /// - Parameters: /// - offset: The location to start extending the selection from. /// - delta: The direction the selection should be extended. `1` for forwards, `-1` for backwards. /// - Returns: The range of the extended selection. - private func extendSelectionContainer(from offset: Int, delta: Int) -> NSRange { + private func extendSelectionPage(from offset: Int, delta: Int) -> NSRange { guard let textView, let endOffset = layoutManager?.textOffsetAtPoint( CGPoint( x: delta > 0 ? textView.frame.maxX : textView.frame.minX, diff --git a/Sources/CodeEditTextView/TextSelectionManager/TextSelectionManager.swift b/Sources/CodeEditTextView/TextSelectionManager/TextSelectionManager.swift index bc7b3955..889ab818 100644 --- a/Sources/CodeEditTextView/TextSelectionManager/TextSelectionManager.swift +++ b/Sources/CodeEditTextView/TextSelectionManager/TextSelectionManager.swift @@ -52,8 +52,7 @@ public class TextSelectionManager: NSObject { case word case line case visualLine - /// Eg: Bottom of screen - case container + case page case document } diff --git a/Sources/CodeEditTextView/TextView/TextView+Move.swift b/Sources/CodeEditTextView/TextView/TextView+Move.swift index 849b4f47..d5b83100 100644 --- a/Sources/CodeEditTextView/TextView/TextView+Move.swift +++ b/Sources/CodeEditTextView/TextView/TextView+Move.swift @@ -158,4 +158,24 @@ extension TextView { selectionManager.moveSelections(direction: .down, destination: .document, modifySelection: true) updateAfterMove() } + + override public func pageUp(_ sender: Any?) { + selectionManager.moveSelections(direction: .up, destination: .page) + updateAfterMove() + } + + override public func pageUpAndModifySelection(_ sender: Any?) { + selectionManager.moveSelections(direction: .up, destination: .page, modifySelection: true) + updateAfterMove() + } + + override public func pageDown(_ sender: Any?) { + selectionManager.moveSelections(direction: .down, destination: .page) + updateAfterMove() + } + + override public func pageDownAndModifySelection(_ sender: Any?) { + selectionManager.moveSelections(direction: .down, destination: .page, modifySelection: true) + updateAfterMove() + } }