import AppKit import HelderCore import HelderServices /// The AppKit side effects a workspace triggers: dialogs, the pasteboard, Finder and the log file. @MainActor public protocol SystemActing: AnyObject { /// The folder picker; nil when cancelled. func chooseFolder() async -> URL? func copy(_ text: String) func pasteboardText() -> String? func revealInFinder(_ url: URL) /// Adds the text as a new block in Plak, which takes focus. func sendToPlak(_ text: String) /// Asked before a file with unsaved edits closes. func confirmUnsavedClose(_ path: String) -> UnsavedCloseChoice func openLog() func revealLog() /// Starts another Helder process on `root`, or on the launcher when nil; each project runs in its own process. func openNewInstance(_ root: URL?) } public final class LiveSystemActions: SystemActing { public nonisolated init() {} public func chooseFolder() async -> URL? { let panel = NSOpenPanel() panel.canChooseDirectories = true panel.canChooseFiles = false panel.allowsMultipleSelection = false panel.prompt = "Open" panel.message = "Choose a project folder" let response = await panel.begin() return response == .OK ? panel.url : nil } public func copy(_ text: String) { NSPasteboard.general.clearContents() NSPasteboard.general.setString(text, forType: .string) } public func pasteboardText() -> String? { NSPasteboard.general.string(forType: .string) } public func revealInFinder(_ url: URL) { NSWorkspace.shared.activateFileViewerSelecting([url]) } public func sendToPlak(_ text: String) { switch Plak.delivery(for: text) { case let .append(url)?: NSWorkspace.shared.open(url) case .paste?: pasteToPlak(text) case nil: Log.error("plak", "could not build the append URL") } } /// Long text travels on the pasteboard; the user's own items return a second later, /// unless something else was copied in that second. private func pasteToPlak(_ text: String) { let pasteboard = NSPasteboard.general let saved = (pasteboard.pasteboardItems ?? []).map { item in item.types.compactMap { type in item.data(forType: type).map { (type, $0) } } } copy(text) let ours = pasteboard.changeCount NSWorkspace.shared.open(Plak.pasteURL) DispatchQueue.main.asyncAfter(deadline: .now() + 1) { guard pasteboard.changeCount == ours else { return } pasteboard.clearContents() guard !saved.isEmpty else { return } pasteboard.writeObjects(saved.map { pairs in let item = NSPasteboardItem() for (type, data) in pairs { item.setData(data, forType: type) } return item }) } } public func confirmUnsavedClose(_ path: String) -> UnsavedCloseChoice { let alert = NSAlert() alert.messageText = "Save changes to \(PathUtils.basename(path))?" alert.informativeText = "\(path) has unsaved edits. They are lost if you close without saving." alert.alertStyle = .warning alert.addButton(withTitle: "Save") alert.addButton(withTitle: "Don’t Save") alert.addButton(withTitle: "Cancel") switch alert.runModal() { case .alertFirstButtonReturn: return .save case .alertSecondButtonReturn: return .discard default: return .cancel } } public func openLog() { NSWorkspace.shared.open(Self.logURL) } public func revealLog() { revealInFinder(Self.logURL) } public func openNewInstance(_ root: URL?) { let bundle = Bundle.main.bundleURL guard bundle.pathExtension == "app" else { guard let executable = Bundle.main.executableURL else { return } do { try InstanceLauncher.spawn(executable, root: root) } catch { Log.error("window", "could not start a new instance: \(error.localizedDescription)") } return } let configuration = NSWorkspace.OpenConfiguration() configuration.createsNewApplicationInstance = true configuration.activates = true configuration.arguments = InstanceLauncher.arguments(for: root) NSWorkspace.shared.openApplication(at: bundle, configuration: configuration) { _, error in if let error { Log.error("window", "could not start a new instance: \(error.localizedDescription)") } } } private static var logURL: URL { FileLogger.defaultDirectory.appendingPathComponent("helder.log") } }