Files
helder/Sources/HelderUI/Shell/SystemActions.swift
T

93 lines
3.3 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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") }
}