-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #684 from bitmovin/feature/add-keyboard-support-to…
…-the-new-settings-panel Add keyboard support to the new `SettingsPanel` design
- Loading branch information
Showing
10 changed files
with
125 additions
and
20 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
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
65 changes: 65 additions & 0 deletions
65
src/ts/components/settings/InteractiveSettingsPanelItem.ts
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,65 @@ | ||
import { SettingsPanelItem, SettingsPanelItemConfig } from './SettingsPanelItem'; | ||
import { Event, EventDispatcher, NoArgs } from '../../EventDispatcher'; | ||
import { PlayerAPI } from 'bitmovin-player'; | ||
import { UIInstanceManager } from '../../UIManager'; | ||
import { getKeyMapForPlatform } from '../../spatialnavigation/getKeyMapForPlatform'; | ||
import { Action } from '../../spatialnavigation/types'; | ||
|
||
/** | ||
* A settings panel item that can be interacted with using the keyboard or mouse. | ||
* Can be used when no interactive element is present as child item. | ||
*/ | ||
export class InteractiveSettingsPanelItem<Config extends SettingsPanelItemConfig> extends SettingsPanelItem<Config> { | ||
private events = { | ||
onClick: new EventDispatcher<InteractiveSettingsPanelItem<Config>, NoArgs>(), | ||
}; | ||
|
||
constructor(config: Config) { | ||
super(config); | ||
} | ||
|
||
configure(player: PlayerAPI, uimanager: UIInstanceManager) { | ||
super.configure(player, uimanager); | ||
|
||
const handleClickEvent = (event: UIEvent) => { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
this.onClickEvent(); | ||
}; | ||
|
||
this.getDomElement().on('click touchend', handleClickEvent); | ||
|
||
// Listen to keyboard events and trigger the click event when a select key is detected | ||
const handleKeyDown = (event: KeyboardEvent) => { | ||
const action = getKeyMapForPlatform()[event.keyCode]; | ||
const acceptedKeys = ['Enter', ' ']; | ||
const acceptedCodes = ['Enter', 'Space']; | ||
|
||
if (action === Action.SELECT || acceptedKeys.includes(event.key) || acceptedCodes.includes(event.code)) { | ||
handleClickEvent(event); | ||
} | ||
}; | ||
|
||
this.onFocusedChanged.subscribe((_, args) => { | ||
if (args.focused) { | ||
// Only listen to keyboard events when the element is focused | ||
this.getDomElement().on('keydown', handleKeyDown); | ||
} else { | ||
// Unregister the keyboard event listener when the element loses focus | ||
this.getDomElement().off('keydown', handleKeyDown); | ||
} | ||
}); | ||
} | ||
|
||
protected onClickEvent() { | ||
this.events.onClick.dispatch(this); | ||
} | ||
|
||
/** | ||
* Gets the event that is fired when the SettingsPanelItem is clicked. | ||
* @returns {Event<InteractiveSettingsPanelItem<Config>, NoArgs>} | ||
*/ | ||
get onClick(): Event<InteractiveSettingsPanelItem<Config>, NoArgs> { | ||
return this.events.onClick.getEvent(); | ||
} | ||
} |
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