Frame.cpp 8.9 KB

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