Frame.cpp 8.6 KB

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