Skip to content

Commit

Permalink
Improvements for DOM input capture, allows text input handling. Preve…
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanolson committed Apr 4, 2024
1 parent 1b6dc3d commit 7603e70
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 9 deletions.
8 changes: 4 additions & 4 deletions js/input/EventContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export default class EventContext<out DOMEvent extends Event = Event> {
}

/**
* DOM (Scenery) nodes set dataset.noPreventDefault on their container if they don't want preventDefault to be called.
* We search up the tree to detect this.
* DOM (Scenery) nodes set dataset.sceneryAllowInput on their container if they don't want preventDefault to be called,
* or other effects that block input (e.g. setPointerCapture). We search up the tree to detect this.
*/
public hasNoPreventDefault(): boolean {
public allowsDOMInput(): boolean {
const target = this.domEvent?.target;


Expand All @@ -39,7 +39,7 @@ export default class EventContext<out DOMEvent extends Event = Event> {

while ( element ) {
// For DOM nodes, we can check for a data attribute
if ( element instanceof HTMLElement && element.dataset?.noPreventDefault === 'true' ) {
if ( element instanceof HTMLElement && element.dataset?.sceneryAllowInput === 'true' ) {
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions js/input/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ export default class Input extends PhetioObject {
!( this.passiveEvents === true ) &&
( callback !== this.mouseDown || platform.edge ) &&
batchType !== BatchedDOMEventType.ALT_TYPE &&
!context.hasNoPreventDefault()
!context.allowsDOMInput()
) {
// We cannot prevent a passive event, so don't try
context.domEvent.preventDefault();
Expand Down Expand Up @@ -1386,7 +1386,7 @@ export default class Input extends PhetioObject {
// this element (it will bubble). See https://github.com/phetsims/scenery/issues/464 and
// http://news.qooxdoo.org/mouse-capturing.
const target = this.attachToWindow ? document.body : this.display.domElement;
if ( target.setPointerCapture && context.domEvent.pointerId ) {
if ( target.setPointerCapture && context.domEvent.pointerId && !context.allowsDOMInput() ) {
// NOTE: This will error out if run on a playback destination, where a pointer with the given ID does not exist.
target.setPointerCapture( context.domEvent.pointerId );
}
Expand Down
2 changes: 1 addition & 1 deletion js/nodes/DOM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export default class DOM extends Node {
public isInputAllowed(): boolean { return this._allowInput; }

private invalidateAllowInput( allowInput: boolean ): void {
this._container.dataset.noPreventDefault = allowInput ? 'true' : 'false';
this._container.dataset.sceneryAllowInput = allowInput ? 'true' : 'false';
this._container.style.pointerEvents = allowInput ? 'auto' : 'none';
}

Expand Down
3 changes: 1 addition & 2 deletions tests/sandbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@
width: 1,
height: 1,
accessibility: true,
listenToOnlyElement: true,
allowCSSHacks: false
listenToOnlyElement: true
} );

const isDescendant = function (parent, child) {
Expand Down

0 comments on commit 7603e70

Please sign in to comment.