ghostty/macos/Sources/ContentView.swift
Mitchell Hashimoto 92870e4e60 macos: confirm on quit
2023-03-27 09:41:00 -07:00

49 lines
1.8 KiB
Swift

import SwiftUI
import GhosttyKit
struct ContentView: View {
let ghostty: Ghostty.AppState
@EnvironmentObject private var appDelegate: AppDelegate
var body: some View {
switch ghostty.readiness {
case .loading:
Text("Loading")
.onChange(of: appDelegate.confirmQuit) { value in
guard value else { return }
NSApplication.shared.reply(toApplicationShouldTerminate: true)
}
case .error:
ErrorView()
.onChange(of: appDelegate.confirmQuit) { value in
guard value else { return }
NSApplication.shared.reply(toApplicationShouldTerminate: true)
}
case .ready:
Ghostty.TerminalSplit(onClose: Self.closeWindow)
.ghosttyApp(ghostty.app!)
.confirmationDialog(
"Quit Ghostty?",
isPresented: $appDelegate.confirmQuit) {
Button("Close Ghostty", role: .destructive) {
NSApplication.shared.reply(toApplicationShouldTerminate: true)
}
.keyboardShortcut(.defaultAction)
Button("Cancel", role: .cancel) {
NSApplication.shared.reply(toApplicationShouldTerminate: false)
}
.keyboardShortcut(.cancelAction)
} message: {
Text("All terminal sessions will be terminated.")
}
}
}
static func closeWindow() {
guard let currentWindow = NSApp.keyWindow else { return }
currentWindow.close()
}
}