ViewImplementation.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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::set_preferred_color_scheme(Web::CSS::PreferredColorScheme color_scheme)
  54. {
  55. client().async_set_preferred_color_scheme(color_scheme);
  56. }
  57. DeprecatedString ViewImplementation::selected_text()
  58. {
  59. return client().get_selected_text();
  60. }
  61. void ViewImplementation::select_all()
  62. {
  63. client().async_select_all();
  64. }
  65. void ViewImplementation::get_source()
  66. {
  67. client().async_get_source();
  68. }
  69. void ViewImplementation::inspect_dom_tree()
  70. {
  71. client().async_inspect_dom_tree();
  72. }
  73. void ViewImplementation::inspect_accessibility_tree()
  74. {
  75. client().async_inspect_accessibility_tree();
  76. }
  77. ErrorOr<ViewImplementation::DOMNodeProperties> ViewImplementation::inspect_dom_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> pseudo_element)
  78. {
  79. auto response = client().inspect_dom_node(node_id, pseudo_element);
  80. if (!response.has_style())
  81. return Error::from_string_view("Inspected node returned no style"sv);
  82. return DOMNodeProperties {
  83. .computed_style_json = TRY(String::from_deprecated_string(response.take_computed_style())),
  84. .resolved_style_json = TRY(String::from_deprecated_string(response.take_resolved_style())),
  85. .custom_properties_json = TRY(String::from_deprecated_string(response.take_custom_properties())),
  86. .node_box_sizing_json = TRY(String::from_deprecated_string(response.take_node_box_sizing())),
  87. };
  88. }
  89. void ViewImplementation::clear_inspected_dom_node()
  90. {
  91. client().inspect_dom_node(0, {});
  92. }
  93. i32 ViewImplementation::get_hovered_node_id()
  94. {
  95. return client().get_hovered_node_id();
  96. }
  97. void ViewImplementation::debug_request(DeprecatedString const& request, DeprecatedString const& argument)
  98. {
  99. client().async_debug_request(request, argument);
  100. }
  101. void ViewImplementation::run_javascript(StringView js_source)
  102. {
  103. client().async_run_javascript(js_source);
  104. }
  105. }