Frame.cpp 6.0 KB

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