VisualViewport.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/Bindings/VisualViewportPrototype.h>
  8. #include <LibWeb/CSS/VisualViewport.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/DOM/EventDispatcher.h>
  11. #include <LibWeb/DOM/IDLEventListener.h>
  12. #include <LibWeb/HTML/EventHandler.h>
  13. #include <LibWeb/HTML/EventNames.h>
  14. namespace Web::CSS {
  15. JS::NonnullGCPtr<VisualViewport> VisualViewport::create(DOM::Document& document)
  16. {
  17. return document.heap().allocate<VisualViewport>(document.realm(), document);
  18. }
  19. VisualViewport::VisualViewport(DOM::Document& document)
  20. : DOM::EventTarget(document.realm())
  21. , m_document(document)
  22. {
  23. }
  24. void VisualViewport::initialize(JS::Realm& realm)
  25. {
  26. Base::initialize(realm);
  27. set_prototype(&Bindings::ensure_web_prototype<Bindings::VisualViewportPrototype>(realm, "VisualViewport"));
  28. }
  29. void VisualViewport::visit_edges(Cell::Visitor& visitor)
  30. {
  31. Base::visit_edges(visitor);
  32. visitor.visit(m_document);
  33. }
  34. // https://drafts.csswg.org/cssom-view/#dom-visualviewport-offsetleft
  35. double VisualViewport::offset_left() const
  36. {
  37. // 1. If the visual viewport’s associated document is not fully active, return 0.
  38. if (!m_document->is_fully_active())
  39. return 0;
  40. // 2. Otherwise, return the offset of the left edge of the visual viewport from the left edge of the layout viewport.
  41. VERIFY(m_document->navigable());
  42. return m_document->viewport_rect().left().to_double();
  43. }
  44. // https://drafts.csswg.org/cssom-view/#dom-visualviewport-offsettop
  45. double VisualViewport::offset_top() const
  46. {
  47. // 1. If the visual viewport’s associated document is not fully active, return 0.
  48. if (!m_document->is_fully_active())
  49. return 0;
  50. // 2. Otherwise, return the offset of the top edge of the visual viewport from the top edge of the layout viewport.
  51. VERIFY(m_document->navigable());
  52. return m_document->viewport_rect().top().to_double();
  53. }
  54. // https://drafts.csswg.org/cssom-view/#dom-visualviewport-pageleft
  55. double VisualViewport::page_left() const
  56. {
  57. // 1. If the visual viewport’s associated document is not fully active, return 0.
  58. if (!m_document->is_fully_active())
  59. return 0;
  60. // FIXME: 2. Otherwise, return the offset of the left edge of the visual viewport
  61. // from the left edge of the initial containing block of the layout viewport’s document.
  62. return offset_left();
  63. }
  64. // https://drafts.csswg.org/cssom-view/#dom-visualviewport-pagetop
  65. double VisualViewport::page_top() const
  66. {
  67. // 1. If the visual viewport’s associated document is not fully active, return 0.
  68. if (!m_document->is_fully_active())
  69. return 0;
  70. // FIXME: 2. Otherwise, return the offset of the top edge of the visual viewport
  71. // from the top edge of the initial containing block of the layout viewport’s document.
  72. return offset_top();
  73. }
  74. // https://drafts.csswg.org/cssom-view/#dom-visualviewport-width
  75. double VisualViewport::width() const
  76. {
  77. // 1. If the visual viewport’s associated document is not fully active, return 0.
  78. if (!m_document->is_fully_active())
  79. return 0;
  80. // 2. Otherwise, return the width of the visual viewport
  81. // FIXME: excluding the width of any rendered vertical classic scrollbar that is fixed to the visual viewport.
  82. VERIFY(m_document->navigable());
  83. return m_document->viewport_rect().width().to_double();
  84. }
  85. // https://drafts.csswg.org/cssom-view/#dom-visualviewport-height
  86. double VisualViewport::height() const
  87. {
  88. // 1. If the visual viewport’s associated document is not fully active, return 0.
  89. if (!m_document->is_fully_active())
  90. return 0;
  91. // 2. Otherwise, return the height of the visual viewport
  92. // FIXME: excluding the height of any rendered vertical classic scrollbar that is fixed to the visual viewport.
  93. VERIFY(m_document->navigable());
  94. return m_document->viewport_rect().height().to_double();
  95. }
  96. // https://drafts.csswg.org/cssom-view/#dom-visualviewport-scale
  97. double VisualViewport::scale() const
  98. {
  99. // FIXME: Implement.
  100. return 1;
  101. }
  102. void VisualViewport::set_onresize(WebIDL::CallbackType* event_handler)
  103. {
  104. set_event_handler_attribute(HTML::EventNames::resize, event_handler);
  105. }
  106. WebIDL::CallbackType* VisualViewport::onresize()
  107. {
  108. return event_handler_attribute(HTML::EventNames::resize);
  109. }
  110. void VisualViewport::set_onscroll(WebIDL::CallbackType* event_handler)
  111. {
  112. set_event_handler_attribute(HTML::EventNames::scroll, event_handler);
  113. }
  114. WebIDL::CallbackType* VisualViewport::onscroll()
  115. {
  116. return event_handler_attribute(HTML::EventNames::scroll);
  117. }
  118. void VisualViewport::set_onscrollend(WebIDL::CallbackType* event_handler)
  119. {
  120. set_event_handler_attribute(HTML::EventNames::scrollend, event_handler);
  121. }
  122. WebIDL::CallbackType* VisualViewport::onscrollend()
  123. {
  124. return event_handler_attribute(HTML::EventNames::scrollend);
  125. }
  126. }