-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HotkeyData, and better types for KeyDescriptor, see #1266
- Loading branch information
1 parent
65b3ce0
commit 6c672d1
Showing
8 changed files
with
161 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,13 +32,12 @@ | |
* @author Jonathan Olson <[email protected]> | ||
*/ | ||
|
||
import { EnglishKey, EnglishStringToCodeMap, hotkeyManager, OneKeyStroke, scenery } from '../imports.js'; | ||
import { AllowedKeysString, EnglishStringToCodeMap, hotkeyManager, KeyDescriptor, OneKeyStroke, scenery } from '../imports.js'; | ||
import optionize from '../../../phet-core/js/optionize.js'; | ||
import EnabledComponent, { EnabledComponentOptions } from '../../../axon/js/EnabledComponent.js'; | ||
import TProperty from '../../../axon/js/TProperty.js'; | ||
import BooleanProperty from '../../../axon/js/BooleanProperty.js'; | ||
import CallbackTimer from '../../../axon/js/CallbackTimer.js'; | ||
import KeyDescriptor from './KeyDescriptor.js'; | ||
import DerivedProperty from '../../../axon/js/DerivedProperty.js'; | ||
import TReadOnlyProperty from '../../../axon/js/TReadOnlyProperty.js'; | ||
|
||
|
@@ -109,7 +108,7 @@ export default class Hotkey extends EnabledComponent { | |
public readonly keyDescriptorProperty: TReadOnlyProperty<KeyDescriptor>; | ||
|
||
// All keys that are part of this hotkey (key + modifierKeys) as defined by the current KeyDescriptor. | ||
public keysProperty: TReadOnlyProperty<EnglishKey[]>; | ||
public keysProperty: TReadOnlyProperty<AllowedKeysString[]>; | ||
|
||
// A Property that tracks whether the hotkey is currently pressed. | ||
// Will be true if it meets the following conditions: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2024, University of Colorado Boulder | ||
|
||
/** | ||
* Data pertaining to a hotkey, including keystrokes and associated metadata for documentation and the keyboard help | ||
* dialog. | ||
* | ||
* @author Jesse Greenberg (PhET Interactive Simulations) | ||
*/ | ||
|
||
import { KeyDescriptor, OneKeyStroke, scenery } from '../../../scenery/js/imports.js'; | ||
import optionize from '../../../phet-core/js/optionize.js'; | ||
import TReadOnlyProperty from '../../../axon/js/TReadOnlyProperty.js'; | ||
import DerivedProperty from '../../../axon/js/DerivedProperty.js'; | ||
import InstanceRegistry from '../../../phet-core/js/documentation/InstanceRegistry.js'; | ||
|
||
// The type for a serialized HotkeyData object for documentation (binder). | ||
type SerializedHotkeyData = { | ||
keyStrings: string[]; | ||
repoName: string; | ||
binderName: string; | ||
global: boolean; | ||
}; | ||
|
||
export type HotkeyDataOptions = { | ||
|
||
// The list of keystrokes that will trigger the hotkey. Wrapping in a Property allows for i18n in the future. | ||
keyStringProperties: TReadOnlyProperty<OneKeyStroke>[]; | ||
|
||
// The visual label for this Hotkey in the Keyboard Help dialog. This will also be used as the label in | ||
// generated documentation, unless binderName is provided. | ||
keyboardHelpDialogLabelStringProperty?: TReadOnlyProperty<string> | null; | ||
|
||
// The PDOM label and description for this Hotkey in the Keyboard Help dialog. | ||
keyboardHelpDialogPDOMLabelStringProperty?: TReadOnlyProperty<string> | string | null; | ||
|
||
// Data for binder (generated documentation). | ||
repoName: string; // Name of the repository where the hotkey is defined. | ||
global?: boolean; // Is this Hotkey global? | ||
binderName?: string; // If there is no keyboardHelpDialogLabelStringProperty, this name will be used in documentation. | ||
}; | ||
|
||
export default class HotkeyData { | ||
public readonly keyStringProperties: TReadOnlyProperty<OneKeyStroke>[]; | ||
public readonly keyboardHelpDialogLabelStringProperty: TReadOnlyProperty<string> | null; | ||
public readonly keyboardHelpDialogPDOMLabelStringProperty: TReadOnlyProperty<string> | string | null; | ||
|
||
// KeyDescriptors derived from keyStringProperties. | ||
public readonly keyDescriptorsProperty: TReadOnlyProperty<KeyDescriptor[]>; | ||
|
||
private readonly repoName: string; | ||
private readonly global: boolean; | ||
private readonly binderName: string; | ||
|
||
public constructor( providedOptions: HotkeyDataOptions ) { | ||
assert && assert( providedOptions.binderName || providedOptions.keyboardHelpDialogLabelStringProperty, | ||
'You must provide some label for the hotkey' ); | ||
|
||
const options = optionize<HotkeyDataOptions>()( { | ||
keyboardHelpDialogPDOMLabelStringProperty: null, | ||
keyboardHelpDialogLabelStringProperty: null, | ||
global: false, | ||
binderName: '' | ||
}, providedOptions ); | ||
|
||
this.keyStringProperties = options.keyStringProperties; | ||
this.keyboardHelpDialogLabelStringProperty = options.keyboardHelpDialogLabelStringProperty; | ||
this.keyboardHelpDialogPDOMLabelStringProperty = options.keyboardHelpDialogPDOMLabelStringProperty; | ||
|
||
this.repoName = options.repoName; | ||
this.global = options.global; | ||
this.binderName = options.binderName; | ||
|
||
this.keyDescriptorsProperty = DerivedProperty.deriveAny( this.keyStringProperties, () => { | ||
return this.keyStringProperties.map( keyStringProperty => { | ||
return KeyDescriptor.keyStrokeToKeyDescriptor( keyStringProperty.value ); | ||
} ); | ||
} ); | ||
|
||
// Add this Hotkey to the binder registry for documentation. See documentation in the binder repository | ||
// for more information about how this is done. | ||
assert && phet?.chipper?.queryParameters?.binder && InstanceRegistry.registerHotkey( this ); | ||
} | ||
|
||
/** | ||
* Returns true if any of the keyStringProperties of this HotkeyData have the given keyStroke. | ||
*/ | ||
public hasKeyStroke( keyStroke: OneKeyStroke ): boolean { | ||
return this.keyStringProperties.some( keyStringProperty => keyStringProperty.value === keyStroke ); | ||
} | ||
|
||
/** | ||
* Serialization for usage with binder (generated documentation). | ||
*/ | ||
public serialize(): SerializedHotkeyData { | ||
return { | ||
keyStrings: this.keyStringProperties.map( keyStringProperty => keyStringProperty.value ), | ||
binderName: ( this.binderName || this.keyboardHelpDialogLabelStringProperty?.value )!, | ||
repoName: this.repoName, | ||
global: this.global | ||
}; | ||
} | ||
|
||
/** | ||
* Dispose of owned Properties to prevent memory leaks. | ||
*/ | ||
public dispose(): void { | ||
this.keyDescriptorsProperty.dispose(); | ||
} | ||
|
||
/** | ||
* Combine the keyStringProperties of an array of HotkeyData into a single array. Useful if you want to combine | ||
* multiple HotkeyData for a single KeyboardListener. | ||
*/ | ||
public static combineKeyStringProperties( hotkeyDataArray: HotkeyData[] ): TReadOnlyProperty<OneKeyStroke>[] { | ||
return hotkeyDataArray.reduce<TReadOnlyProperty<OneKeyStroke>[]>( ( accumulator, hotkeyData ) => { | ||
return accumulator.concat( hotkeyData.keyStringProperties ); | ||
}, [] ); | ||
} | ||
} | ||
|
||
scenery.register( 'HotkeyData', HotkeyData ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
* @author Jonathan Olson <[email protected]> | ||
*/ | ||
|
||
import { EnglishKey, eventCodeToEnglishString, FocusManager, globalHotkeyRegistry, globalKeyStateTracker, Hotkey, KeyboardUtils, metaEnglishKeys, Node, scenery } from '../imports.js'; | ||
import { AllowedKeysString, EnglishKeyString, eventCodeToEnglishString, FocusManager, globalHotkeyRegistry, globalKeyStateTracker, Hotkey, KeyboardUtils, metaEnglishKeys, Node, scenery } from '../imports.js'; | ||
import DerivedProperty, { UnknownDerivedProperty } from '../../../axon/js/DerivedProperty.js'; | ||
import TProperty from '../../../axon/js/TProperty.js'; | ||
import TinyProperty from '../../../axon/js/TinyProperty.js'; | ||
|
@@ -46,12 +46,12 @@ class HotkeyManager { | |
private readonly enabledHotkeysProperty: TProperty<Hotkey[]> = new TinyProperty( [] ); | ||
|
||
// The set of EnglishKeys that are currently pressed. | ||
private englishKeysDown: Set<EnglishKey> = new Set<EnglishKey>(); | ||
private englishKeysDown: Set<EnglishKeyString> = new Set<EnglishKeyString>(); | ||
|
||
// The current set of modifier keys (pressed or not) based on current enabled hotkeys | ||
// NOTE: Pressed modifier keys will prevent any other Hotkeys from becoming active. For example if you have a hotkey | ||
// with 'b+x', pressing 'b' will prevent any other hotkeys from becoming active. | ||
private modifierKeys: EnglishKey[] = []; | ||
private modifierKeys: AllowedKeysString[] = []; | ||
|
||
// Hotkeys that are actively pressed | ||
private readonly activeHotkeys: Set<Hotkey> = new Set<Hotkey>(); | ||
|
@@ -246,7 +246,7 @@ class HotkeyManager { | |
* 2. All modifier keys in the hotkey's modifierKeys pressed | ||
* 3. All modifier keys not in the hotkey's modifierKeys (but in the other hotkeys above) not pressed | ||
*/ | ||
private getHotkeysForMainKey( mainKey: EnglishKey ): Hotkey[] { | ||
private getHotkeysForMainKey( mainKey: EnglishKeyString ): Hotkey[] { | ||
|
||
// If the main key isn't down, there's no way it could be active | ||
if ( !this.englishKeysDown.has( mainKey ) ) { | ||
|