Skip to content

Commit

Permalink
allow grace if changing audioEnabled when audio is not supported (for…
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Oct 4, 2022
1 parent aa77186 commit 0f26686
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
7 changes: 5 additions & 2 deletions js/A11yButtonsHBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,19 @@ class A11yButtonsHBox extends HBox {
a11yButtons.push( preferencesButton );
}

const supportsAudioPreferences = sim.preferencesModel.supportsAudioPreferences();

// For consistent PhET-iO support, we eagerly create the audio toggle button in every sim. But it is only
// added to the a11yButtons when sound is fully enabled in a sim runtime.
const audioToggleButton = new NavigationBarAudioToggleButton( audioManager.audioEnabledProperty, backgroundColorProperty, {
tandem: options.tandem.createTandem( 'audioToggleButton' ),
pointerAreaDilationX: 1,
pointerAreaDilationY: 0.15
pointerAreaDilationY: 0.15,
supportsAudioPreferences: supportsAudioPreferences
} );

// only put the sound on/off button on the nav bar if the sound library is enabled
if ( sim.preferencesModel.supportsAudioPreferences() ) {
if ( supportsAudioPreferences ) {
a11yButtons.push( audioToggleButton );
}

Expand Down
30 changes: 18 additions & 12 deletions js/NavigationBarAudioToggleButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import BooleanProperty from '../../axon/js/BooleanProperty.js';
import TReadOnlyProperty from '../../axon/js/TReadOnlyProperty.js';
import Vector2 from '../../dot/js/Vector2.js';
import { Shape } from '../../kite/js/imports.js';
import optionize, { EmptySelfOptions } from '../../phet-core/js/optionize.js';
import optionize from '../../phet-core/js/optionize.js';
import PickRequired from '../../phet-core/js/types/PickRequired.js';
import { Color, Node, Path, Rectangle, voicingManager } from '../../scenery/js/imports.js';
import ToggleNode from '../../sun/js/ToggleNode.js';
Expand Down Expand Up @@ -49,7 +49,11 @@ const MIN_CURVE_RADIUS = MED_CURVE_RADIUS - RADIUS_STEPPER;
const CURVE_ANGLE = Math.PI / 2.7;
const NEG_CURVE_ANGLE = CURVE_ANGLE * -1.0;

type SelfOptions = EmptySelfOptions;
type SelfOptions = {

// This button is always created for a consistent PhET-iO API, but has limited functionality if audio is not supported.
supportsAudioPreferences: boolean;
};
type NavigationBarAudioToggleButtonOptions = SelfOptions & PickRequired<JoistButtonOptions, 'tandem'> & Pick<JoistButtonOptions, 'pointerAreaDilationX' | 'pointerAreaDilationY'>;

class NavigationBarAudioToggleButton extends JoistButton {
Expand All @@ -58,7 +62,7 @@ class NavigationBarAudioToggleButton extends JoistButton {
backgroundColorProperty: TReadOnlyProperty<Color>,
providedOptions: NavigationBarAudioToggleButtonOptions ) {

const options = optionize<NavigationBarAudioToggleButtonOptions, EmptySelfOptions, JoistButtonOptions>()( {
const options = optionize<NavigationBarAudioToggleButtonOptions, SelfOptions, JoistButtonOptions>()( {

// JoistButton options
highlightExtensionWidth: 5 + 3.6,
Expand Down Expand Up @@ -163,17 +167,19 @@ class NavigationBarAudioToggleButton extends JoistButton {
// pdom attribute lets user know when the toggle is pressed, linked lazily so that an alert isn't triggered
// on construction and must be unlinked in dispose
const soundUtterance = new ActivationUtterance();
const pressedListener = ( value: boolean ) => {
this.setPDOMAttribute( 'aria-pressed', value );

soundUtterance.alert = value ? JoistStrings.a11y.soundToggle.alert.simSoundOn
: JoistStrings.a11y.soundToggle.alert.simSoundOff;
this.alertDescriptionUtterance( soundUtterance );
if ( voicingManager.voicingFullyEnabledProperty.value ) {
voicingManager.speakIgnoringEnabled( soundUtterance );
const enabledListener = ( enabled: boolean ) => {
if ( options.supportsAudioPreferences ) {
this.setPDOMAttribute( 'aria-pressed', enabled );

soundUtterance.alert = enabled ? JoistStrings.a11y.soundToggle.alert.simSoundOn
: JoistStrings.a11y.soundToggle.alert.simSoundOff;
this.alertDescriptionUtterance( soundUtterance );
if ( voicingManager.voicingFullyEnabledProperty.value ) {
voicingManager.speakIgnoringEnabled( soundUtterance );
}
}
};
soundEnabledProperty.lazyLink( pressedListener );
soundEnabledProperty.lazyLink( enabledListener );
this.setPDOMAttribute( 'aria-pressed', soundEnabledProperty.get() );

// If no subcomponents of the audioManager that are enabled disable this button
Expand Down

0 comments on commit 0f26686

Please sign in to comment.