WebContentView.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <LibGfx/SystemTheme.h>
  32. WebContentView::WebContentView()
  33. {
  34. m_client = WebContentClient::construct(*this);
  35. client().post_message(Messages::WebContentServer::UpdateSystemTheme(Gfx::current_system_theme_buffer_id()));
  36. }
  37. WebContentView::~WebContentView()
  38. {
  39. }
  40. void WebContentView::load(const URL& url)
  41. {
  42. client().post_message(Messages::WebContentServer::LoadURL(url));
  43. }
  44. void WebContentView::paint_event(GUI::PaintEvent& event)
  45. {
  46. GUI::ScrollableWidget::paint_event(event);
  47. GUI::Painter painter(*this);
  48. painter.add_clip_rect(frame_inner_rect());
  49. painter.add_clip_rect(event.rect());
  50. painter.translate(frame_thickness(), frame_thickness());
  51. ASSERT(m_bitmap);
  52. painter.blit({ 0, 0 }, *m_bitmap, m_bitmap->rect());
  53. }
  54. void WebContentView::resize_event(GUI::ResizeEvent& event)
  55. {
  56. GUI::ScrollableWidget::resize_event(event);
  57. auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, available_size());
  58. m_bitmap = bitmap->to_bitmap_backed_by_shared_buffer();
  59. m_bitmap->shared_buffer()->share_with(client().server_pid());
  60. client().post_message(Messages::WebContentServer::SetViewportRect(Gfx::IntRect({ horizontal_scrollbar().value(), vertical_scrollbar().value() }, m_bitmap->size())));
  61. request_repaint();
  62. }
  63. void WebContentView::mousedown_event(GUI::MouseEvent& event)
  64. {
  65. client().post_message(Messages::WebContentServer::MouseDown(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers()));
  66. }
  67. void WebContentView::mouseup_event(GUI::MouseEvent& event)
  68. {
  69. client().post_message(Messages::WebContentServer::MouseUp(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers()));
  70. }
  71. void WebContentView::mousemove_event(GUI::MouseEvent& event)
  72. {
  73. client().post_message(Messages::WebContentServer::MouseMove(to_content_position(event.position()), event.button(), event.buttons(), event.modifiers()));
  74. }
  75. void WebContentView::notify_server_did_paint(Badge<WebContentClient>, i32 shbuf_id)
  76. {
  77. if (m_bitmap->shbuf_id() == shbuf_id)
  78. update();
  79. }
  80. void WebContentView::notify_server_did_invalidate_content_rect(Badge<WebContentClient>, [[maybe_unused]] const Gfx::IntRect& content_rect)
  81. {
  82. #ifdef DEBUG_SPAM
  83. dbg() << "server did invalidate content_rect: " << content_rect << ", current shbuf_id=" << m_bitmap->shbuf_id();
  84. #endif
  85. request_repaint();
  86. }
  87. void WebContentView::notify_server_did_change_selection(Badge<WebContentClient>)
  88. {
  89. request_repaint();
  90. }
  91. void WebContentView::notify_server_did_layout(Badge<WebContentClient>, const Gfx::IntSize& content_size)
  92. {
  93. set_content_size(content_size);
  94. }
  95. void WebContentView::notify_server_did_change_title(Badge<WebContentClient>, const String& title)
  96. {
  97. if (on_title_change)
  98. on_title_change(title);
  99. }
  100. void WebContentView::did_scroll()
  101. {
  102. client().post_message(Messages::WebContentServer::SetViewportRect(visible_content_rect()));
  103. request_repaint();
  104. }
  105. void WebContentView::request_repaint()
  106. {
  107. client().post_message(Messages::WebContentServer::Paint(m_bitmap->rect().translated(horizontal_scrollbar().value(), vertical_scrollbar().value()), m_bitmap->shbuf_id()));
  108. }
  109. WebContentClient& WebContentView::client()
  110. {
  111. return *m_client;
  112. }