macos: more robust undo tab that goes back to the same position

pull/7535/head
Mitchell Hashimoto 2025-06-06 11:13:47 -07:00
parent 5f74445b14
commit e1847da139
No known key found for this signature in database
GPG Key ID: 523D5DC389D273BC
1 changed files with 20 additions and 23 deletions

View File

@ -417,35 +417,13 @@ class TerminalController: BaseTerminalController {
// Undo
if let undoManager, let undoState {
// Get the current tab index before closing
let tabIndex = tabGroup.windows.firstIndex(of: window) ?? 0
// Register undo action to restore the tab
undoManager.setActionName("Close Tab")
undoManager.registerUndo(
withTarget: ghostty,
expiresAfter: undoExpiration) { ghostty in
// Create a new window controller with the saved state
let newController = TerminalController(ghostty, with: undoState)
if let newWindow = newController.window {
// Add the window back to the tab group at the correct position
if let targetWindow = tabGroup.windows.dropFirst(tabIndex).first {
// Insert after the target window
targetWindow.addTabbedWindow(newWindow, ordered: .above)
} else if let targetWindow = tabGroup.windows.last {
// Add at the end if the original position is beyond current tabs
targetWindow.addTabbedWindow(newWindow, ordered: .above)
} else if let firstWindow = tabGroup.windows.first {
// Fallback: add to the beginning if needed
firstWindow.addTabbedWindow(newWindow, ordered: .below)
}
// Make it the key window
newWindow.makeKeyAndOrderFront(nil)
}
// Register redo action
undoManager.registerUndo(
withTarget: newController,
@ -510,6 +488,8 @@ class TerminalController: BaseTerminalController {
let frame: NSRect
let surfaceTree: SplitTree<Ghostty.SurfaceView>
let focusedSurface: UUID?
let tabIndex: Int?
private(set) weak var tabGroup: NSWindowTabGroup?
}
convenience init(_ ghostty: Ghostty.App,
@ -522,6 +502,21 @@ class TerminalController: BaseTerminalController {
if let window {
window.setFrame(undoState.frame, display: true)
// If we have a tab group and index, restore the tab to its original position
if let tabGroup = undoState.tabGroup,
let tabIndex = undoState.tabIndex {
if tabIndex < tabGroup.windows.count {
// Find the window that is currently at that index
let currentWindow = tabGroup.windows[tabIndex]
currentWindow.addTabbedWindow(window, ordered: .below)
} else {
tabGroup.windows.last?.addTabbedWindow(window, ordered: .above)
}
// Make it the key window
window.makeKeyAndOrderFront(nil)
}
// Restore focus to the previously focused surface
if let focusedUUID = undoState.focusedSurface,
let focusTarget = surfaceTree.first(where: { $0.uuid == focusedUUID }) {
@ -538,7 +533,9 @@ class TerminalController: BaseTerminalController {
return .init(
frame: window.frame,
surfaceTree: surfaceTree,
focusedSurface: focusedSurface?.uuid)
focusedSurface: focusedSurface?.uuid,
tabIndex: window.tabGroup?.windows.firstIndex(of: window),
tabGroup: window.tabGroup)
}
//MARK: - NSWindowController