@@ -90,10 +90,28 @@ function revealInScrollers(target: HTMLElement) {
9090export function createWorkspaceFocusController ( doc : Document ) {
9191 const lastFocused = new Map < string , HTMLElement > ( ) ;
9292 let lastArea : string | undefined ;
93+ // An area that took focus itself from the keyboard. Chromium's :focus-visible also matches
94+ // a clicked area after any later key press, so the ring and window return read this mark.
95+ let keyboardArea : HTMLElement | undefined ;
96+ let previousTarget : EventTarget | null = null ;
97+ const unmarkKeyboardArea = ( ) => {
98+ keyboardArea ?. removeAttribute ( "data-workspace-keyboard-focus" ) ;
99+ keyboardArea = undefined ;
100+ } ;
101+ const markKeyboardArea = ( area : HTMLElement ) => {
102+ if ( ! area . matches ( ":focus-visible" ) ) return ;
103+ keyboardArea = area ;
104+ area . setAttribute ( "data-workspace-keyboard-focus" , "" ) ;
105+ } ;
93106 const remember = ( event : FocusEvent ) => {
107+ if ( event . target !== keyboardArea ) unmarkKeyboardArea ( ) ;
108+ // Window activation refocuses the same element; only focus arriving from elsewhere counts.
109+ const arrived = event . target !== previousTarget ;
110+ previousTarget = event . target ;
94111 if ( ! ( event . target instanceof HTMLElement ) ) return ;
95112 const area = workspaceArea ( event . target ) ;
96113 if ( ! area ?. dataset . workspaceArea ) return ;
114+ if ( arrived && event . target === area ) markKeyboardArea ( area ) ;
97115 lastArea = area . dataset . workspaceArea ;
98116 const key = area . dataset . workspaceFocusKey ?? lastArea ;
99117 lastFocused . delete ( key ) ;
@@ -162,6 +180,8 @@ export function createWorkspaceFocusController(doc: Document) {
162180 const active = root instanceof ShadowRoot ? root . activeElement : doc . activeElement ;
163181 if ( active === target || ( active && target . contains ( active ) ) ) {
164182 if ( target !== area && ! isConversation ) revealInScrollers ( target ) ;
183+ // Focusing the already focused area fires no focusin, as when F6 has nowhere else to go.
184+ if ( target === area ) markKeyboardArea ( area ) ;
165185 return true ;
166186 }
167187 }
@@ -308,7 +328,11 @@ export function createWorkspaceFocusController(doc: Document) {
308328 const onUserInput = ( ) => {
309329 if ( cancelOnInput ) cancelRequest ( ) ;
310330 } ;
311- doc . addEventListener ( "pointerdown" , onUserInput , true ) ;
331+ const onPointerDown = ( ) => {
332+ unmarkKeyboardArea ( ) ;
333+ onUserInput ( ) ;
334+ } ;
335+ doc . addEventListener ( "pointerdown" , onPointerDown , true ) ;
312336 doc . addEventListener ( "keydown" , onUserInput , true ) ;
313337
314338 return {
@@ -320,9 +344,10 @@ export function createWorkspaceFocusController(doc: Document) {
320344 request,
321345 dispose : ( ) => {
322346 cancelRequest ( ) ;
347+ unmarkKeyboardArea ( ) ;
323348 doc . removeEventListener ( "focusin" , remember ) ;
324349 doc . removeEventListener ( "focusout" , recover ) ;
325- doc . removeEventListener ( "pointerdown" , onUserInput , true ) ;
350+ doc . removeEventListener ( "pointerdown" , onPointerDown , true ) ;
326351 doc . removeEventListener ( "keydown" , onUserInput , true ) ;
327352 } ,
328353 } ;
0 commit comments