Skip to content

Commit e439425

Browse files
committed
Completely Optional State Variables
1 parent 8046a17 commit e439425

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

Package.resolved

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/CodeEditSourceEditor/SourceEditor/SourceEditor.swift

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public struct SourceEditor: NSViewControllerRepresentable {
9090
string: "",
9191
language: language,
9292
configuration: configuration,
93-
cursorPositions: state.cursorPositions,
93+
cursorPositions: state.cursorPositions ?? [],
9494
highlightProviders: context.coordinator.highlightProviders,
9595
undoManager: undoManager,
9696
coordinators: coordinators
@@ -104,8 +104,8 @@ public struct SourceEditor: NSViewControllerRepresentable {
104104
if controller.textView == nil {
105105
controller.loadView()
106106
}
107-
if !state.cursorPositions.isEmpty {
108-
controller.setCursorPositions(state.cursorPositions)
107+
if !(state.cursorPositions?.isEmpty ?? true) {
108+
controller.setCursorPositions(state.cursorPositions ?? [])
109109
}
110110

111111
context.coordinator.setController(controller)
@@ -124,7 +124,9 @@ public struct SourceEditor: NSViewControllerRepresentable {
124124
context.coordinator.isUpdateFromTextView = false
125125
} else {
126126
context.coordinator.isUpdatingFromRepresentable = true
127-
controller.setCursorPositions(state.cursorPositions)
127+
if let cursorPositions = state.cursorPositions {
128+
controller.setCursorPositions(cursorPositions)
129+
}
128130

129131
if let scrollPosition = state.scrollPosition {
130132
controller.scrollView.scroll(controller.scrollView.contentView, to: scrollPosition)
@@ -136,11 +138,12 @@ public struct SourceEditor: NSViewControllerRepresentable {
136138
controller.findViewController?.viewModel.findText = findText
137139
}
138140

139-
if let findController = controller.findViewController,
140-
findController.viewModel.isShowingFindPanel != state.findPanelVisible {
141+
if let findPanelVisible = state.findPanelVisible,
142+
let findController = controller.findViewController,
143+
findController.viewModel.isShowingFindPanel != findPanelVisible {
141144
// Needs to be on the next runloop, not many great ways to do this besides a dispatch...
142145
DispatchQueue.main.async {
143-
if state.findPanelVisible {
146+
if findPanelVisible {
144147
findController.showFindPanel()
145148
} else {
146149
findController.hideFindPanel()

Sources/CodeEditSourceEditor/SourceEditor/SourceEditorState.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
import AppKit
99

1010
public struct SourceEditorState: Equatable, Hashable, Sendable, Codable {
11-
public var cursorPositions: [CursorPosition] = []
11+
public var cursorPositions: [CursorPosition]?
1212
public var scrollPosition: CGPoint?
1313
public var findText: String?
14-
public var findPanelVisible: Bool = false
14+
public var findPanelVisible: Bool?
1515

1616
public init(
1717
cursorPositions: [CursorPosition],
1818
scrollPosition: CGPoint? = nil,
1919
findText: String? = nil,
20-
findPanelVisible: Bool = false
20+
findPanelVisible: Bool? = nil
2121
) {
2222
self.cursorPositions = cursorPositions
2323
self.scrollPosition = scrollPosition

0 commit comments

Comments
 (0)