phetsims / scenery

Scenery is an HTML5 scene graph.
MIT License
54 stars 12 forks source link

Hotkeys and their help content are duplicated and spread out. #1266

Closed pixelzoom closed 2 months ago

pixelzoom commented 3 years ago

I discovered this while working on adding a hotkey to keyboard help for Fourier in https://github.com/phetsims/fourier-making-waves/issues/93. MAL was used as the model for doing this. What I see is that the help content for a hotkey is not at all related to the actual implementation of a hotkey, so the info for a hotkey is spread out and duplicated.

An example from MAL.

MAL has keyboard help content for TimeControlNode, which uses PlayControlButton.

In PlayControlButton.js, the hotkey is implemented as Alt+K:

          globalKeyStateTracker.altKeyDown &&
          KeyboardUtils.isKeyEvent( event, KeyboardUtils.KEY_K )

In MoleculesAndLightKeyboardHelpContent.js, utility method KeyboardHelpSection.createPlayPauseKeyRow is used to create the keyboard help content:

const playPauseRow = KeyboardHelpSection.createPlayPauseKeyRow( pauseOrPlayShortcutString, pauseOrPlayShortcutDescriptionString, rowOptions );

It looks like this:

screenshot_1162

Looking at KeyboardHelpSection.createPlayPauseKeyRow, it hardcodes 'K':

  static createPlayPauseKeyRow( labelString, labelInnerContent, options ) {
    return KeyboardHelpSection.createGlobalHotkeyRow( labelString, labelInnerContent, new LetterKeyNode( 'K' ), options );
  }

Looking at pauseOrPlayShortcutDescriptionString, it hardcodes both 'Alt' and 'K':

      "pauseOrPlayShortcutDescription": {
        "value": "Pause or play action in Observation Window with Alt key plus K."
      },

So I've identified 3 places where the 'K' key is specified.

Now looking into KeyboardHelpSection.createGlobalHotkeyRow, it also hardcodes 'Alt' via TextKeyNode.alt():

  static createGlobalHotkeyRow( labelString, labelInnerContent, keyIcon, options ) {
    return KeyboardHelpSection.labelWithIcon(
      labelString,
      KeyboardHelpIconFactory.iconPlusIcon( TextKeyNode.alt(), keyIcon ),
      labelInnerContent,
      options
    );
  }

Now I've identified 3 places where the Alt key is specified.

This is not a scalable/sustainable way to specify hot keys and their help content. If I wanted to change this hotkey from Alt+K to Option-M, I'd have to find and change 6 places in the code.

jessegreenberg commented 2 months ago

This issue was slated for this iteration. To summarize the changes we have made:

jessegreenberg commented 2 months ago

The notes above complete this issue, closing.