ghostty/macos/Sources/ContentView.swift
Thorsten Ball eea37033bf macOS: change quit dialog to handle return by removing role
This is, as it turns out, the simple version of #166.

It changes the quit dialog to handle return by removing the
`.destructive` role from the `Quit` button.

When that role is set, the `keyboardShortcut(.defaultAction)` doesn't have an effect.

With the role removed, the dialog handles the return key to quit the
application

As I wrote in #166, I can understand if you don't want this change, but
I personally think it's nice to be able to only use the keyboard. iTerm2
also handles return like this in the "Are you sure?" dialog.
2023-07-01 14:52:27 +02:00

61 lines
2.3 KiB
Swift

import SwiftUI
import GhosttyKit
struct ContentView: View {
let ghostty: Ghostty.AppState
// We need access to our app delegate to know if we're quitting or not.
@EnvironmentObject private var appDelegate: AppDelegate
// We need access to our window to know if we're the key window to determine
// if we show the quit confirmation or not.
@State private var window: NSWindow?
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:
let confirmQuitting = Binding<Bool>(get: {
self.appDelegate.confirmQuit && (self.window?.isKeyWindow ?? false)
}, set: {
self.appDelegate.confirmQuit = $0
})
Ghostty.TerminalSplit(onClose: Self.closeWindow)
.ghosttyApp(ghostty.app!)
.background(WindowAccessor(window: $window))
.confirmationDialog(
"Quit Ghostty?",
isPresented: confirmQuitting) {
Button("Close Ghostty") {
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()
}
}