Frame.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/InProcessWebView.h>
  29. #include <LibWeb/Layout/LayoutBreak.h>
  30. #include <LibWeb/Layout/LayoutDocument.h>
  31. #include <LibWeb/Layout/LayoutText.h>
  32. #include <LibWeb/Layout/LayoutWidget.h>
  33. #include <LibWeb/Page/Frame.h>
  34. namespace Web {
  35. Frame::Frame(DOM::Element& host_element, Frame& main_frame)
  36. : m_page(main_frame.page())
  37. , m_main_frame(main_frame)
  38. , m_loader(*this)
  39. , m_event_handler({}, *this)
  40. , m_host_element(host_element.make_weak_ptr())
  41. {
  42. setup();
  43. }
  44. Frame::Frame(Page& page)
  45. : m_page(page)
  46. , m_main_frame(*this)
  47. , m_loader(*this)
  48. , m_event_handler({}, *this)
  49. {
  50. setup();
  51. }
  52. Frame::~Frame()
  53. {
  54. }
  55. void Frame::setup()
  56. {
  57. m_cursor_blink_timer = Core::Timer::construct(500, [this] {
  58. if (!is_focused_frame())
  59. return;
  60. if (m_cursor_position.node() && m_cursor_position.node()->layout_node()) {
  61. m_cursor_blink_state = !m_cursor_blink_state;
  62. m_cursor_position.node()->layout_node()->set_needs_display();
  63. }
  64. });
  65. }
  66. bool Frame::is_focused_frame() const
  67. {
  68. return this == &page().focused_frame();
  69. }
  70. void Frame::set_document(DOM::Document* document)
  71. {
  72. if (m_document == document)
  73. return;
  74. if (m_document)
  75. m_document->detach_from_frame({}, *this);
  76. m_document = document;
  77. if (m_document)
  78. m_document->attach_to_frame({}, *this);
  79. page().client().page_did_set_document_in_main_frame(m_document);
  80. }
  81. void Frame::set_size(const Gfx::IntSize& size)
  82. {
  83. if (m_size == size)
  84. return;
  85. m_size = size;
  86. if (m_document)
  87. m_document->layout();
  88. }
  89. void Frame::set_viewport_rect(const Gfx::IntRect& rect)
  90. {
  91. if (m_viewport_rect == rect)
  92. return;
  93. m_viewport_rect = rect;
  94. if (m_document && m_document->layout_node())
  95. m_document->layout_node()->did_set_viewport_rect({}, rect);
  96. }
  97. void Frame::set_needs_display(const Gfx::IntRect& rect)
  98. {
  99. if (!m_viewport_rect.intersects(rect))
  100. return;
  101. if (is_main_frame()) {
  102. page().client().page_did_invalidate(to_main_frame_rect(rect));
  103. return;
  104. }
  105. if (host_element() && host_element()->layout_node())
  106. host_element()->layout_node()->set_needs_display();
  107. }
  108. void Frame::did_scroll(Badge<InProcessWebView>)
  109. {
  110. if (!m_document)
  111. return;
  112. if (!m_document->layout_node())
  113. return;
  114. m_document->layout_node()->for_each_in_subtree_of_type<LayoutWidget>([&](auto& layout_widget) {
  115. layout_widget.update_widget();
  116. return IterationDecision::Continue;
  117. });
  118. }
  119. void Frame::scroll_to_anchor(const String& fragment)
  120. {
  121. if (!document())
  122. return;
  123. auto element = document()->get_element_by_id(fragment);
  124. if (!element) {
  125. auto candidates = document()->get_elements_by_name(fragment);
  126. for (auto& candidate : candidates) {
  127. if (is<HTML::HTMLAnchorElement>(candidate)) {
  128. element = downcast<HTML::HTMLAnchorElement>(candidate);
  129. break;
  130. }
  131. }
  132. }
  133. if (!element || !element->layout_node())
  134. return;
  135. auto& layout_node = *element->layout_node();
  136. Gfx::FloatRect float_rect { layout_node.box_type_agnostic_position(), { (float)viewport_rect().width(), (float)viewport_rect().height() } };
  137. if (is<LayoutBox>(layout_node)) {
  138. auto& layout_box = downcast<LayoutBox>(layout_node);
  139. auto padding_box = layout_box.box_model().padding_box(layout_box);
  140. float_rect.move_by(-padding_box.left, -padding_box.top);
  141. }
  142. page().client().page_did_request_scroll_into_view(enclosing_int_rect(float_rect));
  143. }
  144. Gfx::IntRect Frame::to_main_frame_rect(const Gfx::IntRect& a_rect)
  145. {
  146. auto rect = a_rect;
  147. rect.set_location(to_main_frame_position(a_rect.location()));
  148. return rect;
  149. }
  150. Gfx::IntPoint Frame::to_main_frame_position(const Gfx::IntPoint& a_position)
  151. {
  152. auto position = a_position;
  153. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  154. if (ancestor->is_main_frame())
  155. break;
  156. if (!ancestor->host_element())
  157. return {};
  158. if (!ancestor->host_element()->layout_node())
  159. return {};
  160. position.move_by(ancestor->host_element()->layout_node()->box_type_agnostic_position().to_type<int>());
  161. }
  162. return position;
  163. }
  164. void Frame::set_cursor_position(const DOM::Position& position)
  165. {
  166. if (m_cursor_position == position)
  167. return;
  168. if (m_cursor_position.node() && m_cursor_position.node()->layout_node())
  169. m_cursor_position.node()->layout_node()->set_needs_display();
  170. m_cursor_position = position;
  171. if (m_cursor_position.node() && m_cursor_position.node()->layout_node())
  172. m_cursor_position.node()->layout_node()->set_needs_display();
  173. dbg() << "Cursor position: " << m_cursor_position;
  174. }
  175. String Frame::selected_text() const
  176. {
  177. StringBuilder builder;
  178. if (!m_document)
  179. return {};
  180. auto* layout_root = m_document->layout_node();
  181. if (!layout_root)
  182. return {};
  183. if (!layout_root->selection().is_valid())
  184. return {};
  185. auto selection = layout_root->selection().normalized();
  186. if (selection.start().layout_node == selection.end().layout_node) {
  187. if (!is<LayoutText>(*selection.start().layout_node))
  188. return "";
  189. return downcast<LayoutText>(*selection.start().layout_node).text_for_rendering().substring(selection.start().index_in_node, selection.end().index_in_node - selection.start().index_in_node);
  190. }
  191. // Start node
  192. auto layout_node = selection.start().layout_node;
  193. if (is<LayoutText>(*layout_node)) {
  194. auto& text = downcast<LayoutText>(*layout_node).text_for_rendering();
  195. builder.append(text.substring(selection.start().index_in_node, text.length() - selection.start().index_in_node));
  196. }
  197. // Middle nodes
  198. layout_node = layout_node->next_in_pre_order();
  199. while (layout_node && layout_node != selection.end().layout_node) {
  200. if (is<LayoutText>(*layout_node))
  201. builder.append(downcast<LayoutText>(*layout_node).text_for_rendering());
  202. else if (is<LayoutBreak>(*layout_node) || is<LayoutBlock>(*layout_node))
  203. builder.append('\n');
  204. layout_node = layout_node->next_in_pre_order();
  205. }
  206. // End node
  207. ASSERT(layout_node == selection.end().layout_node);
  208. if (is<LayoutText>(*layout_node)) {
  209. auto& text = downcast<LayoutText>(*layout_node).text_for_rendering();
  210. builder.append(text.substring(0, selection.end().index_in_node));
  211. }
  212. return builder.to_string();
  213. }
  214. }