Skip to content
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): Add Subtitles menu for Dash #1400

Merged
merged 6 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/lib/viewers/controls/media/MediaSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import MediaSettingsMenuQuality, {
Props as QualityProps,
} from './MediaSettingsMenuQuality';
import MediaSettingsMenuRate, { Props as RateProps } from './MediaSettingsMenuRate';
import MediaSettingsMenuSubtitles, { Props as SubtitlesProps } from './MediaSettingsMenuSubtitles';
import Settings, { Menu, Props as SettingsProps } from '../settings';

export type Props = Partial<AudioTracksProps> &
Partial<QualityProps> &
Partial<SettingsProps> &
Partial<SubtitlesProps> &
AutoplayProps &
RateProps & { className?: string };

Expand All @@ -25,16 +27,23 @@ export default function MediaSettings({
onAutoplayChange,
onQualityChange,
onRateChange,
onSubtitleChange,
quality,
rate,
subtitle,
subtitles = [],
toggle,
}: Props): JSX.Element {
const { displayLanguage: subtitleDisplayLanguage } = subtitles.find(({ id }) => subtitle === id) || {
ConradJChan marked this conversation as resolved.
Show resolved Hide resolved
displayLanguage: __('off'),
};
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;
const showSubtitles = subtitles.length > 0;

return (
<Settings badge={badge} className={className} toggle={toggle}>
Expand All @@ -59,6 +68,14 @@ export default function MediaSettings({
value={getQualityLabel(quality)}
/>
)}
{showSubtitles && (
<Settings.MenuItem
data-testid="bp-media-settings-subtitles"
label={`${__('subtitles')}/CC`}
target={Menu.SUBTITLES}
value={subtitleDisplayLanguage}
/>
)}
{showAudioTrackItems && (
<Settings.MenuItem
data-testid="bp-media-settings-audiotracks"
Expand All @@ -74,6 +91,13 @@ export default function MediaSettings({
{quality && onQualityChange && (
<MediaSettingsMenuQuality onQualityChange={onQualityChange} quality={quality} />
)}
{showSubtitles && onSubtitleChange && (
ConradJChan marked this conversation as resolved.
Show resolved Hide resolved
<MediaSettingsMenuSubtitles
onSubtitleChange={onSubtitleChange}
subtitle={subtitle}
subtitles={subtitles}
/>
)}
{showAudioTrackItems && (
<MediaSettingsMenuAudioTracks
audioTrack={audioTrack}
Expand Down
45 changes: 45 additions & 0 deletions src/lib/viewers/controls/media/MediaSettingsMenuSubtitles.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from 'react';
import Settings, { Menu } from '../settings';

export const SUBTITLES_OFF = -1;
ConradJChan marked this conversation as resolved.
Show resolved Hide resolved

export type Subtitle = {
displayLanguage: string;
id: number;
};

export type Props = {
subtitle?: number;
subtitles: Array<Subtitle>;
onSubtitleChange: (id: number) => void;
};

export default function MediaSettingsMenuSubtitles({ subtitle, subtitles, onSubtitleChange }: Props): JSX.Element {
const { setActiveMenu } = React.useContext(Settings.Context);

const handleChange = (value: number): void => {
setActiveMenu(Menu.MAIN);
onSubtitleChange(value);
};

return (
<Settings.Menu name={Menu.SUBTITLES}>
<Settings.MenuBack label={`${__('subtitles')}/CC`} />
<Settings.RadioItem
isSelected={subtitle === SUBTITLES_OFF}
label={__('off')}
onChange={handleChange}
value={SUBTITLES_OFF}
/>
{subtitles.map(({ displayLanguage, id }) => (
<Settings.RadioItem
key={id}
isSelected={subtitle === id}
label={displayLanguage}
onChange={handleChange}
value={id}
/>
))}
</Settings.Menu>
);
}
26 changes: 26 additions & 0 deletions src/lib/viewers/controls/media/SubtitlesToggle.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@import './styles';

.bp-SubtitlesToggle {
@include bp-MediaButton;

&[aria-pressed='true'] {
.bp-SubtitlesToggle-text {
color: $white;
background-color: $box-blue;
}
}
}

.bp-SubtitlesToggle-text {
display: flex;
align-items: center;
justify-content: center;
width: 24px;
height: 24px;
color: $white;
font-weight: bold;
font-size: 12px;
letter-spacing: .1em;
background-color: rgba($black, .3); // Anything less than .3 looks too transparent on IE/Edge
border-radius: 4px;
}
23 changes: 23 additions & 0 deletions src/lib/viewers/controls/media/SubtitlesToggle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import noop from 'lodash/noop';
import MediaToggle from './MediaToggle';
import './SubtitlesToggle.scss';

export type Props = {
isShowingSubtitles?: boolean;
onSubtitlesToggle: (isShowingSubtitles: boolean) => void;
};

export default function SubtitlesToggle({ isShowingSubtitles, onSubtitlesToggle = noop }: Props): JSX.Element {
return (
<MediaToggle
aria-pressed={isShowingSubtitles}
className="bp-SubtitlesToggle"
data-testid="bp-media-controls-cc-button"
ConradJChan marked this conversation as resolved.
Show resolved Hide resolved
onClick={(): void => onSubtitlesToggle(!isShowingSubtitles)}
title={__('media_subtitles_cc')}
>
<div className="bp-SubtitlesToggle-text">CC</div>
</MediaToggle>
);
}
19 changes: 18 additions & 1 deletion src/lib/viewers/media/DashControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import HDBadge from '../controls/media/HDBadge';
import MediaFullscreenToggle, { Props as MediaFullscreenToggleProps } from '../controls/media/MediaFullscreenToggle';
import MediaSettings, { Props as MediaSettingsProps } from '../controls/media/MediaSettings';
import PlayPauseToggle, { Props as PlayControlsProps } from '../controls/media/PlayPauseToggle';
import SubtitlesToggle, { Props as SubtitlesToggleProps } from '../controls/media/SubtitlesToggle';
import TimeControls, { Props as TimeControlsProps } from '../controls/media/TimeControls';
import VolumeControls, { Props as VolumeControlsProps } from '../controls/media/VolumeControls';
import { SUBTITLES_OFF } from '../controls/media/MediaSettingsMenuSubtitles';
ConradJChan marked this conversation as resolved.
Show resolved Hide resolved
import './DashControls.scss';

export type Props = DurationLabelsProps &
MediaFullscreenToggleProps &
MediaSettingsProps &
PlayControlsProps &
SubtitlesToggleProps &
TimeControlsProps &
VolumeControlsProps & { isPlayingHD?: boolean };

Expand All @@ -31,12 +34,18 @@ export default function DashControls({
onPlayPause,
onQualityChange,
onRateChange,
onSubtitleChange,
onSubtitlesToggle,
onTimeChange,
onVolumeChange,
quality,
rate,
subtitle,
subtitles = [],
volume,
}: Props): JSX.Element {
const showSubtitlesToggle = subtitles.length > 0;
ConradJChan marked this conversation as resolved.
Show resolved Hide resolved

return (
<div className="bp-DashControls" data-testid="media-controls-wrapper">
<TimeControls
Expand All @@ -54,7 +63,12 @@ export default function DashControls({
</div>

<div className="bp-DashControls-group">
{/* CC Toggle */}
{showSubtitlesToggle && (
<SubtitlesToggle
isShowingSubtitles={subtitle !== SUBTITLES_OFF}
onSubtitlesToggle={onSubtitlesToggle}
/>
)}
<MediaSettings
audioTrack={audioTrack}
audioTracks={audioTracks}
Expand All @@ -65,8 +79,11 @@ export default function DashControls({
onAutoplayChange={onAutoplayChange}
onQualityChange={onQualityChange}
onRateChange={onRateChange}
onSubtitleChange={onSubtitleChange}
quality={quality}
rate={rate}
subtitle={subtitle}
subtitles={subtitles}
/>
<MediaFullscreenToggle onFullscreenToggle={onFullscreenToggle} />
</div>
Expand Down
Loading