@@ -36,6 +36,12 @@ export type NewTabsState = {
3636 /** The current session is in an open-and-empty span of the side region, which opened its New tab once. */
3737 readonly openedEmpty : boolean
3838 readonly panes : Readonly < Record < SessionPane , PaneNewTabs > >
39+ /**
40+ * The pane whose New tab the person just asked for, with + or ⌘T or by opening the side region: it gives the keyboard
41+ * to its first field once that renders. A New tab that opens by itself, as when another session routes into an open
42+ * side region, leaves the keyboard where it is.
43+ */
44+ readonly focus : SessionPane | undefined
3945 /** The region as the last `sync` saw it, which the next one compares against. */
4046 readonly snapshot : RegionTabSnapshot | undefined
4147}
@@ -53,6 +59,10 @@ export type NewTabsEvent =
5359 | { readonly type : "open.selected" ; readonly pane : SessionPane }
5460 /** `empty`: the strip holds no tab, so closing the last New tab empties the region. */
5561 | { readonly type : "dismiss" ; readonly id : string ; readonly empty : boolean }
62+ /** The person opens the side region: the New tab it shows, if any, takes the keyboard. */
63+ | { readonly type : "focus.requested" ; readonly pane : SessionPane }
64+ /** The New tab gave the keyboard to its field. */
65+ | { readonly type : "focus.taken" }
5666
5767/** The last New tab of a strip with no tabs closed: the region closes, as closing its last tab does. */
5868export type NewTabsEffect = { readonly type : "emptied" }
@@ -63,13 +73,18 @@ export type NewTabsTransition = {
6373}
6474
6575export function createNewTabsState ( ) : NewTabsState {
66- return { serial : 0 , session : "" , openedEmpty : false , panes : emptyPanes ( ) , snapshot : undefined }
76+ return { serial : 0 , session : "" , openedEmpty : false , panes : emptyPanes ( ) , focus : undefined , snapshot : undefined }
6777}
6878
6979export function transitionNewTabs ( state : NewTabsState , event : NewTabsEvent ) : NewTabsTransition {
7080 if ( event . type === "sync" ) return { state : sync ( state , event . snapshot ) , effects : [ ] }
7181
72- if ( event . type === "open" ) return { state : open ( state , event . pane ) , effects : [ ] }
82+ if ( event . type === "open" ) return { state : { ...open ( state , event . pane ) , focus : event . pane } , effects : [ ] }
83+
84+ if ( event . type === "focus.requested" )
85+ return { state : state . focus === event . pane ? state : { ...state , focus : event . pane } , effects : [ ] }
86+
87+ if ( event . type === "focus.taken" ) return { state : state . focus ? { ...state , focus : undefined } : state , effects : [ ] }
7388
7489 if ( event . type === "select.new" ) {
7590 const pane = state . panes [ event . pane ]
@@ -122,14 +137,19 @@ export function selectShownNewTab(state: NewTabsState, pane: SessionPane, paneAc
122137 return shown ( state . panes [ pane ] , paneActive )
123138}
124139
140+ /** The pane's New tab should take the keyboard now. */
141+ export function selectNewTabFocus ( state : NewTabsState , pane : SessionPane ) {
142+ return state . focus === pane
143+ }
144+
125145export function selectHasNewTabs ( state : NewTabsState ) {
126146 return SESSION_PANES . some ( ( pane ) => state . panes [ pane ] . ids . length > 0 )
127147}
128148
129149function sync ( state : NewTabsState , snapshot : RegionTabSnapshot ) : NewTabsState {
130150 // Another session starts with none, and opens one when it routes into an open, empty side region.
131151 if ( snapshot . session !== state . session ) {
132- const reset = { ...state , session : snapshot . session , panes : emptyPanes ( ) , snapshot }
152+ const reset = { ...state , session : snapshot . session , panes : emptyPanes ( ) , focus : undefined , snapshot }
133153 const openedEmpty = snapshot . sideOpen && snapshot . empty
134154
135155 return openedEmpty ? open ( { ...reset , openedEmpty } , "side" ) : { ...reset , openedEmpty }
@@ -169,12 +189,28 @@ function sync(state: NewTabsState, snapshot: RegionTabSnapshot): NewTabsState {
169189 const want = snapshot . sideOpen && snapshot . empty
170190 const opened = want && ! state . openedEmpty
171191
172- const result =
192+ const opening =
173193 opened && selectShownNewTab ( synced , "side" , snapshot . panes . side . active ) === undefined
174194 ? open ( { ...synced , openedEmpty : true } , "side" )
175195 : { ...synced , openedEmpty : want }
176196
177- if ( result . panes === state . panes && result . openedEmpty === state . openedEmpty && result . snapshot === previous )
197+ // A request whose pane shows a tab rather than a New tab is over: the person opened the region onto a tab, or picked
198+ // one. The side pane's waits while the region is closed, as the request comes just before it opens.
199+ const focus = opening . focus
200+
201+ const stale =
202+ focus !== undefined &&
203+ ( focus !== "side" || snapshot . sideOpen ) &&
204+ selectShownNewTab ( opening , focus , snapshot . panes [ focus ] . active ) === undefined
205+
206+ const result = stale ? { ...opening , focus : undefined } : opening
207+
208+ if (
209+ result . panes === state . panes &&
210+ result . openedEmpty === state . openedEmpty &&
211+ result . focus === state . focus &&
212+ result . snapshot === previous
213+ )
178214 return state
179215
180216 return result
0 commit comments