diff options
author | Simon Hausmann <simon.hausmann@nokia.com> | 2012-06-20 13:01:08 +0200 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@nokia.com> | 2012-06-20 13:01:08 +0200 |
commit | 49233e234e5c787396cadb2cea33b31ae0cd65c1 (patch) | |
tree | 5410cb9a8fd53168bb60d62c54b654d86f03c38d /Source/WebCore/rendering/RenderMediaControlsChromium.cpp | |
parent | b211c645d8ab690f713515dfdc84d80b11c27d2c (diff) | |
download | qtwebkit-49233e234e5c787396cadb2cea33b31ae0cd65c1.tar.gz |
Imported WebKit commit 3a8c29f35d00659d2ce7a0ccdfa8304f14e82327 (http://svn.webkit.org/repository/webkit/trunk@120813)
New snapshot with Windows build fixes
Diffstat (limited to 'Source/WebCore/rendering/RenderMediaControlsChromium.cpp')
-rw-r--r-- | Source/WebCore/rendering/RenderMediaControlsChromium.cpp | 271 |
1 files changed, 179 insertions, 92 deletions
diff --git a/Source/WebCore/rendering/RenderMediaControlsChromium.cpp b/Source/WebCore/rendering/RenderMediaControlsChromium.cpp index 72e12c881..7fa55271a 100644 --- a/Source/WebCore/rendering/RenderMediaControlsChromium.cpp +++ b/Source/WebCore/rendering/RenderMediaControlsChromium.cpp @@ -74,14 +74,25 @@ static bool paintMediaMuteButton(RenderObject* object, const PaintInfo& paintInf if (!mediaElement) return false; - static Image* soundFull = platformResource("mediaSoundFull"); - static Image* soundNone = platformResource("mediaSoundNone"); - static Image* soundDisabled = platformResource("mediaSoundDisabled"); + static Image* soundLevel3 = platformResource("mediaplayerSoundLevel3"); + static Image* soundLevel2 = platformResource("mediaplayerSoundLevel2"); + static Image* soundLevel1 = platformResource("mediaplayerSoundLevel1"); + static Image* soundLevel0 = platformResource("mediaplayerSoundLevel0"); + static Image* soundDisabled = platformResource("mediaplayerSoundDisabled"); if (!hasSource(mediaElement) || !mediaElement->hasAudio()) return paintMediaButton(paintInfo.context, rect, soundDisabled); - return paintMediaButton(paintInfo.context, rect, mediaElement->muted() ? soundNone: soundFull); + if (mediaElement->muted() || mediaElement->volume() <= 0) + return paintMediaButton(paintInfo.context, rect, soundLevel0); + + if (mediaElement->volume() <= 0.33) + return paintMediaButton(paintInfo.context, rect, soundLevel1); + + if (mediaElement->volume() <= 0.66) + return paintMediaButton(paintInfo.context, rect, soundLevel2); + + return paintMediaButton(paintInfo.context, rect, soundLevel3); } static bool paintMediaPlayButton(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect) @@ -90,9 +101,9 @@ static bool paintMediaPlayButton(RenderObject* object, const PaintInfo& paintInf if (!mediaElement) return false; - static Image* mediaPlay = platformResource("mediaPlay"); - static Image* mediaPause = platformResource("mediaPause"); - static Image* mediaPlayDisabled = platformResource("mediaPlayDisabled"); + static Image* mediaPlay = platformResource("mediaplayerPlay"); + static Image* mediaPause = platformResource("mediaplayerPause"); + static Image* mediaPlayDisabled = platformResource("mediaplayerPlayDisabled"); if (!hasSource(mediaElement)) return paintMediaButton(paintInfo.context, rect, mediaPlayDisabled); @@ -102,10 +113,75 @@ static bool paintMediaPlayButton(RenderObject* object, const PaintInfo& paintInf static Image* getMediaSliderThumb() { - static Image* mediaSliderThumb = platformResource("mediaSliderThumb"); + static Image* mediaSliderThumb = platformResource("mediaplayerSliderThumb"); return mediaSliderThumb; } +static void paintRoundedSliderBackground(const IntRect& rect, const RenderStyle* style, GraphicsContext* context) +{ + int borderRadius = rect.height() / 2; + IntSize radii(borderRadius, borderRadius); + Color sliderBackgroundColor = Color(29, 29, 29); + context->save(); + context->fillRoundedRect(rect, radii, radii, radii, radii, sliderBackgroundColor, ColorSpaceDeviceRGB); + context->restore(); +} + +static void paintSliderRangeHighlight(const IntRect& rect, const RenderStyle* style, GraphicsContext* context, float startFraction, float widthFraction) +{ + if (startFraction < 0) + startFraction = 0; + if (widthFraction > 1) + widthFraction = 1; + float endFraction = startFraction + widthFraction; + if (endFraction > 1) { + widthFraction = widthFraction - startFraction; + endFraction = startFraction + widthFraction; + } + + // Set rectangle to highlight range. + IntRect highlightRect = rect; + int startOffset = startFraction * rect.width(); + int endOffset = rect.width() - endFraction * rect.width(); + int rangeWidth = widthFraction * rect.width(); + highlightRect.move(startOffset, 0); + highlightRect.setWidth(rangeWidth); + + // Don't bother drawing an empty area. + if (highlightRect.isEmpty()) + return; + + // Calculate border radius; need to avoid being smaller than half the slider height + // because of https://bugs.webkit.org/show_bug.cgi?id=30143. + int borderRadius = rect.height() / 2; + IntSize radii(borderRadius, borderRadius); + + // Calculate white-grey gradient. + IntPoint sliderTopLeft = highlightRect.location(); + IntPoint sliderBottomLeft = sliderTopLeft; + sliderBottomLeft.move(0, highlightRect.height()); + Color startColor = Color(220, 220, 220); + Color endColor = Color(240, 240, 240); + RefPtr<Gradient> gradient = Gradient::create(sliderTopLeft, sliderBottomLeft); + gradient->addColorStop(0.0, startColor); + gradient->addColorStop(1.0, endColor); + + // Fill highlight rectangle with gradient, potentially rounded if on left or right edge. + context->save(); + context->setFillGradient(gradient); + + if (startOffset < borderRadius && endOffset < borderRadius) + context->fillRoundedRect(highlightRect, radii, radii, radii, radii, startColor, ColorSpaceDeviceRGB); + else if (startOffset < borderRadius) + context->fillRoundedRect(highlightRect, radii, IntSize(0, 0), radii, IntSize(0, 0), startColor, ColorSpaceDeviceRGB); + else if (endOffset < borderRadius) + context->fillRoundedRect(highlightRect, IntSize(0, 0), radii, IntSize(0, 0), radii, startColor, ColorSpaceDeviceRGB); + else + context->fillRect(highlightRect); + + context->restore(); +} + static bool paintMediaSlider(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect) { HTMLMediaElement* mediaElement = toParentMediaElement(object); @@ -115,23 +191,10 @@ static bool paintMediaSlider(RenderObject* object, const PaintInfo& paintInfo, c RenderStyle* style = object->style(); GraphicsContext* context = paintInfo.context; - // Draw the border of the time bar. - // FIXME: this should be a rounded rect but need to fix GraphicsContextSkia first. - // https://bugs.webkit.org/show_bug.cgi?id=30143 - context->save(); - context->setShouldAntialias(true); - context->setStrokeStyle(SolidStroke); - context->setStrokeColor(style->visitedDependentColor(CSSPropertyBorderLeftColor), ColorSpaceDeviceRGB); - context->setStrokeThickness(style->borderLeftWidth()); - context->setFillColor(style->visitedDependentColor(CSSPropertyBackgroundColor), ColorSpaceDeviceRGB); - context->drawRect(rect); - context->restore(); + paintRoundedSliderBackground(rect, style, context); // Draw the buffered range. Since the element may have multiple buffered ranges and it'd be // distracting/'busy' to show all of them, show only the buffered range containing the current play head. - IntRect bufferedRect = rect; - bufferedRect.inflate(-style->borderLeftWidth()); - RefPtr<TimeRanges> bufferedTimeRanges = mediaElement->buffered(); float duration = mediaElement->duration(); float currentTime = mediaElement->currentTime(); @@ -146,27 +209,8 @@ static bool paintMediaSlider(RenderObject* object, const PaintInfo& paintInfo, c float startFraction = start / duration; float endFraction = end / duration; float widthFraction = endFraction - startFraction; - bufferedRect.move(startFraction * bufferedRect.width(), 0); - bufferedRect.setWidth(widthFraction * bufferedRect.width()); - - // Don't bother drawing an empty area. - if (bufferedRect.isEmpty()) - return true; - - IntPoint sliderTopLeft = bufferedRect.location(); - IntPoint sliderBottomLeft = sliderTopLeft; - sliderBottomLeft.move(0, bufferedRect.height()); - - RefPtr<Gradient> gradient = Gradient::create(sliderTopLeft, sliderBottomLeft); - Color startColor = object->style()->visitedDependentColor(CSSPropertyColor); - gradient->addColorStop(0.0, startColor); - gradient->addColorStop(1.0, Color(startColor.red() / 2, startColor.green() / 2, startColor.blue() / 2, startColor.alpha())); - - context->save(); - context->setStrokeStyle(NoStroke); - context->setFillGradient(gradient); - context->fillRect(bufferedRect); - context->restore(); + + paintSliderRangeHighlight(rect, style, context, startFraction, widthFraction); return true; } @@ -189,6 +233,8 @@ static bool paintMediaSliderThumb(RenderObject* object, const PaintInfo& paintIn return paintMediaButton(paintInfo.context, rect, mediaSliderThumb); } +const int mediaVolumeSliderThumbWidth = 24; + static bool paintMediaVolumeSlider(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect) { HTMLMediaElement* mediaElement = toParentMediaElement(object); @@ -196,55 +242,47 @@ static bool paintMediaVolumeSlider(RenderObject* object, const PaintInfo& paintI return false; GraphicsContext* context = paintInfo.context; - Color originalColor = context->strokeColor(); - if (originalColor != Color::white) - context->setStrokeColor(Color::white, ColorSpaceDeviceRGB); + RenderStyle* style = object->style(); + + paintRoundedSliderBackground(rect, style, context); - int x = rect.x() + rect.width() / 2; - context->drawLine(IntPoint(x, rect.y()), IntPoint(x, rect.y() + rect.height())); + // Calculate volume position for white background rectangle. + float volume = mediaElement->volume(); + if (isnan(volume) || volume < 0) + return true; + if (volume > 1) + volume = 1; + if (!hasSource(mediaElement) || !mediaElement->hasAudio() || mediaElement->muted()) + volume = 0; + + // Calculate the position relative to the center of the thumb. + float fillWidth = 0; + if (volume > 0) { + float thumbCenter = mediaVolumeSliderThumbWidth / 2; + float zoomLevel = style->effectiveZoom(); + float positionWidth = volume * (rect.width() - (zoomLevel * thumbCenter)); + fillWidth = positionWidth + (zoomLevel * thumbCenter / 2); + } + + paintSliderRangeHighlight(rect, style, context, 0.0, fillWidth / rect.width()); - if (originalColor != Color::white) - context->setStrokeColor(originalColor, ColorSpaceDeviceRGB); return true; } static bool paintMediaVolumeSliderThumb(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect) { - static Image* mediaVolumeSliderThumb = platformResource("mediaVolumeSliderThumb"); - return paintMediaButton(paintInfo.context, rect, mediaVolumeSliderThumb); -} - -static bool paintMediaTimelineContainer(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect) -{ - HTMLMediaElement* mediaElement = toParentMediaElement(object); + ASSERT(object->node()); + Node* hostNode = object->node()->shadowAncestorNode(); + ASSERT(hostNode); + HTMLMediaElement* mediaElement = toParentMediaElement(hostNode); if (!mediaElement) return false; - if (!rect.isEmpty()) { - GraphicsContext* context = paintInfo.context; - Color originalColor = context->strokeColor(); - float originalThickness = context->strokeThickness(); - StrokeStyle originalStyle = context->strokeStyle(); - - context->setStrokeStyle(SolidStroke); - - // Draw the left border using CSS defined width and color. - context->setStrokeThickness(object->style()->borderLeftWidth()); - context->setStrokeColor(object->style()->visitedDependentColor(CSSPropertyBorderLeftColor).rgb(), ColorSpaceDeviceRGB); - context->drawLine(IntPoint(rect.x() + 1, rect.y()), - IntPoint(rect.x() + 1, rect.y() + rect.height())); - - // Draw the right border using CSS defined width and color. - context->setStrokeThickness(object->style()->borderRightWidth()); - context->setStrokeColor(object->style()->visitedDependentColor(CSSPropertyBorderRightColor).rgb(), ColorSpaceDeviceRGB); - context->drawLine(IntPoint(rect.x() + rect.width() - 1, rect.y()), - IntPoint(rect.x() + rect.width() - 1, rect.y() + rect.height())); - - context->setStrokeColor(originalColor, ColorSpaceDeviceRGB); - context->setStrokeThickness(originalThickness); - context->setStrokeStyle(originalStyle); - } - return true; + if (!hasSource(mediaElement) || !mediaElement->hasAudio()) + return true; + + static Image* mediaVolumeSliderThumb = platformResource("mediaplayerVolumeSliderThumb"); + return paintMediaButton(paintInfo.context, rect, mediaVolumeSliderThumb); } static bool paintMediaFullscreenButton(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect) @@ -253,8 +291,8 @@ static bool paintMediaFullscreenButton(RenderObject* object, const PaintInfo& pa if (!mediaElement) return false; - DEFINE_STATIC_LOCAL(Image*, mediaFullscreen, (platformResource("mediaFullscreen"))); - return paintMediaButton(paintInfo.context, rect, mediaFullscreen); + static Image* mediaFullscreenButton = platformResource("mediaplayerFullscreen"); + return paintMediaButton(paintInfo.context, rect, mediaFullscreenButton); } bool RenderMediaControlsChromium::paintMediaControlsPart(MediaControlElementType part, RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect) @@ -274,8 +312,6 @@ bool RenderMediaControlsChromium::paintMediaControlsPart(MediaControlElementType return paintMediaVolumeSlider(object, paintInfo, rect); case MediaVolumeSliderThumb: return paintMediaVolumeSliderThumb(object, paintInfo, rect); - case MediaTimelineContainer: - return paintMediaTimelineContainer(object, paintInfo, rect); case MediaEnterFullscreenButton: case MediaExitFullscreenButton: return paintMediaFullscreenButton(object, paintInfo, rect); @@ -283,6 +319,7 @@ bool RenderMediaControlsChromium::paintMediaControlsPart(MediaControlElementType case MediaSeekBackButton: case MediaSeekForwardButton: case MediaVolumeSliderContainer: + case MediaTimelineContainer: case MediaCurrentTimeDisplay: case MediaTimeRemainingDisplay: case MediaControlsPanel: @@ -301,24 +338,74 @@ bool RenderMediaControlsChromium::paintMediaControlsPart(MediaControlElementType return false; } +const int mediaSliderThumbWidth = 32; +const int mediaSliderThumbHeight = 24; +const int mediaVolumeSliderThumbHeight = 24; + void RenderMediaControlsChromium::adjustMediaSliderThumbSize(RenderStyle* style) { - static Image* mediaSliderThumb = platformResource("mediaSliderThumb"); - static Image* mediaVolumeSliderThumb = platformResource("mediaVolumeSliderThumb"); + static Image* mediaSliderThumb = platformResource("mediaplayerSliderThumb"); + static Image* mediaVolumeSliderThumb = platformResource("mediaplayerVolumeSliderThumb"); + int width = 0; + int height = 0; Image* thumbImage = 0; - if (style->appearance() == MediaSliderThumbPart) + if (style->appearance() == MediaSliderThumbPart) { thumbImage = mediaSliderThumb; - else if (style->appearance() == MediaVolumeSliderThumbPart) + width = mediaSliderThumbWidth; + height = mediaSliderThumbHeight; + } else if (style->appearance() == MediaVolumeSliderThumbPart) { thumbImage = mediaVolumeSliderThumb; + width = mediaVolumeSliderThumbWidth; + height = mediaVolumeSliderThumbHeight; + } float zoomLevel = style->effectiveZoom(); if (thumbImage) { - style->setWidth(Length(static_cast<int>(thumbImage->width() * zoomLevel), Fixed)); - style->setHeight(Length(static_cast<int>(thumbImage->height() * zoomLevel), Fixed)); + style->setWidth(Length(static_cast<int>(width * zoomLevel), Fixed)); + style->setHeight(Length(static_cast<int>(height * zoomLevel), Fixed)); } } +static String formatChromiumMediaControlsTime(float time, float duration) +{ + if (!isfinite(time)) + time = 0; + if (!isfinite(duration)) + duration = 0; + int seconds = static_cast<int>(fabsf(time)); + int hours = seconds / (60 * 60); + int minutes = (seconds / 60) % 60; + seconds %= 60; + + // duration defines the format of how the time is rendered + int durationSecs = static_cast<int>(fabsf(duration)); + int durationHours = durationSecs / (60 * 60); + int durationMins = (durationSecs / 60) % 60; + + if (durationHours || hours) + return String::format("%s%01d:%02d:%02d", (time < 0 ? "-" : ""), hours, minutes, seconds); + if (durationMins > 9) + return String::format("%s%02d:%02d", (time < 0 ? "-" : ""), minutes, seconds); + + return String::format("%s%01d:%02d", (time < 0 ? "-" : ""), minutes, seconds); +} + +String RenderMediaControlsChromium::formatMediaControlsTime(float time) +{ + return formatChromiumMediaControlsTime(time, time); +} + +String RenderMediaControlsChromium::formatMediaControlsCurrentTime(float currentTime, float duration) +{ + return formatChromiumMediaControlsTime(currentTime, duration); +} + +String RenderMediaControlsChromium::formatMediaControlsRemainingTime(float currentTime, float duration) +{ + return formatChromiumMediaControlsTime(currentTime - duration, duration); +} + #endif // #if ENABLE(VIDEO) } // namespace WebCore |