#include "config.h"
#include "RenderMarquee.h"
#include "FrameView.h"
#include "HTMLMarqueeElement.h"
#include "HTMLNames.h"
#include "RenderLayer.h"
#include "RenderView.h"
namespace WebCore {
using namespace HTMLNames;
RenderMarquee::RenderMarquee(RenderLayer* layer)
: m_layer(layer)
, m_timer(*this, &RenderMarquee::timerFired)
{
layer->setConstrainsScrollingToContentEdge(false);
}
RenderMarquee::~RenderMarquee() = default;
int RenderMarquee::marqueeSpeed() const
{
int result = m_layer->renderer().style().marqueeSpeed();
Element* element = m_layer->renderer().element();
if (is<HTMLMarqueeElement>(element))
result = std::max(result, downcast<HTMLMarqueeElement>(*element).minimumDelay());
return result;
}
static MarqueeDirection reverseDirection(MarqueeDirection direction)
{
switch (direction) {
case MarqueeDirection::Auto:
return MarqueeDirection::Auto;
case MarqueeDirection::Left:
return MarqueeDirection::Right;
case MarqueeDirection::Right:
return MarqueeDirection::Left;
case MarqueeDirection::Up:
return MarqueeDirection::Down;
case MarqueeDirection::Down:
return MarqueeDirection::Up;
case MarqueeDirection::Backward:
return MarqueeDirection::Forward;
case MarqueeDirection::Forward:
return MarqueeDirection::Backward;
}
return MarqueeDirection::Auto;
}
MarqueeDirection RenderMarquee::direction() const
{
MarqueeDirection result = m_layer->renderer().style().marqueeDirection();
TextDirection dir = m_layer->renderer().style().direction();
if (result == MarqueeDirection::Auto)
result = MarqueeDirection::Backward;
if (result == MarqueeDirection::Forward)
result = (dir == TextDirection::LTR) ? MarqueeDirection::Right : MarqueeDirection::Left;
if (result == MarqueeDirection::Backward)
result = (dir == TextDirection::LTR) ? MarqueeDirection::Left : MarqueeDirection::Right;
Length increment = m_layer->renderer().style().marqueeIncrement();
if (increment.isNegative())
result = reverseDirection(result);
return result;
}
bool RenderMarquee::isHorizontal() const
{
return direction() == MarqueeDirection::Left || direction() == MarqueeDirection::Right;
}
int RenderMarquee::computePosition(MarqueeDirection dir, bool stopAtContentEdge)
{
RenderBox* box = m_layer->renderBox();
ASSERT(box);
auto& boxStyle = box->style();
if (isHorizontal()) {
bool ltr = boxStyle.isLeftToRightDirection();
LayoutUnit clientWidth = box->clientWidth();
LayoutUnit contentWidth = ltr ? box->maxPreferredLogicalWidth() : box->minPreferredLogicalWidth();
if (ltr)
contentWidth += (box->paddingRight() - box->borderLeft());
else {
contentWidth = box->width() - contentWidth;
contentWidth += (box->paddingLeft() - box->borderRight());
}
if (dir == MarqueeDirection::Right) {
if (stopAtContentEdge)
return std::max<LayoutUnit>(0, ltr ? (contentWidth - clientWidth) : (clientWidth - contentWidth));
return ltr ? contentWidth : clientWidth;
}
if (stopAtContentEdge)
return std::min<LayoutUnit>(0, ltr ? (contentWidth - clientWidth) : (clientWidth - contentWidth));
return ltr ? -clientWidth : -contentWidth;
}
int contentHeight = box->layoutOverflowRect().maxY() - box->borderTop() + box->paddingBottom();
int clientHeight = roundToInt(box->clientHeight());
if (dir == MarqueeDirection::Up) {
if (stopAtContentEdge)
return std::min(contentHeight - clientHeight, 0);
return -clientHeight;
}
if (stopAtContentEdge)
return std::max(contentHeight - clientHeight, 0);
return contentHeight;
}
void RenderMarquee::start()
{
if (m_timer.isActive() || m_layer->renderer().style().marqueeIncrement().isZero())
return;
if (!m_suspended && !m_stopped) {
if (isHorizontal())
m_layer->scrollToOffset(ScrollOffset(m_start, 0), ScrollType::Programmatic, ScrollClamping::Unclamped);
else
m_layer->scrollToOffset(ScrollOffset(0, m_start), ScrollType::Programmatic, ScrollClamping::Unclamped);
} else {
m_suspended = false;
m_stopped = false;
}
m_timer.startRepeating(1_ms * speed());
}
void RenderMarquee::suspend()
{
m_timer.stop();
m_suspended = true;
}
void RenderMarquee::stop()
{
m_timer.stop();
m_stopped = true;
}
void RenderMarquee::updateMarqueePosition()
{
bool activate = (m_totalLoops <= 0 || m_currentLoop < m_totalLoops);
if (activate) {
MarqueeBehavior behavior = m_layer->renderer().style().marqueeBehavior();
m_start = computePosition(direction(), behavior == MarqueeBehavior::Alternate);
m_end = computePosition(reverseDirection(direction()), behavior == MarqueeBehavior::Alternate || behavior == MarqueeBehavior::Slide);
if (!m_stopped)
start();
}
}
void RenderMarquee::updateMarqueeStyle()
{
auto& style = m_layer->renderer().style();
if (m_direction != style.marqueeDirection() || (m_totalLoops != style.marqueeLoopCount() && m_currentLoop >= m_totalLoops))
m_currentLoop = 0;
m_totalLoops = style.marqueeLoopCount();
m_direction = style.marqueeDirection();
if (m_layer->renderer().isHTMLMarquee()) {
if (m_totalLoops <= 0 && style.marqueeBehavior() == MarqueeBehavior::Slide)
m_totalLoops = 1;
}
if (speed() != marqueeSpeed()) {
m_speed = marqueeSpeed();
if (m_timer.isActive())
m_timer.startRepeating(1_ms * speed());
}
bool activate = (m_totalLoops <= 0 || m_currentLoop < m_totalLoops);
if (activate && !m_timer.isActive())
m_layer->renderer().setNeedsLayout();
else if (!activate && m_timer.isActive())
m_timer.stop();
}
void RenderMarquee::timerFired()
{
if (m_layer->renderer().view().needsLayout())
return;
if (m_reset) {
m_reset = false;
if (isHorizontal())
m_layer->scrollToXOffset(m_start);
else
m_layer->scrollToYOffset(m_start);
return;
}
const RenderStyle& style = m_layer->renderer().style();
int endPoint = m_end;
int range = m_end - m_start;
int newPos;
if (range == 0)
newPos = m_end;
else {
bool addIncrement = direction() == MarqueeDirection::Up || direction() == MarqueeDirection::Left;
bool isReversed = style.marqueeBehavior() == MarqueeBehavior::Alternate && m_currentLoop % 2;
if (isReversed) {
endPoint = m_start;
range = -range;
addIncrement = !addIncrement;
}
bool positive = range > 0;
int clientSize = (isHorizontal() ? roundToInt(m_layer->renderBox()->clientWidth()) : roundToInt(m_layer->renderBox()->clientHeight()));
int increment = abs(intValueForLength(m_layer->renderer().style().marqueeIncrement(), clientSize));
int currentPos = (isHorizontal() ? m_layer->scrollOffset().x() : m_layer->scrollOffset().y());
newPos = currentPos + (addIncrement ? increment : -increment);
if (positive)
newPos = std::min(newPos, endPoint);
else
newPos = std::max(newPos, endPoint);
}
if (newPos == endPoint) {
m_currentLoop++;
if (m_totalLoops > 0 && m_currentLoop >= m_totalLoops)
m_timer.stop();
else if (style.marqueeBehavior() != MarqueeBehavior::Alternate)
m_reset = true;
}
if (isHorizontal())
m_layer->scrollToXOffset(newPos);
else
m_layer->scrollToYOffset(newPos);
}
}