Ladybird/Qt: Port the Inspector to the WebView property tables

This commit is contained in:
Timothy Flynn 2023-11-27 16:10:03 -05:00 committed by Andreas Kling
parent 0037fdaf11
commit b5d5e48ffc
Notes: sideshowbarker 2024-07-17 06:39:26 +09:00
3 changed files with 1 additions and 189 deletions

View File

@ -5,13 +5,8 @@
*/
#include "InspectorWidget.h"
#include <AK/JsonObject.h>
#include <LibWebView/InspectorClient.h>
#include <QCloseEvent>
#include <QHeaderView>
#include <QSplitter>
#include <QTabWidget>
#include <QTableView>
#include <QVBoxLayout>
namespace Ladybird {
@ -27,43 +22,8 @@ InspectorWidget::InspectorWidget(WebContentView& content_view)
m_inspector_client = make<WebView::InspectorClient>(content_view, *m_inspector_view);
m_inspector_client->on_dom_node_properties_received = [this](auto properties_or_error) {
if (properties_or_error.is_error()) {
clear_style_json();
} else {
auto properties = properties_or_error.release_value();
load_style_json(properties.computed_style_json, properties.resolved_style_json, properties.custom_properties_json);
}
};
setLayout(new QVBoxLayout);
auto* splitter = new QSplitter(this);
splitter->setOrientation(Qt::Vertical);
layout()->addWidget(splitter);
splitter->addWidget(m_inspector_view.ptr());
splitter->setStretchFactor(0, 2);
auto add_table_tab = [&](auto* tab_widget, auto name) {
auto* table_view = new QTableView;
table_view->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
table_view->verticalHeader()->setVisible(false);
table_view->horizontalHeader()->setVisible(false);
auto* container = new QWidget;
container->setLayout(new QVBoxLayout);
container->layout()->addWidget(table_view);
tab_widget->addTab(container, name);
return table_view;
};
auto* node_tabs = new QTabWidget;
m_computed_style_table = add_table_tab(node_tabs, "Computed");
m_resolved_style_table = add_table_tab(node_tabs, "Resolved");
m_custom_properties_table = add_table_tab(node_tabs, "Variables");
splitter->addWidget(node_tabs);
layout()->addWidget(m_inspector_view.ptr());
setWindowTitle("Inspector");
resize(875, 825);
@ -79,7 +39,6 @@ void InspectorWidget::inspect()
void InspectorWidget::reset()
{
m_inspector_client->reset();
clear_style_json();
}
void InspectorWidget::select_hovered_node()
@ -92,30 +51,6 @@ void InspectorWidget::select_default_node()
m_inspector_client->select_default_node();
}
void InspectorWidget::load_style_json(StringView computed_style_json, StringView resolved_style_json, StringView custom_properties_json)
{
m_computed_style_model = PropertyTableModel::create(PropertyTableModel::Type::StyleProperties, computed_style_json).release_value_but_fixme_should_propagate_errors();
m_computed_style_table->setModel(m_computed_style_model);
m_resolved_style_model = PropertyTableModel::create(PropertyTableModel::Type::StyleProperties, resolved_style_json).release_value_but_fixme_should_propagate_errors();
m_resolved_style_table->setModel(m_resolved_style_model);
m_custom_properties_model = PropertyTableModel::create(PropertyTableModel::Type::StyleProperties, custom_properties_json).release_value_but_fixme_should_propagate_errors();
m_custom_properties_table->setModel(m_custom_properties_model);
}
void InspectorWidget::clear_style_json()
{
m_computed_style_table->setModel(nullptr);
m_computed_style_model = nullptr;
m_resolved_style_table->setModel(nullptr);
m_resolved_style_model = nullptr;
m_custom_properties_table->setModel(nullptr);
m_custom_properties_model = nullptr;
}
void InspectorWidget::closeEvent(QCloseEvent* event)
{
event->accept();

View File

@ -6,14 +6,10 @@
#pragma once
#include "ModelAdapter.h"
#include "WebContentView.h"
#include <AK/StringView.h>
#include <LibWebView/Forward.h>
#include <QWidget>
class QTableView;
namespace Ladybird {
class WebContentView;
@ -32,21 +28,10 @@ public:
void select_default_node();
private:
void load_style_json(StringView computed_style_json, StringView resolved_style_json, StringView custom_properties_json);
void clear_style_json();
void closeEvent(QCloseEvent*) override;
OwnPtr<WebContentView> m_inspector_view;
OwnPtr<WebView::InspectorClient> m_inspector_client;
OwnPtr<PropertyTableModel> m_computed_style_model;
OwnPtr<PropertyTableModel> m_resolved_style_model;
OwnPtr<PropertyTableModel> m_custom_properties_model;
QTableView* m_computed_style_table { nullptr };
QTableView* m_resolved_style_table { nullptr };
QTableView* m_custom_properties_table { nullptr };
};
}

View File

@ -1,108 +0,0 @@
/*
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include "StringUtils.h"
#include <AK/JsonValue.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <LibWeb/CSS/Selector.h>
#include <LibWebView/ModelIndex.h>
#include <LibWebView/PropertyTableModel.h>
#include <QAbstractItemModel>
namespace Ladybird {
template<typename ModelType>
class ModelAdapter : public QAbstractItemModel {
public:
using Type = typename ModelType::Type;
static ErrorOr<NonnullOwnPtr<ModelAdapter>> create(Type type, StringView model, QObject* parent = nullptr)
{
auto json_model = TRY(JsonValue::from_string(model));
if (!json_model.is_object())
return Error::from_string_literal("Expected model to be a JSON object");
return adopt_own(*new ModelAdapter(type, move(json_model), parent));
}
virtual int rowCount(QModelIndex const& parent) const override
{
return m_model.row_count(to_web_view_model_index(parent));
}
virtual int columnCount(QModelIndex const& parent) const override
{
return m_model.column_count(to_web_view_model_index(parent));
}
virtual QModelIndex index(int row, int column, QModelIndex const& parent) const override
{
auto index = m_model.index(row, column, to_web_view_model_index(parent));
return to_qt_model_index(index);
}
virtual QModelIndex parent(QModelIndex const& index) const override
{
if constexpr (requires { m_model.parent(declval<WebView::ModelIndex const&>()); }) {
auto parent = m_model.parent(to_web_view_model_index(index));
return to_qt_model_index(parent);
} else {
return {};
}
}
virtual QVariant data(QModelIndex const& index, int role) const override
{
if (role == Qt::DisplayRole) {
auto text = m_model.text_for_display(to_web_view_model_index(index));
return qstring_from_ak_string(text);
}
return {};
}
QModelIndex index_for_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> const& pseudo_element) const
{
if constexpr (requires { m_model.index_for_node(node_id, pseudo_element); }) {
auto parent = m_model.index_for_node(node_id, pseudo_element);
return to_qt_model_index(parent);
} else {
return {};
}
}
private:
ModelAdapter(Type type, JsonValue model, QObject* parent)
: QAbstractItemModel(parent)
, m_model(type, move(model))
{
}
ALWAYS_INLINE QModelIndex to_qt_model_index(WebView::ModelIndex const& index) const
{
if (!index.is_valid())
return {};
return createIndex(index.row, index.column, index.internal_data);
}
ALWAYS_INLINE WebView::ModelIndex to_web_view_model_index(QModelIndex const& index) const
{
if (!index.isValid())
return {};
return { index.row(), index.column(), index.constInternalPointer() };
}
ModelType m_model;
};
using PropertyTableModel = ModelAdapter<WebView::PropertyTableModel>;
}