Animation.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TemporaryChange.h>
  7. #include <LibWeb/Animations/Animation.h>
  8. #include <LibWeb/Animations/AnimationEffect.h>
  9. #include <LibWeb/Animations/AnimationPlaybackEvent.h>
  10. #include <LibWeb/Animations/DocumentTimeline.h>
  11. #include <LibWeb/Bindings/Intrinsics.h>
  12. #include <LibWeb/DOM/Document.h>
  13. #include <LibWeb/HTML/Window.h>
  14. #include <LibWeb/WebIDL/ExceptionOr.h>
  15. #include <LibWeb/WebIDL/Promise.h>
  16. namespace Web::Animations {
  17. JS_DEFINE_ALLOCATOR(Animation);
  18. // https://www.w3.org/TR/web-animations-1/#dom-animation-animation
  19. JS::NonnullGCPtr<Animation> Animation::create(JS::Realm& realm, JS::GCPtr<AnimationEffect> effect, JS::GCPtr<AnimationTimeline> timeline)
  20. {
  21. // 1. Let animation be a new Animation object.
  22. auto animation = realm.heap().allocate<Animation>(realm, realm);
  23. // 2. Run the procedure to set the timeline of an animation on animation passing timeline as the new timeline or, if
  24. // a timeline argument is missing, passing the default document timeline of the Document associated with the
  25. // Window that is the current global object.
  26. if (!timeline) {
  27. auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
  28. timeline = window.associated_document().timeline();
  29. }
  30. animation->set_timeline(timeline);
  31. // 3. Run the procedure to set the associated effect of an animation on animation passing source as the new effect.
  32. animation->set_effect(effect);
  33. return animation;
  34. }
  35. WebIDL::ExceptionOr<JS::NonnullGCPtr<Animation>> Animation::construct_impl(JS::Realm& realm, JS::GCPtr<AnimationEffect> effect, JS::GCPtr<AnimationTimeline> timeline)
  36. {
  37. return create(realm, effect, timeline);
  38. }
  39. // https://www.w3.org/TR/web-animations-1/#animation-set-the-associated-effect-of-an-animation
  40. void Animation::set_effect(JS::GCPtr<AnimationEffect> new_effect)
  41. {
  42. // Setting this attribute updates the object’s associated effect using the procedure to set the associated effect of
  43. // an animation.
  44. // 1. Let old effect be the current associated effect of animation, if any.
  45. auto old_effect = m_effect;
  46. // 2. If new effect is the same object as old effect, abort this procedure.
  47. if (new_effect == old_effect)
  48. return;
  49. // 3. If animation has a pending pause task, reschedule that task to run as soon as animation is ready.
  50. if (m_pending_pause_task == TaskState::Pending)
  51. m_pending_pause_task = TaskState::RunAsSoonAsReady;
  52. // 4. If animation has a pending play task, reschedule that task to run as soon as animation is ready to play ne
  53. // effect.
  54. if (m_pending_play_task == TaskState::Pending)
  55. m_pending_play_task = TaskState::RunAsSoonAsReady;
  56. // 5. If new effect is not null and if new effect is the associated effect of another animation, previous animation,
  57. // run the procedure to set the associated effect of an animation (this procedure) on previous animation passing
  58. // null as new effect.
  59. if (new_effect && new_effect->associated_animation() != this) {
  60. if (auto animation = new_effect->associated_animation())
  61. animation->set_effect({});
  62. }
  63. // 6. Let the associated effect of animation be new effect.
  64. if (new_effect)
  65. new_effect->set_associated_animation(this);
  66. if (m_effect)
  67. m_effect->set_associated_animation({});
  68. m_effect = new_effect;
  69. // 7. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false,
  70. // and the synchronously notify flag set to false.
  71. update_finished_state(DidSeek::No, SynchronouslyNotify::No);
  72. }
  73. // https://www.w3.org/TR/web-animations-1/#animation-set-the-timeline-of-an-animation
  74. void Animation::set_timeline(JS::GCPtr<AnimationTimeline> new_timeline)
  75. {
  76. // Setting this attribute updates the object’s timeline using the procedure to set the timeline of an animation.
  77. // 1. Let old timeline be the current timeline of animation, if any.
  78. auto old_timeline = m_timeline;
  79. // 2. If new timeline is the same object as old timeline, abort this procedure.
  80. if (new_timeline == old_timeline)
  81. return;
  82. // 3. Let the timeline of animation be new timeline.
  83. if (m_timeline)
  84. m_timeline->disassociate_with_animation(*this);
  85. m_timeline = new_timeline;
  86. m_timeline->associate_with_animation(*this);
  87. // 4. If the start time of animation is resolved, make animation’s hold time unresolved.
  88. if (m_start_time.has_value())
  89. m_hold_time = {};
  90. // 5. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false,
  91. // and the synchronously notify flag set to false.
  92. update_finished_state(DidSeek::No, SynchronouslyNotify::No);
  93. }
  94. // https://www.w3.org/TR/web-animations-1/#dom-animation-starttime
  95. // https://www.w3.org/TR/web-animations-1/#set-the-start-time
  96. void Animation::set_start_time(Optional<double> const& new_start_time)
  97. {
  98. // Setting this attribute updates the start time using the procedure to set the start time of this object to the new
  99. // value.
  100. // 1. Let timeline time be the current time value of the timeline that animation is associated with. If there is no
  101. // timeline associated with animation or the associated timeline is inactive, let the timeline time be
  102. // unresolved.
  103. auto timeline_time = m_timeline && !m_timeline->is_inactive() ? m_timeline->current_time() : Optional<double> {};
  104. // 2. If timeline time is unresolved and new start time is resolved, make animation’s hold time unresolved.
  105. if (!timeline_time.has_value() && new_start_time.has_value())
  106. m_hold_time = {};
  107. // 3. Let previous current time be animation’s current time.
  108. auto previous_current_time = current_time();
  109. // 4. Apply any pending playback rate on animation.
  110. apply_any_pending_playback_rate();
  111. // 5. Set animation’s start time to new start time.
  112. m_start_time = new_start_time;
  113. // 6. Update animation’s hold time based on the first matching condition from the following,
  114. // -> If new start time is resolved,
  115. if (new_start_time.has_value()) {
  116. // If animation’s playback rate is not zero, make animation’s hold time unresolved.
  117. if (m_playback_rate != 0.0)
  118. m_hold_time = {};
  119. }
  120. // -> Otherwise (new start time is unresolved),
  121. else {
  122. // Set animation’s hold time to previous current time even if previous current time is unresolved.
  123. m_hold_time = previous_current_time;
  124. }
  125. // 7. If animation has a pending play task or a pending pause task, cancel that task and resolve animation’s current
  126. // ready promise with animation.
  127. if (m_pending_play_task == TaskState::Pending || m_pending_pause_task == TaskState::Pending) {
  128. m_pending_play_task = TaskState::None;
  129. m_pending_pause_task = TaskState::None;
  130. WebIDL::resolve_promise(realm(), current_ready_promise(), this);
  131. }
  132. // 8. Run the procedure to update an animation’s finished state for animation with the did seek flag set to true,
  133. // and the synchronously notify flag set to false.
  134. update_finished_state(DidSeek::Yes, SynchronouslyNotify::No);
  135. }
  136. // https://www.w3.org/TR/web-animations-1/#animation-current-time
  137. Optional<double> Animation::current_time() const
  138. {
  139. // The current time is calculated from the first matching condition from below:
  140. // -> If the animation’s hold time is resolved,
  141. if (m_hold_time.has_value()) {
  142. // The current time is the animation’s hold time.
  143. return m_hold_time.value();
  144. }
  145. // -> If any of the following are true:
  146. // - the animation has no associated timeline, or
  147. // - the associated timeline is inactive, or
  148. // - the animation’s start time is unresolved.
  149. if (!m_timeline || m_timeline->is_inactive() || !m_start_time.has_value()) {
  150. // The current time is an unresolved time value.
  151. return {};
  152. }
  153. // -> Otherwise,
  154. // current time = (timeline time - start time) × playback rate
  155. // Where timeline time is the current time value of the associated timeline. The playback rate value is defined
  156. // in §4.4.15 Speed control.
  157. return (m_timeline->current_time().value() - m_start_time.value()) * playback_rate();
  158. }
  159. // https://www.w3.org/TR/web-animations-1/#animation-set-the-current-time
  160. WebIDL::ExceptionOr<void> Animation::set_current_time(Optional<double> const& seek_time)
  161. {
  162. // 1. Run the steps to silently set the current time of animation to seek time.
  163. TRY(silently_set_current_time(seek_time));
  164. // 2. If animation has a pending pause task, synchronously complete the pause operation by performing the following
  165. // steps:
  166. if (m_pending_pause_task == TaskState::Pending) {
  167. // 1. Set animation’s hold time to seek time.
  168. m_hold_time = seek_time;
  169. // 2. Apply any pending playback rate to animation.
  170. apply_any_pending_playback_rate();
  171. // 3. Make animation’s start time unresolved.
  172. m_start_time = {};
  173. // 4. Cancel the pending pause task.
  174. m_pending_pause_task = TaskState::None;
  175. // 5 Resolve animation’s current ready promise with animation.
  176. WebIDL::resolve_promise(realm(), current_ready_promise(), this);
  177. }
  178. // 3. Run the procedure to update an animation’s finished state for animation with the did seek flag set to true,
  179. // and the synchronously notify flag set to false.
  180. update_finished_state(DidSeek::Yes, SynchronouslyNotify::No);
  181. return {};
  182. }
  183. // https://www.w3.org/TR/web-animations-1/#dom-animation-playbackrate
  184. // https://www.w3.org/TR/web-animations-1/#set-the-playback-rate
  185. WebIDL::ExceptionOr<void> Animation::set_playback_rate(double new_playback_rate)
  186. {
  187. // Setting this attribute follows the procedure to set the playback rate of this object to the new value.
  188. // 1. Clear any pending playback rate on animation.
  189. m_pending_playback_rate = {};
  190. // 2. Let previous time be the value of the current time of animation before changing the playback rate.
  191. auto previous_time = current_time();
  192. // 3. Let previous playback rate be the current effective playback rate of animation.
  193. auto previous_playback_rate = playback_rate();
  194. // 4. Set the playback rate to new playback rate.
  195. m_playback_rate = new_playback_rate;
  196. // 5. Perform the steps corresponding to the first matching condition from the following, if any:
  197. // -> If animation is associated with a monotonically increasing timeline and the previous time is resolved,
  198. if (m_timeline && m_timeline->is_monotonically_increasing() && previous_time.has_value()) {
  199. // set the current time of animation to previous time.
  200. TRY(set_current_time(previous_time));
  201. }
  202. // -> If animation is associated with a non-null timeline that is not monotonically increasing, the start time of
  203. // animation is resolved, associated effect end is not infinity, and either:
  204. // - the previous playback rate < 0 and the new playback rate ≥ 0, or
  205. // - the previous playback rate ≥ 0 and the new playback rate < 0,
  206. else if (m_timeline && !m_timeline->is_monotonically_increasing() && m_start_time.has_value() && !isinf(associated_effect_end()) && ((previous_playback_rate < 0.0 && new_playback_rate >= 0.0) || (previous_playback_rate >= 0 && new_playback_rate < 0))) {
  207. // Set animation’s start time to the result of evaluating associated effect end - start time for animation.
  208. m_start_time = associated_effect_end() - m_start_time.value();
  209. }
  210. return {};
  211. }
  212. // https://www.w3.org/TR/web-animations-1/#animation-play-state
  213. Bindings::AnimationPlayState Animation::play_state() const
  214. {
  215. // The play state of animation, animation, at a given moment is the state corresponding to the first matching
  216. // condition from the following:
  217. // -> All of the following conditions are true:
  218. // - The current time of animation is unresolved, and
  219. // - the start time of animation is unresolved, and
  220. // - animation does not have either a pending play task or a pending pause task,
  221. auto current_time = this->current_time();
  222. if (!current_time.has_value() && !m_start_time.has_value() && !pending()) {
  223. // → idle
  224. return Bindings::AnimationPlayState::Idle;
  225. }
  226. // -> Either of the following conditions are true:
  227. // - animation has a pending pause task, or
  228. // - both the start time of animation is unresolved and it does not have a pending play task,
  229. if (m_pending_pause_task == TaskState::Pending || (!m_start_time.has_value() && m_pending_play_task == TaskState::None)) {
  230. // → paused
  231. return Bindings::AnimationPlayState::Paused;
  232. }
  233. // -> For animation, current time is resolved and either of the following conditions are true:
  234. // - animation’s effective playback rate > 0 and current time ≥ associated effect end; or
  235. // - animation’s effective playback rate < 0 and current time ≤ 0,
  236. auto effective_playback_rate = this->effective_playback_rate();
  237. if (current_time.has_value() && ((effective_playback_rate > 0.0 && current_time.value() >= associated_effect_end()) || (effective_playback_rate < 0.0 && current_time.value() <= 0.0))) {
  238. // → finished
  239. return Bindings::AnimationPlayState::Finished;
  240. }
  241. // -> Otherwise,
  242. // → running
  243. return Bindings::AnimationPlayState::Running;
  244. }
  245. // https://www.w3.org/TR/web-animations-1/#animation-time-to-timeline-time
  246. Optional<double> Animation::convert_an_animation_time_to_timeline_time(Optional<double> time) const
  247. {
  248. // 1. If time is unresolved, return time.
  249. if (!time.has_value())
  250. return time;
  251. // 2. If time is infinity, return an unresolved time value.
  252. if (isinf(time.value()))
  253. return {};
  254. // 3. If animation’s playback rate is zero, return an unresolved time value.
  255. if (m_playback_rate == 0.0)
  256. return {};
  257. // 4. If animation’s start time is unresolved, return an unresolved time value.
  258. if (!m_start_time.has_value())
  259. return {};
  260. // 5. Return the result of calculating: time × (1 / playback rate) + start time (where playback rate and start time
  261. // are the playback rate and start time of animation, respectively).
  262. return (time.value() * (1.0 / m_playback_rate)) + m_start_time.value();
  263. }
  264. // https://www.w3.org/TR/web-animations-1/#animation-time-to-origin-relative-time
  265. Optional<double> Animation::convert_a_timeline_time_to_an_origin_relative_time(Optional<double> time) const
  266. {
  267. // 1. Let timeline time be the result of converting time from an animation time to a timeline time.
  268. auto timeline_time = convert_an_animation_time_to_timeline_time(time);
  269. // 2. If timeline time is unresolved, return time.
  270. if (!timeline_time.has_value())
  271. return time;
  272. // 3. If animation is not associated with a timeline, return an unresolved time value.
  273. if (!m_timeline)
  274. return {};
  275. // 4. If animation is associated with an inactive timeline, return an unresolved time value.
  276. if (m_timeline->is_inactive())
  277. return {};
  278. // 5. If there is no procedure to convert a timeline time to an origin-relative time for the timeline associated
  279. // with animation, return an unresolved time value.
  280. if (!m_timeline->can_convert_a_timeline_time_to_an_origin_relative_time())
  281. return {};
  282. // 6. Return the result of converting timeline time to an origin-relative time using the procedure defined for the
  283. // timeline associated with animation.
  284. return m_timeline->convert_a_timeline_time_to_an_origin_relative_time(timeline_time);
  285. }
  286. // https://www.w3.org/TR/web-animations-1/#animation-document-for-timing
  287. JS::GCPtr<DOM::Document> Animation::document_for_timing() const
  288. {
  289. // An animation’s document for timing is the Document with which its timeline is associated. If an animation is not
  290. // associated with a timeline, or its timeline is not associated with a document, then it has no document for
  291. // timing.
  292. if (!m_timeline)
  293. return {};
  294. return m_timeline->associated_document();
  295. }
  296. // https://www.w3.org/TR/web-animations-1/#associated-effect-end
  297. double Animation::associated_effect_end() const
  298. {
  299. // The associated effect end of an animation is equal to the end time of the animation’s associated effect. If the
  300. // animation has no associated effect, the associated effect end is zero.
  301. return m_effect ? m_effect->end_time() : 0.0;
  302. }
  303. // https://www.w3.org/TR/web-animations-1/#effective-playback-rate
  304. double Animation::effective_playback_rate() const
  305. {
  306. // The effective playback rate of an animation is its pending playback rate, if set, otherwise it is the animation’s
  307. // playback rate.
  308. return m_pending_playback_rate.has_value() ? m_pending_playback_rate.value() : m_playback_rate;
  309. }
  310. // https://www.w3.org/TR/web-animations-1/#apply-any-pending-playback-rate
  311. void Animation::apply_any_pending_playback_rate()
  312. {
  313. // 1. If animation does not have a pending playback rate, abort these steps.
  314. if (!m_pending_playback_rate.has_value())
  315. return;
  316. // 2. Set animation’s playback rate to its pending playback rate.
  317. m_playback_rate = m_pending_playback_rate.value();
  318. // 3. Clear animation’s pending playback rate.
  319. m_pending_playback_rate = {};
  320. }
  321. // https://www.w3.org/TR/web-animations-1/#animation-silently-set-the-current-time
  322. WebIDL::ExceptionOr<void> Animation::silently_set_current_time(Optional<double> seek_time)
  323. {
  324. // 1. If seek time is an unresolved time value, then perform the following steps.
  325. if (!seek_time.has_value()) {
  326. // 1. If the current time is resolved, then throw a TypeError.
  327. if (current_time().has_value()) {
  328. return WebIDL::SimpleException {
  329. WebIDL::SimpleExceptionType::TypeError,
  330. "Cannot change an animation's current time from a resolve value to an unresolved value"sv
  331. };
  332. }
  333. // 2. Abort these steps.
  334. return {};
  335. }
  336. // 2. Update either animation’s hold time or start time as follows:
  337. // -> If any of the following conditions are true:
  338. // - animation’s hold time is resolved, or
  339. // - animation’s start time is unresolved, or
  340. // - animation has no associated timeline or the associated timeline is inactive, or
  341. // - animation’s playback rate is 0,
  342. if (m_hold_time.has_value() || !m_start_time.has_value() || !m_timeline || m_timeline->is_inactive() || m_playback_rate == 0.0) {
  343. // Set animation’s hold time to seek time.
  344. m_hold_time = seek_time;
  345. }
  346. // -> Otherwise,
  347. else {
  348. // Set animation’s start time to the result of evaluating timeline time - (seek time / playback rate) where
  349. // timeline time is the current time value of timeline associated with animation.
  350. m_start_time = m_timeline->current_time().value() - (seek_time.value() / m_playback_rate);
  351. }
  352. // 3. If animation has no associated timeline or the associated timeline is inactive, make animation’s start time
  353. // unresolved.
  354. if (!m_timeline || m_timeline->is_inactive())
  355. m_start_time = {};
  356. // 4. Make animation’s previous current time unresolved.
  357. m_previous_current_time = {};
  358. return {};
  359. }
  360. // https://www.w3.org/TR/web-animations-1/#update-an-animations-finished-state
  361. void Animation::update_finished_state(DidSeek did_seek, SynchronouslyNotify synchronously_notify)
  362. {
  363. // 1. Let the unconstrained current time be the result of calculating the current time substituting an unresolved
  364. // time value for the hold time if did seek is false. If did seek is true, the unconstrained current time is
  365. // equal to the current time.
  366. //
  367. // Note: This is required to accommodate timelines that may change direction. Without this definition, a once-
  368. // finished animation would remain finished even when its timeline progresses in the opposite direction.
  369. Optional<double> unconstrained_current_time;
  370. if (did_seek == DidSeek::No) {
  371. TemporaryChange change(m_hold_time, {});
  372. unconstrained_current_time = current_time();
  373. } else {
  374. unconstrained_current_time = current_time();
  375. }
  376. // 2. If all three of the following conditions are true,
  377. // - the unconstrained current time is resolved, and
  378. // - animation’s start time is resolved, and
  379. // - animation does not have a pending play task or a pending pause task,
  380. if (unconstrained_current_time.has_value() && m_start_time.has_value() && !pending()) {
  381. // then update animation’s hold time based on the first matching condition for animation from below, if any:
  382. // -> If playback rate > 0 and unconstrained current time is greater than or equal to associated effect end,
  383. auto associated_effect_end = this->associated_effect_end();
  384. if (m_playback_rate > 0.0 && unconstrained_current_time.value() >= associated_effect_end) {
  385. // If did seek is true, let the hold time be the value of unconstrained current time.
  386. if (did_seek == DidSeek::Yes) {
  387. m_hold_time = unconstrained_current_time;
  388. }
  389. // If did seek is false, let the hold time be the maximum value of previous current time and associated
  390. // effect end. If the previous current time is unresolved, let the hold time be associated effect end.
  391. else if (m_previous_current_time.has_value()) {
  392. m_hold_time = max(m_previous_current_time.value(), associated_effect_end);
  393. } else {
  394. m_hold_time = associated_effect_end;
  395. }
  396. }
  397. // -> If playback rate < 0 and unconstrained current time is less than or equal to 0,
  398. else if (m_playback_rate < 0.0 && unconstrained_current_time.value() <= 0.0) {
  399. // If did seek is true, let the hold time be the value of unconstrained current time.
  400. if (did_seek == DidSeek::Yes) {
  401. m_hold_time = unconstrained_current_time;
  402. }
  403. // If did seek is false, let the hold time be the minimum value of previous current time and zero. If the
  404. // previous current time is unresolved, let the hold time be zero.
  405. else if (m_previous_current_time.has_value()) {
  406. m_hold_time = min(m_previous_current_time.value(), 0.0);
  407. } else {
  408. m_hold_time = 0.0;
  409. }
  410. }
  411. // -> If playback rate ≠ 0, and animation is associated with an active timeline,
  412. else if (m_playback_rate != 0.0 && m_timeline && !m_timeline->is_inactive()) {
  413. // Perform the following steps:
  414. // 1. If did seek is true and the hold time is resolved, let animation’s start time be equal to the result
  415. // of evaluating timeline time - (hold time / playback rate) where timeline time is the current time
  416. // value of timeline associated with animation.
  417. if (did_seek == DidSeek::Yes && m_hold_time.has_value())
  418. m_start_time = m_timeline->current_time().value() - (m_hold_time.value() / m_playback_rate);
  419. // 2. Let the hold time be unresolved.
  420. m_hold_time = {};
  421. }
  422. }
  423. // 3. Set the previous current time of animation be the result of calculating its current time.
  424. m_previous_current_time = current_time();
  425. // 4. Let current finished state be true if the play state of animation is finished. Otherwise, let it be false.
  426. auto current_finished_state = play_state() == Bindings::AnimationPlayState::Finished;
  427. // 5. If current finished state is true and the current finished promise is not yet resolved, perform the following
  428. // steps:
  429. if (current_finished_state && !m_current_finished_promise_resolved) {
  430. // 1. Let finish notification steps refer to the following procedure:
  431. JS::SafeFunction<void()> finish_notification_steps = [&]() {
  432. if (m_should_abort_finish_notification_microtask) {
  433. m_should_abort_finish_notification_microtask = false;
  434. m_has_finish_notification_microtask_scheduled = false;
  435. return;
  436. }
  437. // 1. If animation’s play state is not equal to finished, abort these steps.
  438. if (play_state() != Bindings::AnimationPlayState::Finished)
  439. return;
  440. // 2. Resolve animation’s current finished promise object with animation.
  441. WebIDL::resolve_promise(realm(), current_finished_promise(), this);
  442. m_current_finished_promise_resolved = true;
  443. // 3. Create an AnimationPlaybackEvent, finishEvent.
  444. // 4. Set finishEvent’s type attribute to finish.
  445. // 5. Set finishEvent’s currentTime attribute to the current time of animation.
  446. auto& realm = this->realm();
  447. AnimationPlaybackEventInit init;
  448. init.current_time = current_time();
  449. auto finish_event = heap().allocate<AnimationPlaybackEvent>(realm, realm, "finish"_fly_string, init);
  450. // 6. Set finishEvent’s timelineTime attribute to the current time of the timeline with which animation is
  451. // associated. If animation is not associated with a timeline, or the timeline is inactive, let
  452. // timelineTime be null.
  453. if (m_timeline && !m_timeline->is_inactive())
  454. finish_event->set_timeline_time(m_timeline->current_time());
  455. else
  456. finish_event->set_timeline_time({});
  457. // 7. If animation has a document for timing, then append finishEvent to its document for timing's pending
  458. // animation event queue along with its target, animation. For the scheduled event time, use the result
  459. // of converting animation’s associated effect end to an origin-relative time.
  460. if (auto document_for_timing = this->document_for_timing()) {
  461. document_for_timing->append_pending_animation_event({ .event = finish_event,
  462. .target = *this,
  463. .scheduled_event_time = convert_a_timeline_time_to_an_origin_relative_time(associated_effect_end()) });
  464. }
  465. // Otherwise, queue a task to dispatch finishEvent at animation. The task source for this task is the DOM
  466. // manipulation task source.
  467. else {
  468. HTML::queue_global_task(HTML::Task::Source::DOMManipulation, realm.global_object(), [this, finish_event]() {
  469. dispatch_event(finish_event);
  470. });
  471. }
  472. m_has_finish_notification_microtask_scheduled = false;
  473. };
  474. // 2. If synchronously notify is true, cancel any queued microtask to run the finish notification steps for this
  475. // animation, and run the finish notification steps immediately.
  476. if (synchronously_notify == SynchronouslyNotify::Yes) {
  477. m_should_abort_finish_notification_microtask = false;
  478. finish_notification_steps();
  479. m_should_abort_finish_notification_microtask = true;
  480. }
  481. // Otherwise, if synchronously notify is false, queue a microtask to run finish notification steps for
  482. // animation unless there is already a microtask queued to run those steps for animation.
  483. else {
  484. if (!m_has_finish_notification_microtask_scheduled)
  485. HTML::queue_a_microtask({}, move(finish_notification_steps));
  486. m_has_finish_notification_microtask_scheduled = true;
  487. m_should_abort_finish_notification_microtask = false;
  488. }
  489. }
  490. // 6. If current finished state is false and animation’s current finished promise is already resolved, set
  491. // animation’s current finished promise to a new promise in the relevant Realm of animation.
  492. if (!current_finished_state && m_current_finished_promise_resolved) {
  493. m_current_finished_promise = WebIDL::create_promise(realm());
  494. m_current_finished_promise_resolved = false;
  495. }
  496. // Invalidate the style of our target element, if applicable
  497. if (m_effect) {
  498. if (auto target = m_effect->target())
  499. target->invalidate_style();
  500. }
  501. }
  502. JS::NonnullGCPtr<WebIDL::Promise> Animation::current_ready_promise() const
  503. {
  504. if (!m_current_ready_promise) {
  505. // The current ready promise is initially a resolved Promise created using the procedure to create a new
  506. // resolved Promise with the animation itself as its value and created in the relevant Realm of the animation.
  507. m_current_ready_promise = WebIDL::create_resolved_promise(realm(), this);
  508. }
  509. return *m_current_ready_promise;
  510. }
  511. JS::NonnullGCPtr<WebIDL::Promise> Animation::current_finished_promise() const
  512. {
  513. if (!m_current_finished_promise) {
  514. // The current finished promise is initially a pending Promise object.
  515. m_current_finished_promise = WebIDL::create_promise(realm());
  516. }
  517. return *m_current_finished_promise;
  518. }
  519. Animation::Animation(JS::Realm& realm)
  520. : DOM::EventTarget(realm)
  521. {
  522. }
  523. void Animation::initialize(JS::Realm& realm)
  524. {
  525. Base::initialize(realm);
  526. set_prototype(&Bindings::ensure_web_prototype<Bindings::AnimationPrototype>(realm, "Animation"_fly_string));
  527. }
  528. void Animation::visit_edges(Cell::Visitor& visitor)
  529. {
  530. Base::visit_edges(visitor);
  531. visitor.visit(m_effect);
  532. visitor.visit(m_timeline);
  533. visitor.visit(m_current_ready_promise);
  534. visitor.visit(m_current_finished_promise);
  535. }
  536. }