Skip to content

Commit

Permalink
Converting dot Utils usage, see phetsims/dot#4
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanolson committed Feb 14, 2025
1 parent 9d89623 commit 25f28ea
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
4 changes: 2 additions & 2 deletions js/NumberPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import StringUnionProperty from '../../axon/js/StringUnionProperty.js';
import TReadOnlyProperty from '../../axon/js/TReadOnlyProperty.js';
import Dimension2 from '../../dot/js/Dimension2.js';
import Range from '../../dot/js/Range.js';
import Utils from '../../dot/js/Utils.js';
import Shape from '../../kite/js/Shape.js';
import InstanceRegistry from '../../phet-core/js/documentation/InstanceRegistry.js';
import optionize, { combineOptions, EmptySelfOptions } from '../../phet-core/js/optionize.js';
Expand All @@ -41,6 +40,7 @@ import TSoundPlayer from '../../tambo/js/TSoundPlayer.js';
import PhetioObject from '../../tandem/js/PhetioObject.js';
import Tandem from '../../tandem/js/Tandem.js';
import sun from './sun.js';
import { toFixed } from '../../dot/js/util/toFixed.js';

const ButtonStateValues = [ 'up', 'down', 'over', 'out' ] as const;
type ButtonState = ( typeof ButtonStateValues )[number];
Expand Down Expand Up @@ -200,7 +200,7 @@ export default class NumberPicker extends AccessibleNumberSpinner( Node, 0 ) {
}, providedOptions );

if ( !options.formatValue ) {
options.formatValue = ( value: number ) => Utils.toFixed( value, options.decimalPlaces );
options.formatValue = ( value: number ) => toFixed( value, options.decimalPlaces );
}

// Color of arrows and top/bottom gradient when pressed
Expand Down
9 changes: 5 additions & 4 deletions js/Slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import TReadOnlyProperty from '../../axon/js/TReadOnlyProperty.js';
import CompletePiecewiseLinearFunction from '../../dot/js/CompletePiecewiseLinearFunction.js';
import Dimension2 from '../../dot/js/Dimension2.js';
import Range from '../../dot/js/Range.js';
import Utils from '../../dot/js/Utils.js';
import { clamp } from '../../dot/js/util/clamp.js';
import Vector2 from '../../dot/js/Vector2.js';
import assertMutuallyExclusiveOptions from '../../phet-core/js/assertMutuallyExclusiveOptions.js';
import InstanceRegistry from '../../phet-core/js/documentation/InstanceRegistry.js';
Expand Down Expand Up @@ -51,6 +51,7 @@ import SliderTick, { SliderTickOptions } from './SliderTick.js';
import type SliderTrack from './SliderTrack.js';
import sun from './sun.js';
import SunConstants from './SunConstants.js';
import { linear } from '../../dot/js/util/linear.js';

