restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
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)
|
||||
/// 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 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") }
|
||||
}
|
||||
Reference in New Issue
Block a user