diff --git a/macos/Sources/AppDelegate.swift b/macos/Sources/AppDelegate.swift index 1b6e5bd29..c582b4628 100644 --- a/macos/Sources/AppDelegate.swift +++ b/macos/Sources/AppDelegate.swift @@ -57,6 +57,9 @@ class AppDelegate: NSObject, /// The dock menu private var dockMenu: NSMenu = NSMenu() + /// This is only true before application has become active. + private var applicationHasBecomeActive: Bool = false + /// The ghostty global state. Only one per process. let ghostty: Ghostty.AppState = Ghostty.AppState() @@ -104,13 +107,6 @@ class AppDelegate: NSObject, // Initial config loading configDidReload(ghostty) - // Let's launch our first window. We only do this if we have no other windows. It - // is possible to have other windows if we're opening a URL since `application(_:openFile:)` - // is called before this. - if (terminalManager.windows.count == 0) { - terminalManager.newWindow() - } - // Register our service provider. This must happen after everything // else is initialized. NSApp.servicesProvider = ServiceProvider() @@ -132,6 +128,19 @@ class AppDelegate: NSObject, center.delegate = self } + func applicationDidBecomeActive(_ notification: Notification) { + guard !applicationHasBecomeActive else { return } + applicationHasBecomeActive = true + + // Let's launch our first window. We only do this if we have no other windows. It + // is possible to have other windows in a few scenarios: + // - if we're opening a URL since `application(_:openFile:)` is called before this. + // - if we're restoring from persisted state + if terminalManager.windows.count == 0 { + terminalManager.newWindow() + } + } + func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { return ghostty.shouldQuitAfterLastWindowClosed } diff --git a/macos/Sources/Features/Terminal/TerminalManager.swift b/macos/Sources/Features/Terminal/TerminalManager.swift index 745bfdf24..65ba5b03a 100644 --- a/macos/Sources/Features/Terminal/TerminalManager.swift +++ b/macos/Sources/Features/Terminal/TerminalManager.swift @@ -33,8 +33,8 @@ class TerminalManager { } } - // If we have no main window, just use the first window. - return windows.first + // If we have no main window, just use the last window. + return windows.last } init(_ ghostty: Ghostty.AppState) {