2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/ConfigFile.h>
|
2020-02-06 11:04:00 +00:00
|
|
|
#include <LibGfx/SystemTheme.h>
|
2020-10-01 02:00:59 +00:00
|
|
|
#include <string.h>
|
2019-12-23 19:24:26 +00:00
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
namespace Gfx {
|
|
|
|
|
2019-12-23 19:24:26 +00:00
|
|
|
static SystemTheme dummy_theme;
|
|
|
|
static const SystemTheme* theme_page = &dummy_theme;
|
2021-01-16 16:20:53 +00:00
|
|
|
static Core::AnonymousBuffer theme_buffer;
|
2019-12-23 19:24:26 +00:00
|
|
|
|
2021-01-16 16:20:53 +00:00
|
|
|
Core::AnonymousBuffer& current_system_theme_buffer()
|
2019-12-23 19:24:26 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(theme_buffer.is_valid());
|
2021-01-16 16:20:53 +00:00
|
|
|
return theme_buffer;
|
2019-12-23 19:24:26 +00:00
|
|
|
}
|
|
|
|
|
2021-01-16 16:20:53 +00:00
|
|
|
void set_system_theme(Core::AnonymousBuffer buffer)
|
2019-12-23 19:24:26 +00:00
|
|
|
{
|
2021-01-16 16:20:53 +00:00
|
|
|
theme_buffer = move(buffer);
|
|
|
|
theme_page = theme_buffer.data<SystemTheme>();
|
2019-12-23 19:24:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 20:22:07 +00:00
|
|
|
Core::AnonymousBuffer load_system_theme(Core::ConfigFile const& file)
|
2019-12-23 19:24:26 +00:00
|
|
|
{
|
2021-01-16 16:20:53 +00:00
|
|
|
auto buffer = Core::AnonymousBuffer::create_with_size(sizeof(SystemTheme));
|
2019-12-23 19:24:26 +00:00
|
|
|
|
2021-01-16 16:20:53 +00:00
|
|
|
auto* data = buffer.data<SystemTheme>();
|
2019-12-23 19:24:26 +00:00
|
|
|
|
2020-01-05 23:47:55 +00:00
|
|
|
auto get_color = [&](auto& name) {
|
2021-08-25 20:22:07 +00:00
|
|
|
auto color_string = file.read_entry("Colors", name);
|
2019-12-23 19:24:26 +00:00
|
|
|
auto color = Color::from_string(color_string);
|
|
|
|
if (!color.has_value())
|
|
|
|
return Color(Color::Black);
|
|
|
|
return color.value();
|
|
|
|
};
|
|
|
|
|
2020-07-17 00:27:55 +00:00
|
|
|
auto get_metric = [&](auto& name, auto role) {
|
2021-08-25 20:22:07 +00:00
|
|
|
int metric = file.read_num_entry("Metrics", name, -1);
|
2020-07-17 00:27:55 +00:00
|
|
|
if (metric == -1) {
|
|
|
|
switch (role) {
|
|
|
|
case (int)MetricRole::TitleHeight:
|
|
|
|
return 19;
|
|
|
|
case (int)MetricRole::TitleButtonHeight:
|
|
|
|
return 15;
|
|
|
|
case (int)MetricRole::TitleButtonWidth:
|
|
|
|
return 15;
|
|
|
|
default:
|
2021-01-11 12:32:26 +00:00
|
|
|
dbgln("Metric {} has no fallback value!", name);
|
2020-07-17 00:27:55 +00:00
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return metric;
|
|
|
|
};
|
|
|
|
|
2021-02-10 05:12:32 +00:00
|
|
|
auto get_path = [&](auto& name, auto role, bool allow_empty) {
|
2021-08-25 20:22:07 +00:00
|
|
|
auto path = file.read_entry("Paths", name);
|
2020-07-29 20:09:04 +00:00
|
|
|
if (path.is_empty()) {
|
|
|
|
switch (role) {
|
|
|
|
case (int)PathRole::TitleButtonIcons:
|
|
|
|
return "/res/icons/16x16/";
|
|
|
|
default:
|
2021-02-10 05:12:32 +00:00
|
|
|
return allow_empty ? "" : "/res/";
|
2020-07-29 20:09:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return &path[0];
|
|
|
|
};
|
|
|
|
|
2020-08-21 17:51:17 +00:00
|
|
|
#undef __ENUMERATE_COLOR_ROLE
|
|
|
|
#define __ENUMERATE_COLOR_ROLE(role) \
|
2020-10-01 02:00:59 +00:00
|
|
|
data->color[(int)ColorRole::role] = get_color(#role).value();
|
2020-08-21 17:51:17 +00:00
|
|
|
ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
|
|
|
|
#undef __ENUMERATE_COLOR_ROLE
|
2019-12-23 19:24:26 +00:00
|
|
|
|
2020-07-17 00:27:55 +00:00
|
|
|
#define DO_METRIC(x) \
|
|
|
|
data->metric[(int)MetricRole::x] = get_metric(#x, (int)MetricRole::x)
|
|
|
|
|
|
|
|
DO_METRIC(TitleHeight);
|
|
|
|
DO_METRIC(TitleButtonWidth);
|
|
|
|
DO_METRIC(TitleButtonHeight);
|
|
|
|
|
2021-02-10 05:12:32 +00:00
|
|
|
#define DO_PATH(x, allow_empty) \
|
2020-10-01 02:00:59 +00:00
|
|
|
do { \
|
2021-02-10 05:12:32 +00:00
|
|
|
auto path = get_path(#x, (int)PathRole::x, allow_empty); \
|
2020-10-01 02:00:59 +00:00
|
|
|
memcpy(data->path[(int)PathRole::x], path, min(strlen(path) + 1, sizeof(data->path[(int)PathRole::x]))); \
|
|
|
|
data->path[(int)PathRole::x][sizeof(data->path[(int)PathRole::x]) - 1] = '\0'; \
|
|
|
|
} while (0)
|
2020-07-29 20:09:04 +00:00
|
|
|
|
2021-02-10 05:12:32 +00:00
|
|
|
DO_PATH(TitleButtonIcons, false);
|
2021-02-11 22:12:19 +00:00
|
|
|
DO_PATH(ActiveWindowShadow, true);
|
|
|
|
DO_PATH(InactiveWindowShadow, true);
|
2021-04-13 14:18:20 +00:00
|
|
|
DO_PATH(TaskbarShadow, true);
|
2021-02-10 05:12:32 +00:00
|
|
|
DO_PATH(MenuShadow, true);
|
|
|
|
DO_PATH(TooltipShadow, true);
|
2020-07-29 20:09:04 +00:00
|
|
|
|
2019-12-23 19:24:26 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
2020-02-06 10:56:38 +00:00
|
|
|
|
2021-08-25 20:22:07 +00:00
|
|
|
Core::AnonymousBuffer load_system_theme(String const& path)
|
|
|
|
{
|
|
|
|
return load_system_theme(Core::ConfigFile::open(path));
|
|
|
|
}
|
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
}
|