WebContentView.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 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 "WebContentView.h"
  27. #include "WebContentClient.h"
  28. #include <AK/SharedBuffer.h>
  29. #include <LibGUI/Painter.h>
  30. #include <LibGUI/ScrollBar.h>
  31. #include <LibGUI/Window.h>
  32. #include <LibGfx/SystemTheme.h>
  33. WebContentView::WebContentView()
  34. {
  35. set_should_hide_unnecessary_scrollbars(true);
  36. m_client = WebContentClient::construct(*this);
  37. client().post_message(Messages::WebContentServer::UpdateSystemTheme(Gfx::current_system_theme_buffer_id()));
  38. }
  39. WebContentView::~WebContentView()
  40. {
  41. }
  42. void WebContentView::load(const URL& url)
  43. {
  44. m_url = url;
  45. client().post_message(Messages::WebContentServer::LoadURL(url));
  46. }
  47. void WebContentView::paint_event(GUI::PaintEvent& event)
  48. {
  49. GUI::ScrollableWidget::paint_event(event);
  50. GUI::Painter painter(*this);
  51. painter.add_clip_rect(frame_inner_rect());
  52. painter.add_clip_rect(event.rect());
  53. painter.translate(frame_thickness(), frame_thickness());
  54. ASSERT(m_front_bitmap);
  55. painter.blit({ 0, 0 }, *m_front_bitmap, m_front_bitmap->rect());
  56. }
  57. void WebContentView::resize_event(GUI::ResizeEvent& event)
  58. {
  59. GUI::ScrollableWidget::resize_event(event);
  60. m_front_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, available_size())->to_bitmap_backed_by_shared_buffer();
  61. m_front_bitmap->shared_buffer()->share_with(client().server_pid());
  62. m_back_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, available_size())->to_bitmap_backed_by_shared_buffer();
  63. m_back_bitmap->shared_buffer()->share_with(client().server_pid());
  64. client().post_message(Messages::WebContentServer::SetViewportRect(Gfx::IntRect({ horizontal_scrollbar().value(), vertical_scrollbar().value() }, available_size())));
  65. request_repaint();
  66. }
  67. void WebContentView::mousedown_event(GUI::MouseEvent& event)
  68. {
  69. client().post_message(Messages::WebContentServer::MouseDown(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers()));
  70. }
  71. void WebContentView::mouseup_event(GUI::MouseEvent& event)
  72. {
  73. client().post_message(Messages::WebContentServer::MouseUp(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers()));
  74. }
  75. void WebContentView::mousemove_event(GUI::MouseEvent& event)
  76. {
  77. client().post_message(Messages::WebContentServer::MouseMove(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers()));
  78. }
  79. void WebContentView::notify_server_did_paint(Badge<WebContentClient>, i32 shbuf_id)
  80. {
  81. if (m_back_bitmap->shbuf_id() == shbuf_id) {
  82. swap(m_back_bitmap, m_front_bitmap);
  83. update();
  84. }
  85. }
  86. void WebContentView::notify_server_did_invalidate_content_rect(Badge<WebContentClient>, [[maybe_unused]] const Gfx::IntRect& content_rect)
  87. {
  88. #ifdef DEBUG_SPAM
  89. dbg() << "server did invalidate content_rect: " << content_rect << ", current shbuf_id=" << m_bitmap->shbuf_id();
  90. #endif
  91. request_repaint();
  92. }
  93. void WebContentView::notify_server_did_change_selection(Badge<WebContentClient>)
  94. {
  95. request_repaint();
  96. }
  97. void WebContentView::notify_server_did_layout(Badge<WebContentClient>, const Gfx::IntSize& content_size)
  98. {
  99. set_content_size(content_size);
  100. }
  101. void WebContentView::notify_server_did_change_title(Badge<WebContentClient>, const String& title)
  102. {
  103. if (on_title_change)
  104. on_title_change(title);
  105. }
  106. void WebContentView::notify_server_did_request_scroll_into_view(Badge<WebContentClient>, const Gfx::IntRect& rect)
  107. {
  108. scroll_into_view(rect, true, true);
  109. }
  110. void WebContentView::notify_server_did_hover_link(Badge<WebContentClient>, const URL& url)
  111. {
  112. if (window())
  113. window()->set_override_cursor(GUI::StandardCursor::Hand);
  114. if (on_link_hover)
  115. on_link_hover(url);
  116. }
  117. void WebContentView::notify_server_did_unhover_link(Badge<WebContentClient>)
  118. {
  119. if (window())
  120. window()->set_override_cursor(GUI::StandardCursor::None);
  121. if (on_link_hover)
  122. on_link_hover({});
  123. }
  124. void WebContentView::notify_server_did_click_link(Badge<WebContentClient>, const URL& url, const String& target, unsigned int modifiers)
  125. {
  126. if (on_link_click)
  127. on_link_click(url, target, modifiers);
  128. }
  129. void WebContentView::notify_server_did_middle_click_link(Badge<WebContentClient>, const URL& url, const String& target, unsigned int modifiers)
  130. {
  131. if (on_link_middle_click)
  132. on_link_middle_click(url, target, modifiers);
  133. }
  134. void WebContentView::notify_server_did_start_loading(Badge<WebContentClient>, const URL& url)
  135. {
  136. if (on_load_start)
  137. on_load_start(url);
  138. }
  139. void WebContentView::did_scroll()
  140. {
  141. client().post_message(Messages::WebContentServer::SetViewportRect(visible_content_rect()));
  142. request_repaint();
  143. }
  144. void WebContentView::request_repaint()
  145. {
  146. client().post_message(Messages::WebContentServer::Paint(m_back_bitmap->rect().translated(horizontal_scrollbar().value(), vertical_scrollbar().value()), m_back_bitmap->shbuf_id()));
  147. }
  148. WebContentClient& WebContentView::client()
  149. {
  150. return *m_client;
  151. }