Skip to content

Commit

Permalink
PDOMTree cannot change focus while scenery is already responding to a…
Browse files Browse the repository at this point in the history
  • Loading branch information
jessegreenberg committed Sep 6, 2024
1 parent 44e2c80 commit 9722647
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 5 additions & 1 deletion js/accessibility/pdom/PDOMTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,11 @@ const PDOMTree = {
* @private
*/
afterOp( focusedNode ) {
focusedNode && focusedNode.focusable && focusedNode.focus();

// If Scenery is in the middle of dispatching focus events, it is buggy to change focus again internally.
if ( !BrowserEvents.dispatchingFocusEvents ) {
focusedNode && focusedNode.focusable && focusedNode.focus();
}
BrowserEvents.blockFocusCallbacks = false;
},

Expand Down
13 changes: 12 additions & 1 deletion js/input/BrowserEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const BrowserEvents = {
// caused by user interaction.
blockFocusCallbacks: false,

// True while Scenery is dispatching focus and blur related events. Scenery (PDOMTree) needs to restore focus
// after operations, but that can be very buggy while focus events are already being handled.
dispatchingFocusEvents: false,

/**
* Adds a Display to the list of displays that will be notified of input events.
* @public
Expand Down Expand Up @@ -763,12 +767,16 @@ const BrowserEvents = {

// NOTE: Will be called without a proper 'this' reference. Do NOT rely on it here.

BrowserEvents.dispatchingFocusEvents = true;

// Update state related to focus immediately and allowing for reentrancy for focus state
// that must match the browser's focus state.
FocusManager.updatePDOMFocusFromEvent( BrowserEvents.attachedDisplays, domEvent, true );

BrowserEvents.batchWindowEvent( new EventContext( domEvent ), BatchedDOMEventType.ALT_TYPE, 'focusIn', true );

BrowserEvents.dispatchingFocusEvents = false;

sceneryLog && sceneryLog.OnInput && sceneryLog.pop();
},

Expand All @@ -778,12 +786,15 @@ const BrowserEvents = {

// NOTE: Will be called without a proper 'this' reference. Do NOT rely on it here.

BrowserEvents.dispatchingFocusEvents = true;

// Update state related to focus immediately and allowing for reentrancy for focus state
// that must match the browser's focus state.
FocusManager.updatePDOMFocusFromEvent( BrowserEvents.attachedDisplays, domEvent, false );

BrowserEvents.batchWindowEvent( new EventContext( domEvent ), BatchedDOMEventType.ALT_TYPE, 'focusOut', true );

BrowserEvents.dispatchingFocusEvents = false;

sceneryLog && sceneryLog.OnInput && sceneryLog.pop();
},

Expand Down

0 comments on commit 9722647

Please sign in to comment.