Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct ContentView: View {
@State private var font: NSFont = NSFont.monospacedSystemFont(ofSize: 12, weight: .regular)
@AppStorage("wrapLines") private var wrapLines: Bool = true
@State private var cursorPositions: [CursorPosition] = []
@AppStorage("systemCursor") private var useSystemCursor: Bool = false

init(document: Binding<CodeEditSourceEditorExampleDocument>, fileURL: URL?) {
self._document = document
Expand All @@ -32,6 +33,13 @@ struct ContentView: View {
LanguagePicker(language: $language)
.frame(maxWidth: 100)
Toggle("Wrap Lines", isOn: $wrapLines)
if #available(macOS 14, *) {
Toggle("Use System Cursor", isOn: $useSystemCursor)
} else {
Toggle("Use System Cursor", isOn: $useSystemCursor)
.disabled(true)
.help("macOS 14 required")
}
Spacer()
Text(getLabel(cursorPositions))
}
Expand All @@ -47,7 +55,8 @@ struct ContentView: View {
tabWidth: 4,
lineHeight: 1.2,
wrapLines: wrapLines,
cursorPositions: $cursorPositions
cursorPositions: $cursorPositions,
useSystemCursor: useSystemCursor
)
}
.onAppear {
Expand Down
4 changes: 2 additions & 2 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
/// character's width between characters, etc. Defaults to `1.0`
/// - bracketPairHighlight: The type of highlight to use to highlight bracket pairs.
/// See `BracketPairHighlight` for more information. Defaults to `nil`
/// - useSystemCursor: If true, uses the system cursor on `>=macOS 14`.
/// - undoManager: The undo manager for the text view. Defaults to `nil`, which will create a new CEUndoManager
/// - coordinators: Any text coordinators for the view to use. See ``TextViewCoordinator`` for more information.
public init(
Expand All @@ -62,6 +63,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
isSelectable: Bool = true,
letterSpacing: Double = 1.0,
bracketPairHighlight: BracketPairHighlight? = nil,
useSystemCursor: Bool = true,
undoManager: CEUndoManager? = nil,
coordinators: [any TextViewCoordinator] = []
) {
Expand All @@ -82,6 +84,11 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
self.isSelectable = isSelectable
self.letterSpacing = letterSpacing
self.bracketPairHighlight = bracketPairHighlight
if #available(macOS 14, *) {
self.useSystemCursor = useSystemCursor
} else {
self.useSystemCursor = false
}
self.undoManager = undoManager
self.coordinators = coordinators
}
Expand Down Expand Up @@ -131,6 +138,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
isSelectable: Bool = true,
letterSpacing: Double = 1.0,
bracketPairHighlight: BracketPairHighlight? = nil,
useSystemCursor: Bool = true,
undoManager: CEUndoManager? = nil,
coordinators: [any TextViewCoordinator] = []
) {
Expand All @@ -151,6 +159,11 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
self.isSelectable = isSelectable
self.letterSpacing = letterSpacing
self.bracketPairHighlight = bracketPairHighlight
if #available(macOS 14, *) {
self.useSystemCursor = useSystemCursor
} else {
self.useSystemCursor = false
}
self.undoManager = undoManager
self.coordinators = coordinators
}
Expand All @@ -172,6 +185,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
private var isSelectable: Bool
private var letterSpacing: Double
private var bracketPairHighlight: BracketPairHighlight?
private var useSystemCursor: Bool
private var undoManager: CEUndoManager?
package var coordinators: [any TextViewCoordinator]

Expand All @@ -195,6 +209,7 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
isEditable: isEditable,
isSelectable: isSelectable,
letterSpacing: letterSpacing,
useSystemCursor: useSystemCursor,
bracketPairHighlight: bracketPairHighlight,
undoManager: undoManager
)
Expand Down Expand Up @@ -238,6 +253,15 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
return
}

updateControllerParams(controller: controller)

controller.reloadUI()
return
}

/// Update the parameters of the controller.
/// - Parameter controller: The controller to update.
func updateControllerParams(controller: TextViewController) {
if controller.font != font {
controller.font = font
}
Expand Down Expand Up @@ -276,12 +300,16 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
controller.letterSpacing = letterSpacing
}

controller.bracketPairHighlight = bracketPairHighlight
if controller.useSystemCursor != useSystemCursor {
controller.useSystemCursor = useSystemCursor
}

controller.reloadUI()
return
controller.bracketPairHighlight = bracketPairHighlight
}

/// Checks if the controller needs updating.
/// - Parameter controller: The controller to check.
/// - Returns: True, if the controller's parameters should be updated.
func paramsAreEqual(controller: NSViewControllerType) -> Bool {
controller.font == font &&
controller.isEditable == isEditable &&
Expand All @@ -296,7 +324,8 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
controller.indentOption == indentOption &&
controller.tabWidth == tabWidth &&
controller.letterSpacing == letterSpacing &&
controller.bracketPairHighlight == bracketPairHighlight
controller.bracketPairHighlight == bracketPairHighlight &&
controller.useSystemCursor == useSystemCursor
}
}

Expand Down
21 changes: 21 additions & 0 deletions Sources/CodeEditSourceEditor/Controller/TextViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ public class TextViewController: NSViewController {
}
}

/// If true, uses the system cursor on macOS 14 or greater.
public var useSystemCursor: Bool {
get {
textView.useSystemCursor
}
set {
if #available(macOS 14, *) {
textView.useSystemCursor = newValue
}
}
}

var highlighter: Highlighter?

/// The tree sitter client managed by the source editor.
Expand Down Expand Up @@ -198,6 +210,7 @@ public class TextViewController: NSViewController {
isEditable: Bool,
isSelectable: Bool,
letterSpacing: Double,
useSystemCursor: Bool,
bracketPairHighlight: BracketPairHighlight?,
undoManager: CEUndoManager? = nil
) {
Expand All @@ -221,6 +234,13 @@ public class TextViewController: NSViewController {

super.init(nibName: nil, bundle: nil)

let platformGuardedSystemCursor: Bool
if #available(macOS 14, *) {
platformGuardedSystemCursor = useSystemCursor
} else {
platformGuardedSystemCursor = false
}

self.textView = TextView(
string: string,
font: font,
Expand All @@ -230,6 +250,7 @@ public class TextViewController: NSViewController {
isEditable: isEditable,
isSelectable: isSelectable,
letterSpacing: letterSpacing,
useSystemCursor: platformGuardedSystemCursor,
delegate: self
)
}
Expand Down
1 change: 1 addition & 0 deletions Tests/CodeEditSourceEditorTests/TagEditingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ final class TagEditingTests: XCTestCase {
isEditable: true,
isSelectable: true,
letterSpacing: 1.0,
useSystemCursor: false,
bracketPairHighlight: .flash
)
let tsClient = TreeSitterClient(executor: .init(forceSync: true))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ final class TextViewControllerTests: XCTestCase {
isEditable: true,
isSelectable: true,
letterSpacing: 1.0,
useSystemCursor: false,
bracketPairHighlight: .flash
)

Expand Down