From fc6def01a1649e73dd17607d746b43770d8ee288 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 7 Dec 2023 12:35:19 -0800 Subject: [PATCH] macos: new tab button creates new tab after short delay Fixes #1010 I can't explain this. See the comment in the diff. --- .../Features/Terminal/TerminalController.swift | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/macos/Sources/Features/Terminal/TerminalController.swift b/macos/Sources/Features/Terminal/TerminalController.swift index cdbad1b84..c4c2752a2 100644 --- a/macos/Sources/Features/Terminal/TerminalController.swift +++ b/macos/Sources/Features/Terminal/TerminalController.swift @@ -99,7 +99,7 @@ class TerminalController: NSWindowController, NSWindowDelegate, guard let windows = self.window?.tabbedWindows else { return } guard let cfg = ghostty.config else { return } - + // We only listen for frame changes if we have more than 1 window, // otherwise the accessory view doesn't matter. tabListenForFrame = windows.count > 1 @@ -189,7 +189,21 @@ class TerminalController: NSWindowController, NSWindowDelegate, override func newWindowForTab(_ sender: Any?) { // Trigger the ghostty core event logic for a new tab. guard let surface = self.focusedSurface?.surface else { return } - ghostty.newTab(surface: surface) + + // This is necessary due to a really strange issue on macOS that I was never + // able to figure out: if we create a new tab in this function directly, then + // it is incorrectly added to the wrong index in `tabGroup.windows`. The macOS + // `tabGroup.windows` documentation says that will always be in visual order + // of the tab but I was finding that not to be true. By introducing a small delay, + // I noticed everything works. I can't explain it and I'd rather not do this so + // if someone can figure this out that'd be great. + // + // See: https://github.com/mitchellh/ghostty/issues/1010 + // Last reproduced on macOS 14.1 + DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) { [weak self] in + guard let s = self else { return } + s.ghostty.newTab(surface: surface) + } } //MARK: - NSWindowDelegate