mirror of
https://github.com/coteditor/CotEditor.git
synced 2024-09-11 19:27:30 +03:00
Restore last opened documents in directory document
This commit is contained in:
parent
9add81cb6a
commit
4f838b2b2d
@ -26,9 +26,16 @@
|
||||
import AppKit
|
||||
import Observation
|
||||
import UniformTypeIdentifiers
|
||||
import OSLog
|
||||
|
||||
@Observable final class DirectoryDocument: NSDocument {
|
||||
|
||||
private enum SerializationKey {
|
||||
|
||||
static let documents = "documents"
|
||||
}
|
||||
|
||||
|
||||
// MARK: Public Properties
|
||||
|
||||
private(set) var fileNode: FileNode?
|
||||
@ -56,6 +63,35 @@ import UniformTypeIdentifiers
|
||||
}
|
||||
|
||||
|
||||
override func encodeRestorableState(with coder: NSCoder, backgroundQueue queue: OperationQueue) {
|
||||
|
||||
super.encodeRestorableState(with: coder, backgroundQueue: queue)
|
||||
|
||||
let fileData = self.documents
|
||||
.compactMap(\.fileURL)
|
||||
.compactMap { try? $0.bookmarkData(options: .withSecurityScope) }
|
||||
coder.encode(fileData, forKey: SerializationKey.documents)
|
||||
}
|
||||
|
||||
|
||||
override func restoreState(with coder: NSCoder) {
|
||||
|
||||
super.restoreState(with: coder)
|
||||
|
||||
if let fileData = coder.decodeArrayOfObjects(ofClass: NSData.self, forKey: SerializationKey.documents) as? [Data] {
|
||||
let fileURLs = fileData.compactMap {
|
||||
var isStale = false
|
||||
return try? URL(resolvingBookmarkData: $0, options: .withSecurityScope, bookmarkDataIsStale: &isStale)
|
||||
}
|
||||
Task {
|
||||
for fileURL in fileURLs {
|
||||
await self.openDocument(at: fileURL)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
override func makeWindowControllers() {
|
||||
|
||||
let document = Document()
|
||||
@ -237,6 +273,7 @@ import UniformTypeIdentifiers
|
||||
document.close()
|
||||
self.documents.removeFirst(document)
|
||||
}
|
||||
self.invalidateRestorableState()
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user