ViewImplementation.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Error.h>
  7. #include <AK/String.h>
  8. #include <LibWebView/ViewImplementation.h>
  9. namespace WebView {
  10. WebContentClient& ViewImplementation::client()
  11. {
  12. VERIFY(m_client_state.client);
  13. return *m_client_state.client;
  14. }
  15. WebContentClient const& ViewImplementation::client() const
  16. {
  17. VERIFY(m_client_state.client);
  18. return *m_client_state.client;
  19. }
  20. void ViewImplementation::load(AK::URL const& url)
  21. {
  22. m_url = url;
  23. client().async_load_url(url);
  24. }
  25. void ViewImplementation::load_html(StringView html, AK::URL const& url)
  26. {
  27. m_url = url;
  28. client().async_load_html(html, url);
  29. }
  30. void ViewImplementation::load_empty_document()
  31. {
  32. load_html(""sv, {});
  33. }
  34. void ViewImplementation::zoom_in()
  35. {
  36. if (m_zoom_level >= ZOOM_MAX_LEVEL)
  37. return;
  38. m_zoom_level += ZOOM_STEP;
  39. update_zoom();
  40. }
  41. void ViewImplementation::zoom_out()
  42. {
  43. if (m_zoom_level <= ZOOM_MIN_LEVEL)
  44. return;
  45. m_zoom_level -= ZOOM_STEP;
  46. update_zoom();
  47. }
  48. void ViewImplementation::reset_zoom()
  49. {
  50. m_zoom_level = 1.0f;
  51. update_zoom();
  52. }
  53. void ViewImplementation::get_source()
  54. {
  55. client().async_get_source();
  56. }
  57. void ViewImplementation::inspect_dom_tree()
  58. {
  59. client().async_inspect_dom_tree();
  60. }
  61. void ViewImplementation::inspect_accessibility_tree()
  62. {
  63. client().async_inspect_accessibility_tree();
  64. }
  65. ErrorOr<ViewImplementation::DOMNodeProperties> ViewImplementation::inspect_dom_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> pseudo_element)
  66. {
  67. auto response = client().inspect_dom_node(node_id, pseudo_element);
  68. if (!response.has_style())
  69. return Error::from_string_view("Inspected node returned no style"sv);
  70. return DOMNodeProperties {
  71. .computed_style_json = TRY(String::from_deprecated_string(response.take_computed_style())),
  72. .resolved_style_json = TRY(String::from_deprecated_string(response.take_resolved_style())),
  73. .custom_properties_json = TRY(String::from_deprecated_string(response.take_custom_properties())),
  74. .node_box_sizing_json = TRY(String::from_deprecated_string(response.take_node_box_sizing())),
  75. };
  76. }
  77. void ViewImplementation::clear_inspected_dom_node()
  78. {
  79. client().inspect_dom_node(0, {});
  80. }
  81. i32 ViewImplementation::get_hovered_node_id()
  82. {
  83. return client().get_hovered_node_id();
  84. }
  85. }