macos: show close confirmation if running process exists

This commit is contained in:
Mitchell Hashimoto
2023-03-26 10:40:38 -07:00
parent bc9973d37f
commit a1831ecacb
2 changed files with 37 additions and 4 deletions

View File

@ -133,10 +133,10 @@ extension Ghostty {
}
static func closeSurface(_ userdata: UnsafeMutableRawPointer?, processAlive: Bool) {
// TODO: handle processAlive to show confirmation dialog. We probably want to attach
// it to the notification and let downstream handle it.
guard let surface = self.surfaceUserdata(from: userdata) else { return }
NotificationCenter.default.post(name: Notification.ghosttyCloseSurface, object: surface)
NotificationCenter.default.post(name: Notification.ghosttyCloseSurface, object: surface, userInfo: [
"process_alive": processAlive,
])
}
static func focusSplit(_ userdata: UnsafeMutableRawPointer?, direction: ghostty_split_focus_direction_e) {

View File

@ -205,6 +205,9 @@ extension Ghostty {
/// This will be set to true when the split requests that is become closed.
@Binding var requestClose: Bool
/// This controls whether we're actively confirming if we want to close or not.
@State private var confirmClose: Bool = false
var body: some View {
let center = NotificationCenter.default
let pub = center.publisher(for: Notification.ghosttyNewSplit, object: leaf.surface)
@ -212,8 +215,38 @@ extension Ghostty {
let pubFocus = center.publisher(for: Notification.ghosttyFocusSplit, object: leaf.surface)
SurfaceWrapper(surfaceView: leaf.surface)
.onReceive(pub) { onNewSplit(notification: $0) }
.onReceive(pubClose) { _ in requestClose = true }
.onReceive(pubClose) { onClose(notification: $0) }
.onReceive(pubFocus) { onMoveFocus(notification: $0) }
.confirmationDialog(
"Close Terminal?",
isPresented: $confirmClose) {
Button("Close the Terminal", role: .destructive) {
confirmClose = false
requestClose = true
}
.keyboardShortcut(.defaultAction)
} message: {
Text("The terminal still has a running process. If you close the terminal " +
"the process will be killed.")
}
}
private func onClose(notification: SwiftUI.Notification) {
var processAlive = false
if let valueAny = notification.userInfo?["process_alive"] {
if let value = valueAny as? Bool {
processAlive = value
}
}
// If the child process is not alive, then we exit immediately
guard processAlive else {
requestClose = true
return
}
// Child process is alive, so we want to show a confirmation.
confirmClose = true
}
private func onNewSplit(notification: SwiftUI.Notification) {