Quellcode durchsuchen

LibWeb+Browser+Ladybird: Add menu action to dump paint tree

Andreas Kling vor 2 Jahren
Ursprung
Commit
72d817d4ea

+ 6 - 0
Ladybird/BrowserWindow.cpp

@@ -176,6 +176,12 @@ BrowserWindow::BrowserWindow(Browser::CookieJar& cookie_jar, StringView webdrive
         debug_request("dump-layout-tree");
     });
 
+    auto* dump_paint_tree_action = new QAction("Dump Paint Tree", this);
+    debug_menu->addAction(dump_paint_tree_action);
+    QObject::connect(dump_paint_tree_action, &QAction::triggered, this, [this] {
+        debug_request("dump-paint-tree");
+    });
+
     auto* dump_stacking_context_tree_action = new QAction("Dump Stacking Context Tree", this);
     dump_stacking_context_tree_action->setIcon(QIcon(QString("%1/res/icons/16x16/layers.png").arg(s_serenity_resource_root.characters())));
     debug_menu->addAction(dump_stacking_context_tree_action);

+ 5 - 0
Userland/Applications/Browser/BrowserWindow.cpp

@@ -364,6 +364,11 @@ void BrowserWindow::build_menus()
             active_tab().view().debug_request("dump-layout-tree");
         },
         this));
+    debug_menu.add_action(GUI::Action::create(
+        "Dump &Paint Tree", g_icon_bag.layout, [this](auto&) {
+            active_tab().view().debug_request("dump-paint-tree");
+        },
+        this));
     debug_menu.add_action(GUI::Action::create(
         "Dump S&tacking Context Tree", g_icon_bag.layers, [this](auto&) {
             active_tab().view().debug_request("dump-stacking-context-tree");

+ 48 - 0
Userland/Libraries/LibWeb/Dump.cpp

@@ -29,6 +29,7 @@
 #include <LibWeb/Layout/SVGBox.h>
 #include <LibWeb/Layout/TextNode.h>
 #include <LibWeb/Painting/PaintableBox.h>
+#include <LibWeb/Painting/TextPaintable.h>
 #include <stdio.h>
 
 namespace Web {
@@ -678,4 +679,51 @@ ErrorOr<void> dump_sheet(StringBuilder& builder, CSS::StyleSheet const& sheet)
     return {};
 }
 
+void dump_tree(Painting::Paintable const& paintable)
+{
+    StringBuilder builder;
+    dump_tree(builder, paintable, true);
+    dbgln("{}", builder.string_view());
+}
+
+void dump_tree(StringBuilder& builder, Painting::Paintable const& paintable, bool colorize, int indent)
+{
+    for (int i = 0; i < indent; ++i)
+        builder.append("  "sv);
+
+    StringView paintable_with_lines_color_on = ""sv;
+    StringView paintable_box_color_on = ""sv;
+    StringView text_paintable_color_on = ""sv;
+    StringView paintable_color_on = ""sv;
+    StringView color_off = ""sv;
+
+    if (colorize) {
+        paintable_with_lines_color_on = "\033[34m"sv;
+        paintable_box_color_on = "\033[33m"sv;
+        text_paintable_color_on = "\033[35m"sv;
+        paintable_color_on = "\033[32m"sv;
+        color_off = "\033[0m"sv;
+    }
+
+    if (is<Painting::PaintableWithLines>(paintable))
+        builder.append(paintable_with_lines_color_on);
+    else if (is<Painting::PaintableBox>(paintable))
+        builder.append(paintable_box_color_on);
+    else if (is<Painting::TextPaintable>(paintable))
+        builder.append(text_paintable_color_on);
+    else
+        builder.append(paintable_color_on);
+
+    builder.appendff("{}{} ({})", paintable.class_name(), color_off, paintable.layout_node().debug_description());
+
+    if (paintable.layout_node().is_box()) {
+        auto const& paint_box = static_cast<Painting::PaintableBox const&>(paintable);
+        builder.appendff(" {}", paint_box.absolute_border_box_rect());
+    }
+    builder.append("\n"sv);
+    for (auto const* child = paintable.first_child(); child; child = child->next_sibling()) {
+        dump_tree(builder, *child, colorize, indent + 1);
+    }
+}
+
 }

+ 2 - 0
Userland/Libraries/LibWeb/Dump.h

@@ -16,6 +16,8 @@ void dump_tree(StringBuilder&, DOM::Node const&);
 void dump_tree(DOM::Node const&);
 void dump_tree(StringBuilder&, Layout::Node const&, bool show_box_model = false, bool show_specified_style = false, bool colorize = false);
 void dump_tree(Layout::Node const&, bool show_box_model = true, bool show_specified_style = false);
+void dump_tree(StringBuilder&, Painting::Paintable const&, bool colorize = false, int indent = 0);
+void dump_tree(Painting::Paintable const&);
 ErrorOr<void> dump_sheet(StringBuilder&, CSS::StyleSheet const&);
 ErrorOr<void> dump_sheet(CSS::StyleSheet const&);
 ErrorOr<void> dump_rule(StringBuilder&, CSS::CSSRule const&, int indent_levels = 0);

+ 7 - 0
Userland/Services/WebContent/ConnectionFromClient.cpp

@@ -341,6 +341,13 @@ void ConnectionFromClient::debug_request(DeprecatedString const& request, Deprec
         }
     }
 
+    if (request == "dump-paint-tree") {
+        if (auto* doc = page().top_level_browsing_context().active_document()) {
+            if (auto* paintable = doc->paintable())
+                Web::dump_tree(*paintable);
+        }
+    }
+
     if (request == "dump-stacking-context-tree") {
         if (auto* doc = page().top_level_browsing_context().active_document()) {
             if (auto* viewport = doc->layout_node()) {