Skip to content

Commit

Permalink
Use System Cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
thecoolwinter committed Jun 16, 2024
1 parent cf85789 commit de0e38d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "94cf62b3ba8d4bed62680a282d4c25f9c63c2efb",
"version" : "1.1.0"
"revision" : "ee97538f5b81ae89698fd95938896dec5217b148",
"version" : "1.1.1"
}
},
{
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "94cf62b3ba8d4bed62680a282d4c25f9c63c2efb",
"version" : "1.1.0"
"revision" : "ee97538f5b81ae89698fd95938896dec5217b148",
"version" : "1.1.1"
}
},
{
Expand Down
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 @@ -276,6 +291,10 @@ public struct CodeEditSourceEditor: NSViewControllerRepresentable {
controller.letterSpacing = letterSpacing
}

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

controller.bracketPairHighlight = bracketPairHighlight

controller.reloadUI()
Expand All @@ -296,7 +315,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

0 comments on commit de0e38d

Please sign in to comment.