-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathWorkspaceDocument.swift
287 lines (250 loc) Β· 10.4 KB
/
WorkspaceDocument.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//
// WorkspaceDocument.swift
// CodeEdit
//
// Created by Pavel Kasila on 17.03.22.
//
import AppKit
import SwiftUI
import Combine
import Foundation
import LanguageServerProtocol
@objc(WorkspaceDocument)
final class WorkspaceDocument: NSDocument, ObservableObject, NSToolbarDelegate {
@Published var sortFoldersOnTop: Bool = true
/// A string used to filter the displayed files and folders in the project navigator area based on user input.
@Published var navigatorFilter: String = ""
private var workspaceState: [String: Any] {
get {
let key = "workspaceState-\(self.fileURL?.absoluteString ?? "")"
return UserDefaults.standard.object(forKey: key) as? [String: Any] ?? [:]
}
set {
let key = "workspaceState-\(self.fileURL?.absoluteString ?? "")"
UserDefaults.standard.set(newValue, forKey: key)
}
}
var workspaceFileManager: CEWorkspaceFileManager?
var editorManager: EditorManager? = EditorManager()
var statusBarViewModel: StatusBarViewModel? = StatusBarViewModel()
var utilityAreaModel: UtilityAreaViewModel? = UtilityAreaViewModel()
var issueNavigatorViewModel: IssueNavigatorViewModel? = IssueNavigatorViewModel()
var searchState: SearchState?
var openQuicklyViewModel: OpenQuicklyViewModel?
var commandsPaletteState: QuickActionsViewModel?
var listenerModel: WorkspaceNotificationModel = .init()
var sourceControlManager: SourceControlManager?
var taskManager: TaskManager?
var workspaceSettingsManager: CEWorkspaceSettings?
var taskNotificationHandler: TaskNotificationHandler = TaskNotificationHandler()
@Published var notificationPanel = NotificationPanelViewModel()
private var cancellables = Set<AnyCancellable>()
override init() {
super.init()
// Observe changes to notification panel
notificationPanel.objectWillChange
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
self?.objectWillChange.send()
}
.store(in: &cancellables)
}
deinit {
cancellables.forEach { $0.cancel() }
NotificationCenter.default.removeObserver(self)
}
func getFromWorkspaceState(_ key: WorkspaceStateKey) -> Any? {
return workspaceState[key.rawValue]
}
func addToWorkspaceState(key: WorkspaceStateKey, value: Any?) {
if let value {
workspaceState.updateValue(value, forKey: key.rawValue)
} else {
workspaceState.removeValue(forKey: key.rawValue)
}
}
// MARK: NSDocument
private let ignoredFilesAndDirectory = [
".DS_Store"
]
override static var autosavesInPlace: Bool {
false
}
override var isDocumentEdited: Bool {
false
}
override func makeWindowControllers() {
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 1400, height: 900),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered,
defer: false
)
// Note For anyone hoping to switch back to a Root-SwiftUI window:
// See Commit 0200c87 for more details and to see what was previously here.
// -----
// Setting the "min size" like this is hacky, but SwiftUI overrides the contentRect and
// any of the built-in window size functions & autosave stuff. So we have to set it like this.
// SwiftUI also ignores this value, so it just manages to set the initial window size. *Hopefully* this
// is fixed in the future.
// ----
let windowController = CodeEditWindowController(
window: window,
workspace: self
)
if let rectString = getFromWorkspaceState(.workspaceWindowSize) as? String {
window.setFrame(NSRectFromString(rectString), display: true, animate: false)
} else {
window.setFrame(NSRect(x: 0, y: 0, width: 1400, height: 900), display: true, animate: false)
window.center()
}
window.setAccessibilityIdentifier("workspace")
window.setAccessibilityDocument(self.fileURL?.absoluteString)
self.addWindowController(windowController)
window.makeKeyAndOrderFront(nil)
}
// MARK: Set Up Workspace
private func initWorkspaceState(_ url: URL) throws {
// Ensure the URL ends with a "/" to prevent certain URL(filePath:relativeTo) initializers from
// placing the file one directory above our workspace. This quick fix appends a "/" if needed.
var url = url
if !url.absoluteString.hasSuffix("/") {
url = URL(filePath: url.absoluteURL.path(percentEncoded: false) + "/")
}
self.fileURL = url
self.displayName = url.lastPathComponent
let sourceControlManager = SourceControlManager(
workspaceURL: url,
editorManager: editorManager!
)
self.workspaceFileManager = .init(
folderUrl: url,
ignoredFilesAndFolders: Set(ignoredFilesAndDirectory),
sourceControlManager: sourceControlManager
)
self.sourceControlManager = sourceControlManager
sourceControlManager.fileManager = workspaceFileManager
self.searchState = .init(self)
self.openQuicklyViewModel = .init(fileURL: url)
self.commandsPaletteState = .init()
self.workspaceSettingsManager = CEWorkspaceSettings(workspaceURL: url)
if let workspaceSettingsManager {
self.taskManager = TaskManager(
workspaceSettings: workspaceSettingsManager.settings,
workspaceURL: url
)
}
editorManager?.restoreFromState(self)
utilityAreaModel?.restoreFromState(self)
issueNavigatorViewModel?.initialize(projectName: displayName)
}
override func read(from url: URL, ofType typeName: String) throws {
try initWorkspaceState(url)
}
override func write(to url: URL, ofType typeName: String) throws {}
// MARK: Close Workspace
override func close() {
super.close()
editorManager?.saveRestorationState(self)
utilityAreaModel?.saveRestorationState(self)
cancellables.forEach({ $0.cancel() })
statusBarViewModel = nil
utilityAreaModel = nil
searchState = nil
editorManager = nil
openQuicklyViewModel = nil
commandsPaletteState = nil
sourceControlManager = nil
workspaceFileManager?.cleanUp()
workspaceFileManager = nil
workspaceSettingsManager?.cleanUp()
workspaceSettingsManager = nil
taskManager = nil
}
/// Determines the windows should be closed.
///
/// This method iterates all edited documents If there are any edited documents.
///
/// A panel giving the user the choice of canceling, discarding changes, or saving is presented while iteration.
///
/// If the user chooses cancel on the panel, iteration is broken.
///
/// In the last step, `shouldCloseSelector` is called with true if all documents are clean, otherwise false
///
/// - Parameters:
/// - windowController: The windowController may be closed.
/// - delegate: The object which is a target of `shouldCloseSelector`.
/// - shouldClose: The callback which receives result of this method.
/// - contextInfo: The additional info which is not used in this method.
override func shouldCloseWindowController(
_ windowController: NSWindowController,
delegate: Any?,
shouldClose shouldCloseSelector: Selector?,
contextInfo: UnsafeMutableRawPointer?
) {
guard let object = (delegate as? NSObject),
let shouldCloseSelector = shouldCloseSelector,
let contextInfo = contextInfo
else {
super.shouldCloseWindowController(
windowController,
delegate: delegate,
shouldClose: shouldCloseSelector,
contextInfo: contextInfo
)
return
}
// Save unsaved changes before closing
let editedCodeFiles = editorManager?.editorLayout
.gatherOpenFiles()
.compactMap(\.fileDocument)
.filter(\.isDocumentEdited) ?? []
for editedCodeFile in editedCodeFiles {
let shouldClose = UnsafeMutablePointer<Bool>.allocate(capacity: 1)
shouldClose.initialize(to: true)
defer {
_ = shouldClose.move()
shouldClose.deallocate()
}
// Present a panel giving the user the choice of canceling, discarding changes, or saving.
editedCodeFile.canClose(
withDelegate: self,
shouldClose: #selector(document(_:shouldClose:contextInfo:)),
contextInfo: shouldClose
)
// pointee becomes false when user select cancel
guard shouldClose.pointee else {
break
}
}
// Invoke shouldCloseSelector at delegate
let implementation = object.method(for: shouldCloseSelector)
let function = unsafeBitCast(
implementation,
to: (@convention(c)(Any, Selector, Any, Bool, UnsafeMutableRawPointer?) -> Void).self
)
let areAllOpenedCodeFilesClean = editorManager?.editorLayout.gatherOpenFiles()
.compactMap(\.fileDocument)
.allSatisfy { !$0.isDocumentEdited } ?? false
function(object, shouldCloseSelector, self, areAllOpenedCodeFilesClean, contextInfo)
}
// MARK: NSDocument delegate
/// Receives result of `canClose` and then, set `shouldClose` to `contextInfo`'s `pointee`.
///
/// - Parameters:
/// - document: The document may be closed.
/// - shouldClose: The result of user selection.
/// `shouldClose` becomes false if the user selects cancel, otherwise true.
/// - contextInfo: The additional info which will be set `shouldClose`.
/// `contextInfo` must be `UnsafeMutablePointer<Bool>`.
@objc
func document(
_ document: NSDocument,
shouldClose: Bool,
contextInfo: UnsafeMutableRawPointer
) {
let opaquePtr = OpaquePointer(contextInfo)
let mutablePointer = UnsafeMutablePointer<Bool>(opaquePtr)
mutablePointer.pointee = shouldClose
}
}