VideoBox.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/HTML/HTMLVideoElement.h>
  7. #include <LibWeb/Layout/VideoBox.h>
  8. #include <LibWeb/Painting/VideoPaintable.h>
  9. namespace Web::Layout {
  10. VideoBox::VideoBox(DOM::Document& document, DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style)
  11. : ReplacedBox(document, element, move(style))
  12. {
  13. browsing_context().register_viewport_client(*this);
  14. }
  15. void VideoBox::finalize()
  16. {
  17. Base::finalize();
  18. // NOTE: We unregister from the browsing context in finalize() to avoid trouble
  19. // in the scenario where our BrowsingContext has already been swept by GC.
  20. browsing_context().unregister_viewport_client(*this);
  21. }
  22. HTML::HTMLVideoElement& VideoBox::dom_node()
  23. {
  24. return static_cast<HTML::HTMLVideoElement&>(ReplacedBox::dom_node());
  25. }
  26. HTML::HTMLVideoElement const& VideoBox::dom_node() const
  27. {
  28. return static_cast<HTML::HTMLVideoElement const&>(ReplacedBox::dom_node());
  29. }
  30. int VideoBox::preferred_width() const
  31. {
  32. return dom_node().attribute(HTML::AttributeNames::width).to_int().value_or(dom_node().video_width());
  33. }
  34. int VideoBox::preferred_height() const
  35. {
  36. return dom_node().attribute(HTML::AttributeNames::height).to_int().value_or(dom_node().video_height());
  37. }
  38. void VideoBox::prepare_for_replaced_layout()
  39. {
  40. auto width = static_cast<float>(dom_node().video_width());
  41. set_intrinsic_width(width);
  42. auto height = static_cast<float>(dom_node().video_height());
  43. set_intrinsic_height(height);
  44. if (width != 0 && height != 0)
  45. set_intrinsic_aspect_ratio(width / height);
  46. else
  47. set_intrinsic_aspect_ratio({});
  48. }
  49. void VideoBox::browsing_context_did_set_viewport_rect(CSSPixelRect const&)
  50. {
  51. // FIXME: Several steps in HTMLMediaElement indicate we may optionally handle whether the media object
  52. // is in view. Implement those steps.
  53. }
  54. JS::GCPtr<Painting::Paintable> VideoBox::create_paintable() const
  55. {
  56. return Painting::VideoPaintable::create(*this);
  57. }
  58. }