Skip to content

Commit

Permalink
Add and hook up locale switch, see: phetsims/number-pairs#22
Browse files Browse the repository at this point in the history
  • Loading branch information
marlitas committed Dec 12, 2024
1 parent 6583095 commit ddd7cc4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
4 changes: 2 additions & 2 deletions js/common/model/NumberSuiteCommonPreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class NumberSuiteCommonPreferences {
// preference Properties directly controlled by UI

// whether a second locale should be shown. A toggle switch is added on screens that support two locales when true.
public readonly showSecondLocaleProperty: Property<boolean>;
public readonly secondLocaleEnabledProperty: Property<boolean>;

// the second locale
public readonly secondLocaleProperty: LocaleProperty;
Expand Down Expand Up @@ -57,7 +57,7 @@ class NumberSuiteCommonPreferences {
public constructor( allURL: string ) {

// if a valid second locale was provided via a query parameter, display the second locale on sim startup
this.showSecondLocaleProperty = new BooleanProperty( !!NumberSuiteCommonQueryParameters.secondLocale );
this.secondLocaleEnabledProperty = new BooleanProperty( !!NumberSuiteCommonQueryParameters.secondLocale );

// if a secondLocale was provided via a query parameter, use that, otherwise default to the primaryLocale
this.secondLocaleProperty = new LocaleProperty( NumberSuiteCommonQueryParameters.secondLocale as Locale || localeProperty.value );
Expand Down
27 changes: 17 additions & 10 deletions js/common/view/LocaleSwitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ import NumberSuiteCommonUtteranceQueue from '../../../../number-suite-common/js/
import StringUtils from '../../../../phetcommon/js/util/StringUtils.js';
import PhetFont from '../../../../scenery-phet/js/PhetFont.js';
import { Text } from '../../../../scenery/js/imports.js';
import ABSwitch from '../../../../sun/js/ABSwitch.js';
import ABSwitch, { ABSwitchOptions } from '../../../../sun/js/ABSwitch.js';
import numberSuiteCommon from '../../numberSuiteCommon.js';
import optionize, { EmptySelfOptions } from '../../../../phet-core/js/optionize.js';

type SelfOptions = EmptySelfOptions;
type LocaleSwitchOptions = SelfOptions & ABSwitchOptions;

// constants
const TOGGLE_SWITCH_SIZE = new Dimension2( 40, 20 );
Expand All @@ -27,9 +31,10 @@ const TEXT_OPTIONS = {
export default class LocaleSwitch extends ABSwitch<boolean> {

public constructor(
preferences: NumberSuiteCommonPreferences,
preferences: Pick<NumberSuiteCommonPreferences, 'secondLocaleProperty' | 'secondLocaleEnabledProperty' | 'isPrimaryLocaleProperty' | 'autoHearEnabledProperty'>,
utteranceQueue: NumberSuiteCommonUtteranceQueue,
maxWidth: number
maxWidth: number,
providedOptions?: LocaleSwitchOptions
) {

const firstLanguageStringProperty = new DerivedProperty( [ localeProperty ], StringUtils.localeToLocalizedName );
Expand All @@ -40,15 +45,17 @@ export default class LocaleSwitch extends ABSwitch<boolean> {
const firstLanguageText = new Text( firstLanguageStringProperty, TEXT_OPTIONS );
const secondLanguageText = new Text( secondLanguageStringProperty, TEXT_OPTIONS );

const options = optionize<LocaleSwitchOptions, SelfOptions, ABSwitchOptions>()( {
spacing: SPACING,
toggleSwitchOptions: {
size: TOGGLE_SWITCH_SIZE
},
visibleProperty: new DerivedProperty( [ preferences.secondLocaleEnabledProperty ], showSecondLocale => showSecondLocale )
}, providedOptions );
super( preferences.isPrimaryLocaleProperty,
true, firstLanguageText,
false, secondLanguageText, {
spacing: SPACING,
toggleSwitchOptions: {
size: TOGGLE_SWITCH_SIZE
},
visibleProperty: new DerivedProperty( [ preferences.showSecondLocaleProperty ], showSecondLocale => showSecondLocale )
}
false, secondLanguageText,
options
);

// Speak speechData if autoHear is turned on.
Expand Down
2 changes: 1 addition & 1 deletion js/common/view/NumberSuiteCommonPreferencesNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default class NumberSuiteCommonPreferencesNode extends HBox {
const secondLanguageControl = new SecondLanguageControl(
preferences.secondLocaleProperty,
preferences.secondVoiceProperty,
preferences.showSecondLocaleProperty,
preferences.secondLocaleEnabledProperty,
preferences.isPrimaryLocaleProperty,
preferences.allURL,
utteranceQueue, {
Expand Down

0 comments on commit ddd7cc4

Please sign in to comment.