VisualViewport.cpp 4.9 KB

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