Skip to content

Commit

Permalink
Optionally Use System Cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
thecoolwinter committed Feb 9, 2024
1 parent cf4ee3b commit f58a7c7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class TextSelectionManager: NSObject {

public class TextSelection: Hashable, Equatable {
public var range: NSRange
weak var view: CursorView?
weak var view: NSView?
var boundingRect: CGRect = .zero
var suggestedXPos: CGFloat?
/// The position this selection should 'rotate' around when modifying selections.
Expand Down Expand Up @@ -71,12 +71,17 @@ public class TextSelectionManager: NSObject {

public var insertionPointColor: NSColor = NSColor.labelColor {
didSet {
textSelections.forEach { $0.view?.color = insertionPointColor }
textSelections.compactMap({ $0.view as? CursorView }).forEach { $0.color = insertionPointColor }
}
}
public var highlightSelectedLine: Bool = true
public var selectedLineBackgroundColor: NSColor = NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
public var selectionBackgroundColor: NSColor = NSColor.selectedTextBackgroundColor
public var useSystemCursor: Bool = false {
didSet {
updateSelectionViews()
}
}

internal(set) public var textSelections: [TextSelection] = []
weak var layoutManager: TextLayoutManager?
Expand Down Expand Up @@ -174,15 +179,26 @@ public class TextSelectionManager: NSObject {
textSelection.view?.removeFromSuperview()
textSelection.view = nil

let cursorView = CursorView(color: insertionPointColor)
let cursorView: NSView

if useSystemCursor, #available(macOS 14.0, *) {
let systemCursorView = NSTextInsertionIndicator(frame: .zero)
cursorView = systemCursorView
systemCursorView.displayMode = .visible
} else {
let internalCursorView = CursorView(color: insertionPointColor)
cursorView = internalCursorView
cursorTimer.register(internalCursorView)
}

cursorView.frame.origin = cursorOrigin
cursorView.frame.size.height = layoutManager?.estimateLineHeight() ?? 0

textView?.addSubview(cursorView)

textSelection.view = cursorView
textSelection.boundingRect = cursorView.frame

cursorTimer.register(cursorView)

didUpdate = true
}
} else if !textSelection.range.isEmpty && textSelection.view != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ extension TextView {
NotificationCenter.default.post(name: Self.textWillChangeNotification, object: self)
layoutManager.beginTransaction()
textStorage.beginEditing()
// Can't insert an ssempty string into an empty range. One must be not empty
_undoManager?.beginGrouping()

// Can't insert an empty string into an empty range. One must be not empty
for range in ranges.sorted(by: { $0.location > $1.location }) where
(delegate?.textView(self, shouldReplaceContentsIn: range, with: string) ?? true)
&& (!range.isEmpty || !string.isEmpty) {
Expand All @@ -38,6 +40,8 @@ extension TextView {

delegate?.textView(self, didReplaceContentsIn: range, with: string)
}

_undoManager?.endGrouping()
layoutManager.endTransaction()
textStorage.endEditing()
selectionManager.notifyAfterEdit()
Expand Down
18 changes: 17 additions & 1 deletion Sources/CodeEditTextView/TextView/TextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@ public class TextView: NSView, NSTextContent {
layoutManager.lineBreakStrategy = newValue
}
}

/// Determines if the text view uses the macOS system cursor or a ``CursorView`` for cursors.
///
/// - Important: Only available after macOS 14.
public var useSystemCursor: Bool {
get {
selectionManager?.useSystemCursor ?? false
}
set {
guard #available(macOS 14, *) else {
logger.warning("useSystemCursor only available after macOS 14.")
return
}
selectionManager?.useSystemCursor = newValue
}
}

open var contentType: NSTextContentType?

Expand All @@ -203,7 +219,7 @@ public class TextView: NSView, NSTextContent {
(" " as NSString).size(withAttributes: [.font: font]).width
}

var _undoManager: CEUndoManager?
internal(set) public var _undoManager: CEUndoManager?
@objc dynamic open var allowsUndo: Bool

var scrollView: NSScrollView? {
Expand Down

0 comments on commit f58a7c7

Please sign in to comment.