Skip to content

Commit ac12f8f

Browse files
committed
checkpoint: do the recommendation — AgentApp.swift
Auto-checkpoint after successful build (1 file). Task: do the recommendation Changed: M Agent/AgentApp.swift
1 parent 15fe7eb commit ac12f8f

1 file changed

Lines changed: 61 additions & 1 deletion

File tree

‎Agent/AgentApp.swift‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,52 @@ private func post(_ name: Notification.Name) {
7171
NotificationCenter.default.post(name: name, object: nil)
7272
}
7373

74+
/// Intercepts the main window's close button. While any agent tab is running the window is hidden
75+
/// (orderOut) instead of closed so its exact state survives; a Dock click brings it back.
76+
/// Every other delegate call is forwarded to SwiftUI's own window delegate.
77+
final class MainWindowDelegateProxy: NSObject, NSWindowDelegate {
78+
nonisolated(unsafe) weak var original: NSWindowDelegate?
79+
80+
@MainActor static var anyAgentRunning: Bool {
81+
guard let vm = AgentsMenuDelegate.shared.viewModel else { return false }
82+
return vm.isRunning || vm.scriptTabs.contains { $0.isBusy }
83+
}
84+
85+
func windowShouldClose(_ sender: NSWindow) -> Bool {
86+
MainActor.assumeIsolated {
87+
guard Self.anyAgentRunning else {
88+
return original?.windowShouldClose?(sender) ?? true
89+
}
90+
sender.orderOut(nil)
91+
return false
92+
}
93+
}
94+
95+
override func responds(to aSelector: Selector!) -> Bool {
96+
super.responds(to: aSelector) || (original?.responds(to: aSelector) ?? false)
97+
}
98+
99+
override func forwardingTarget(for aSelector: Selector!) -> Any? {
100+
if original?.responds(to: aSelector) == true { return original }
101+
return super.forwardingTarget(for: aSelector)
102+
}
103+
}
104+
74105
final class AppDelegate: NSObject, NSApplicationDelegate {
106+
private let windowDelegateProxy = MainWindowDelegateProxy()
107+
private weak var mainWindow: NSWindow?
108+
75109
func applicationDidFinishLaunching(_ notification: Notification) {
76110
// Initialize accessibility defaults so UserDefaults keys exist before isRestricted() checks
77111
_ = AccessibilityEnabled.shared
78112

79-
// Persist window frame across launches and tiling
113+
// Persist window frame across launches and tiling; hook close button while agents run
80114
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
81115
if let window = NSApplication.shared.windows.first {
82116
window.setFrameAutosaveName("AgentMainWindow")
117+
self.mainWindow = window
118+
self.windowDelegateProxy.original = window.delegate
119+
window.delegate = self.windowDelegateProxy
83120
}
84121
}
85122

@@ -120,7 +157,30 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
120157
mainMenu.insertItem(agentsItem, at: insertIdx)
121158
}
122159

160+
/// Dock click / reopen: bring back the hidden main window (exact state preserved) instead of
161+
/// letting SwiftUI create a fresh one.
162+
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
163+
if !flag, let window = mainWindow {
164+
window.makeKeyAndOrderFront(nil)
165+
return false
166+
}
167+
return true
168+
}
169+
123170
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
171+
// Cmd-Q guard: confirm before killing running agents
172+
if MainWindowDelegateProxy.anyAgentRunning {
173+
let alert = NSAlert()
174+
alert.messageText = "Agents are still running"
175+
alert.informativeText = "Quitting will stop every running agent tab. Quit anyway?"
176+
alert.alertStyle = .warning
177+
alert.addButton(withTitle: "Quit")
178+
alert.addButton(withTitle: "Cancel")
179+
mainWindow?.makeKeyAndOrderFront(nil)
180+
if alert.runModal() != .alertFirstButtonReturn {
181+
return .terminateCancel
182+
}
183+
}
124184
// Tell the view model to stop all running tasks, MCP servers, etc.
125185
NotificationCenter.default.post(name: .appWillQuit, object: nil)
126186
// Drain compilation queue before exit to prevent stdout deadlock with C++ static destructors.

0 commit comments

Comments
 (0)