update the UI

This commit is contained in:
2026-09-29 10:03:15 +02:00
parent 1d29d0952d
commit 2925127110
8 changed files with 219 additions and 62 deletions
+41 -9
View File
@@ -24,7 +24,7 @@ public struct TooBigNotice: Equatable {
}
/// Everything the editor keeps for one path, so a rename or a delete moves or drops it in one step.
/// It outlives the file being open, so switching back finds unsaved edits where they were.
/// Only the open file holds a buffer: opening a file asks about unsaved edits first, then reads disk.
struct FileState: Equatable {
/// The mode the file asked for; `presentation(for:)` resolves it against the file.
var mode: ViewMode?
@@ -339,13 +339,41 @@ public final class EditorStore {
// MARK: The open file
/// Opens a file in place of the current one: history, mode, side, a read from disk, and a line jump with a flash.
/// A changed file lands in Actual unless `showDiff`; `defaultMode` only covers a file without a stored mode.
public func open(_ path: String, _ options: OpenOptions = OpenOptions()) {
/// Opens a file in place of the current one, always reading it fresh from disk. Unsaved edits in the open
/// file (the same path too) are auto-saved, or ask save, discard or cancel; `accepted` runs once the open
/// goes ahead, at once or after the save. Returns true when it went ahead at once.
@discardableResult
public func open(_ path: String, _ options: OpenOptions = OpenOptions(),
accepted: (@MainActor () -> Void)? = nil) -> Bool {
if FileType.tooBigNames.contains(PathUtils.basename(path)) {
tooBig = TooBigNotice(path: path)
return
accepted?()
return true
}
if let active = activePath, isDirty(active) {
let silent = editorConfig.autoSave && !isDeleted(active)
switch silent ? .save : confirmUnsavedClose?(active) ?? .cancel {
case .cancel:
return false
case .discard:
dropBuffer(active)
case .save:
track { store in
do { try await store.save(path: active, announce: !silent) } catch { return }
store.show(path, options)
accepted?()
}
return false
}
}
show(path, options)
accepted?()
return true
}
/// History, mode, side, a read from disk that wins over any buffer, and a line jump with a flash.
/// A changed file lands in Actual unless `showDiff`; `defaultMode` only covers a file without a stored mode.
private func show(_ path: String, _ options: OpenOptions) {
history = RecentLists.pushing(path, onto: history)
activePath = path
selection = nil
@@ -360,19 +388,23 @@ public final class EditorStore {
flashLine(path: path, line: line)
}
if options.showDiff && changed { setMode(.diff, for: path) }
track { await $0.load(path, discardEdits: false) }
track { await $0.load(path, discardEdits: true) }
persistSoon()
}
/// Drops a file's unsaved edits without asking and, when it is the open file, shows the empty state.
/// `requestCloseActive` asks first.
public func close(_ path: String) {
private func dropBuffer(_ path: String) {
cancelAutoSave(path)
if states[path]?.buffer != nil {
states[path]?.buffer = nil
bumpRevision(path)
}
if states[path]?.isDeleted == true { states[path] = nil }
}
/// Drops a file's unsaved edits without asking and, when it is the open file, shows the empty state.
/// `requestCloseActive` asks first.
public func close(_ path: String) {
dropBuffer(path)
if cursor?.path == path { cursor = nil }
if selection?.path == path { selection = nil; selectedCode = nil }
if activePath == path { activePath = nil }