macos: simplify the code to a more understandable style

pull/9509/head
Mitchell Hashimoto 2025-11-07 14:19:44 -08:00
parent 1eecd448e9
commit 04563a16b6
No known key found for this signature in database
GPG Key ID: 523D5DC389D273BC
2 changed files with 15 additions and 20 deletions

View File

@ -287,7 +287,6 @@ class BaseTerminalController: NSWindowController,
func confirmClose(
messageText: String,
informativeText: String,
attachedWindow: NSWindow? = nil,
completion: @escaping () -> Void
) {
// If we already have an alert, we need to wait for that one.
@ -295,7 +294,7 @@ class BaseTerminalController: NSWindowController,
// If there is no window to attach the modal then we assume success
// since we'll never be able to show the modal.
guard let window = attachedWindow ?? self.window else {
guard let window else {
completion()
return
}

View File

@ -817,12 +817,14 @@ class TerminalController: BaseTerminalController, TabGroupCloseCoordinator.Contr
/// Close all windows, asking for confirmation if necessary.
static func closeAllWindows() {
let confirmWindow: NSWindow? = all
.first { $0.surfaceTree.contains(where: { $0.needsConfirmQuit }) }?
.surfaceTree.first { $0.needsConfirmQuit }?
// The window we use for confirmations. Try to find the first window that
// needs quit confirmation. This lets us attach the confirmation to something
// that is running.
guard let confirmWindow = all
.first(where: { $0.surfaceTree.contains(where: { $0.needsConfirmQuit }) })?
.surfaceTree.first(where: { $0.needsConfirmQuit })?
.window
guard let confirmWindow else {
else {
closeAllWindowsImmediately()
return
}
@ -1153,25 +1155,19 @@ class TerminalController: BaseTerminalController, TabGroupCloseCoordinator.Contr
// if we're closing the window. If we don't have a tabgroup for any
// reason we check ourselves.
let windows: [NSWindow] = window.tabGroup?.windows ?? [window]
let confirmWindow: NSWindow? = windows
.first {
($0.windowController as? TerminalController)?.surfaceTree.contains(where: { $0.needsConfirmQuit }) == true
}
.flatMap {
($0.windowController as? TerminalController)?.surfaceTree.first(where: { $0.needsConfirmQuit })
}?.window
// If none need confirmation then we can just close all the windows.
guard let confirmWindow else {
guard let confirmController = windows
.compactMap({ $0.windowController as? TerminalController })
.first(where: { $0.surfaceTree.contains(where: { $0.needsConfirmQuit }) })
else {
closeWindowImmediately()
return
}
confirmClose(
// We call confirmClose on the proper controller so the alert is
// attached to the window that needs confirmation.
confirmController.confirmClose(
messageText: "Close Window?",
informativeText: "All terminal sessions in this window will be terminated.",
attachedWindow: confirmWindow,
) {
self.closeWindowImmediately()
}