Commit 07b346a0 authored by Becca Hughes's avatar Becca Hughes Committed by Commit Bot

Media Controls: Clean up the DOM

Do not create the volume slider on the new media controls
as we don't need it. Also, avoids adding elements to the
DOM that are permanently hidden.

BUG=821961,821414

Change-Id: Ia0e334b6079450d4a3b084371629f6b6ba37176c
Reviewed-on: https://chromium-review.googlesource.com/991293
Commit-Queue: Becca Hughes <beccahughes@chromium.org>
Reviewed-by: default avatarMounir Lamouri <mlamouri@chromium.org>
Cr-Commit-Position: refs/heads/master@{#547743}
parent de05c7c5
...@@ -155,6 +155,12 @@ bool ShouldShowFullscreenButton(const HTMLMediaElement& media_element) { ...@@ -155,6 +155,12 @@ bool ShouldShowFullscreenButton(const HTMLMediaElement& media_element) {
return true; return true;
} }
void MaybeAppendChild(Element* parent, Element* child) {
DCHECK(parent);
if (child)
parent->AppendChild(child);
}
bool ShouldShowPictureInPictureButton(HTMLMediaElement& media_element) { bool ShouldShowPictureInPictureButton(HTMLMediaElement& media_element) {
return media_element.SupportsPictureInPicture(); return media_element.SupportsPictureInPicture();
} }
...@@ -438,7 +444,8 @@ MediaControlsImpl* MediaControlsImpl::Create(HTMLMediaElement& media_element, ...@@ -438,7 +444,8 @@ MediaControlsImpl* MediaControlsImpl::Create(HTMLMediaElement& media_element,
// | | (-internal-media-controls-button-panel) // | | (-internal-media-controls-button-panel)
// | | <video> only, otherwise children are directly attached to parent // | | <video> only, otherwise children are directly attached to parent
// | +-MediaControlPlayButtonElement // | +-MediaControlPlayButtonElement
// | | (-webkit-media-controls-play-button) // | | (-webkit-media-controls-play-button)
// | | {only present if audio only or ModernMediaControls is disabled}
// | +-MediaControlCurrentTimeDisplayElement // | +-MediaControlCurrentTimeDisplayElement
// | | (-webkit-media-controls-current-time-display) // | | (-webkit-media-controls-current-time-display)
// | +-MediaControlRemainingTimeDisplayElement // | +-MediaControlRemainingTimeDisplayElement
...@@ -450,16 +457,20 @@ MediaControlsImpl* MediaControlsImpl::Create(HTMLMediaElement& media_element, ...@@ -450,16 +457,20 @@ MediaControlsImpl* MediaControlsImpl::Create(HTMLMediaElement& media_element,
// | | (-webkit-media-controls-mute-button) // | | (-webkit-media-controls-mute-button)
// | +-MediaControlVolumeSliderElement // | +-MediaControlVolumeSliderElement
// | | (-webkit-media-controls-volume-slider) // | | (-webkit-media-controls-volume-slider)
// | | {if not ModernMediaControlsEnabled}
// | +-MediaControlPictureInPictureButtonElement // | +-MediaControlPictureInPictureButtonElement
// | | (-webkit-media-controls-picture-in-picture-button) // | | (-webkit-media-controls-picture-in-picture-button)
// | +-MediaControlFullscreenButtonElement // | +-MediaControlFullscreenButtonElement
// | | (-webkit-media-controls-fullscreen-button) // | | (-webkit-media-controls-fullscreen-button)
// | +-MediaControlDownloadButtonElement // | +-MediaControlDownloadButtonElement
// | | (-internal-media-controls-download-button) // | | (-internal-media-controls-download-button)
// | | {on the overflow menu if ModernMediaControls is enabled}
// | +-MediaControlToggleClosedCaptionsButtonElement // | +-MediaControlToggleClosedCaptionsButtonElement
// | | (-webkit-media-controls-toggle-closed-captions-button) // | | (-webkit-media-controls-toggle-closed-captions-button)
// | | {on the overflow menu if ModernMediaControls is enabled}
// | +-MediaControlCastButtonElement // | +-MediaControlCastButtonElement
// | (-internal-media-controls-cast-button) // | (-internal-media-controls-cast-button)
// | {on the overflow menu if ModernMediaControls is enabled}
// \-MediaControlTimelineElement // \-MediaControlTimelineElement
// (-webkit-media-controls-timeline) // (-webkit-media-controls-timeline)
// +-MediaControlTextTrackListElement // +-MediaControlTextTrackListElement
...@@ -516,9 +527,12 @@ void MediaControlsImpl::InitializeControls() { ...@@ -516,9 +527,12 @@ void MediaControlsImpl::InitializeControls() {
timeline_ = new MediaControlTimelineElement(*this); timeline_ = new MediaControlTimelineElement(*this);
mute_button_ = new MediaControlMuteButtonElement(*this); mute_button_ = new MediaControlMuteButtonElement(*this);
volume_slider_ = new MediaControlVolumeSliderElement(*this); // The volume slider should be shown if we are using the legacy controls.
if (PreferHiddenVolumeControls(GetDocument())) if (!IsModern()) {
volume_slider_->SetIsWanted(false); volume_slider_ = new MediaControlVolumeSliderElement(*this);
if (PreferHiddenVolumeControls(GetDocument()))
volume_slider_->SetIsWanted(false);
}
// TODO(apacible): Enable for modern controls when SVG is added. // TODO(apacible): Enable for modern controls when SVG is added.
if (RuntimeEnabledFeatures::PictureInPictureEnabled() && !IsModern() && if (RuntimeEnabledFeatures::PictureInPictureEnabled() && !IsModern() &&
...@@ -585,14 +599,19 @@ void MediaControlsImpl::PopulatePanel() { ...@@ -585,14 +599,19 @@ void MediaControlsImpl::PopulatePanel() {
Element* button_panel = panel_; Element* button_panel = panel_;
if (IsModern() && MediaElement().IsHTMLVideoElement() && if (IsModern() && MediaElement().IsHTMLVideoElement() &&
!is_acting_as_audio_controls_) { !is_acting_as_audio_controls_) {
if (scrubbing_message_) MaybeAppendChild(panel_, scrubbing_message_);
panel_->AppendChild(scrubbing_message_);
panel_->AppendChild(overlay_play_button_); panel_->AppendChild(overlay_play_button_);
panel_->AppendChild(media_button_panel_); panel_->AppendChild(media_button_panel_);
button_panel = media_button_panel_; button_panel = media_button_panel_;
} }
button_panel->AppendChild(play_button_); // The play button should only be on the button panel if we are playing audio
// only or are using the legacy controls.
if (!IsModern() || is_acting_as_audio_controls_ ||
MediaElement().IsHTMLAudioElement()) {
button_panel->AppendChild(play_button_);
}
button_panel->AppendChild(current_time_display_); button_panel->AppendChild(current_time_display_);
button_panel->AppendChild(duration_display_); button_panel->AppendChild(duration_display_);
...@@ -604,15 +623,20 @@ void MediaControlsImpl::PopulatePanel() { ...@@ -604,15 +623,20 @@ void MediaControlsImpl::PopulatePanel() {
panel_->AppendChild(timeline_); panel_->AppendChild(timeline_);
button_panel->AppendChild(mute_button_); button_panel->AppendChild(mute_button_);
button_panel->AppendChild(volume_slider_);
if (picture_in_picture_button_) MaybeAppendChild(button_panel, volume_slider_);
button_panel->AppendChild(picture_in_picture_button_); MaybeAppendChild(button_panel, picture_in_picture_button_);
button_panel->AppendChild(fullscreen_button_); button_panel->AppendChild(fullscreen_button_);
button_panel->AppendChild(download_button_);
button_panel->AppendChild(cast_button_); // The download, cast and captions buttons should not be present on the modern
button_panel->AppendChild(toggle_closed_captions_button_); // controls button panel.
if (!IsModern()) {
button_panel->AppendChild(download_button_);
button_panel->AppendChild(cast_button_);
button_panel->AppendChild(toggle_closed_captions_button_);
}
button_panel->AppendChild(overflow_menu_); button_panel->AppendChild(overflow_menu_);
} }
...@@ -1367,15 +1391,17 @@ bool MediaControlsImpl::ContainsRelatedTarget(Event* event) { ...@@ -1367,15 +1391,17 @@ bool MediaControlsImpl::ContainsRelatedTarget(Event* event) {
void MediaControlsImpl::OnVolumeChange() { void MediaControlsImpl::OnVolumeChange() {
mute_button_->UpdateDisplayType(); mute_button_->UpdateDisplayType();
volume_slider_->SetVolume(MediaElement().muted() ? 0
: MediaElement().volume());
// Update visibility of volume controls. // Update visibility of volume controls.
// TODO(mlamouri): it should not be part of the volumechange handling because // TODO(mlamouri): it should not be part of the volumechange handling because
// it is using audio availability as input. // it is using audio availability as input.
BatchedControlUpdate batch(this); BatchedControlUpdate batch(this);
volume_slider_->SetIsWanted(MediaElement().HasAudio() && if (volume_slider_) {
!PreferHiddenVolumeControls(GetDocument())); volume_slider_->SetVolume(MediaElement().muted() ? 0
: MediaElement().volume());
volume_slider_->SetIsWanted(MediaElement().HasAudio() &&
!PreferHiddenVolumeControls(GetDocument()));
}
if (ShouldShowDisabledControls()) { if (ShouldShowDisabledControls()) {
mute_button_->SetIsWanted(true); mute_button_->SetIsWanted(true);
mute_button_->setAttribute( mute_button_->setAttribute(
......
...@@ -50,6 +50,22 @@ bool MediaControlsWindowEventListener::operator==( ...@@ -50,6 +50,22 @@ bool MediaControlsWindowEventListener::operator==(
return this == &other; return this == &other;
} }
void MediaControlsWindowEventListener::MaybeAddClickEventListener(
Element* element) {
if (!element)
return;
element->addEventListener(EventTypeNames::click, this, false);
}
void MediaControlsWindowEventListener::MaybeRemoveClickEventListener(
Element* element) {
if (!element)
return;
element->removeEventListener(EventTypeNames::click, this, false);
}
void MediaControlsWindowEventListener::Start() { void MediaControlsWindowEventListener::Start() {
if (is_active_) if (is_active_)
return; return;
...@@ -74,12 +90,8 @@ void MediaControlsWindowEventListener::Start() { ...@@ -74,12 +90,8 @@ void MediaControlsWindowEventListener::Start() {
false); false);
media_controls_->cast_button_->addEventListener(EventTypeNames::click, this, media_controls_->cast_button_->addEventListener(EventTypeNames::click, this,
false); false);
media_controls_->volume_slider_->addEventListener(EventTypeNames::click, this, MaybeAddClickEventListener(media_controls_->volume_slider_);
false); MaybeAddClickEventListener(media_controls_->overlay_play_button_);
if (media_controls_->overlay_play_button_) {
media_controls_->overlay_play_button_->addEventListener(
EventTypeNames::click, this, false);
}
is_active_ = true; is_active_ = true;
} }
...@@ -109,12 +121,8 @@ void MediaControlsWindowEventListener::Stop() { ...@@ -109,12 +121,8 @@ void MediaControlsWindowEventListener::Stop() {
false); false);
media_controls_->cast_button_->removeEventListener(EventTypeNames::click, media_controls_->cast_button_->removeEventListener(EventTypeNames::click,
this, false); this, false);
media_controls_->volume_slider_->removeEventListener(EventTypeNames::click, MaybeRemoveClickEventListener(media_controls_->volume_slider_);
this, false); MaybeRemoveClickEventListener(media_controls_->overlay_play_button_);
if (media_controls_->overlay_play_button_) {
media_controls_->overlay_play_button_->removeEventListener(
EventTypeNames::click, this, false);
}
is_active_ = false; is_active_ = false;
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
namespace blink { namespace blink {
class Element;
class MediaControlsImpl; class MediaControlsImpl;
class MediaControlsWindowEventListener final : public EventListener { class MediaControlsWindowEventListener final : public EventListener {
...@@ -30,6 +31,10 @@ class MediaControlsWindowEventListener final : public EventListener { ...@@ -30,6 +31,10 @@ class MediaControlsWindowEventListener final : public EventListener {
void handleEvent(ExecutionContext*, Event*) override; void handleEvent(ExecutionContext*, Event*) override;
// Adds or removes a click event listener if the provided element is not null.
void MaybeAddClickEventListener(Element*);
void MaybeRemoveClickEventListener(Element*);
Member<MediaControlsImpl> media_controls_; Member<MediaControlsImpl> media_controls_;
Callback callback_; Callback callback_;
bool is_active_; bool is_active_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment