WebContentView.cpp 3.9 KB

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