| 1 | /* |
| 2 | * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this library; see the file COPYING.LIB. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include "config.h" |
| 22 | #include "RenderProgress.h" |
| 23 | |
| 24 | #include "HTMLProgressElement.h" |
| 25 | #include "RenderTheme.h" |
| 26 | #include <wtf/IsoMallocInlines.h> |
| 27 | #include <wtf/RefPtr.h> |
| 28 | |
| 29 | namespace WebCore { |
| 30 | |
| 31 | WTF_MAKE_ISO_ALLOCATED_IMPL(RenderProgress); |
| 32 | |
| 33 | RenderProgress::RenderProgress(HTMLElement& element, RenderStyle&& style) |
| 34 | : RenderBlockFlow(element, WTFMove(style)) |
| 35 | , m_position(HTMLProgressElement::InvalidPosition) |
| 36 | , m_animationTimer(*this, &RenderProgress::animationTimerFired) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | RenderProgress::~RenderProgress() = default; |
| 41 | |
| 42 | void RenderProgress::updateFromElement() |
| 43 | { |
| 44 | HTMLProgressElement* element = progressElement(); |
| 45 | if (m_position == element->position()) |
| 46 | return; |
| 47 | m_position = element->position(); |
| 48 | |
| 49 | updateAnimationState(); |
| 50 | repaint(); |
| 51 | RenderBlockFlow::updateFromElement(); |
| 52 | } |
| 53 | |
| 54 | RenderBox::LogicalExtentComputedValues RenderProgress::computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit logicalTop) const |
| 55 | { |
| 56 | auto computedValues = RenderBox::computeLogicalHeight(logicalHeight, logicalTop); |
| 57 | LayoutRect frame = frameRect(); |
| 58 | if (isHorizontalWritingMode()) |
| 59 | frame.setHeight(computedValues.m_extent); |
| 60 | else |
| 61 | frame.setWidth(computedValues.m_extent); |
| 62 | IntSize frameSize = theme().progressBarRectForBounds(*this, snappedIntRect(frame)).size(); |
| 63 | computedValues.m_extent = isHorizontalWritingMode() ? frameSize.height() : frameSize.width(); |
| 64 | return computedValues; |
| 65 | } |
| 66 | |
| 67 | double RenderProgress::animationProgress() const |
| 68 | { |
| 69 | return m_animating ? (fmod((MonotonicTime::now() - m_animationStartTime).seconds(), m_animationDuration.seconds()) / m_animationDuration.seconds()) : 0; |
| 70 | } |
| 71 | |
| 72 | bool RenderProgress::isDeterminate() const |
| 73 | { |
| 74 | return (HTMLProgressElement::IndeterminatePosition != position() |
| 75 | && HTMLProgressElement::InvalidPosition != position()); |
| 76 | } |
| 77 | |
| 78 | void RenderProgress::animationTimerFired() |
| 79 | { |
| 80 | repaint(); |
| 81 | if (!m_animationTimer.isActive() && m_animating) |
| 82 | m_animationTimer.startOneShot(m_animationRepeatInterval); |
| 83 | } |
| 84 | |
| 85 | void RenderProgress::updateAnimationState() |
| 86 | { |
| 87 | m_animationDuration = theme().animationDurationForProgressBar(*this); |
| 88 | m_animationRepeatInterval = theme().animationRepeatIntervalForProgressBar(*this); |
| 89 | |
| 90 | bool animating = style().hasAppearance() && m_animationDuration > 0_s; |
| 91 | if (animating == m_animating) |
| 92 | return; |
| 93 | |
| 94 | m_animating = animating; |
| 95 | if (m_animating) { |
| 96 | m_animationStartTime = MonotonicTime::now(); |
| 97 | m_animationTimer.startOneShot(m_animationRepeatInterval); |
| 98 | } else |
| 99 | m_animationTimer.stop(); |
| 100 | } |
| 101 | |
| 102 | HTMLProgressElement* RenderProgress::progressElement() const |
| 103 | { |
| 104 | if (!element()) |
| 105 | return nullptr; |
| 106 | |
| 107 | if (is<HTMLProgressElement>(*element())) |
| 108 | return downcast<HTMLProgressElement>(element()); |
| 109 | |
| 110 | ASSERT(element()->shadowHost()); |
| 111 | return downcast<HTMLProgressElement>(element()->shadowHost()); |
| 112 | } |
| 113 | |
| 114 | } // namespace WebCore |
| 115 | |
| 116 | |