Skip to content

Commit 159409e

Browse files
committed
browser/gui: Handle CSS special backgrounds
If the html or body element has a non-transparent background set, that should be used as the canvas background and not just as the element background. This is currently done in hacky way outside of //render as doing correctly, not painting over things outside of the web page, without hacks in //render will require //gfx functionality not in place yet.
1 parent 30b6e58 commit 159409e

1 file changed

Lines changed: 36 additions & 1 deletion

File tree

browser/gui/app.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "css/rule.h"
88
#include "dom/dom.h"
9+
#include "gfx/color.h"
910
#include "gfx/opengl_canvas.h"
1011
#include "gfx/painter.h"
1112
#include "render/render.h"
@@ -18,13 +19,15 @@
1819
#include <imgui_stdlib.h>
1920
#include <spdlog/spdlog.h>
2021

22+
#include <algorithm>
2123
#include <cmath>
2224
#include <cstdlib>
2325
#include <functional>
2426
#include <optional>
2527
#include <sstream>
2628
#include <string_view>
2729
#include <utility>
30+
#include <variant>
2831

2932
using namespace std::literals;
3033

@@ -470,9 +473,41 @@ void App::run_layout_widget() const {
470473
void App::clear_render_surface() {
471474
if (render_debug_) {
472475
window_.clear();
473-
} else {
476+
return;
477+
}
478+
479+
if (!page_loaded_) {
474480
window_.clear(sf::Color(255, 255, 255));
481+
return;
475482
}
483+
484+
// https://www.w3.org/TR/css-backgrounds-3/#special-backgrounds
485+
// If html or body has a background set, use that as the canvas background.
486+
// TODO(robinlinden): This should be done in //render, but requires new
487+
// //gfx APIs that I want to think a bit about.
488+
if (auto html_bg = engine_.layout().get_property<css::PropertyId::BackgroundColor>();
489+
html_bg != gfx::Color::from_css_name("transparent")) {
490+
window_.clear(sf::Color(html_bg->as_rgba_u32()));
491+
return;
492+
}
493+
494+
auto body = std::ranges::find_if(engine_.layout().children, [](layout::LayoutBox const &c) {
495+
return c.node && std::holds_alternative<dom::Element>(c.node->node)
496+
&& std::get<dom::Element>(c.node->node).name == "body";
497+
});
498+
499+
if (body == end(engine_.layout().children)) {
500+
window_.clear(sf::Color(255, 255, 255));
501+
return;
502+
}
503+
504+
if (auto body_bg = body->get_property<css::PropertyId::BackgroundColor>();
505+
body_bg != gfx::Color::from_css_name("transparent")) {
506+
window_.clear(sf::Color(body_bg->as_rgba_u32()));
507+
return;
508+
}
509+
510+
window_.clear(sf::Color(255, 255, 255));
476511
}
477512

478513
void App::render_layout() {

0 commit comments

Comments
 (0)