// constants
const DEFAULT_HORIZONTAL_TRACK_SIZE = new Dimension2( 100, 5 );
Expand Down Expand Up @@ -473,13 +474,13 @@ export default class Slider extends Sizable( AccessibleSlider( Node, 0 ) ) {
if ( this.proposedValue === null ) {

// clamp the current value to the enabled range if it changes
valueProperty.set( Utils.clamp( valueProperty.value, enabledRange.min, enabledRange.max ) );
valueProperty.set( clamp( valueProperty.value, enabledRange.min, enabledRange.max ) );
}
else {

// The user is holding the thumb, which may be outside the enabledRange. In that case, expanding the range
// could accommodate the outer value
const proposedValueInEnabledRange = Utils.clamp( this.proposedValue, enabledRange.min, enabledRange.max );
const proposedValueInEnabledRange = clamp( this.proposedValue, enabledRange.min, enabledRange.max );
const proposedValueInConstrainedRange = options.constrainValue( proposedValueInEnabledRange );
valueProperty.set( proposedValueInConstrainedRange );
}
Expand Down Expand Up @@ -691,7 +692,7 @@ class SliderConstraint extends LayoutConstraint {
// Takes a tick's value into the [0,1] range. This should be multiplied times the potential INTERIOR track width
// in order to get the position the tick should be at.
const normalizeTickValue = ( value: number ) => {
return Utils.linear( track.rangeProperty.value.min, track.rangeProperty.value.max, 0, 1, value );
return linear( track.rangeProperty.value.min, track.rangeProperty.value.max, 0, 1, value );
};

// NOTE: Due to visual overflow, our track's range (including the thumb extension) will actually go from
Expand Down
4 changes: 2 additions & 2 deletions js/ToggleSwitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import Emitter from '../../axon/js/Emitter.js';
import Property from '../../axon/js/Property.js';
import TEmitter from '../../axon/js/TEmitter.js';
import Dimension2 from '../../dot/js/Dimension2.js';
import Utils from '../../dot/js/Utils.js';
import Vector2 from '../../dot/js/Vector2.js';
import Shape from '../../kite/js/Shape.js';
import optionize from '../../phet-core/js/optionize.js';
Expand All @@ -38,6 +37,7 @@ import PhetioObject from '../../tandem/js/PhetioObject.js';
import Tandem from '../../tandem/js/Tandem.js';
import Utterance, { TAlertable } from '../../utterance-queue/js/Utterance.js';
import sun from './sun.js';
import { clamp } from '../../dot/js/util/clamp.js';

// constants
const DEFAULT_SIZE = new Dimension2( 60, 30 );
Expand Down Expand Up @@ -307,7 +307,7 @@ export default class ToggleSwitch<T> extends Voicing( Node ) {
const viewPoint = listener.getCurrentTarget().globalToLocalPoint( event.pointer.point );
const halfThumbWidth = thumbNode.width / 2;
const halfLineWidth = trackNode.lineWidth / 2;
thumbNode.centerX = Utils.clamp( viewPoint.x, halfThumbWidth - halfLineWidth, options.size.width - halfThumbWidth + halfLineWidth );
thumbNode.centerX = clamp( viewPoint.x, halfThumbWidth - halfLineWidth, options.size.width - halfThumbWidth + halfLineWidth );
rightTrackFillRectangle.rectWidth = thumbNode.right - halfLineWidth;

// whether the thumb is dragged outside of the possible range far enough beyond our threshold to potentially
Expand Down
15 changes: 9 additions & 6 deletions js/accessibility/AccessibleValueHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Property from '../../../axon/js/Property.js';
import TProperty from '../../../axon/js/TProperty.js';
import TReadOnlyProperty from '../../../axon/js/TReadOnlyProperty.js';
import Range from '../../../dot/js/Range.js';
import Utils from '../../../dot/js/Utils.js';
import { clamp } from '../../../dot/js/util/clamp.js';
import assertHasProperties from '../../../phet-core/js/assertHasProperties.js';
import optionize, { combineOptions } from '../../../phet-core/js/optionize.js';
import Orientation from '../../../phet-core/js/Orientation.js';
Expand All @@ -45,6 +45,9 @@ import Utterance from '../../../utterance-queue/js/Utterance.js';
import UtteranceQueue from '../../../utterance-queue/js/UtteranceQueue.js';
import sun from '../sun.js';
import AccessibleValueHandlerHotkeyDataCollection from './AccessibleValueHandlerHotkeyDataCollection.js';
import { numberOfDecimalPlaces } from '../../../dot/js/util/numberOfDecimalPlaces.js';
import { roundSymmetric } from '../../../dot/js/util/roundSymmetric.js';
import { equalsEpsilon } from '../../../dot/js/util/equalsEpsilon.js';

// constants
const DEFAULT_TAG_NAME = 'input';
Expand Down Expand Up @@ -893,7 +896,7 @@ const AccessibleValueHandler = <SuperType extends Constructor<Node>>( Type: Supe
}

// limit the value to the enabled range
this._valueProperty.set( Utils.clamp( constrainedValue, this._enabledRangeProperty.get().min, this._enabledRangeProperty.get().max ) );
this._valueProperty.set( clamp( constrainedValue, this._enabledRangeProperty.get().min, this._enabledRangeProperty.get().max ) );

// optional callback after the valueProperty is set (even if set to the same value) so that the listener
// can use the new value.
Expand Down Expand Up @@ -996,7 +999,7 @@ const AccessibleValueHandler = <SuperType extends Constructor<Node>>( Type: Supe
}

// limit to enabled range
newValue = Utils.clamp( newValue, this._enabledRangeProperty.get().min, this._enabledRangeProperty.get().max );
newValue = clamp( newValue, this._enabledRangeProperty.get().min, this._enabledRangeProperty.get().max );

// optionally constrain value
this._valueProperty.set( this._constrainValue( this._pdomMapValue( newValue, this._valueProperty.get() ) ) );
Expand Down Expand Up @@ -1221,7 +1224,7 @@ const AccessibleValueHandler = <SuperType extends Constructor<Node>>( Type: Supe
if ( platform.mobileSafari ) {

const smallestStep = Math.min( this.keyboardStep, this.shiftKeyboardStep, this.pageKeyboardStep );
stepValue = Math.pow( 10, -Utils.numberOfDecimalPlaces( smallestStep ) );
stepValue = Math.pow( 10, -numberOfDecimalPlaces( smallestStep ) );

const mappedMin = this._getMappedValue( this._enabledRangeProperty.get().min );
const mappedMax = this._getMappedValue( this._enabledRangeProperty.get().max );
Expand Down Expand Up @@ -1308,7 +1311,7 @@ const roundValue = function( newValue: number, currentValue: number, stepSize: n
if ( stepSize !== 0 ) {

// round the value to the nearest keyboard step
roundValue = Utils.roundSymmetric( roundValue / stepSize ) * stepSize;
roundValue = roundSymmetric( roundValue / stepSize ) * stepSize;

// go back a step if we went too far due to rounding
roundValue = correctRounding( roundValue, currentValue, stepSize );
Expand All @@ -1329,7 +1332,7 @@ const correctRounding = function( newValue: number, currentValue: number, stepSi

// it is possible that proposedStep will be larger than the stepSize but only because of precision
// constraints with floating point values, don't correct if that is the cases
const stepsAboutEqual = Utils.equalsEpsilon( proposedStep, stepSize, 1e-14 );
const stepsAboutEqual = equalsEpsilon( proposedStep, stepSize, 1e-14 );
if ( stepToFar && !stepsAboutEqual ) {
correctedValue += ( newValue > currentValue ) ? ( -stepSize ) : stepSize;
}
Expand Down

0 comments on commit 25f28ea

Please sign in to comment.