VideoBox.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. JS_DEFINE_ALLOCATOR(VideoBox);
  11. VideoBox::VideoBox(DOM::Document& document, DOM::Element& element, CSS::StyleProperties style)
  12. : ReplacedBox(document, element, move(style))
  13. {
  14. document.register_viewport_client(*this);
  15. }
  16. void VideoBox::finalize()
  17. {
  18. Base::finalize();
  19. // NOTE: We unregister from the document in finalize() to avoid trouble
  20. // in the scenario where our Document has already been swept by GC.
  21. document().unregister_viewport_client(*this);
  22. }
  23. HTML::HTMLVideoElement& VideoBox::dom_node()
  24. {
  25. return static_cast<HTML::HTMLVideoElement&>(ReplacedBox::dom_node());
  26. }
  27. HTML::HTMLVideoElement const& VideoBox::dom_node() const
  28. {
  29. return static_cast<HTML::HTMLVideoElement const&>(ReplacedBox::dom_node());
  30. }
  31. void VideoBox::prepare_for_replaced_layout()
  32. {
  33. CSSPixels width = dom_node().video_width();
  34. set_natural_width(width);
  35. CSSPixels height = dom_node().video_height();
  36. set_natural_height(height);
  37. if (width != 0 && height != 0)
  38. set_natural_aspect_ratio(width / height);
  39. else
  40. set_natural_aspect_ratio({});
  41. }
  42. void VideoBox::did_set_viewport_rect(CSSPixelRect const&)
  43. {
  44. // FIXME: Several steps in HTMLMediaElement indicate we may optionally handle whether the media object
  45. // is in view. Implement those steps.
  46. }
  47. JS::GCPtr<Painting::Paintable> VideoBox::create_paintable() const
  48. {
  49. return Painting::VideoPaintable::create(*this);
  50. }
  51. }