InspectorWidget.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "InspectorWidget.h"
  8. #include <LibGUI/BoxLayout.h>
  9. #include <LibGUI/Splitter.h>
  10. #include <LibGUI/TabWidget.h>
  11. #include <LibGUI/TableView.h>
  12. #include <LibGUI/TreeView.h>
  13. #include <LibWeb/DOM/Document.h>
  14. #include <LibWeb/DOM/Element.h>
  15. #include <LibWeb/DOMTreeModel.h>
  16. #include <LibWeb/OutOfProcessWebView.h>
  17. #include <LibWeb/StylePropertiesModel.h>
  18. namespace Browser {
  19. void InspectorWidget::set_inspected_node(i32 node_id)
  20. {
  21. if (!m_dom_json.has_value()) {
  22. // DOM Tree hasn't been loaded yet, so make a note to inspect it later.
  23. m_pending_inspect_node_id = node_id;
  24. return;
  25. }
  26. auto* model = verify_cast<Web::DOMTreeModel>(m_dom_tree_view->model());
  27. auto index = model->index_for_node(node_id);
  28. if (!index.is_valid()) {
  29. dbgln("InspectorWidget told to inspect non-existent node, id={}", node_id);
  30. return;
  31. }
  32. m_dom_tree_view->expand_all_parents_of(index);
  33. m_dom_tree_view->set_cursor(index, GUI::AbstractView::SelectionUpdate::Set);
  34. set_inspected_node(index);
  35. }
  36. void InspectorWidget::set_inspected_node(GUI::ModelIndex const index)
  37. {
  38. auto* json = static_cast<JsonObject const*>(index.internal_data());
  39. i32 inspected_node = json ? json->get("id").to_i32() : 0;
  40. if (inspected_node == m_inspected_node_id)
  41. return;
  42. m_inspected_node_id = inspected_node;
  43. auto maybe_inspected_node_properties = m_web_view->inspect_dom_node(m_inspected_node_id);
  44. if (maybe_inspected_node_properties.has_value()) {
  45. auto inspected_node_properties = maybe_inspected_node_properties.value();
  46. load_style_json(inspected_node_properties.specified_values_json, inspected_node_properties.computed_values_json);
  47. } else {
  48. clear_style_json();
  49. }
  50. }
  51. InspectorWidget::InspectorWidget()
  52. {
  53. set_layout<GUI::VerticalBoxLayout>();
  54. auto& splitter = add<GUI::VerticalSplitter>();
  55. auto& top_tab_widget = splitter.add<GUI::TabWidget>();
  56. m_dom_tree_view = top_tab_widget.add_tab<GUI::TreeView>("DOM");
  57. m_dom_tree_view->on_selection_change = [this] {
  58. const auto& index = m_dom_tree_view->selection().first();
  59. set_inspected_node(index);
  60. };
  61. m_layout_tree_view = top_tab_widget.add_tab<GUI::TreeView>("Layout");
  62. m_layout_tree_view->on_selection_change = [this] {
  63. const auto& index = m_layout_tree_view->selection().first();
  64. set_inspected_node(index);
  65. };
  66. auto& bottom_tab_widget = splitter.add<GUI::TabWidget>();
  67. m_style_table_view = bottom_tab_widget.add_tab<GUI::TableView>("Styles");
  68. m_computed_style_table_view = bottom_tab_widget.add_tab<GUI::TableView>("Computed");
  69. }
  70. InspectorWidget::~InspectorWidget()
  71. {
  72. }
  73. void InspectorWidget::select_default_node()
  74. {
  75. clear_style_json();
  76. // FIXME: Select the <body> element, or else the root node.
  77. m_dom_tree_view->collapse_tree();
  78. m_dom_tree_view->set_cursor({}, GUI::AbstractView::SelectionUpdate::ClearIfNotSelected);
  79. m_layout_tree_view->collapse_tree();
  80. m_layout_tree_view->set_cursor({}, GUI::AbstractView::SelectionUpdate::ClearIfNotSelected);
  81. }
  82. void InspectorWidget::set_dom_json(String json)
  83. {
  84. if (m_dom_json.has_value() && m_dom_json.value() == json)
  85. return;
  86. m_dom_json = json;
  87. m_dom_tree_view->set_model(Web::DOMTreeModel::create(m_dom_json->view()));
  88. // FIXME: Support the LayoutTreeModel
  89. // m_layout_tree_view->set_model(Web::LayoutTreeModel::create(*document));
  90. if (m_pending_inspect_node_id.has_value()) {
  91. i32 node_id = m_pending_inspect_node_id.value();
  92. m_pending_inspect_node_id.clear();
  93. set_inspected_node(node_id);
  94. } else {
  95. select_default_node();
  96. }
  97. }
  98. void InspectorWidget::set_dom_node_properties_json(i32 node_id, String specified_values_json, String computed_values_json)
  99. {
  100. if (node_id != m_inspected_node_id) {
  101. dbgln("Got data for the wrong node id! Wanted {}, got {}", m_inspected_node_id, node_id);
  102. return;
  103. }
  104. load_style_json(specified_values_json, computed_values_json);
  105. }
  106. void InspectorWidget::load_style_json(String specified_values_json, String computed_values_json)
  107. {
  108. m_inspected_node_specified_values_json = specified_values_json;
  109. m_inspected_node_computed_values_json = computed_values_json;
  110. m_style_table_view->set_model(Web::StylePropertiesModel::create(m_inspected_node_specified_values_json.value().view()));
  111. m_computed_style_table_view->set_model(Web::StylePropertiesModel::create(m_inspected_node_computed_values_json.value().view()));
  112. }
  113. void InspectorWidget::clear_style_json()
  114. {
  115. m_inspected_node_specified_values_json.clear();
  116. m_inspected_node_computed_values_json.clear();
  117. m_style_table_view->set_model(nullptr);
  118. m_computed_style_table_view->set_model(nullptr);
  119. }
  120. }