Skip to content

Commit 6f73a2f

Browse files
authored
Merge branch 'mac' into scrolling
2 parents 48bdfee + f507bf9 commit 6f73a2f

24 files changed

Lines changed: 618 additions & 486 deletions

Sources/Runestone/TextView/Navigation/GoToLineSelection.swift renamed to Sources/Runestone/TextView/Core/GoToLineSelection.swift

File renamed without changes.

Sources/Runestone/TextView/Core/LayoutManager.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ final class LayoutManager {
8585
var lineHeightMultiplier: CGFloat = 1
8686
var constrainingLineWidth: CGFloat {
8787
if isLineWrappingEnabled {
88-
return scrollViewWidth - textContainerInset.left - textContainerInset.right - safeAreaInsets.left - safeAreaInsets.right
88+
return scrollViewWidth
89+
- gutterWidthService.gutterWidth
90+
- textContainerInset.left - textContainerInset.right
91+
- safeAreaInsets.left - safeAreaInsets.right
8992
} else {
9093
// Rendering multiple very long lines is very expensive. In order to let the editor remain useable,
9194
// we set a very high maximum line width when line wrapping is disabled.

Sources/Runestone/TextView/Core/LineNavigationService.swift

Lines changed: 0 additions & 121 deletions
This file was deleted.

Sources/Runestone/TextView/Core/Mac/SelectionService.swift

Lines changed: 0 additions & 45 deletions
This file was deleted.

Sources/Runestone/TextView/Core/Mac/TextView_Mac+NSTextInputClient.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ import AppKit
44
extension TextView: NSTextInputClient {
55
// swiftlint:disable:next prohibited_super_call
66
override public func doCommand(by selector: Selector) {
7-
#if DEBUG
8-
print(NSStringFromSelector(selector))
9-
#endif
7+
// #if DEBUG
8+
// print(NSStringFromSelector(selector))
9+
// #endif
1010
super.doCommand(by: selector)
1111
}
1212

1313
public func insertText(_ string: Any, replacementRange: NSRange) {
1414
guard let string = string as? String else {
1515
return
1616
}
17-
textViewController.selectionService.resetPreviouslySelectedRange()
1817
let range = replacementRange.location == NSNotFound ? textViewController.rangeForInsertingText : replacementRange
1918
if textViewController.shouldChangeText(in: range, replacementText: string) {
2019
textViewController.replaceText(in: range, with: string)

Sources/Runestone/TextView/Core/Mac/TextView_Mac.swift

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ open class TextView: NSView {
497497
textViewController.layoutIfNeeded()
498498
textViewController.handleContentSizeUpdateIfNeeded()
499499
updateCaretFrame()
500+
updateSelectedRectangles()
500501
}
501502

502503
override public func layoutSubtreeIfNeeded() {
@@ -517,6 +518,33 @@ open class TextView: NSView {
517518
}
518519
}
519520

521+
override public func mouseDown(with event: NSEvent) {
522+
super.mouseDown(with: event)
523+
if let location = locationClosestToPoint(in: event) {
524+
textViewController.move(to: location)
525+
textViewController.startDraggingSelection(from: location)
526+
}
527+
}
528+
529+
override public func mouseDragged(with event: NSEvent) {
530+
super.mouseDragged(with: event)
531+
if let location = locationClosestToPoint(in: event) {
532+
textViewController.extendDraggedSelection(to: location)
533+
}
534+
}
535+
536+
override public func mouseUp(with event: NSEvent) {
537+
super.mouseUp(with: event)
538+
if let location = locationClosestToPoint(in: event) {
539+
textViewController.extendDraggedSelection(to: location)
540+
}
541+
}
542+
543+
override public func resetCursorRects() {
544+
super.resetCursorRects()
545+
addCursorRect(bounds, cursor: .iBeam)
546+
}
547+
520548
/// Sets the current _state_ of the editor. The state contains the text to be displayed by the editor and
521549
/// various additional information about the text that the editor needs to show the text.
522550
///
@@ -538,7 +566,6 @@ public extension TextView {
538566
guard var selectedRange = textViewController.markedRange ?? textViewController.selectedRange?.nonNegativeLength else {
539567
return
540568
}
541-
textViewController.selectionService.resetPreviouslySelectedRange()
542569
if selectedRange.length == 0 {
543570
selectedRange.location -= 1
544571
selectedRange.length = 1
@@ -704,11 +731,6 @@ public extension TextView {
704731
textViewController.moveToEndOfDocumentAndModifySelection()
705732
}
706733

707-
override func mouseDown(with event: NSEvent) {
708-
let point = scrollContentView.convert(event.locationInWindow, from: nil)
709-
textViewController.moveToLocation(closestTo: point)
710-
}
711-
712734
/// Copy the selected text.
713735
///
714736
/// - Parameter sender: The object calling this method.
@@ -726,7 +748,6 @@ public extension TextView {
726748
@objc func paste(_ sender: Any?) {
727749
let selectedRange = selectedRange()
728750
if let string = NSPasteboard.general.string(forType: .string) {
729-
print(string)
730751
let preparedText = textViewController.prepareTextForInsertion(string)
731752
textViewController.replaceText(in: selectedRange, with: preparedText)
732753
}
@@ -881,6 +902,15 @@ private extension TextView {
881902
}
882903
}
883904

905+
// MARK: - Location
906+
private extension TextView {
907+
private func locationClosestToPoint(in event: NSEvent) -> Int? {
908+
let point = scrollContentView.convert(event.locationInWindow, from: nil)
909+
let adjustedPoint = CGPoint(x: point.x - gutterWidth - textContainerInset.left, y: point.y)
910+
return textViewController.layoutManager.closestIndex(to: adjustedPoint)
911+
}
912+
}
913+
884914
// MARK: - TextViewControllerDelegate
885915
extension TextView: TextViewControllerDelegate {
886916
func textViewControllerDidChangeText(_ textViewController: TextViewController) {

0 commit comments

Comments
 (0)