Skip to content

Commit 0ff9725

Browse files
use FocusHighlightPath, see phetsims/scenery#676
1 parent 4e58c6f commit 0ff9725

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

js/photon-absorption/view/EmissionRateControlSliderNode.js

+38-24
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ define( function( require ) {
1111
'use strict';
1212

1313
// modules
14+
var Bounds2 = require( 'DOT/Bounds2' );
1415
var ButtonListener = require( 'SCENERY/input/ButtonListener' );
1516
var Dimension2 = require( 'DOT/Dimension2' );
16-
var FocusOverlay = require( 'SCENERY/overlays/FocusOverlay' );
17+
var FocusHighlightPath = require( 'SCENERY/accessibility/FocusHighlightPath' );
1718
var HSlider = require( 'SUN/HSlider' );
1819
var inherit = require( 'PHET_CORE/inherit' );
1920
var LinearGradient = require( 'SCENERY/util/LinearGradient' );
@@ -29,6 +30,8 @@ define( function( require ) {
2930
// constants
3031
var THUMB_SIZE = new Dimension2( 10, 18 ); // size of the slider thumb
3132
var TRACK_SIZE = new Dimension2( 50, 0.25 ); // size of the slider track
33+
var THUMB_RECTANGLE_WIDTH = 30; // a background rectangle behind the thumb, made visible when the slider has focus
34+
var THUMB_RECTANGLE_HEIGHT = 45; // a background rectangle behind the thumb, made visible when the slider has focus
3235

3336
/**
3437
* Constructor for an emission rate control slider.
@@ -50,31 +53,31 @@ define( function( require ) {
5053
// Create the slider. Frequency mapped from 0 to 1 so that there is a direct map to PhotonEmitterNode 'on' image
5154
// opacity.
5255
var sliderRange = new Range( 0, 1 );
56+
var sliderThumb = new EmissionRateThumbNode();
5357
this.emissionRateControlSlider = new HSlider( model.emissionFrequencyProperty, sliderRange, {
5458
trackSize: TRACK_SIZE,
5559
thumbFillEnabled: 'rgb(0, 203, 230)',
56-
thumbNode: new EmissionRateThumbNode(),
60+
thumbNode: sliderThumb,
5761
tandem: tandem,
5862
numberDecimalPlaces: 1,
5963
keyboardStep: sliderRange.getLength() / 10,
6064
shiftKeyboardStep: sliderRange.getLength() / 20,
6165
pageKeyboardStep: sliderRange.getLength() / 5
6266
} ); // @private
6367

64-
// a11y
65-
// create custom focus highlight with gray bg and thumb on top
66-
var customFocusHighlight = Rectangle.bounds( this.emissionRateControlSlider.focusHighlight.bounds.dilated( 2 ), {
67-
fill: '#ababab',
68-
stroke: FocusOverlay.innerFocusColor,
69-
lineWidth: 4
70-
// children: [ customFocusHighlightThumb ]
71-
});
68+
var sliderBounds = sliderThumb.backgroundRectangle.bounds;
69+
var parentBounds = sliderThumb.localToParentBounds( sliderBounds );
70+
this.emissionRateControlSlider.focusHighlight = new FocusHighlightPath( Shape.bounds( parentBounds ) );
7271

73-
var customFocusHighlightThumb = new EmissionRateThumbNode();
74-
customFocusHighlightThumb.center = customFocusHighlight.center;
75-
customFocusHighlight.addChild( customFocusHighlightThumb );
76-
77-
this.emissionRateControlSlider.setFocusHighlight( customFocusHighlight );
72+
// a11y
73+
this.emissionRateControlSlider.addAccessibleInputListener( {
74+
focus: function() {
75+
sliderThumb.backgroundRectangle.visible = true;
76+
},
77+
blur: function() {
78+
sliderThumb.backgroundRectangle.visible = false;
79+
}
80+
} );
7881

7982
// width of the background rectangle is larger than the slider to accentuate the thumb.
8083
var backgroundOffset = 4;
@@ -163,7 +166,7 @@ define( function( require ) {
163166
*/
164167
function EmissionRateThumbNode() {
165168

166-
var self = this;
169+
Node.call( this );
167170

168171
// draw the partial octagon shape of the slider.
169172
var thumbShape = new Shape();
@@ -175,17 +178,28 @@ define( function( require ) {
175178
thumbShape.horizontalLineTo( 0 );
176179
thumbShape.close();
177180

181+
// @public (a11y) - so it can be made invisible, helps focus highlight stand out
182+
this.backgroundRectangle = Rectangle.bounds( new Bounds2( 0, 0, THUMB_RECTANGLE_WIDTH, THUMB_RECTANGLE_HEIGHT ), {
183+
fill: '#ababab',
184+
visible: false // not visible until slider is focused
185+
} );
186+
178187
// supertype constructor
179-
Path.call( this, thumbShape, {
188+
var thumbPath = new Path( thumbShape, {
180189
lineWidth: 1,
181190
lineJoin: 'bevel',
182191
stroke: 'black',
183192
fill: 'rgb(0, 203, 230)'
184193
} );
185194

195+
thumbPath.center = this.backgroundRectangle.center;
196+
197+
this.addChild( this.backgroundRectangle );
198+
this.addChild( thumbPath );
199+
186200
// draw three lines along the vertical of the thumbNode.
187201
for ( var n = 1; n < 4; n++ ) {
188-
self.addChild( new Path(
202+
thumbPath.addChild( new Path(
189203
Shape.lineSegment(
190204
n * THUMB_SIZE.width / 5,
191205
THUMB_SIZE.height / 5,
@@ -200,25 +214,25 @@ define( function( require ) {
200214
// highlight thumb on pointer over
201215
var buttonListener = new ButtonListener( {
202216
over: function( event ) {
203-
self.fill = 'rgb(80,250,255)';
217+
thumbPath.fill = 'rgb(80,250,255)';
204218
},
205219
up: function( event ) {
206-
self.fill = 'rgb(0, 203, 230)';
220+
thumbPath.fill = 'rgb(0, 203, 230)';
207221
}
208222
} );
209-
self.addInputListener( buttonListener );
223+
thumbPath.addInputListener( buttonListener );
210224

211225
// make this easier to grab in touch environments
212-
this.touchArea = this.localBounds.dilatedXY( 20, 20 );
226+
this.touchArea = thumbPath.bounds.dilatedXY( 20, 20 );
213227

214228
this.disposeEmissionRateThumbNode = function() {
215-
self.removeInputListener( buttonListener );
229+
thumbPath.removeInputListener( buttonListener );
216230
};
217231
}
218232

219233
moleculesAndLight.register( 'EmissionRateThumbNode', EmissionRateThumbNode );
220234

221-
inherit( Path, EmissionRateThumbNode, {
235+
inherit( Node, EmissionRateThumbNode, {
222236
dispose: function() {
223237
this.disposeEmissionRateThumbNode();
224238
Path.prototype.dispose.call( this );

0 commit comments

Comments
 (0)