Frame.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibWeb/DOM/Document.h>
  27. #include <LibWeb/Frame/Frame.h>
  28. #include <LibWeb/Layout/LayoutDocument.h>
  29. #include <LibWeb/Layout/LayoutWidget.h>
  30. #include <LibWeb/PageView.h>
  31. namespace Web {
  32. Frame::Frame(Element& host_element, Frame& main_frame)
  33. : m_page(main_frame.page())
  34. , m_main_frame(main_frame)
  35. , m_loader(*this)
  36. , m_event_handler({}, *this)
  37. , m_host_element(host_element.make_weak_ptr())
  38. {
  39. }
  40. Frame::Frame(Page& page)
  41. : m_page(page)
  42. , m_main_frame(*this)
  43. , m_loader(*this)
  44. , m_event_handler({}, *this)
  45. {
  46. }
  47. Frame::~Frame()
  48. {
  49. }
  50. void Frame::set_document(Document* document)
  51. {
  52. if (m_document == document)
  53. return;
  54. if (m_document)
  55. m_document->detach_from_frame({}, *this);
  56. m_document = document;
  57. if (m_document)
  58. m_document->attach_to_frame({}, *this);
  59. page().client().page_did_set_document_in_main_frame(m_document);
  60. }
  61. void Frame::set_size(const Gfx::IntSize& size)
  62. {
  63. if (m_size == size)
  64. return;
  65. m_size = size;
  66. if (m_document)
  67. m_document->layout();
  68. }
  69. void Frame::set_viewport_rect(const Gfx::IntRect& rect)
  70. {
  71. if (m_viewport_rect == rect)
  72. return;
  73. m_viewport_rect = rect;
  74. if (m_document && m_document->layout_node())
  75. m_document->layout_node()->did_set_viewport_rect({}, rect);
  76. }
  77. void Frame::set_needs_display(const Gfx::IntRect& rect)
  78. {
  79. if (!m_viewport_rect.intersects(rect))
  80. return;
  81. if (is_main_frame()) {
  82. page().client().page_did_invalidate(to_main_frame_rect(rect));
  83. return;
  84. }
  85. if (host_element() && host_element()->layout_node())
  86. host_element()->layout_node()->set_needs_display();
  87. }
  88. void Frame::did_scroll(Badge<PageView>)
  89. {
  90. if (!m_document)
  91. return;
  92. if (!m_document->layout_node())
  93. return;
  94. m_document->layout_node()->for_each_in_subtree_of_type<LayoutWidget>([&](auto& layout_widget) {
  95. layout_widget.update_widget();
  96. return IterationDecision::Continue;
  97. });
  98. }
  99. void Frame::scroll_to_anchor(const String& fragment)
  100. {
  101. // FIXME: This logic is backwards, the work should be done in here,
  102. // and then we just request that the "view" scrolls to a certain content offset.
  103. page().client().page_did_request_scroll_to_anchor(fragment);
  104. }
  105. Gfx::IntRect Frame::to_main_frame_rect(const Gfx::IntRect& a_rect)
  106. {
  107. auto rect = a_rect;
  108. rect.set_location(to_main_frame_position(a_rect.location()));
  109. return rect;
  110. }
  111. Gfx::IntPoint Frame::to_main_frame_position(const Gfx::IntPoint& a_position)
  112. {
  113. auto position = a_position;
  114. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  115. if (ancestor->is_main_frame())
  116. break;
  117. if (!ancestor->host_element())
  118. return {};
  119. if (!ancestor->host_element()->layout_node())
  120. return {};
  121. position.move_by(ancestor->host_element()->layout_node()->box_type_agnostic_position().to_int_point());
  122. }
  123. return position;
  124. }
  125. }