From 1d727320b47d9d6884d006c8e6bc42e079dcf3d8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 28 Sep 2024 19:11:04 -0700 Subject: [PATCH] macos: if initializing new surface tree, move focus to it --- .../QuickTerminal/QuickTerminalController.swift | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/macos/Sources/Features/QuickTerminal/QuickTerminalController.swift b/macos/Sources/Features/QuickTerminal/QuickTerminalController.swift index 698be98bc..e28bda0fd 100644 --- a/macos/Sources/Features/QuickTerminal/QuickTerminalController.swift +++ b/macos/Sources/Features/QuickTerminal/QuickTerminalController.swift @@ -105,7 +105,19 @@ class QuickTerminalController: BaseTerminalController { // tree can be nil if for example we run "eixt" in the terminal and force // animate out. if (surfaceTree == nil) { - surfaceTree = .leaf(.init(ghostty.app!, baseConfig: nil)) + let leaf: Ghostty.SplitNode.Leaf = .init(ghostty.app!, baseConfig: nil) + surfaceTree = .leaf(leaf) + focusedSurface = leaf.surface + + // We need to grab first responder but it takes a few loop cycles + // before the view is attached to the window so we do it async. + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(250)) { + // We should probably retry here but I was never able to trigger this. + // If this happens though its a crash so let's avoid it. + guard let leafWindow = leaf.surface.window, + leafWindow == window else { return } + window.makeFirstResponder(leaf.surface) + } } }