89 lines
2.5 KiB
Swift
89 lines
2.5 KiB
Swift
import AppKit
|
|
import SwiftUI
|
|
|
|
typealias ViewState = State
|
|
|
|
@MainActor
|
|
final class AppDelegate: NSObject, NSApplicationDelegate {
|
|
var connection: Connection?
|
|
private var quitting = false
|
|
|
|
func applicationDidFinishLaunching(_ notification: Notification) {
|
|
Notifier.shared.install()
|
|
}
|
|
|
|
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
|
|
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()
|
|
CommandMenu("Go") {
|
|
goButton("Switch Task", "k")
|
|
goButton("Project", "p")
|
|
goButton("Lead", "l")
|
|
goButton("Merge", "s")
|
|
Divider()
|
|
goButton("Keyboard Shortcuts", "/")
|
|
}
|
|
}
|
|
}
|
|
|
|
private func goButton(_ label: String, _ key: String) -> some View {
|
|
Button(label) {
|
|
if let store = connection.store {
|
|
runGo(key, store, overlays)
|
|
}
|
|
}
|
|
.keyboardShortcut(KeyEquivalent(Character(key)))
|
|
.disabled(connection.store == nil || overlays.isOpen)
|
|
}
|
|
}
|