macos: only show alert once

This commit is contained in:
Mitchell Hashimoto
2023-10-30 14:56:59 -07:00
parent 350a9da68b
commit 7fe6f15d2d

View File

@ -27,6 +27,9 @@ class TerminalController: NSWindowController, NSWindowDelegate, TerminalViewDele
/// Fullscreen state management. /// Fullscreen state management.
private let fullscreenHandler = FullScreenHandler() private let fullscreenHandler = FullScreenHandler()
/// True when an alert is active so we don't overlap multiple.
private var alert: NSAlert? = nil
/// The style mask to use for the new window /// The style mask to use for the new window
private var styleMask: NSWindow.StyleMask { private var styleMask: NSWindow.StyleMask {
var mask: NSWindow.StyleMask = [.resizable, .closable, .miniaturizable] var mask: NSWindow.StyleMask = [.resizable, .closable, .miniaturizable]
@ -119,7 +122,8 @@ class TerminalController: NSWindowController, NSWindowDelegate, TerminalViewDele
// If our surfaces don't require confirmation, close. // If our surfaces don't require confirmation, close.
if (!node.needsConfirmQuit()) { return true } if (!node.needsConfirmQuit()) { return true }
// We require confirmation, so show an alert. // We require confirmation, so show an alert as long as we aren't already.
if (alert == nil) {
let alert = NSAlert() let alert = NSAlert()
alert.messageText = "Close Terminal?" alert.messageText = "Close Terminal?"
alert.informativeText = "The terminal still has a running process. If you close the " + alert.informativeText = "The terminal still has a running process. If you close the " +
@ -128,6 +132,7 @@ class TerminalController: NSWindowController, NSWindowDelegate, TerminalViewDele
alert.addButton(withTitle: "Cancel") alert.addButton(withTitle: "Cancel")
alert.alertStyle = .warning alert.alertStyle = .warning
alert.beginSheetModal(for: window, completionHandler: { response in alert.beginSheetModal(for: window, completionHandler: { response in
self.alert = nil
switch (response) { switch (response) {
case .alertFirstButtonReturn: case .alertFirstButtonReturn:
window.close() window.close()
@ -137,6 +142,9 @@ class TerminalController: NSWindowController, NSWindowDelegate, TerminalViewDele
} }
}) })
self.alert = alert
}
return false return false
} }