From 61451942e83e6bdef4910d0bfa7d76b381ef3bd6 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 30 Oct 2023 09:53:59 -0700 Subject: [PATCH] macos: implement last surface close to close window --- .../Features/Terminal/TerminalController.swift | 4 ++++ macos/Sources/Features/Terminal/TerminalView.swift | 11 +++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/macos/Sources/Features/Terminal/TerminalController.swift b/macos/Sources/Features/Terminal/TerminalController.swift index 664e7ce38..95bb035cb 100644 --- a/macos/Sources/Features/Terminal/TerminalController.swift +++ b/macos/Sources/Features/Terminal/TerminalController.swift @@ -95,6 +95,10 @@ class TerminalController: NSWindowController, NSWindowDelegate, TerminalViewDele self.window?.contentResizeIncrements = to } + func lastSurfaceDidClose() { + self.window?.close() + } + //MARK: - Notifications @objc private func onGotoTab(notification: SwiftUI.Notification) { diff --git a/macos/Sources/Features/Terminal/TerminalView.swift b/macos/Sources/Features/Terminal/TerminalView.swift index e10bf0b29..727a99a83 100644 --- a/macos/Sources/Features/Terminal/TerminalView.swift +++ b/macos/Sources/Features/Terminal/TerminalView.swift @@ -10,12 +10,16 @@ protocol TerminalViewDelegate: AnyObject { /// The cell size changed. func cellSizeDidChange(to: NSSize) + + /// The last surface closed so there are no active surfaces. + func lastSurfaceDidClose() } extension TerminalViewDelegate { func focusedSurfaceDidChange(to: Ghostty.SurfaceView?) {} func titleDidChange(to: String) {} func cellSizeDidChange(to: NSSize) {} + func lastSurfaceDidClose() {} } struct TerminalView: View { @@ -66,7 +70,7 @@ struct TerminalView: View { DebugBuildWarningView() } - Ghostty.TerminalSplit(onClose: Self.closeWindow, baseConfig: nil) + Ghostty.TerminalSplit(onClose: onClose, baseConfig: nil) .ghosttyApp(ghostty.app!) .ghosttyConfig(ghostty.config!) .focused($focused) @@ -85,8 +89,7 @@ struct TerminalView: View { } } - static func closeWindow() { - guard let currentWindow = NSApp.keyWindow else { return } - currentWindow.close() + func onClose() { + self.delegate?.lastSurfaceDidClose() } }