Frame.cpp 8.0 KB

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