AnimationTimeline.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Animations/Animation.h>
  7. #include <LibWeb/Animations/AnimationTimeline.h>
  8. #include <LibWeb/DOM/Document.h>
  9. namespace Web::Animations {
  10. JS_DEFINE_ALLOCATOR(AnimationTimeline);
  11. WebIDL::ExceptionOr<void> AnimationTimeline::set_current_time(Optional<double> value)
  12. {
  13. if (value == m_current_time)
  14. return {};
  15. if (m_is_monotonically_increasing && m_current_time.has_value()) {
  16. if (!value.has_value() || value.value() < m_current_time.value())
  17. m_is_monotonically_increasing = false;
  18. }
  19. m_current_time = value;
  20. for (auto& animation : m_associated_animations)
  21. TRY(animation->set_current_time(value));
  22. return {};
  23. }
  24. void AnimationTimeline::set_associated_document(JS::GCPtr<DOM::Document> document)
  25. {
  26. if (document)
  27. document->associate_with_timeline(*this);
  28. if (m_associated_document)
  29. m_associated_document->disassociate_with_timeline(*this);
  30. m_associated_document = document;
  31. }
  32. // https://www.w3.org/TR/web-animations-1/#inactive-timeline
  33. bool AnimationTimeline::is_inactive() const
  34. {
  35. // A timeline is considered to be inactive when its time value is unresolved.
  36. return !m_current_time.has_value();
  37. }
  38. AnimationTimeline::AnimationTimeline(JS::Realm& realm)
  39. : Bindings::PlatformObject(realm)
  40. {
  41. }
  42. AnimationTimeline::~AnimationTimeline()
  43. {
  44. if (m_associated_document)
  45. m_associated_document->disassociate_with_timeline(*this);
  46. }
  47. void AnimationTimeline::initialize(JS::Realm& realm)
  48. {
  49. Base::initialize(realm);
  50. set_prototype(&Bindings::ensure_web_prototype<Bindings::AnimationTimelinePrototype>(realm, "AnimationTimeline"_fly_string));
  51. }
  52. void AnimationTimeline::visit_edges(Cell::Visitor& visitor)
  53. {
  54. Base::visit_edges(visitor);
  55. visitor.visit(m_associated_document);
  56. for (auto const& animation : m_associated_animations)
  57. visitor.visit(animation);
  58. }
  59. }