From 0e61d84749007ed73a980489b59befc22bb76bf8 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 13 Oct 2019 16:31:31 +0200 Subject: [PATCH] LibHTML: Run second layout pass if first layout adds/removes scrollbars If you do a layout and it turns out that the page contents don't fit in the viewport vertically, we add a vertical scrollbar. Since the scrollbar takes up some horizontal space, this reduces the amount of space available to the page. So we have to do a second layout pass. :^) Fixes #650. --- Libraries/LibHTML/HtmlView.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Libraries/LibHTML/HtmlView.cpp b/Libraries/LibHTML/HtmlView.cpp index 98197b03b4..7dc89defa8 100644 --- a/Libraries/LibHTML/HtmlView.cpp +++ b/Libraries/LibHTML/HtmlView.cpp @@ -71,10 +71,21 @@ void HtmlView::layout_and_sync_size() if (!document()) return; + bool had_vertical_scrollbar = vertical_scrollbar().is_visible(); + bool had_horizontal_scrollbar = horizontal_scrollbar().is_visible(); + main_frame().set_size(available_size()); document()->layout(); set_content_size(layout_root()->rect().size()); + // NOTE: If layout caused us to gain or lose scrollbars, we have to lay out again + // since the scrollbars now take up some of the available space. + if (had_vertical_scrollbar != vertical_scrollbar().is_visible() || had_horizontal_scrollbar != horizontal_scrollbar().is_visible()) { + main_frame().set_size(available_size()); + document()->layout(); + set_content_size(layout_root()->rect().size()); + } + #ifdef HTML_DEBUG dbgprintf("\033[33;1mLayout tree after layout:\033[0m\n"); ::dump_tree(*layout_root());