@@ -93,6 +93,8 @@ export interface ThreadRightPanelState {
9393 dismissedDeviceSurfaceIds ?: string [ ] ;
9494 maximized ?: true ;
9595 proactiveDiffTurnId ?: RunId ;
96+ /** The terminal tab last in front, which the terminal shortcut returns to. */
97+ lastTerminalSurfaceId ?: string ;
9698}
9799
98100export interface ThreadPanelVisibility {
@@ -300,13 +302,26 @@ const upsertSurface = (
300302 activeSurfaceId : activate ? surface . id : current . activeSurfaceId ,
301303} ) ;
302304
305+ const rememberTerminalSurface = ( state : ThreadRightPanelState ) : ThreadRightPanelState => {
306+ const active = state . surfaces . find ( ( surface ) => surface . id === state . activeSurfaceId ) ;
307+ const last =
308+ active ?. kind === "terminal"
309+ ? active . id
310+ : state . surfaces . some ( ( surface ) => surface . id === state . lastTerminalSurfaceId )
311+ ? state . lastTerminalSurfaceId
312+ : undefined ;
313+ if ( last === state . lastTerminalSurfaceId ) return state ;
314+ const { lastTerminalSurfaceId : _previous , ...rest } = state ;
315+ return last === undefined ? rest : { ...rest , lastTerminalSurfaceId : last } ;
316+ } ;
317+
303318const updateThreadStateMap = (
304319 byThreadKey : Record < string , ThreadRightPanelState > ,
305320 threadKey : string ,
306321 updater : ( current : ThreadRightPanelState ) => ThreadRightPanelState ,
307322) : Record < string , ThreadRightPanelState > => {
308323 const current = byThreadKey [ threadKey ] ?? EMPTY_THREAD_STATE ;
309- const next = updater ( current ) ;
324+ const next = rememberTerminalSurface ( updater ( current ) ) ;
310325 if (
311326 ! next . isOpen &&
312327 next . activeSurfaceId === null &&
@@ -1193,7 +1208,9 @@ export function resolveRightPanelTerminalToggle(
11931208 const terminal =
11941209 selected ?. kind === "terminal"
11951210 ? selected
1196- : state . surfaces . findLast ( ( surface ) => surface . kind === "terminal" ) ;
1211+ : ( state . surfaces . find (
1212+ ( surface ) => surface . kind === "terminal" && surface . id === state . lastTerminalSurfaceId ,
1213+ ) ?? state . surfaces . findLast ( ( surface ) => surface . kind === "terminal" ) ) ;
11971214 return terminal ? { kind : "show" , surfaceId : terminal . id } : { kind : "create" } ;
11981215}
11991216
0 commit comments