import Foundation import HelderCore import HelderServices extension WorkspaceModel { /// Switches this window to another project folder and remembers it in the recents. /// Unsaved edits are asked about first; a cancel keeps the current project. public func openProject(_ url: URL) async { let url = url.standardizedFileURL overlay.dismiss() if url == root { return } guard await prepareToClose() else { return } await closeProject() root = url attach(url) await loadProject() isReady = true } /// Asks about every unsaved file, then writes the session. /// False when the user cancelled: the window or project must stay. public func prepareToClose() async -> Bool { if let editor, !(await editor.resolveUnsaved()) { return false } await editor?.persist() return true } /// The window is closing: the unsaved prompt already ran, so this only winds the project down. public func windowWillClose() { guard lifecycleTask == nil else { return } lifecycleTask = Task { [weak self] in await self?.closeProject() self?.lifecycleTask = nil } } /// Runs a project switch outside `work`, since the switch waits for `work` to drain. func switchProject(_ operation: @escaping @MainActor () async -> Void) { guard lifecycleTask == nil else { return } lifecycleTask = Task { [weak self] in await operation() self?.lifecycleTask = nil } } func attach(_ root: URL) { generation += 1 let services = app.makeWorkspace(root) self.services = services name = ProjectLocator.name(of: root) title = ProjectLocator.title(of: root) rowFrames = [:] let notify: (String, String?) -> Void = { [weak self] title, detail in self?.toasts.show(title, detail: detail) } let sourceControl = SourceControlStore(git: services.git) sourceControl.notify = notify self.sourceControl = sourceControl let explorer = ExplorerStore(files: services.files, system: app.system) explorer.notify = notify self.explorer = explorer let editor = EditorStore(root: root.path, files: services.files, sourceControl: sourceControl, session: app.session, recentFiles: services.recentFiles) wire(editor, symbols: services.symbols) self.editor = editor cleanCopy = CleanCopyStore(files: services.files) terminal = TerminalStore(projectRoot: root.path) wireTerminal() launcher = LauncherList(recents: recentProjects, excluding: root.path) } private func wire(_ editor: EditorStore, symbols: SymbolServicing) { editor.presentOverlay = { [weak self] in self?.overlay.present($0) } editor.dismissDiffOverlay = { [weak self] in guard let self, case .diff = self.overlay.current else { return } self.overlay.dismiss() } editor.showToast = { [weak self] title, detail in self?.toasts.show(title, detail: detail) } editor.fileSaved = { [weak self, weak editor] _ in guard let self, self.editor === editor else { return } self.scheduleRefresh(.git) } editor.confirmUnsavedClose = { [weak self] path in self?.app.system.confirmUnsavedClose(path) ?? .cancel } editor.symbols = symbols } func loadProject() async { guard let root, let services else { return } let current = generation layout.restore(root: root.path) config = await services.config.resolve() guard generation == current else { return } applyConfig() await app.recentProjects.add(root.path) recentProjects = await app.recentProjects.list() guard generation == current else { return } launcher = LauncherList(recents: recentProjects, excluding: root.path) let sourceControl = sourceControl, explorer = explorer, editor = editor, cleanCopy = cleanCopy async let git: Void = sourceControl?.refresh() ?? () async let tree: Void = explorer?.reload() ?? () async let baseline: Void = cleanCopy?.loadBaseline() ?? () _ = await (git, tree, baseline) await editor?.restore(restoreSession: config.session.restoreOnLaunch) guard generation == current else { return } startWatcher(root) sourceControl?.startPolling() scheduleSymbolLoad(after: timings.symbolsAfterLaunch) } /// Stops the watchers and polling, waits for the work still running against this project, /// then stops reading aloud, saves the session and ends the terminals. func closeProject() async { watcher?.stop() watcher = nil sourceControl?.stopPolling() pendingRefresh = [] let refresh = refreshTask, symbols = symbolsTask refreshTask = nil symbolsTask = nil refreshID = UUID() refresh?.cancel() symbols?.cancel() let running = Array(work.values) running.forEach { $0.cancel() } await refresh?.value await symbols?.value for task in running { await task.value } readAloud.stop() await editor?.flushAutoSaves() await editor?.persist() terminal.terminateAll() } /// Pushes the config into every store that reads it; runs on load and whenever `.helder/config.json` changes. func applyConfig() { terminal.config = config sourceControl?.config = config.git guard let editor else { return } if editor.editorConfig != config.editor { editor.editorConfig = config.editor } let mode = ViewMode(config.git.defaultDiffMode) if editor.defaultMode != mode { editor.defaultMode = mode } } func reloadConfig() async { guard let services else { return } let current = generation let resolved = await services.config.resolve() guard generation == current else { return } config = resolved applyConfig() } private func startWatcher(_ root: URL) { watcher?.stop() let watcher = app.makeWatcher() watcher.start(root: root) { [weak self] event in Task { @MainActor in await self?.handle(event) } } self.watcher = watcher } }