#ifndef CC_ANIMATION_SCROLL_TIMELINE_H_
#define CC_ANIMATION_SCROLL_TIMELINE_H_
#include <optional>
#include <vector>
#include "base/time/time.h"
#include "cc/animation/animation_export.h"
#include "cc/animation/animation_timeline.h"
#include "cc/animation/keyframe_model.h"
#include "cc/paint/element_id.h"
namespace cc {
class ScrollTree;
class CC_ANIMATION_EXPORT ScrollTimeline : public AnimationTimeline {
public:
enum ScrollDirection {
ScrollUp,
ScrollDown,
ScrollLeft,
ScrollRight,
};
struct ScrollOffsets {
ScrollOffsets() = default;
ScrollOffsets(double start_offset, double end_offset) {
start = start_offset;
end = end_offset;
}
bool operator==(const ScrollOffsets& other) const {
return start == other.start && end == other.end;
}
bool operator!=(const ScrollOffsets& other) const {
return !(*this == other);
}
double start = 0;
double end = 0;
};
static constexpr double kScrollTimelineMicrosecondsPerPixel = 16;
ScrollTimeline(std::optional<ElementId> scroller_id,
ScrollDirection direction,
std::optional<ScrollOffsets> scroll_offsets,
int animation_timeline_id);
static scoped_refptr<ScrollTimeline> Create(
std::optional<ElementId> scroller_id,
ScrollDirection direction,
std::optional<ScrollOffsets> scroll_offsets);
scoped_refptr<AnimationTimeline> CreateImplInstance() const override;
virtual bool IsActive(const ScrollTree& scroll_tree,
bool is_active_tree) const;
virtual std::optional<base::TimeTicks> CurrentTime(
const ScrollTree& scroll_tree,
bool is_active_tree) const;
virtual std::optional<base::TimeTicks> Duration(const ScrollTree& scroll_tree,
bool is_active_tree) const;
void UpdateScrollerIdAndScrollOffsets(
std::optional<ElementId> scroller_id,
std::optional<ScrollOffsets> scroll_offsets);
void PushPropertiesTo(AnimationTimeline* impl_timeline) override;
void ActivateTimeline() override;
bool TickScrollLinkedAnimations(
const std::vector<scoped_refptr<Animation>>& ticking_animations,
const ScrollTree& scroll_tree,
bool is_active_tree) override;
std::optional<ElementId> GetActiveIdForTest() const { return active_id(); }
std::optional<ElementId> GetPendingIdForTest() const { return pending_id(); }
ScrollDirection GetDirectionForTest() const { return direction(); }
std::optional<double> GetStartScrollOffsetForTest() const {
std::optional<ScrollOffsets> offsets = pending_offsets();
if (offsets) {
return offsets->start;
}
return std::nullopt;
}
std::optional<double> GetEndScrollOffsetForTest() const {
std::optional<ScrollOffsets> offsets = pending_offsets();
if (offsets) {
return offsets->end;
}
return std::nullopt;
}
bool IsScrollTimeline() const override;
bool IsLinkedToScroller(ElementId scroller) const override;
protected:
~ScrollTimeline() override;
private:
const std::optional<ElementId>& active_id() const {
return active_id_.Read(*this);
}
const std::optional<ElementId>& pending_id() const {
return pending_id_.Read(*this);
}
const ScrollDirection& direction() const { return direction_.Read(*this); }
const std::optional<ScrollOffsets>& active_offsets() const {
return active_offsets_.Read(*this);
}
const std::optional<ScrollOffsets>& pending_offsets() const {
return pending_offsets_.Read(*this);
}
ProtectedSequenceForbidden<std::optional<ElementId>> active_id_;
ProtectedSequenceWritable<std::optional<ElementId>> pending_id_;
ProtectedSequenceReadable<ScrollDirection> direction_;
ProtectedSequenceForbidden<std::optional<ScrollOffsets>> active_offsets_;
ProtectedSequenceWritable<std::optional<ScrollOffsets>> pending_offsets_;
};
inline ScrollTimeline* ToScrollTimeline(AnimationTimeline* timeline) {
DCHECK(timeline->IsScrollTimeline());
return static_cast<ScrollTimeline*>(timeline);
}
inline const ScrollTimeline* ToScrollTimeline(
const AnimationTimeline* timeline) {
DCHECK(timeline->IsScrollTimeline());
return static_cast<const ScrollTimeline*>(timeline);
}
}
#endif