From 3aad646be2f3991e4fa6181439c00f921175810a Mon Sep 17 00:00:00 2001 From: Vivek Roy Date: Sat, 6 Jan 2024 00:35:24 +0530 Subject: [PATCH 1/2] macos: Address window spawning and ordering issues from service. --- macos/Sources/AppDelegate.swift | 18 ++++++++++++------ .../Features/Terminal/TerminalManager.swift | 4 ++-- macos/Sources/Ghostty/AppState.swift | 3 +++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/macos/Sources/AppDelegate.swift b/macos/Sources/AppDelegate.swift index 1b6e5bd29..ded539fba 100644 --- a/macos/Sources/AppDelegate.swift +++ b/macos/Sources/AppDelegate.swift @@ -104,12 +104,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. @@ -132,6 +126,18 @@ class AppDelegate: NSObject, center.delegate = self } + func applicationDidBecomeActive(_ notification: Notification) { + // 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 (ghostty.firstLaunch) { + if terminalManager.windows.count == 0 { + terminalManager.newWindow() + } + ghostty.firstLaunch = false + } + } + 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) { diff --git a/macos/Sources/Ghostty/AppState.swift b/macos/Sources/Ghostty/AppState.swift index 0f4570e5c..240d09ac7 100644 --- a/macos/Sources/Ghostty/AppState.swift +++ b/macos/Sources/Ghostty/AppState.swift @@ -36,6 +36,9 @@ extension Ghostty { /// Optional delegate weak var delegate: GhosttyAppStateDelegate? + /// True when application is first launched. Immidiately set to false after first window is shown. + var firstLaunch: Bool = true + /// The ghostty global configuration. This should only be changed when it is definitely /// safe to change. It is definite safe to change only when the embedded app runtime /// in Ghostty says so (usually, only in a reload configuration callback). From 42c10de81479766f6fc5a3831ca05ae25f66ac9d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 5 Jan 2024 12:32:18 -0800 Subject: [PATCH 2/2] macos: move active state to delegate --- macos/Sources/AppDelegate.swift | 19 +++++++++++-------- macos/Sources/Ghostty/AppState.swift | 3 --- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/macos/Sources/AppDelegate.swift b/macos/Sources/AppDelegate.swift index ded539fba..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,7 +107,6 @@ class AppDelegate: NSObject, // Initial config loading configDidReload(ghostty) - // Register our service provider. This must happen after everything // else is initialized. NSApp.servicesProvider = ServiceProvider() @@ -127,14 +129,15 @@ class AppDelegate: NSObject, } 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 if we're opening a URL since `application(_:openFile:)` - // is called before this. - if (ghostty.firstLaunch) { - if terminalManager.windows.count == 0 { - terminalManager.newWindow() - } - ghostty.firstLaunch = false + // 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() } } diff --git a/macos/Sources/Ghostty/AppState.swift b/macos/Sources/Ghostty/AppState.swift index 240d09ac7..0f4570e5c 100644 --- a/macos/Sources/Ghostty/AppState.swift +++ b/macos/Sources/Ghostty/AppState.swift @@ -36,9 +36,6 @@ extension Ghostty { /// Optional delegate weak var delegate: GhosttyAppStateDelegate? - /// True when application is first launched. Immidiately set to false after first window is shown. - var firstLaunch: Bool = true - /// The ghostty global configuration. This should only be changed when it is definitely /// safe to change. It is definite safe to change only when the embedded app runtime /// in Ghostty says so (usually, only in a reload configuration callback).