From 2530378f59ff8152a3da94c33569f2c246c9f761 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 12 Oct 2019 15:02:53 +0200 Subject: [PATCH] LibHTML+Browser: Add debug option to draw borders around line boxes This will be very useful when debugging line layout. --- Applications/Browser/main.cpp | 9 +++++++++ Libraries/LibHTML/HtmlView.cpp | 1 + Libraries/LibHTML/HtmlView.h | 4 ++++ Libraries/LibHTML/Layout/LayoutBlock.cpp | 2 ++ Libraries/LibHTML/RenderingContext.h | 4 ++++ 5 files changed, 20 insertions(+) diff --git a/Applications/Browser/main.cpp b/Applications/Browser/main.cpp index c7a57b9b9d..ec33614843 100644 --- a/Applications/Browser/main.cpp +++ b/Applications/Browser/main.cpp @@ -102,6 +102,15 @@ int main(int argc, char** argv) debug_menu->add_action(GAction::create("Dump Layout tree", [&](auto&) { dump_tree(*html_widget->document()->layout_node()); })); + debug_menu->add_separator(); + auto line_box_borders_action = GAction::create("Line box borders", [&](auto& action) { + action.set_checked(!action.is_checked()); + html_widget->set_should_show_line_box_borders(action.is_checked()); + html_widget->update(); + }); + line_box_borders_action->set_checkable(true); + line_box_borders_action->set_checked(false); + debug_menu->add_action(line_box_borders_action); menubar->add_menu(move(debug_menu)); auto help_menu = make("Help"); diff --git a/Libraries/LibHTML/HtmlView.cpp b/Libraries/LibHTML/HtmlView.cpp index f3f8388158..6ac1a9ff0b 100644 --- a/Libraries/LibHTML/HtmlView.cpp +++ b/Libraries/LibHTML/HtmlView.cpp @@ -111,6 +111,7 @@ void HtmlView::paint_event(GPaintEvent& event) painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value()); RenderingContext context { painter }; + context.set_should_show_line_box_borders(m_should_show_line_box_borders); m_layout_root->render(context); } diff --git a/Libraries/LibHTML/HtmlView.h b/Libraries/LibHTML/HtmlView.h index bccd78286a..3d147b8f7e 100644 --- a/Libraries/LibHTML/HtmlView.h +++ b/Libraries/LibHTML/HtmlView.h @@ -23,6 +23,8 @@ public: URL url() const; + void set_should_show_line_box_borders(bool value) { m_should_show_line_box_borders = value; } + Function on_link_click; Function on_title_change; Function on_load_start; @@ -41,4 +43,6 @@ private: RefPtr m_main_frame; RefPtr m_document; RefPtr m_layout_root; + + bool m_should_show_line_box_borders { false }; }; diff --git a/Libraries/LibHTML/Layout/LayoutBlock.cpp b/Libraries/LibHTML/Layout/LayoutBlock.cpp index 4373624c76..86f17ee514 100644 --- a/Libraries/LibHTML/Layout/LayoutBlock.cpp +++ b/Libraries/LibHTML/Layout/LayoutBlock.cpp @@ -204,6 +204,8 @@ void LayoutBlock::render(RenderingContext& context) if (children_are_inline()) { for (auto& line_box : m_line_boxes) { for (auto& fragment : line_box.fragments()) { + if (context.should_show_line_box_borders()) + context.painter().draw_rect(fragment.rect(), Color::Green); fragment.render(context); } } diff --git a/Libraries/LibHTML/RenderingContext.h b/Libraries/LibHTML/RenderingContext.h index 2e50ed82a5..bfba539968 100644 --- a/Libraries/LibHTML/RenderingContext.h +++ b/Libraries/LibHTML/RenderingContext.h @@ -11,6 +11,10 @@ public: GPainter& painter() const { return m_painter; } + bool should_show_line_box_borders() const { return m_should_show_line_box_borders; } + void set_should_show_line_box_borders(bool value) { m_should_show_line_box_borders = value; } + private: GPainter& m_painter; + bool m_should_show_line_box_borders { false }; };