HTMLAudioElement.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/HTMLAudioElementPrototype.h>
  7. #include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
  8. #include <LibWeb/HTML/AudioTrack.h>
  9. #include <LibWeb/HTML/AudioTrackList.h>
  10. #include <LibWeb/HTML/HTMLAudioElement.h>
  11. #include <LibWeb/HTML/Window.h>
  12. #include <LibWeb/Layout/AudioBox.h>
  13. namespace Web::HTML {
  14. JS_DEFINE_ALLOCATOR(HTMLAudioElement);
  15. HTMLAudioElement::HTMLAudioElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  16. : HTMLMediaElement(document, move(qualified_name))
  17. {
  18. }
  19. HTMLAudioElement::~HTMLAudioElement() = default;
  20. void HTMLAudioElement::initialize(JS::Realm& realm)
  21. {
  22. Base::initialize(realm);
  23. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLAudioElement);
  24. }
  25. JS::GCPtr<Layout::Node> HTMLAudioElement::create_layout_node(CSS::StyleProperties style)
  26. {
  27. return heap().allocate<Layout::AudioBox>(document(), *this, move(style));
  28. }
  29. void HTMLAudioElement::adjust_computed_style(CSS::StyleProperties& style)
  30. {
  31. // https://drafts.csswg.org/css-display-3/#unbox
  32. if (style.display().is_contents())
  33. style.set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::None)));
  34. }
  35. Layout::AudioBox* HTMLAudioElement::layout_node()
  36. {
  37. return static_cast<Layout::AudioBox*>(Node::layout_node());
  38. }
  39. Layout::AudioBox const* HTMLAudioElement::layout_node() const
  40. {
  41. return static_cast<Layout::AudioBox const*>(Node::layout_node());
  42. }
  43. void HTMLAudioElement::on_playing()
  44. {
  45. audio_tracks()->for_each_enabled_track([](auto& audio_track) {
  46. audio_track.play({});
  47. });
  48. }
  49. void HTMLAudioElement::on_paused()
  50. {
  51. audio_tracks()->for_each_enabled_track([](auto& audio_track) {
  52. audio_track.pause({});
  53. });
  54. }
  55. void HTMLAudioElement::on_seek(double position, MediaSeekMode seek_mode)
  56. {
  57. audio_tracks()->for_each_enabled_track([&](auto& audio_track) {
  58. audio_track.seek(position, seek_mode);
  59. });
  60. }
  61. void HTMLAudioElement::on_volume_change()
  62. {
  63. audio_tracks()->for_each_enabled_track([&](auto& audio_track) {
  64. audio_track.update_volume();
  65. });
  66. }
  67. }