Skip to content

Commit

Permalink
add enabledProperty to KeyboardDragListener, #1313
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Mar 4, 2022
1 parent b9b75a0 commit 4f4cd37
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions js/listeners/KeyboardDragListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

import Action from '../../../axon/js/Action.js';
import EnabledComponent from '../../../axon/js/EnabledComponent.js';
import Emitter from '../../../axon/js/Emitter.js';
import Property from '../../../axon/js/Property.js';
import stepTimer from '../../../axon/js/stepTimer.js';
Expand All @@ -32,7 +33,7 @@ import EventType from '../../../tandem/js/EventType.js';
import Tandem from '../../../tandem/js/Tandem.js';
import { KeyboardUtils, scenery, SceneryEvent } from '../imports.js';

class KeyboardDragListener {
class KeyboardDragListener extends EnabledComponent {

/**
* @param {Object} [options]
Expand Down Expand Up @@ -89,6 +90,10 @@ class KeyboardDragListener {
// {number} - time interval at which holding down a hotkey group will trigger an associated listener, in ms
hotkeyHoldInterval: 800,

// EnabledComponent
// By default, do not instrument the enabledProperty; opt in with this option. See EnabledComponent
phetioEnabledPropertyInstrumented: false,

// phet-io
tandem: Tandem.REQUIRED,

Expand All @@ -100,6 +105,8 @@ class KeyboardDragListener {
assert && assert( options.shiftDragVelocity <= options.dragVelocity, 'shiftDragVelocity should be less than or equal to shiftDragVelocity, it is intended to provide more fine-grained control' );
assert && assert( options.dragBoundsProperty === null || options.dragBoundsProperty instanceof Property, 'dragBoundsProperty, if provided, should be a Property' );

super( options );

// @private, mutable attributes declared from options, see options for info, as well as getters and setters
this._start = options.start;
this._drag = options.drag;
Expand Down Expand Up @@ -197,6 +204,8 @@ class KeyboardDragListener {
const stepListener = this.step.bind( this );
stepTimer.addListener( stepListener );

this.enabledProperty.lazyLink( this.onEnabledPropertyChange.bind( this ) );

// @private - called in dispose
this._disposeKeyboardDragListener = () => {
stepTimer.removeListener( stepListener );
Expand Down Expand Up @@ -339,6 +348,14 @@ class KeyboardDragListener {
*/
set hotkeyHoldInterval( hotkeyHoldInterval ) { this._hotkeyHoldInterval = hotkeyHoldInterval; }

/**
* @private
* Fired when the enabledProperty changes
*/
onEnabledPropertyChange( enabled ) {
!enabled && this.interrupt();
}

/**
* Implements keyboard dragging when listener is attached to the Node, public so listener is attached
* with addInputListener()
Expand Down Expand Up @@ -386,7 +403,7 @@ class KeyboardDragListener {
}
}

this.dragStartAction.execute( event );
this.canDrag() && this.dragStartAction.execute( event );
}

/**
Expand Down Expand Up @@ -496,6 +513,14 @@ class KeyboardDragListener {
}
}

/**
* @private
* @returns {boolean} - can a drag begin
*/
canDrag() {
return this.enabledProperty.value;
}

/**
* Update the state of hotkeys, and fire hotkey callbacks if one is active.
* @private
Expand Down

0 comments on commit 4f4cd37

Please sign in to comment.