Skip to content

Commit

Permalink
mix AccessibleNumberTweaker into NumberPicker, see #318
Browse files Browse the repository at this point in the history
  • Loading branch information
jessegreenberg authored and marlitas committed Jun 28, 2022
1 parent ff30fd5 commit a10900e
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions js/NumberPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ define( function( require ) {
'use strict';

// modules
var AccessibleNumberTweaker = require( 'SUN/accessibility/AccessibleNumberTweaker' );
var BooleanIO = require( 'ifphetio!PHET_IO/types/BooleanIO' );
var ButtonListener = require( 'SCENERY/input/ButtonListener' );
var Color = require( 'SCENERY/util/Color' );
Expand All @@ -20,7 +21,6 @@ define( function( require ) {
var FireOnHoldInputListener = require( 'SCENERY_PHET/buttons/FireOnHoldInputListener' );
var FocusHighlightPath = require( 'SCENERY/accessibility/FocusHighlightPath' );
var inherit = require( 'PHET_CORE/inherit' );
var KeyboardUtil = require( 'SCENERY/accessibility/KeyboardUtil' );
var LinearGradient = require( 'SCENERY/util/LinearGradient' );
var Node = require( 'SCENERY/nodes/Node' );
var Path = require( 'SCENERY/nodes/Path' );
Expand Down Expand Up @@ -79,9 +79,7 @@ define( function( require ) {
tandem: Tandem.required,

// a11y
tagName: 'input',
inputType: 'number',
inputValue: valueProperty.get(),
a11yPageValueDelta: 2, // {number} - change in value when using page up/page down, see AccessibleNumberTweaker

/**
* Converts a value to a string to be displayed in a Text node. NOTE: If this function can give different strings
Expand Down Expand Up @@ -367,24 +365,6 @@ define( function( require ) {
updateColors( state, enabled, downBackground, self.downArrow, backgroundColors, arrowColors );
} );

// @private (a11y) - update Property value on input from keyboard or assistive technology
this.accessibleInputListener = this.addAccessibleInputListener( {
keydown: function( event ) {

// prevent user from changing value with number or the space keys, arrow keys change value
if ( KeyboardUtil.isArrowKey( event.keyCode ) || KeyboardUtil.isNumberKey( event.keyCode ) || event.keyCode === KeyboardUtil.KEY_SPACE ) {
event.preventDefault();

if ( event.keyCode === KeyboardUtil.KEY_RIGHT_ARROW || event.keyCode === KeyboardUtil.KEY_UP_ARROW ) {
valueProperty.set( Math.min( options.upFunction( valueProperty.get() ), rangeProperty.get().max ) );
}
else if ( event.keyCode === KeyboardUtil.KEY_LEFT_ARROW || event.keyCode === KeyboardUtil.KEY_DOWN_ARROW ) {
valueProperty.set( Math.max( options.downFunction( valueProperty.get() ), rangeProperty.get().min ) );
}
}
}
} );

this.mutate( options );

// Dilate based on consistent technique which brings into account transform of this node.
Expand All @@ -402,6 +382,15 @@ define( function( require ) {
bottomRight: options.cornerRadius
} )
);

// initialize accessibility features - must reach into up function to get delta
this.initializeAccessibleNumberTweaker( valueProperty, rangeProperty, new Property( true ), _.extend( {
a11yValueDelta: options.upFunction( valueProperty.get() ) - valueProperty.get()
}, options ) );

// update style with keyboard input, Emitters owned by this instance and disposed in AccessibleNumberTweaker
this.incrementDownEmitter.addListener( function( isDown ) { upStateProperty.value = ( isDown ? 'down' : 'up' ); } );
this.decrementDownEmitter.addListener( function( isDown ) { downStateProperty.value = ( isDown ? 'down' : 'up' ); } );
}

sceneryPhet.register( 'NumberPicker', NumberPicker );
Expand Down Expand Up @@ -464,7 +453,7 @@ define( function( require ) {
}
};

return inherit( Node, NumberPicker, {
inherit( Node, NumberPicker, {

// @public Ensures that this node is eligible for GC.
dispose: function() {
Expand All @@ -480,7 +469,8 @@ define( function( require ) {

this.valueProperty.unlink( this.valueObserver );

this.removeAccessibleInputListener( this.accessibleInputListener );
// dispose a11y features
this.disposeAccessibleNumberTweaker();

Node.prototype.dispose.call( this );
},
Expand All @@ -492,4 +482,9 @@ define( function( require ) {
this.upArrow.visible = this.downArrow.visible = visible;
}
} );

// mix accessibility into NumberPicker
AccessibleNumberTweaker.mixInto( NumberPicker );

return NumberPicker;
} );

0 comments on commit a10900e

Please sign in to comment.