mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-06-01 04:58:37 +03:00
macos: show close confirmation if running process exists
This commit is contained in:
@ -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) {
|
||||
|
@ -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) {
|
||||
|
Reference in New Issue
Block a user