Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: third_party/WebKit/Source/core/html/shadow/MediaControls.cpp

Issue 2701433003: Hide overlay play button if it can't be shown without clipping (Closed)
Patch Set: Turn on overlay play button in layout test Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011, 2012 Apple Inc. All rights reserved. 2 * Copyright (C) 2011, 2012 Apple Inc. All rights reserved.
3 * Copyright (C) 2011, 2012 Google Inc. All rights reserved. 3 * Copyright (C) 2011, 2012 Google Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 11 matching lines...) Expand all
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */ 25 */
26 26
27 #include "core/html/shadow/MediaControls.h" 27 #include "core/html/shadow/MediaControls.h"
28 28
29 #include "bindings/core/v8/ExceptionState.h" 29 #include "bindings/core/v8/ExceptionState.h"
30 #include "core/dom/ClientRect.h" 30 #include "core/dom/ClientRect.h"
31 #include "core/dom/Fullscreen.h" 31 #include "core/dom/Fullscreen.h"
32 #include "core/dom/ResizeObserver.h"
33 #include "core/dom/ResizeObserverCallback.h"
34 #include "core/dom/ResizeObserverEntry.h"
32 #include "core/dom/TaskRunnerHelper.h" 35 #include "core/dom/TaskRunnerHelper.h"
33 #include "core/events/MouseEvent.h" 36 #include "core/events/MouseEvent.h"
34 #include "core/frame/Settings.h" 37 #include "core/frame/Settings.h"
35 #include "core/html/HTMLMediaElement.h" 38 #include "core/html/HTMLMediaElement.h"
36 #include "core/html/HTMLVideoElement.h" 39 #include "core/html/HTMLVideoElement.h"
37 #include "core/html/shadow/MediaControlsMediaEventListener.h" 40 #include "core/html/shadow/MediaControlsMediaEventListener.h"
38 #include "core/html/shadow/MediaControlsOrientationLockDelegate.h" 41 #include "core/html/shadow/MediaControlsOrientationLockDelegate.h"
39 #include "core/html/shadow/MediaControlsWindowEventListener.h" 42 #include "core/html/shadow/MediaControlsWindowEventListener.h"
40 #include "core/html/track/TextTrackContainer.h" 43 #include "core/html/track/TextTrackContainer.h"
41 #include "core/html/track/TextTrackList.h" 44 #include "core/html/track/TextTrackList.h"
42 #include "core/layout/LayoutObject.h" 45 #include "core/layout/LayoutObject.h"
43 #include "core/layout/LayoutTheme.h" 46 #include "core/layout/LayoutTheme.h"
44 #include "platform/EventDispatchForbiddenScope.h" 47 #include "platform/EventDispatchForbiddenScope.h"
45 48
46 namespace blink { 49 namespace blink {
47 50
51 namespace {
52
53 constexpr int kOverlayPlayButtonWidth = 48;
54 constexpr int kOverlayPlayButtonHeight = 48;
55 constexpr int kOverlayBottomMargin = 10;
56 constexpr int kAndroidMediaPanelHeight = 48;
57
58 // If these change, they must also be changed in
59 // LayoutTests/virtual/android/media/controls/overlay-play-button-narrow.html.
60 constexpr int kMinWidthForOverlayPlayButton = kOverlayPlayButtonWidth;
61 constexpr int kMinHeightForOverlayPlayButton = kOverlayPlayButtonHeight +
62 (2 * kAndroidMediaPanelHeight) +
63 (2 * kOverlayBottomMargin);
64
65 } // anonymous namespace
66
48 // If you change this value, then also update the corresponding value in 67 // If you change this value, then also update the corresponding value in
49 // LayoutTests/media/media-controls.js. 68 // LayoutTests/media/media-controls.js.
50 static const double timeWithoutMouseMovementBeforeHidingMediaControls = 3; 69 static const double timeWithoutMouseMovementBeforeHidingMediaControls = 3;
51 70
52 static bool shouldShowFullscreenButton(const HTMLMediaElement& mediaElement) { 71 static bool shouldShowFullscreenButton(const HTMLMediaElement& mediaElement) {
53 // Unconditionally allow the user to exit fullscreen if we are in it 72 // Unconditionally allow the user to exit fullscreen if we are in it
54 // now. Especially on android, when we might not yet know if 73 // now. Especially on android, when we might not yet know if
55 // fullscreen is supported, we sometimes guess incorrectly and show 74 // fullscreen is supported, we sometimes guess incorrectly and show
56 // the button earlier, and we don't want to remove it here if the 75 // the button earlier, and we don't want to remove it here if the
57 // user chose to enter fullscreen. crbug.com/500732 . 76 // user chose to enter fullscreen. crbug.com/500732 .
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 } 126 }
108 127
109 private: 128 private:
110 Member<MediaControls> m_controls; 129 Member<MediaControls> m_controls;
111 static int s_batchDepth; 130 static int s_batchDepth;
112 }; 131 };
113 132
114 // Count of number open batches for controls visibility. 133 // Count of number open batches for controls visibility.
115 int MediaControls::BatchedControlUpdate::s_batchDepth = 0; 134 int MediaControls::BatchedControlUpdate::s_batchDepth = 0;
116 135
136 class MediaControls::MediaControlsResizeObserverCallback final
137 : public ResizeObserverCallback {
138 public:
139 explicit MediaControlsResizeObserverCallback(MediaControls* controls)
140 : m_controls(controls) {
141 DCHECK(controls);
142 }
143 ~MediaControlsResizeObserverCallback() override = default;
144
145 void handleEvent(const HeapVector<Member<ResizeObserverEntry>>& entries,
146 ResizeObserver* observer) override {
147 DCHECK_EQ(1u, entries.size());
148 DCHECK_EQ(entries[0]->target(), m_controls->m_mediaElement);
149 m_controls->notifyElementSizeChanged(entries[0]->contentRect());
150 }
151
152 DEFINE_INLINE_TRACE() {
153 visitor->trace(m_controls);
154 ResizeObserverCallback::trace(visitor);
155 }
156
157 private:
158 Member<MediaControls> m_controls;
159 };
160
117 MediaControls::MediaControls(HTMLMediaElement& mediaElement) 161 MediaControls::MediaControls(HTMLMediaElement& mediaElement)
118 : HTMLDivElement(mediaElement.document()), 162 : HTMLDivElement(mediaElement.document()),
119 m_mediaElement(&mediaElement), 163 m_mediaElement(&mediaElement),
120 m_overlayEnclosure(nullptr), 164 m_overlayEnclosure(nullptr),
121 m_overlayPlayButton(nullptr), 165 m_overlayPlayButton(nullptr),
122 m_overlayCastButton(nullptr), 166 m_overlayCastButton(nullptr),
123 m_enclosure(nullptr), 167 m_enclosure(nullptr),
124 m_panel(nullptr), 168 m_panel(nullptr),
125 m_playButton(nullptr), 169 m_playButton(nullptr),
126 m_timeline(nullptr), 170 m_timeline(nullptr),
(...skipping 12 matching lines...) Expand all
139 this, 183 this,
140 WTF::bind(&MediaControls::hideAllMenus, wrapWeakPersistent(this)))), 184 WTF::bind(&MediaControls::hideAllMenus, wrapWeakPersistent(this)))),
141 m_orientationLockDelegate(nullptr), 185 m_orientationLockDelegate(nullptr),
142 m_hideMediaControlsTimer(TaskRunnerHelper::get(TaskType::UnspecedTimer, 186 m_hideMediaControlsTimer(TaskRunnerHelper::get(TaskType::UnspecedTimer,
143 &mediaElement.document()), 187 &mediaElement.document()),
144 this, 188 this,
145 &MediaControls::hideMediaControlsTimerFired), 189 &MediaControls::hideMediaControlsTimerFired),
146 m_hideTimerBehaviorFlags(IgnoreNone), 190 m_hideTimerBehaviorFlags(IgnoreNone),
147 m_isMouseOverControls(false), 191 m_isMouseOverControls(false),
148 m_isPausedForScrubbing(false), 192 m_isPausedForScrubbing(false),
149 m_panelWidthChangedTimer(TaskRunnerHelper::get(TaskType::UnspecedTimer, 193 m_resizeObserver(ResizeObserver::create(
mlamouri (slow - plz ping) 2017/03/02 13:56:50 I've just added a `onInsertedIntoDocument` and `on
150 &mediaElement.document()), 194 mediaElement.document(),
151 this, 195 new MediaControlsResizeObserverCallback(this))),
152 &MediaControls::panelWidthChangedTimerFired), 196 m_elementSizeChangedTimer(TaskRunnerHelper::get(TaskType::UnspecedTimer,
153 m_panelWidth(0), 197 &mediaElement.document()),
154 m_keepShowingUntilTimerFires(false) {} 198 this,
199 &MediaControls::elementSizeChangedTimerFired),
200 m_keepShowingUntilTimerFires(false) {
201 m_resizeObserver->observe(m_mediaElement);
202 }
155 203
156 MediaControls* MediaControls::create(HTMLMediaElement& mediaElement, 204 MediaControls* MediaControls::create(HTMLMediaElement& mediaElement,
157 ShadowRoot& shadowRoot) { 205 ShadowRoot& shadowRoot) {
158 MediaControls* controls = new MediaControls(mediaElement); 206 MediaControls* controls = new MediaControls(mediaElement);
159 controls->setShadowPseudoId(AtomicString("-webkit-media-controls")); 207 controls->setShadowPseudoId(AtomicString("-webkit-media-controls"));
160 controls->initializeControls(); 208 controls->initializeControls();
161 controls->reset(); 209 controls->reset();
162 210
163 // Initialize the orientation lock when going fullscreen feature. 211 // Initialize the orientation lock when going fullscreen feature.
164 if (RuntimeEnabledFeatures::videoFullscreenOrientationLockEnabled() && 212 if (RuntimeEnabledFeatures::videoFullscreenOrientationLockEnabled() &&
(...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 stopHideMediaControlsTimer(); 829 stopHideMediaControlsTimer();
782 startHideMediaControlsTimer(); 830 startHideMediaControlsTimer();
783 } 831 }
784 832
785 void MediaControls::onExitedFullscreen() { 833 void MediaControls::onExitedFullscreen() {
786 m_fullscreenButton->setIsFullscreen(false); 834 m_fullscreenButton->setIsFullscreen(false);
787 stopHideMediaControlsTimer(); 835 stopHideMediaControlsTimer();
788 startHideMediaControlsTimer(); 836 startHideMediaControlsTimer();
789 } 837 }
790 838
791 void MediaControls::notifyPanelWidthChanged(const LayoutUnit& newWidth) { 839 void MediaControls::notifyElementSizeChanged(ClientRect* newSize) {
792 // Don't bother to do any work if this matches the most recent panel
793 // width, since we're called after layout.
794 // Note that this code permits a bad frame on resize, since it is 840 // Note that this code permits a bad frame on resize, since it is
795 // run after the relayout / paint happens. It would be great to improve 841 // run after the relayout / paint happens. It would be great to improve
796 // this, but it would be even greater to move this code entirely to 842 // this, but it would be even greater to move this code entirely to
797 // JS and fix it there. 843 // JS and fix it there.
798 m_panelWidth = newWidth.toInt(); 844
845 IntSize oldSize = m_size;
846 m_size.setWidth(newSize->width());
847 m_size.setHeight(newSize->height());
799 848
800 // Adjust for effective zoom. 849 // Adjust for effective zoom.
801 if (!m_panel->layoutObject() || !m_panel->layoutObject()->style()) 850 if (m_panel->layoutObject() && m_panel->layoutObject()->style()) {
802 return; 851 m_size.setWidth(ceil(m_size.width() /
803 m_panelWidth = 852 m_panel->layoutObject()->style()->effectiveZoom()));
804 ceil(m_panelWidth / m_panel->layoutObject()->style()->effectiveZoom()); 853 m_size.setHeight(ceil(m_size.height() /
854 m_panel->layoutObject()->style()->effectiveZoom()));
855 }
805 856
806 m_panelWidthChangedTimer.startOneShot(0, BLINK_FROM_HERE); 857 // Don't bother to do any work if this matches the most recent size.
858 if (oldSize != m_size)
859 m_elementSizeChangedTimer.startOneShot(0, BLINK_FROM_HERE);
807 } 860 }
808 861
809 void MediaControls::panelWidthChangedTimerFired(TimerBase*) { 862 void MediaControls::elementSizeChangedTimerFired(TimerBase*) {
810 computeWhichControlsFit(); 863 computeWhichControlsFit();
811 } 864 }
812 865
813 void MediaControls::computeWhichControlsFit() { 866 void MediaControls::computeWhichControlsFit() {
814 // Hide all controls that don't fit, and show the ones that do. 867 // Hide all controls that don't fit, and show the ones that do.
815 // This might be better suited for a layout, but since JS media controls 868 // This might be better suited for a layout, but since JS media controls
816 // won't benefit from that anwyay, we just do it here like JS will. 869 // won't benefit from that anwyay, we just do it here like JS will.
817 870
818 // Controls that we'll hide / show, in order of decreasing priority. 871 // Controls that we'll hide / show, in order of decreasing priority.
819 MediaControlElement* elements[] = { 872 MediaControlElement* elements[] = {
820 // Exclude m_overflowMenu; we handle it specially. 873 // Exclude m_overflowMenu; we handle it specially.
821 m_playButton.get(), 874 m_playButton.get(),
822 m_fullscreenButton.get(), 875 m_fullscreenButton.get(),
823 m_downloadButton.get(), 876 m_downloadButton.get(),
824 m_timeline.get(), 877 m_timeline.get(),
825 m_muteButton.get(), 878 m_muteButton.get(),
826 m_volumeSlider.get(), 879 m_volumeSlider.get(),
827 m_toggleClosedCaptionsButton.get(), 880 m_toggleClosedCaptionsButton.get(),
828 m_castButton.get(), 881 m_castButton.get(),
829 m_currentTimeDisplay.get(), 882 m_currentTimeDisplay.get(),
830 m_durationDisplay.get(), 883 m_durationDisplay.get(),
831 }; 884 };
832 885
833 // TODO(mlamouri): we need a more dynamic way to find out the width of an 886 // TODO(mlamouri): we need a more dynamic way to find out the width of an
834 // element. 887 // element.
835 const int sliderMargin = 36; // Sliders have 18px margin on each side. 888 const int sliderMargin = 36; // Sliders have 18px margin on each side.
836 889
837 if (!m_panelWidth) { 890 if (!m_size.width()) {
838 // No layout yet -- hide everything, then make them show up later. 891 // No layout yet -- hide everything, then make them show up later.
839 // This prevents the wrong controls from being shown briefly 892 // This prevents the wrong controls from being shown briefly
840 // immediately after the first layout and paint, but before we have 893 // immediately after the first layout and paint, but before we have
841 // a chance to revise them. 894 // a chance to revise them.
842 for (MediaControlElement* element : elements) { 895 for (MediaControlElement* element : elements) {
843 if (element) 896 if (element)
844 element->setDoesFit(false); 897 element->setDoesFit(false);
845 } 898 }
846 return; 899 return;
847 } 900 }
(...skipping 24 matching lines...) Expand all
872 MediaControlElement* firstDisplacedElement = nullptr; 925 MediaControlElement* firstDisplacedElement = nullptr;
873 // For each control that fits, enable it in order of decreasing priority. 926 // For each control that fits, enable it in order of decreasing priority.
874 for (MediaControlElement* element : elements) { 927 for (MediaControlElement* element : elements) {
875 if (!element) 928 if (!element)
876 continue; 929 continue;
877 int width = minimumWidth; 930 int width = minimumWidth;
878 if ((element == m_timeline.get()) || (element == m_volumeSlider.get())) 931 if ((element == m_timeline.get()) || (element == m_volumeSlider.get()))
879 width += sliderMargin; 932 width += sliderMargin;
880 element->shouldShowButtonInOverflowMenu(false); 933 element->shouldShowButtonInOverflowMenu(false);
881 if (element->isWanted()) { 934 if (element->isWanted()) {
882 if (usedWidth + width <= m_panelWidth) { 935 if (usedWidth + width <= m_size.width()) {
883 element->setDoesFit(true); 936 element->setDoesFit(true);
884 usedWidth += width; 937 usedWidth += width;
885 } else { 938 } else {
886 element->setDoesFit(false); 939 element->setDoesFit(false);
887 element->shouldShowButtonInOverflowMenu(true); 940 element->shouldShowButtonInOverflowMenu(true);
888 if (element->hasOverflowButton()) 941 if (element->hasOverflowButton())
889 overflowElements.push_front(element); 942 overflowElements.push_front(element);
890 // We want a way to access the first media element that was 943 // We want a way to access the first media element that was
891 // removed. If we don't end up needing an overflow menu, we can 944 // removed. If we don't end up needing an overflow menu, we can
892 // use the space the overflow menu would have taken up to 945 // use the space the overflow menu would have taken up to
893 // instead display that media element. 946 // instead display that media element.
894 if (!element->hasOverflowButton() && !firstDisplacedElement) 947 if (!element->hasOverflowButton() && !firstDisplacedElement)
895 firstDisplacedElement = element; 948 firstDisplacedElement = element;
896 } 949 }
897 } 950 }
898 } 951 }
899 952
900 // If we don't have at least two overflow elements, we will not show the 953 // If we don't have at least two overflow elements, we will not show the
901 // overflow menu. 954 // overflow menu.
902 if (overflowElements.empty()) { 955 if (overflowElements.empty()) {
903 m_overflowMenu->setIsWanted(false); 956 m_overflowMenu->setIsWanted(false);
904 usedWidth -= minimumWidth; 957 usedWidth -= minimumWidth;
905 if (firstDisplacedElement) { 958 if (firstDisplacedElement) {
906 int width = minimumWidth; 959 int width = minimumWidth;
907 if ((firstDisplacedElement == m_timeline.get()) || 960 if ((firstDisplacedElement == m_timeline.get()) ||
908 (firstDisplacedElement == m_volumeSlider.get())) 961 (firstDisplacedElement == m_volumeSlider.get()))
909 width += sliderMargin; 962 width += sliderMargin;
910 if (usedWidth + width <= m_panelWidth) 963 if (usedWidth + width <= m_size.width())
911 firstDisplacedElement->setDoesFit(true); 964 firstDisplacedElement->setDoesFit(true);
912 } 965 }
913 } else if (overflowElements.size() == 1) { 966 } else if (overflowElements.size() == 1) {
914 m_overflowMenu->setIsWanted(false); 967 m_overflowMenu->setIsWanted(false);
915 overflowElements.front()->setDoesFit(true); 968 overflowElements.front()->setDoesFit(true);
916 } 969 }
970
971 // Decide if the overlay play button fits.
972 if (m_overlayPlayButton) {
973 bool doesFit = m_size.width() >= kMinWidthForOverlayPlayButton &&
974 m_size.height() >= kMinHeightForOverlayPlayButton;
975 m_overlayPlayButton->setDoesFit(doesFit);
976 }
917 } 977 }
918 978
919 void MediaControls::invalidate(Element* element) { 979 void MediaControls::invalidate(Element* element) {
920 if (!element) 980 if (!element)
921 return; 981 return;
922 982
923 if (LayoutObject* layoutObject = element->layoutObject()) 983 if (LayoutObject* layoutObject = element->layoutObject())
924 layoutObject 984 layoutObject
925 ->setShouldDoFullPaintInvalidationIncludingNonCompositingDescendants(); 985 ->setShouldDoFullPaintInvalidationIncludingNonCompositingDescendants();
926 } 986 }
(...skipping 23 matching lines...) Expand all
950 void MediaControls::hideAllMenus() { 1010 void MediaControls::hideAllMenus() {
951 m_windowEventListener->stop(); 1011 m_windowEventListener->stop();
952 1012
953 if (m_overflowList->isWanted()) 1013 if (m_overflowList->isWanted())
954 m_overflowList->setIsWanted(false); 1014 m_overflowList->setIsWanted(false);
955 if (m_textTrackList->isWanted()) 1015 if (m_textTrackList->isWanted())
956 m_textTrackList->setVisible(false); 1016 m_textTrackList->setVisible(false);
957 } 1017 }
958 1018
959 DEFINE_TRACE(MediaControls) { 1019 DEFINE_TRACE(MediaControls) {
1020 visitor->trace(m_resizeObserver);
960 visitor->trace(m_mediaElement); 1021 visitor->trace(m_mediaElement);
961 visitor->trace(m_panel); 1022 visitor->trace(m_panel);
962 visitor->trace(m_overlayPlayButton); 1023 visitor->trace(m_overlayPlayButton);
963 visitor->trace(m_overlayEnclosure); 1024 visitor->trace(m_overlayEnclosure);
964 visitor->trace(m_playButton); 1025 visitor->trace(m_playButton);
965 visitor->trace(m_currentTimeDisplay); 1026 visitor->trace(m_currentTimeDisplay);
966 visitor->trace(m_timeline); 1027 visitor->trace(m_timeline);
967 visitor->trace(m_muteButton); 1028 visitor->trace(m_muteButton);
968 visitor->trace(m_volumeSlider); 1029 visitor->trace(m_volumeSlider);
969 visitor->trace(m_toggleClosedCaptionsButton); 1030 visitor->trace(m_toggleClosedCaptionsButton);
970 visitor->trace(m_fullscreenButton); 1031 visitor->trace(m_fullscreenButton);
971 visitor->trace(m_downloadButton); 1032 visitor->trace(m_downloadButton);
972 visitor->trace(m_durationDisplay); 1033 visitor->trace(m_durationDisplay);
973 visitor->trace(m_enclosure); 1034 visitor->trace(m_enclosure);
974 visitor->trace(m_textTrackList); 1035 visitor->trace(m_textTrackList);
975 visitor->trace(m_overflowMenu); 1036 visitor->trace(m_overflowMenu);
976 visitor->trace(m_overflowList); 1037 visitor->trace(m_overflowList);
977 visitor->trace(m_castButton); 1038 visitor->trace(m_castButton);
978 visitor->trace(m_overlayCastButton); 1039 visitor->trace(m_overlayCastButton);
979 visitor->trace(m_mediaEventListener); 1040 visitor->trace(m_mediaEventListener);
980 visitor->trace(m_windowEventListener); 1041 visitor->trace(m_windowEventListener);
981 visitor->trace(m_orientationLockDelegate); 1042 visitor->trace(m_orientationLockDelegate);
982 HTMLDivElement::trace(visitor); 1043 HTMLDivElement::trace(visitor);
983 } 1044 }
984 1045
985 } // namespace blink 1046 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698