Skip to content

Commit

Permalink
Start Page Up/Down Methods
Browse files Browse the repository at this point in the history
  • Loading branch information
thecoolwinter committed Jun 13, 2024
1 parent 80911be commit 3fe8670
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public class TextSelectionManager: NSObject {
case word
case line
case visualLine
/// Eg: Bottom of screen
case container
case page
case document
}

Expand Down
20 changes: 20 additions & 0 deletions Sources/CodeEditTextView/TextView/TextView+Move.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit 3fe8670

Please sign in to comment.