srctree

Robin Linden parent e776efd6 8d132b42
engine: Make dark mode an option

inlinesplit
browser/gui/app.cpp added: 22, removed: 6, total 16
@@ -13,6 +13,7 @@
#include "gfx/opengl_canvas.h"
#include "gfx/sfml_canvas.h"
#include "layout/layout_box.h"
#include "os/system_info.h"
#include "protocol/handler_factory.h"
#include "protocol/response.h"
#include "render/render.h"
@@ -776,7 +777,10 @@ void App::switch_canvas() {
}
 
engine::Options App::make_options() const {
return {.layout_width = static_cast<int>(window_.getSize().x / scale_)};
return {
.layout_width = static_cast<int>(window_.getSize().x / scale_),
.dark_mode = os::is_dark_mode(),
};
}
 
} // namespace browser::gui
 
engine/engine.cpp added: 22, removed: 6, total 16
@@ -6,6 +6,7 @@
 
#include "archive/zlib.h"
#include "css/default.h"
#include "css/media_query.h"
#include "css/parser.h"
#include "css/style_sheet.h"
#include "dom/dom.h"
@@ -28,6 +29,16 @@
using namespace std::literals;
 
namespace engine {
namespace {
 
css::MediaQuery::Context to_media_context(Options opts) {
return {
.window_width = opts.layout_width,
.color_scheme = opts.dark_mode ? css::ColorScheme::Dark : css::ColorScheme::Light,
};
}
 
} // namespace
 
tl::expected<std::unique_ptr<PageState>, NavigationError> Engine::navigate(uri::Uri uri, Options opts) {
auto result = load(std::move(uri));
@@ -126,7 +137,7 @@ tl::expected<std::unique_ptr<PageState>, NavigationError> Engine::navigate(uri::
 
spdlog::info("Styling dom w/ {} rules", state->stylesheet.rules.size());
state->layout_width = opts.layout_width;
state->styled = style::style_tree(state->dom.html_node, state->stylesheet, {.window_width = state->layout_width});
state->styled = style::style_tree(state->dom.html_node, state->stylesheet, to_media_context(opts));
state->layout = layout::create_layout(*state->styled, state->layout_width, *type_);
 
return state;
@@ -134,7 +145,7 @@ tl::expected<std::unique_ptr<PageState>, NavigationError> Engine::navigate(uri::
 
void Engine::relayout(PageState &state, Options opts) {
state.layout_width = opts.layout_width;
state.styled = style::style_tree(state.dom.html_node, state.stylesheet, {.window_width = state.layout_width});
state.styled = style::style_tree(state.dom.html_node, state.stylesheet, to_media_context(opts));
state.layout = layout::create_layout(*state.styled, state.layout_width, *type_);
}
 
 
engine/engine.h added: 22, removed: 6, total 16
@@ -27,6 +27,7 @@ namespace engine {
struct Options {
// Default chosen by rolling 1d600.
int layout_width{600};
bool dark_mode{false};
};
 
struct PageState {