1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-23 19:15:07 +00:00

Browser: Replace inspector's dom_node_id with a Selection struct

This commit is contained in:
Sam Atkins 2022-03-04 16:16:57 +00:00 committed by Andreas Kling
parent 6de2b62906
commit 2c970b9516
3 changed files with 54 additions and 41 deletions

View file

@ -1,6 +1,6 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org> * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers. * Copyright (c) 2022, the SerenityOS developers.
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
@ -21,33 +21,35 @@
namespace Browser { namespace Browser {
void InspectorWidget::set_inspected_node(i32 node_id) void InspectorWidget::set_selection(Selection selection)
{ {
if (!m_dom_json.has_value()) { if (!m_dom_json.has_value()) {
// DOM Tree hasn't been loaded yet, so make a note to inspect it later. // DOM Tree hasn't been loaded yet, so make a note to inspect it later.
m_pending_inspect_node_id = node_id; m_pending_selection = move(selection);
return; return;
} }
auto* model = verify_cast<Web::DOMTreeModel>(m_dom_tree_view->model()); auto* model = verify_cast<Web::DOMTreeModel>(m_dom_tree_view->model());
auto index = model->index_for_node(node_id); auto index = model->index_for_node(selection.dom_node_id, selection.pseudo_element);
if (!index.is_valid()) { if (!index.is_valid()) {
dbgln("InspectorWidget told to inspect non-existent node, id={}", node_id); dbgln("InspectorWidget told to inspect non-existent node: {}", selection.to_string());
return; return;
} }
m_dom_tree_view->expand_all_parents_of(index); m_dom_tree_view->expand_all_parents_of(index);
m_dom_tree_view->set_cursor(index, GUI::AbstractView::SelectionUpdate::Set); m_dom_tree_view->set_cursor(index, GUI::AbstractView::SelectionUpdate::Set);
set_inspected_node(index); set_selection(index);
} }
void InspectorWidget::set_inspected_node(GUI::ModelIndex const index) void InspectorWidget::set_selection(GUI::ModelIndex const index)
{ {
auto* json = static_cast<JsonObject const*>(index.internal_data()); auto* json = static_cast<JsonObject const*>(index.internal_data());
i32 inspected_node = json ? json->get("id").to_i32() : 0; VERIFY(json);
if (inspected_node == m_inspected_node_id)
Selection selection { json->get("id").to_i32() };
if (selection == m_selection)
return; return;
m_inspected_node_id = inspected_node; m_selection = move(selection);
auto maybe_inspected_node_properties = m_web_view->inspect_dom_node(m_inspected_node_id); auto maybe_inspected_node_properties = m_web_view->inspect_dom_node(m_inspected_node_id);
if (maybe_inspected_node_properties.has_value()) { if (maybe_inspected_node_properties.has_value()) {
@ -75,7 +77,7 @@ InspectorWidget::InspectorWidget()
m_dom_tree_view = dom_tree_container.add<GUI::TreeView>(); m_dom_tree_view = dom_tree_container.add<GUI::TreeView>();
m_dom_tree_view->on_selection_change = [this] { m_dom_tree_view->on_selection_change = [this] {
const auto& index = m_dom_tree_view->selection().first(); const auto& index = m_dom_tree_view->selection().first();
set_inspected_node(index); set_selection(index);
}; };
auto& bottom_tab_widget = splitter.add<GUI::TabWidget>(); auto& bottom_tab_widget = splitter.add<GUI::TabWidget>();
@ -121,10 +123,8 @@ void InspectorWidget::set_dom_json(String json)
m_dom_json = json; m_dom_json = json;
m_dom_tree_view->set_model(Web::DOMTreeModel::create(m_dom_json->view(), *m_dom_tree_view)); m_dom_tree_view->set_model(Web::DOMTreeModel::create(m_dom_json->view(), *m_dom_tree_view));
if (m_pending_inspect_node_id.has_value()) { if (m_pending_selection.has_value()) {
i32 node_id = m_pending_inspect_node_id.value(); set_selection(m_pending_selection.release_value());
m_pending_inspect_node_id.clear();
set_inspected_node(node_id);
} else { } else {
select_default_node(); select_default_node();
} }
@ -137,10 +137,10 @@ void InspectorWidget::clear_dom_json()
clear_style_json(); clear_style_json();
} }
void InspectorWidget::set_dom_node_properties_json(i32 node_id, String specified_values_json, String computed_values_json, String custom_properties_json, String node_box_sizing_json) void InspectorWidget::set_dom_node_properties_json(Selection selection, String specified_values_json, String computed_values_json, String custom_properties_json, String node_box_sizing_json)
{ {
if (node_id != m_inspected_node_id) { if (selection != m_selection) {
dbgln("Got data for the wrong node id! Wanted {}, got {}", m_inspected_node_id, node_id); dbgln("Got data for the wrong node id! Wanted ({}), got ({})", m_selection.to_string(), selection.to_string());
return; return;
} }
@ -150,14 +150,14 @@ void InspectorWidget::set_dom_node_properties_json(i32 node_id, String specified
void InspectorWidget::load_style_json(String specified_values_json, String computed_values_json, String custom_properties_json) void InspectorWidget::load_style_json(String specified_values_json, String computed_values_json, String custom_properties_json)
{ {
m_inspected_node_specified_values_json = specified_values_json; m_selection_specified_values_json = specified_values_json;
m_style_table_view->set_model(Web::StylePropertiesModel::create(m_inspected_node_specified_values_json.value().view())); m_style_table_view->set_model(Web::StylePropertiesModel::create(m_selection_specified_values_json.value().view()));
m_inspected_node_computed_values_json = computed_values_json; m_selection_computed_values_json = computed_values_json;
m_computed_style_table_view->set_model(Web::StylePropertiesModel::create(m_inspected_node_computed_values_json.value().view())); m_computed_style_table_view->set_model(Web::StylePropertiesModel::create(m_selection_computed_values_json.value().view()));
m_inspected_node_custom_properties_json = custom_properties_json; m_selection_custom_properties_json = custom_properties_json;
m_custom_properties_table_view->set_model(Web::StylePropertiesModel::create(m_inspected_node_custom_properties_json.value().view())); m_custom_properties_table_view->set_model(Web::StylePropertiesModel::create(m_selection_custom_properties_json.value().view()));
} }
void InspectorWidget::update_node_box_model(Optional<String> node_box_sizing_json) void InspectorWidget::update_node_box_model(Optional<String> node_box_sizing_json)
@ -192,13 +192,13 @@ void InspectorWidget::update_node_box_model(Optional<String> node_box_sizing_jso
void InspectorWidget::clear_style_json() void InspectorWidget::clear_style_json()
{ {
m_inspected_node_specified_values_json.clear(); m_selection_specified_values_json.clear();
m_style_table_view->set_model(nullptr); m_style_table_view->set_model(nullptr);
m_inspected_node_computed_values_json.clear(); m_selection_computed_values_json.clear();
m_computed_style_table_view->set_model(nullptr); m_computed_style_table_view->set_model(nullptr);
m_inspected_node_custom_properties_json.clear(); m_selection_custom_properties_json.clear();
m_custom_properties_table_view->set_model(nullptr); m_custom_properties_table_view->set_model(nullptr);
m_element_size_view->set_box_model({}); m_element_size_view->set_box_model({});

View file

@ -1,6 +1,6 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org> * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers. * Copyright (c) 2022, the SerenityOS developers.
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
@ -18,20 +18,34 @@ namespace Browser {
class InspectorWidget final : public GUI::Widget { class InspectorWidget final : public GUI::Widget {
C_OBJECT(InspectorWidget) C_OBJECT(InspectorWidget)
public: public:
struct Selection {
i32 dom_node_id { 0 };
bool operator==(Selection const& other) const
{
return dom_node_id == other.dom_node_id;
}
String to_string() const
{
return String::formatted("id: {}", dom_node_id);
}
};
virtual ~InspectorWidget() = default; virtual ~InspectorWidget() = default;
void set_web_view(NonnullRefPtr<Web::OutOfProcessWebView> web_view) { m_web_view = web_view; } void set_web_view(NonnullRefPtr<Web::OutOfProcessWebView> web_view) { m_web_view = web_view; }
void set_dom_json(String); void set_dom_json(String);
void clear_dom_json(); void clear_dom_json();
void set_dom_node_properties_json(i32 node_id, String specified_values_json, String computed_values_json, String custom_properties_json, String node_box_sizing_json); void set_dom_node_properties_json(Selection, String specified_values_json, String computed_values_json, String custom_properties_json, String node_box_sizing_json);
void set_inspected_node(i32 node_id); void set_selection(Selection);
void select_default_node(); void select_default_node();
private: private:
InspectorWidget(); InspectorWidget();
void set_inspected_node(GUI::ModelIndex); void set_selection(GUI::ModelIndex);
void load_style_json(String specified_values_json, String computed_values_json, String custom_properties_json); void load_style_json(String specified_values_json, String computed_values_json, String custom_properties_json);
void update_node_box_model(Optional<String> node_box_sizing_json); void update_node_box_model(Optional<String> node_box_sizing_json);
void clear_style_json(); void clear_style_json();
@ -46,13 +60,12 @@ private:
Web::Layout::BoxModelMetrics m_node_box_sizing; Web::Layout::BoxModelMetrics m_node_box_sizing;
// Multi-process mode
Optional<i32> m_pending_inspect_node_id;
i32 m_inspected_node_id;
Optional<String> m_dom_json; Optional<String> m_dom_json;
Optional<String> m_inspected_node_specified_values_json; Optional<Selection> m_pending_selection;
Optional<String> m_inspected_node_computed_values_json; Selection m_selection;
Optional<String> m_inspected_node_custom_properties_json; Optional<String> m_selection_specified_values_json;
Optional<String> m_selection_computed_values_json;
Optional<String> m_selection_custom_properties_json;
}; };
} }

View file

@ -330,7 +330,7 @@ Tab::Tab(BrowserWindow& window)
}; };
hooks().on_get_dom_node_properties = [this](auto node_id, auto& specified, auto& computed, auto& custom_properties, auto& node_box_sizing) { hooks().on_get_dom_node_properties = [this](auto node_id, auto& specified, auto& computed, auto& custom_properties, auto& node_box_sizing) {
m_dom_inspector_widget->set_dom_node_properties_json(node_id, specified, computed, custom_properties, node_box_sizing); m_dom_inspector_widget->set_dom_node_properties_json({ node_id }, specified, computed, custom_properties, node_box_sizing);
}; };
hooks().on_js_console_new_message = [this](auto message_index) { hooks().on_js_console_new_message = [this](auto message_index) {
@ -537,9 +537,9 @@ void Tab::show_inspector_window(Browser::Tab::InspectorTarget inspector_target)
} }
if (inspector_target == InspectorTarget::HoveredElement) { if (inspector_target == InspectorTarget::HoveredElement) {
Optional<i32> hovered_node = m_web_content_view->get_hovered_node_id(); // FIXME: Handle pseudo-elements
VERIFY(hovered_node.has_value()); auto hovered_node = m_web_content_view->get_hovered_node_id();
m_dom_inspector_widget->set_inspected_node(hovered_node.value()); m_dom_inspector_widget->set_selection({ hovered_node });
} else { } else {
VERIFY(inspector_target == InspectorTarget::Document); VERIFY(inspector_target == InspectorTarget::Document);
m_dom_inspector_widget->select_default_node(); m_dom_inspector_widget->select_default_node();