Frame.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/HTML/HTMLAnchorElement.h>
  28. #include <LibWeb/Frame/Frame.h>
  29. #include <LibWeb/Layout/LayoutDocument.h>
  30. #include <LibWeb/Layout/LayoutWidget.h>
  31. #include <LibWeb/PageView.h>
  32. namespace Web {
  33. Frame::Frame(Element& host_element, Frame& main_frame)
  34. : m_page(main_frame.page())
  35. , m_main_frame(main_frame)
  36. , m_loader(*this)
  37. , m_event_handler({}, *this)
  38. , m_host_element(host_element.make_weak_ptr())
  39. {
  40. }
  41. Frame::Frame(Page& page)
  42. : m_page(page)
  43. , m_main_frame(*this)
  44. , m_loader(*this)
  45. , m_event_handler({}, *this)
  46. {
  47. }
  48. Frame::~Frame()
  49. {
  50. }
  51. void Frame::set_document(Document* document)
  52. {
  53. if (m_document == document)
  54. return;
  55. if (m_document)
  56. m_document->detach_from_frame({}, *this);
  57. m_document = document;
  58. if (m_document)
  59. m_document->attach_to_frame({}, *this);
  60. page().client().page_did_set_document_in_main_frame(m_document);
  61. }
  62. void Frame::set_size(const Gfx::IntSize& size)
  63. {
  64. if (m_size == size)
  65. return;
  66. m_size = size;
  67. if (m_document)
  68. m_document->layout();
  69. }
  70. void Frame::set_viewport_rect(const Gfx::IntRect& rect)
  71. {
  72. if (m_viewport_rect == rect)
  73. return;
  74. m_viewport_rect = rect;
  75. if (m_document && m_document->layout_node())
  76. m_document->layout_node()->did_set_viewport_rect({}, rect);
  77. }
  78. void Frame::set_needs_display(const Gfx::IntRect& rect)
  79. {
  80. if (!m_viewport_rect.intersects(rect))
  81. return;
  82. if (is_main_frame()) {
  83. page().client().page_did_invalidate(to_main_frame_rect(rect));
  84. return;
  85. }
  86. if (host_element() && host_element()->layout_node())
  87. host_element()->layout_node()->set_needs_display();
  88. }
  89. void Frame::did_scroll(Badge<PageView>)
  90. {
  91. if (!m_document)
  92. return;
  93. if (!m_document->layout_node())
  94. return;
  95. m_document->layout_node()->for_each_in_subtree_of_type<LayoutWidget>([&](auto& layout_widget) {
  96. layout_widget.update_widget();
  97. return IterationDecision::Continue;
  98. });
  99. }
  100. void Frame::scroll_to_anchor(const String& fragment)
  101. {
  102. if (!document())
  103. return;
  104. const auto* element = document()->get_element_by_id(fragment);
  105. if (!element) {
  106. auto candidates = document()->get_elements_by_name(fragment);
  107. for (auto* candidate : candidates) {
  108. if (is<HTMLAnchorElement>(*candidate)) {
  109. element = downcast<HTMLAnchorElement>(candidate);
  110. break;
  111. }
  112. }
  113. }
  114. if (!element || !element->layout_node())
  115. return;
  116. auto& layout_node = *element->layout_node();
  117. Gfx::FloatRect float_rect { layout_node.box_type_agnostic_position(), { (float)viewport_rect().width(), (float)viewport_rect().height() } };
  118. if (is<LayoutBox>(layout_node)) {
  119. auto& layout_box = downcast<LayoutBox>(layout_node);
  120. auto padding_box = layout_box.box_model().padding_box(layout_box);
  121. float_rect.move_by(-padding_box.left, -padding_box.top);
  122. }
  123. page().client().page_did_request_scroll_into_view(enclosing_int_rect(float_rect));
  124. }
  125. Gfx::IntRect Frame::to_main_frame_rect(const Gfx::IntRect& a_rect)
  126. {
  127. auto rect = a_rect;
  128. rect.set_location(to_main_frame_position(a_rect.location()));
  129. return rect;
  130. }
  131. Gfx::IntPoint Frame::to_main_frame_position(const Gfx::IntPoint& a_position)
  132. {
  133. auto position = a_position;
  134. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  135. if (ancestor->is_main_frame())
  136. break;
  137. if (!ancestor->host_element())
  138. return {};
  139. if (!ancestor->host_element()->layout_node())
  140. return {};
  141. position.move_by(ancestor->host_element()->layout_node()->box_type_agnostic_position().to_int_point());
  142. }
  143. return position;
  144. }
  145. }