Files
byre/mac/Sources/Byre/ByreApp.swift
T

93 lines
2.6 KiB
Swift
Raw Normal View History

import AppKit
import SwiftUI
#if DEBUG
#error("debug builds are unsupported: build with mac/bundle.sh")
#endif
typealias ViewState = State
2026-10-16 23:30:57 -07:00
@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
}
2026-10-16 23:30:57 -07:00
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 {
2026-10-16 23:30:57 -07:00
@NSApplicationDelegateAdaptor private var delegate: AppDelegate
2026-10-16 22:46:54 -07:00
@ViewState private var connection = Connection()
2026-10-16 23:30:57 -07:00
@ViewState private var overlays = Overlays()
var body: some Scene {
Window("byre", id: "main") {
2026-10-16 22:46:54 -07:00
RootView()
.environment(connection)
2026-10-16 23:30:57 -07:00
.environment(overlays)
.font(Fonts.font(13))
2026-10-16 23:30:57 -07:00
.frame(minWidth: 640, minHeight: 400)
.onAppear {
2026-10-16 23:30:57 -07:00
delegate.connection = connection
}
}
2026-10-16 23:30:57 -07:00
.defaultSize(width: 1280, height: 820)
.commands {
2026-10-16 22:46:54 -07:00
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)
}
}