63 lines
1.6 KiB
Swift
63 lines
1.6 KiB
Swift
import AppKit
|
|
import SwiftUI
|
|
|
|
typealias ViewState = State
|
|
|
|
@MainActor
|
|
final class AppDelegate: NSObject, NSApplicationDelegate {
|
|
var connection: Connection?
|
|
private var quitting = false
|
|
|
|
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
|
|
guard let store = connection?.store, !quitting else {
|
|
return .terminateNow
|
|
}
|
|
quitting = true
|
|
var replied = false
|
|
let reply = {
|
|
if !replied {
|
|
replied = true
|
|
sender.reply(toApplicationShouldTerminate: true)
|
|
}
|
|
}
|
|
Task {
|
|
await store.stop()
|
|
reply()
|
|
}
|
|
Task {
|
|
try? await Task.sleep(for: .seconds(2))
|
|
reply()
|
|
}
|
|
return .terminateLater
|
|
}
|
|
}
|
|
|
|
@main
|
|
struct ByreApp: App {
|
|
@NSApplicationDelegateAdaptor private var delegate: AppDelegate
|
|
@ViewState private var connection = Connection()
|
|
@ViewState private var overlays = Overlays()
|
|
|
|
var body: some Scene {
|
|
Window("byre", id: "main") {
|
|
RootView()
|
|
.environment(connection)
|
|
.environment(overlays)
|
|
.font(Fonts.font(13))
|
|
.frame(minWidth: 640, minHeight: 400)
|
|
.onAppear {
|
|
delegate.connection = connection
|
|
}
|
|
}
|
|
.defaultSize(width: 1280, height: 820)
|
|
.commands {
|
|
CommandGroup(after: .appInfo) {
|
|
Button("Change Server…") {
|
|
connection.changeServer()
|
|
}
|
|
}
|
|
PasteboardCommands()
|
|
}
|
|
}
|
|
}
|