From 90427a8d2204927db5f34ea52ad3c059eaea7c4f Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Fri, 12 Jan 2024 20:22:53 +0100 Subject: [PATCH 1/2] macos: respect window-new-tab-position configuration --- .../Sources/Features/Terminal/TerminalManager.swift | 13 ++++++++++--- macos/Sources/Ghostty/AppState.swift | 10 ++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/macos/Sources/Features/Terminal/TerminalManager.swift b/macos/Sources/Features/Terminal/TerminalManager.swift index af85341c6..ce2e5f5f5 100644 --- a/macos/Sources/Features/Terminal/TerminalManager.swift +++ b/macos/Sources/Features/Terminal/TerminalManager.swift @@ -124,13 +124,20 @@ class TerminalManager { tg.removeWindow(window) } - // Our windows start our invisible. We need to make it visible. If we + // Our windows start out invisible. We need to make it visible. If we // don't do this then various features such as window blur won't work because // the macOS APIs only work on a visible window. controller.showWindow(self) - // Add the window to the tab group and show it - parent.addTabbedWindow(window, ordered: .above) + // Add the window to the tab group and show it. If we already have a tab group + // and we want the new tab to open at the end, then we use the last window in + // the tab group as the parent. + if let last = parent.tabGroup?.windows.last, ghostty.windowNewTabPosition == "end" { + last.addTabbedWindow(window, ordered: .above) + } else { + parent.addTabbedWindow(window, ordered: .above) + } + window.makeKeyAndOrderFront(self) // It takes an event loop cycle until the macOS tabGroup state becomes diff --git a/macos/Sources/Ghostty/AppState.swift b/macos/Sources/Ghostty/AppState.swift index cfd9cdb95..e9c15ee95 100644 --- a/macos/Sources/Ghostty/AppState.swift +++ b/macos/Sources/Ghostty/AppState.swift @@ -84,6 +84,16 @@ extension Ghostty { guard let ptr = v else { return "" } return String(cString: ptr) } + + /// window-new-tab-position + var windowNewTabPosition: String { + guard let config = self.config else { return "" } + var v: UnsafePointer? = nil + let key = "window-new-tab-position" + guard ghostty_config_get(config, &v, key, UInt(key.count)) else { return "" } + guard let ptr = v else { return "" } + return String(cString: ptr) + } /// True if we need to confirm before quitting. var needsConfirmQuit: Bool { From 2bf8dac86477a3f8fd8f50b416d05c4b6b2f175d Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Sat, 13 Jan 2024 08:32:50 +0100 Subject: [PATCH 2/2] macos: use switch statement instead of if-let --- .../Features/Terminal/TerminalManager.swift | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/macos/Sources/Features/Terminal/TerminalManager.swift b/macos/Sources/Features/Terminal/TerminalManager.swift index ce2e5f5f5..b919d5282 100644 --- a/macos/Sources/Features/Terminal/TerminalManager.swift +++ b/macos/Sources/Features/Terminal/TerminalManager.swift @@ -129,12 +129,18 @@ class TerminalManager { // the macOS APIs only work on a visible window. controller.showWindow(self) - // Add the window to the tab group and show it. If we already have a tab group - // and we want the new tab to open at the end, then we use the last window in - // the tab group as the parent. - if let last = parent.tabGroup?.windows.last, ghostty.windowNewTabPosition == "end" { - last.addTabbedWindow(window, ordered: .above) - } else { + // Add the window to the tab group and show it. + switch ghostty.windowNewTabPosition { + case "end": + // If we already have a tab group and we want the new tab to open at the end, + // then we use the last window in the tab group as the parent. + if let last = parent.tabGroup?.windows.last { + last.addTabbedWindow(window, ordered: .above) + } else { + fallthrough + } + case "current": fallthrough + default: parent.addTabbedWindow(window, ordered: .above) }