From 0f429080736e8a863a688d34c713cfe1a468dd53 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Sun, 5 Jan 2020 00:09:35 +0300 Subject: [PATCH] LibHTML: Respect the system theme LibHTML will now use the palette colors for the default document background and the text. As always, a page can override this default styling with CSS if it really wants a specific color or style. Fixes https://github.com/SerenityOS/serenity/issues/963 --- Libraries/LibHTML/DOM/Document.cpp | 10 ++++++---- Libraries/LibHTML/DOM/Document.h | 3 ++- Libraries/LibHTML/HtmlView.cpp | 4 ++-- Libraries/LibHTML/Layout/LayoutListItemMarker.cpp | 3 ++- Libraries/LibHTML/Layout/LayoutText.cpp | 4 ++-- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Libraries/LibHTML/DOM/Document.cpp b/Libraries/LibHTML/DOM/Document.cpp index fb631bd141..80acf9713a 100644 --- a/Libraries/LibHTML/DOM/Document.cpp +++ b/Libraries/LibHTML/DOM/Document.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -116,19 +117,20 @@ void Document::detach_from_frame(Badge, Frame&) m_frame = nullptr; } -Color Document::background_color() const +Color Document::background_color(const Palette& palette) const { + auto default_color = palette.base(); auto* body_element = body(); if (!body_element) - return Color::White; + return default_color; auto* body_layout_node = body_element->layout_node(); if (!body_layout_node) - return Color::White; + return default_color; auto background_color = body_layout_node->style().property(CSS::PropertyID::BackgroundColor); if (!background_color.has_value() || !background_color.value()->is_color()) - return Color::White; + return default_color; return background_color.value()->to_color(*this); } diff --git a/Libraries/LibHTML/DOM/Document.h b/Libraries/LibHTML/DOM/Document.h index 5b1487d0c1..022be5ab06 100644 --- a/Libraries/LibHTML/DOM/Document.h +++ b/Libraries/LibHTML/DOM/Document.h @@ -10,6 +10,7 @@ #include #include +class Palette; class CTimer; class Frame; class HTMLBodyElement; @@ -60,7 +61,7 @@ public: Frame* frame() { return m_frame.ptr(); } const Frame* frame() const { return m_frame.ptr(); } - Color background_color() const; + Color background_color(const Palette&) const; RefPtr background_image() const; Color link_color() const { return m_link_color; } diff --git a/Libraries/LibHTML/HtmlView.cpp b/Libraries/LibHTML/HtmlView.cpp index db6f094317..655a194916 100644 --- a/Libraries/LibHTML/HtmlView.cpp +++ b/Libraries/LibHTML/HtmlView.cpp @@ -38,7 +38,7 @@ HtmlView::HtmlView(GWidget* parent) set_frame_shadow(FrameShadow::Sunken); set_frame_thickness(2); set_should_hide_unnecessary_scrollbars(true); - set_background_color(Color::White); + set_background_role(ColorRole::Base); } HtmlView::~HtmlView() @@ -122,7 +122,7 @@ void HtmlView::paint_event(GPaintEvent& event) return; } - painter.fill_rect(event.rect(), document()->background_color()); + painter.fill_rect(event.rect(), document()->background_color(palette())); if (auto background_bitmap = document()->background_image()) { painter.draw_tiled_bitmap(event.rect(), *background_bitmap); diff --git a/Libraries/LibHTML/Layout/LayoutListItemMarker.cpp b/Libraries/LibHTML/Layout/LayoutListItemMarker.cpp index 98ffc61e07..e2cac2d92e 100644 --- a/Libraries/LibHTML/Layout/LayoutListItemMarker.cpp +++ b/Libraries/LibHTML/Layout/LayoutListItemMarker.cpp @@ -15,5 +15,6 @@ void LayoutListItemMarker::render(RenderingContext& context) Rect bullet_rect { 0, 0, 4, 4 }; bullet_rect.center_within(enclosing_int_rect(rect())); // FIXME: It would be nicer to not have to go via the parent here to get our inherited style. - context.painter().fill_rect(bullet_rect, parent()->style().color_or_fallback(CSS::PropertyID::Color, document(), Color::Black)); + auto color = parent()->style().color_or_fallback(CSS::PropertyID::Color, document(), context.palette().base_text()); + context.painter().fill_rect(bullet_rect, color); } diff --git a/Libraries/LibHTML/Layout/LayoutText.cpp b/Libraries/LibHTML/Layout/LayoutText.cpp index 980abfd0c1..7a93be94b2 100644 --- a/Libraries/LibHTML/Layout/LayoutText.cpp +++ b/Libraries/LibHTML/Layout/LayoutText.cpp @@ -46,7 +46,7 @@ void LayoutText::render_fragment(RenderingContext& context, const LineBoxFragmen if (background_color.has_value() && background_color.value()->is_color()) painter.fill_rect(enclosing_int_rect(fragment.rect()), background_color.value()->to_color(document())); - auto color = style().color_or_fallback(CSS::PropertyID::Color, document(), Color::Black); + auto color = style().color_or_fallback(CSS::PropertyID::Color, document(), context.palette().base_text()); auto text_decoration = style().string_or_fallback(CSS::PropertyID::TextDecoration, "none"); if (document().inspected_node() == &node()) @@ -146,7 +146,7 @@ void LayoutText::split_into_lines(LayoutBlock& container) if (style().string_or_fallback(CSS::PropertyID::WhiteSpace, "normal") == "pre") { split_preformatted_into_lines(container); return; - } + } // Collapse whitespace into single spaces auto utf8_view = Utf8View(node().data());