-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(controls): Audio tracks menu for Dash viewer #1391
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,68 @@ | ||
import React from 'react'; | ||
import noop from 'lodash/noop'; | ||
import getLanguageName from '../../../lang'; | ||
import MediaSettingsMenuAudioTracks, { AudioTrack, Props as AudioTracksProps } from './MediaSettingsAudioTracks'; | ||
import MediaSettingsMenuAutoplay, { Props as AutoplayProps } from './MediaSettingsMenuAutoplay'; | ||
import MediaSettingsMenuRate, { Props as RateProps } from './MediaSettingsMenuRate'; | ||
import Settings, { Menu } from '../settings'; | ||
|
||
export type Props = AutoplayProps & RateProps & { className?: string }; | ||
export type Props = Partial<AudioTracksProps> & AutoplayProps & RateProps & { className?: string }; | ||
|
||
const generateAudioTrackLabel = (language: string, index: number): string => { | ||
let label = `${__('track')} ${index + 1}`; | ||
if (language !== 'und') { | ||
label = `${label} (${getLanguageName(language) || language})`; | ||
} | ||
|
||
return label; | ||
}; | ||
|
||
const addLabels = (audioTracks: Array<AudioTrack>): Array<AudioTrack> => | ||
audioTracks.map((track, index) => { | ||
const { language } = track; | ||
const label = generateAudioTrackLabel(language, index); | ||
return { | ||
...track, | ||
label, | ||
}; | ||
}); | ||
|
||
export default function MediaSettings({ | ||
audioTrack, | ||
audioTracks = [], | ||
autoplay, | ||
className, | ||
onAudioTrackChange = noop, | ||
onAutoplayChange, | ||
onRateChange, | ||
rate, | ||
}: Props): JSX.Element { | ||
const autoValue = autoplay ? __('media_autoplay_enabled') : __('media_autoplay_disabled'); | ||
const rateValue = rate === '1.0' || !rate ? __('media_speed_normal') : rate; | ||
const labelledAudioTracks = React.useMemo(() => addLabels(audioTracks), [audioTracks]); | ||
const hydratedSelectedAudioTrack = labelledAudioTracks.find(({ id }) => audioTrack === id); | ||
const audioTrackLabel = hydratedSelectedAudioTrack ? hydratedSelectedAudioTrack.label : ''; | ||
const showAudioTrackItems = audioTracks.length > 1; | ||
|
||
return ( | ||
<Settings className={className}> | ||
<Settings.Menu name={Menu.MAIN}> | ||
<Settings.MenuItem label={__('media_autoplay')} target={Menu.AUTOPLAY} value={autoValue} /> | ||
<Settings.MenuItem label={__('media_speed')} target={Menu.RATE} value={rateValue} /> | ||
{showAudioTrackItems && ( | ||
<Settings.MenuItem label={__('media_audio')} target={Menu.AUDIO} value={audioTrackLabel} /> | ||
)} | ||
</Settings.Menu> | ||
|
||
<MediaSettingsMenuAutoplay autoplay={autoplay} onAutoplayChange={onAutoplayChange} /> | ||
<MediaSettingsMenuRate onRateChange={onRateChange} rate={rate} /> | ||
{showAudioTrackItems && ( | ||
<MediaSettingsMenuAudioTracks | ||
audioTrack={audioTrack} | ||
audioTracks={labelledAudioTracks} | ||
onAudioTrackChange={onAudioTrackChange} | ||
/> | ||
)} | ||
</Settings> | ||
); | ||
} |
43 changes: 43 additions & 0 deletions
43
src/lib/viewers/controls/media/MediaSettingsAudioTracks.tsx
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,43 @@ | ||
import React from 'react'; | ||
import Settings, { Menu } from '../settings'; | ||
|
||
export type AudioTrack = { | ||
id: number; | ||
label: string; | ||
language: string; | ||
role: string; | ||
}; | ||
|
||
export type Props = { | ||
audioTrack?: number; | ||
audioTracks: Array<AudioTrack>; | ||
onAudioTrackChange: (id: number) => void; | ||
}; | ||
|
||
export default function MediaSettingsMenuAudioTracks({ | ||
audioTrack, | ||
audioTracks, | ||
onAudioTrackChange, | ||
}: Props): JSX.Element { | ||
const { setActiveMenu } = React.useContext(Settings.Context); | ||
|
||
const handleChange = (value: number): void => { | ||
setActiveMenu(Menu.MAIN); | ||
onAudioTrackChange(value); | ||
}; | ||
|
||
return ( | ||
<Settings.Menu name={Menu.AUDIO}> | ||
<Settings.MenuBack label={__('media_audio')} /> | ||
{audioTracks.map(({ id, label }) => ( | ||
<Settings.RadioItem | ||
key={id} | ||
isSelected={audioTrack === id} | ||
label={label} | ||
onChange={handleChange} | ||
value={id} | ||
/> | ||
))} | ||
</Settings.Menu> | ||
); | ||
} |
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,6 @@ | ||
const audioTracks = [ | ||
{ id: 0, label: '', language: 'und', role: 'audio0' }, | ||
{ id: 1, label: '', language: 'en', role: 'audio1' }, | ||
]; | ||
|
||
export default audioTracks; |
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
51 changes: 51 additions & 0 deletions
51
src/lib/viewers/controls/media/__tests__/MediaSettingsAudioTracks-test.tsx
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,51 @@ | ||
import React from 'react'; | ||
import { mount, ReactWrapper } from 'enzyme'; | ||
import audioTracks from '../__mocks__/audioTracks'; | ||
import MediaSettingsAudioTracks from '../MediaSettingsAudioTracks'; | ||
import Settings, { Context, Menu } from '../../settings'; | ||
|
||
describe('MediaSettingsAudioTracks', () => { | ||
const getContext = (): Partial<Context> => ({ setActiveMenu: jest.fn() }); | ||
const getWrapper = (props = {}, context = getContext()): ReactWrapper => | ||
mount( | ||
<MediaSettingsAudioTracks | ||
audioTrack={1} | ||
audioTracks={audioTracks} | ||
onAudioTrackChange={jest.fn()} | ||
{...props} | ||
/>, | ||
{ | ||
wrappingComponent: Settings.Context.Provider, | ||
wrappingComponentProps: { value: context }, | ||
}, | ||
); | ||
|
||
describe('event handlers', () => { | ||
test('should surface the selected item on change', () => { | ||
const onAudioTrackChange = jest.fn(); | ||
const wrapper = getWrapper({ onAudioTrackChange }); | ||
|
||
wrapper.find({ value: 0 }).simulate('click'); | ||
|
||
expect(onAudioTrackChange).toBeCalledWith(0); | ||
}); | ||
|
||
test('should reset the active menu on change', () => { | ||
const context = getContext(); | ||
const wrapper = getWrapper({}, context); | ||
|
||
wrapper.find({ value: 0 }).simulate('click'); | ||
|
||
expect(context.setActiveMenu).toBeCalledWith(Menu.MAIN); | ||
}); | ||
}); | ||
|
||
describe('render', () => { | ||
test('should return a valid wrapper', () => { | ||
const wrapper = getWrapper(); | ||
|
||
expect(wrapper.exists(Settings.MenuBack)).toBe(true); | ||
expect(wrapper.exists(Settings.RadioItem)).toBe(true); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this could probably be simplified, but we can go with it for now. We may need to revisit as we add more menus for Dash, since this component will probably get large quickly.