2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-09-03 13:14:37 +03:00
|
|
|
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2020-01-02 16:55:19 +03:00
|
|
|
#include "InspectorWidget.h"
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
#include <LibGUI/Splitter.h>
|
2020-05-08 22:38:30 +03:00
|
|
|
#include <LibGUI/TabWidget.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/TableView.h>
|
|
|
|
#include <LibGUI/TreeView.h>
|
2020-03-07 12:32:51 +03:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
|
|
|
#include <LibWeb/DOM/Element.h>
|
|
|
|
#include <LibWeb/DOMTreeModel.h>
|
2021-09-02 14:05:32 +03:00
|
|
|
#include <LibWeb/OutOfProcessWebView.h>
|
2020-03-07 12:32:51 +03:00
|
|
|
#include <LibWeb/StylePropertiesModel.h>
|
2020-01-02 16:55:19 +03:00
|
|
|
|
2020-05-08 22:38:30 +03:00
|
|
|
namespace Browser {
|
|
|
|
|
2021-08-27 19:40:25 +03:00
|
|
|
void InspectorWidget::set_inspected_node(i32 node_id)
|
|
|
|
{
|
|
|
|
if (!m_dom_json.has_value()) {
|
|
|
|
// DOM Tree hasn't been loaded yet, so make a note to inspect it later.
|
|
|
|
m_pending_inspect_node_id = node_id;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* model = verify_cast<Web::DOMTreeModel>(m_dom_tree_view->model());
|
|
|
|
auto index = model->index_for_node(node_id);
|
|
|
|
if (!index.is_valid()) {
|
|
|
|
dbgln("InspectorWidget told to inspect non-existent node, id={}", node_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_dom_tree_view->expand_all_parents_of(index);
|
|
|
|
m_dom_tree_view->set_cursor(index, GUI::AbstractView::SelectionUpdate::Set);
|
|
|
|
set_inspected_node(index);
|
|
|
|
}
|
|
|
|
|
2021-09-02 14:05:32 +03:00
|
|
|
void InspectorWidget::set_inspected_node(GUI::ModelIndex const index)
|
2020-06-12 23:30:11 +03:00
|
|
|
{
|
2021-09-02 14:05:32 +03:00
|
|
|
auto* json = static_cast<JsonObject const*>(index.internal_data());
|
|
|
|
i32 inspected_node = json ? json->get("id").to_i32() : 0;
|
|
|
|
if (inspected_node == m_inspected_node_id)
|
|
|
|
return;
|
|
|
|
m_inspected_node_id = inspected_node;
|
|
|
|
|
|
|
|
auto maybe_inspected_node_properties = m_web_view->inspect_dom_node(m_inspected_node_id);
|
|
|
|
if (maybe_inspected_node_properties.has_value()) {
|
|
|
|
auto inspected_node_properties = maybe_inspected_node_properties.value();
|
2021-12-08 14:51:26 +03:00
|
|
|
load_style_json(inspected_node_properties.specified_values_json, inspected_node_properties.computed_values_json, inspected_node_properties.custom_properties_json);
|
2021-09-02 14:05:32 +03:00
|
|
|
} else {
|
2021-08-27 22:21:30 +03:00
|
|
|
clear_style_json();
|
2021-09-02 14:05:32 +03:00
|
|
|
}
|
2021-08-17 19:00:27 +03:00
|
|
|
}
|
|
|
|
|
2020-02-23 14:07:13 +03:00
|
|
|
InspectorWidget::InspectorWidget()
|
2020-01-02 16:55:19 +03:00
|
|
|
{
|
2021-09-13 18:17:43 +03:00
|
|
|
set_fill_with_background_color(true);
|
|
|
|
|
|
|
|
auto& layout = set_layout<GUI::VerticalBoxLayout>();
|
|
|
|
layout.set_margins({ 4, 4, 4, 4 });
|
2020-03-04 21:07:55 +03:00
|
|
|
auto& splitter = add<GUI::VerticalSplitter>();
|
2020-06-12 23:30:11 +03:00
|
|
|
|
|
|
|
auto& top_tab_widget = splitter.add<GUI::TabWidget>();
|
|
|
|
|
2021-09-13 18:17:43 +03:00
|
|
|
auto& dom_tree_container = top_tab_widget.add_tab<GUI::Widget>("DOM");
|
|
|
|
dom_tree_container.set_layout<GUI::VerticalBoxLayout>();
|
|
|
|
dom_tree_container.layout()->set_margins({ 4, 4, 4, 4 });
|
|
|
|
m_dom_tree_view = dom_tree_container.add<GUI::TreeView>();
|
2021-05-25 23:48:43 +03:00
|
|
|
m_dom_tree_view->on_selection_change = [this] {
|
|
|
|
const auto& index = m_dom_tree_view->selection().first();
|
2021-06-08 00:21:16 +03:00
|
|
|
set_inspected_node(index);
|
2020-06-12 23:30:11 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
auto& bottom_tab_widget = splitter.add<GUI::TabWidget>();
|
2020-02-23 14:23:48 +03:00
|
|
|
|
2021-09-13 18:17:43 +03:00
|
|
|
auto& style_table_container = bottom_tab_widget.add_tab<GUI::Widget>("Styles");
|
|
|
|
style_table_container.set_layout<GUI::VerticalBoxLayout>();
|
|
|
|
style_table_container.layout()->set_margins({ 4, 4, 4, 4 });
|
|
|
|
m_style_table_view = style_table_container.add<GUI::TableView>();
|
|
|
|
|
|
|
|
auto& computed_style_table_container = bottom_tab_widget.add_tab<GUI::Widget>("Computed");
|
|
|
|
computed_style_table_container.set_layout<GUI::VerticalBoxLayout>();
|
|
|
|
computed_style_table_container.layout()->set_margins({ 4, 4, 4, 4 });
|
|
|
|
m_computed_style_table_view = computed_style_table_container.add<GUI::TableView>();
|
|
|
|
|
2021-12-08 14:51:26 +03:00
|
|
|
auto& custom_properties_table_container = bottom_tab_widget.add_tab<GUI::Widget>("Variables");
|
|
|
|
custom_properties_table_container.set_layout<GUI::VerticalBoxLayout>();
|
|
|
|
custom_properties_table_container.layout()->set_margins({ 4, 4, 4, 4 });
|
|
|
|
m_custom_properties_table_view = custom_properties_table_container.add<GUI::TableView>();
|
|
|
|
|
2021-09-13 18:17:43 +03:00
|
|
|
m_dom_tree_view->set_focus(true);
|
2020-01-02 16:55:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
InspectorWidget::~InspectorWidget()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-08-27 22:21:30 +03:00
|
|
|
void InspectorWidget::select_default_node()
|
|
|
|
{
|
|
|
|
clear_style_json();
|
|
|
|
|
|
|
|
// FIXME: Select the <body> element, or else the root node.
|
|
|
|
m_dom_tree_view->collapse_tree();
|
|
|
|
m_dom_tree_view->set_cursor({}, GUI::AbstractView::SelectionUpdate::ClearIfNotSelected);
|
|
|
|
}
|
|
|
|
|
2021-06-08 00:21:16 +03:00
|
|
|
void InspectorWidget::set_dom_json(String json)
|
|
|
|
{
|
|
|
|
if (m_dom_json.has_value() && m_dom_json.value() == json)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_dom_json = json;
|
2021-11-02 21:32:26 +03:00
|
|
|
m_dom_tree_view->set_model(Web::DOMTreeModel::create(m_dom_json->view(), *m_dom_tree_view));
|
2021-06-08 00:21:16 +03:00
|
|
|
|
2021-08-27 19:40:25 +03:00
|
|
|
if (m_pending_inspect_node_id.has_value()) {
|
|
|
|
i32 node_id = m_pending_inspect_node_id.value();
|
|
|
|
m_pending_inspect_node_id.clear();
|
|
|
|
set_inspected_node(node_id);
|
2021-08-27 22:21:30 +03:00
|
|
|
} else {
|
|
|
|
select_default_node();
|
2021-08-27 19:40:25 +03:00
|
|
|
}
|
2021-06-08 00:21:16 +03:00
|
|
|
}
|
|
|
|
|
2021-09-01 14:50:47 +03:00
|
|
|
void InspectorWidget::clear_dom_json()
|
|
|
|
{
|
|
|
|
m_dom_json.clear();
|
|
|
|
m_dom_tree_view->set_model(nullptr);
|
|
|
|
clear_style_json();
|
|
|
|
}
|
|
|
|
|
2021-12-08 14:51:26 +03:00
|
|
|
void InspectorWidget::set_dom_node_properties_json(i32 node_id, String specified_values_json, String computed_values_json, String custom_properties_json)
|
2021-09-02 14:05:32 +03:00
|
|
|
{
|
|
|
|
if (node_id != m_inspected_node_id) {
|
|
|
|
dbgln("Got data for the wrong node id! Wanted {}, got {}", m_inspected_node_id, node_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-08 14:51:26 +03:00
|
|
|
load_style_json(specified_values_json, computed_values_json, custom_properties_json);
|
2021-08-27 19:40:25 +03:00
|
|
|
}
|
|
|
|
|
2021-12-08 14:51:26 +03:00
|
|
|
void InspectorWidget::load_style_json(String specified_values_json, String computed_values_json, String custom_properties_json)
|
2021-08-27 19:40:25 +03:00
|
|
|
{
|
2021-09-02 14:05:32 +03:00
|
|
|
m_inspected_node_specified_values_json = specified_values_json;
|
|
|
|
m_style_table_view->set_model(Web::StylePropertiesModel::create(m_inspected_node_specified_values_json.value().view()));
|
2021-12-08 14:51:26 +03:00
|
|
|
|
|
|
|
m_inspected_node_computed_values_json = computed_values_json;
|
2021-09-02 14:05:32 +03:00
|
|
|
m_computed_style_table_view->set_model(Web::StylePropertiesModel::create(m_inspected_node_computed_values_json.value().view()));
|
2021-12-08 14:51:26 +03:00
|
|
|
|
|
|
|
m_inspected_node_custom_properties_json = custom_properties_json;
|
|
|
|
m_custom_properties_table_view->set_model(Web::StylePropertiesModel::create(m_inspected_node_custom_properties_json.value().view()));
|
2021-09-02 14:05:32 +03:00
|
|
|
}
|
|
|
|
|
2021-08-27 22:21:30 +03:00
|
|
|
void InspectorWidget::clear_style_json()
|
|
|
|
{
|
|
|
|
m_inspected_node_specified_values_json.clear();
|
|
|
|
m_style_table_view->set_model(nullptr);
|
2021-12-08 14:51:26 +03:00
|
|
|
|
|
|
|
m_inspected_node_computed_values_json.clear();
|
2021-08-27 22:21:30 +03:00
|
|
|
m_computed_style_table_view->set_model(nullptr);
|
2021-12-08 14:51:26 +03:00
|
|
|
|
|
|
|
m_inspected_node_custom_properties_json.clear();
|
|
|
|
m_custom_properties_table_view->set_model(nullptr);
|
2021-08-27 22:21:30 +03:00
|
|
|
}
|
|
|
|
|
2020-05-08 22:38:30 +03:00
|
|
|
}
|