macos: confirm on quit

This commit is contained in:
Mitchell Hashimoto
2023-03-27 09:41:00 -07:00
parent 9f128afe31
commit 92870e4e60
3 changed files with 61 additions and 10 deletions

View File

@@ -0,0 +1,48 @@
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()
}
}

View File

@@ -18,15 +18,7 @@ struct GhosttyApp: App {
var body: some Scene {
WindowGroup {
switch ghostty.readiness {
case .loading:
Text("Loading")
case .error:
ErrorView()
case .ready:
Ghostty.TerminalSplit(onClose: Self.closeWindow)
.ghosttyApp(ghostty.app!)
}
ContentView(ghostty: ghostty)
}
.backport.defaultSize(width: 800, height: 600)
.commands {
@@ -110,7 +102,9 @@ struct GhosttyApp: App {
}
}
class AppDelegate: NSObject, NSApplicationDelegate {
class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
@Published var confirmQuit: Bool = false
// See CursedMenuManager for more information.
private var menuManager: CursedMenuManager?
@@ -124,6 +118,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// we can't create from SwiftUI.
menuManager = CursedMenuManager()
}
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
confirmQuit = true
return .terminateLater
}
}
/// SwiftUI as of macOS 13.x provides no way to manage the default menu items that are created