Frame.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/BreakNode.h>
  30. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  31. #include <LibWeb/Layout/TextNode.h>
  32. #include <LibWeb/Layout/WidgetBox.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)
  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 m_page && &m_page->focused_frame() == this;
  69. }
  70. void Frame::set_document(DOM::Document* document)
  71. {
  72. if (m_document == document)
  73. return;
  74. m_cursor_position = {};
  75. if (m_document)
  76. m_document->detach_from_frame({}, *this);
  77. m_document = document;
  78. if (m_document) {
  79. m_document->attach_to_frame({}, *this);
  80. if (m_page)
  81. m_page->client().page_did_change_title(m_document->title());
  82. }
  83. if (m_page)
  84. m_page->client().page_did_set_document_in_main_frame(m_document);
  85. }
  86. void Frame::set_size(const Gfx::IntSize& size)
  87. {
  88. if (m_size == size)
  89. return;
  90. m_size = size;
  91. if (m_document)
  92. m_document->layout();
  93. }
  94. void Frame::set_viewport_scroll_offset(const Gfx::IntPoint& offset)
  95. {
  96. if (m_viewport_scroll_offset == offset)
  97. return;
  98. m_viewport_scroll_offset = offset;
  99. if (m_document && m_document->layout_node())
  100. m_document->layout_node()->did_set_viewport_rect({}, viewport_rect());
  101. }
  102. void Frame::set_needs_display(const Gfx::IntRect& rect)
  103. {
  104. if (!viewport_rect().intersects(rect))
  105. return;
  106. if (is_main_frame()) {
  107. if (m_page)
  108. m_page->client().page_did_invalidate(to_main_frame_rect(rect));
  109. return;
  110. }
  111. if (host_element() && host_element()->layout_node())
  112. host_element()->layout_node()->set_needs_display();
  113. }
  114. void Frame::did_scroll(Badge<InProcessWebView>)
  115. {
  116. if (!m_document)
  117. return;
  118. if (!m_document->layout_node())
  119. return;
  120. m_document->layout_node()->for_each_in_subtree_of_type<Layout::WidgetBox>([&](auto& layout_widget) {
  121. layout_widget.update_widget();
  122. return IterationDecision::Continue;
  123. });
  124. }
  125. void Frame::scroll_to_anchor(const String& fragment)
  126. {
  127. if (!document())
  128. return;
  129. auto element = document()->get_element_by_id(fragment);
  130. if (!element) {
  131. auto candidates = document()->get_elements_by_name(fragment);
  132. for (auto& candidate : candidates) {
  133. if (is<HTML::HTMLAnchorElement>(candidate)) {
  134. element = downcast<HTML::HTMLAnchorElement>(candidate);
  135. break;
  136. }
  137. }
  138. }
  139. if (!element || !element->layout_node())
  140. return;
  141. auto& layout_node = *element->layout_node();
  142. Gfx::FloatRect float_rect { layout_node.box_type_agnostic_position(), { (float)viewport_rect().width(), (float)viewport_rect().height() } };
  143. if (is<Layout::Box>(layout_node)) {
  144. auto& layout_box = downcast<Layout::Box>(layout_node);
  145. auto padding_box = layout_box.box_model().padding_box();
  146. float_rect.move_by(-padding_box.left, -padding_box.top);
  147. }
  148. if (m_page)
  149. m_page->client().page_did_request_scroll_into_view(enclosing_int_rect(float_rect));
  150. }
  151. Gfx::IntRect Frame::to_main_frame_rect(const Gfx::IntRect& a_rect)
  152. {
  153. auto rect = a_rect;
  154. rect.set_location(to_main_frame_position(a_rect.location()));
  155. return rect;
  156. }
  157. Gfx::IntPoint Frame::to_main_frame_position(const Gfx::IntPoint& a_position)
  158. {
  159. auto position = a_position;
  160. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  161. if (ancestor->is_main_frame())
  162. break;
  163. if (!ancestor->host_element())
  164. return {};
  165. if (!ancestor->host_element()->layout_node())
  166. return {};
  167. position.move_by(ancestor->host_element()->layout_node()->box_type_agnostic_position().to_type<int>());
  168. }
  169. return position;
  170. }
  171. void Frame::set_cursor_position(const DOM::Position& position)
  172. {
  173. if (m_cursor_position == position)
  174. return;
  175. if (m_cursor_position.node() && m_cursor_position.node()->layout_node())
  176. m_cursor_position.node()->layout_node()->set_needs_display();
  177. m_cursor_position = position;
  178. if (m_cursor_position.node() && m_cursor_position.node()->layout_node())
  179. m_cursor_position.node()->layout_node()->set_needs_display();
  180. dbg() << "Cursor position: " << m_cursor_position;
  181. }
  182. String Frame::selected_text() const
  183. {
  184. StringBuilder builder;
  185. if (!m_document)
  186. return {};
  187. auto* layout_root = m_document->layout_node();
  188. if (!layout_root)
  189. return {};
  190. if (!layout_root->selection().is_valid())
  191. return {};
  192. auto selection = layout_root->selection().normalized();
  193. if (selection.start().layout_node == selection.end().layout_node) {
  194. if (!is<Layout::TextNode>(*selection.start().layout_node))
  195. return "";
  196. return downcast<Layout::TextNode>(*selection.start().layout_node).text_for_rendering().substring(selection.start().index_in_node, selection.end().index_in_node - selection.start().index_in_node);
  197. }
  198. // Start node
  199. auto layout_node = selection.start().layout_node;
  200. if (is<Layout::TextNode>(*layout_node)) {
  201. auto& text = downcast<Layout::TextNode>(*layout_node).text_for_rendering();
  202. builder.append(text.substring(selection.start().index_in_node, text.length() - selection.start().index_in_node));
  203. }
  204. // Middle nodes
  205. layout_node = layout_node->next_in_pre_order();
  206. while (layout_node && layout_node != selection.end().layout_node) {
  207. if (is<Layout::TextNode>(*layout_node))
  208. builder.append(downcast<Layout::TextNode>(*layout_node).text_for_rendering());
  209. else if (is<Layout::BreakNode>(*layout_node) || is<Layout::BlockBox>(*layout_node))
  210. builder.append('\n');
  211. layout_node = layout_node->next_in_pre_order();
  212. }
  213. // End node
  214. ASSERT(layout_node == selection.end().layout_node);
  215. if (is<Layout::TextNode>(*layout_node)) {
  216. auto& text = downcast<Layout::TextNode>(*layout_node).text_for_rendering();
  217. builder.append(text.substring(0, selection.end().index_in_node));
  218. }
  219. return builder.to_string();
  220. }
  221. }