2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2021-10-26 13:30:52 +00:00
|
|
|
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
2022-01-01 17:26:19 +00:00
|
|
|
* Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2022-04-02 17:27:22 +00:00
|
|
|
#include <AK/LexicalPath.h>
|
|
|
|
#include <AK/QuickSort.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/ConfigFile.h>
|
2022-04-02 17:27:22 +00:00
|
|
|
#include <LibCore/DirIterator.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;
|
2022-04-01 17:58:27 +00:00
|
|
|
static SystemTheme const* 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
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ErrorOr<Core::AnonymousBuffer> load_system_theme(Core::ConfigFile const& file, Optional<ByteString> const& color_scheme)
|
2019-12-23 19:24:26 +00:00
|
|
|
{
|
2022-12-06 16:26:13 +00:00
|
|
|
auto buffer = TRY(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
|
|
|
|
2023-02-12 13:48:23 +00:00
|
|
|
if (color_scheme.has_value()) {
|
|
|
|
if (color_scheme.value().length() > 255)
|
|
|
|
return Error::from_string_literal("Passed an excessively long color scheme pathname");
|
|
|
|
if (color_scheme.value() != "Custom"sv)
|
|
|
|
memcpy(data->path[(int)PathRole::ColorScheme], color_scheme.value().characters(), color_scheme.value().length());
|
|
|
|
else
|
|
|
|
memcpy(buffer.data<SystemTheme>(), theme_buffer.data<SystemTheme>(), sizeof(SystemTheme));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto get_color = [&](auto& name) -> Optional<Color> {
|
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);
|
2023-02-12 13:48:23 +00:00
|
|
|
if (color_scheme.has_value() && color_scheme.value() == "Custom"sv)
|
|
|
|
return color;
|
2022-12-02 16:15:28 +00:00
|
|
|
if (!color.has_value()) {
|
|
|
|
auto maybe_color_config = Core::ConfigFile::open(data->path[(int)PathRole::ColorScheme]);
|
|
|
|
if (maybe_color_config.is_error())
|
|
|
|
maybe_color_config = Core::ConfigFile::open("/res/color-schemes/Default.ini");
|
|
|
|
auto color_config = maybe_color_config.release_value();
|
|
|
|
if (name == "ColorSchemeBackground"sv)
|
|
|
|
color = Gfx::Color::from_string(color_config->read_entry("Primary", "Background"));
|
|
|
|
else if (name == "ColorSchemeForeground"sv)
|
|
|
|
color = Gfx::Color::from_string(color_config->read_entry("Primary", "Foreground"));
|
|
|
|
else if (strncmp(name, "Bright", 6) == 0)
|
|
|
|
color = Gfx::Color::from_string(color_config->read_entry("Bright", name + 6));
|
|
|
|
else
|
|
|
|
color = Gfx::Color::from_string(color_config->read_entry("Normal", name));
|
|
|
|
|
|
|
|
if (!color.has_value())
|
|
|
|
return Color(Color::Black);
|
|
|
|
}
|
2019-12-23 19:24:26 +00:00
|
|
|
return color.value();
|
|
|
|
};
|
|
|
|
|
2021-10-26 13:30:52 +00:00
|
|
|
auto get_flag = [&](auto& name) {
|
2021-11-01 16:24:50 +00:00
|
|
|
return file.read_bool_entry("Flags", name, false);
|
2021-10-26 13:30:52 +00:00
|
|
|
};
|
|
|
|
|
2022-01-01 17:26:19 +00:00
|
|
|
auto get_alignment = [&](auto& name, auto role) {
|
|
|
|
auto alignment = file.read_entry("Alignments", name).to_lowercase();
|
|
|
|
if (alignment.is_empty()) {
|
|
|
|
switch (role) {
|
|
|
|
case (int)AlignmentRole::TitleAlignment:
|
|
|
|
return Gfx::TextAlignment::CenterLeft;
|
|
|
|
default:
|
|
|
|
dbgln("Alignment {} has no fallback value!", name);
|
|
|
|
return Gfx::TextAlignment::CenterLeft;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alignment == "left" || alignment == "centerleft")
|
|
|
|
return Gfx::TextAlignment::CenterLeft;
|
|
|
|
else if (alignment == "right" || alignment == "centerright")
|
|
|
|
return Gfx::TextAlignment::CenterRight;
|
|
|
|
else if (alignment == "center")
|
|
|
|
return Gfx::TextAlignment::Center;
|
|
|
|
|
|
|
|
dbgln("Alignment {} has an invalid value!", name);
|
|
|
|
return Gfx::TextAlignment::CenterLeft;
|
|
|
|
};
|
|
|
|
|
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) {
|
2021-12-28 21:44:12 +00:00
|
|
|
case (int)MetricRole::BorderThickness:
|
|
|
|
return 4;
|
|
|
|
case (int)MetricRole::BorderRadius:
|
|
|
|
return 0;
|
2020-07-17 00:27:55 +00:00
|
|
|
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];
|
|
|
|
};
|
|
|
|
|
2022-12-02 16:15:28 +00:00
|
|
|
#define ENCODE_PATH(x, allow_empty) \
|
|
|
|
do { \
|
|
|
|
auto path = get_path(#x, (int)PathRole::x, allow_empty); \
|
|
|
|
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)
|
|
|
|
|
|
|
|
ENCODE_PATH(TitleButtonIcons, false);
|
|
|
|
ENCODE_PATH(ActiveWindowShadow, true);
|
|
|
|
ENCODE_PATH(InactiveWindowShadow, true);
|
|
|
|
ENCODE_PATH(TaskbarShadow, true);
|
|
|
|
ENCODE_PATH(MenuShadow, true);
|
|
|
|
ENCODE_PATH(TooltipShadow, true);
|
2023-02-12 13:48:23 +00:00
|
|
|
if (!color_scheme.has_value())
|
|
|
|
ENCODE_PATH(ColorScheme, true);
|
2022-12-02 16:15:28 +00:00
|
|
|
|
2020-08-21 17:51:17 +00:00
|
|
|
#undef __ENUMERATE_COLOR_ROLE
|
2023-02-12 13:48:23 +00:00
|
|
|
#define __ENUMERATE_COLOR_ROLE(role) \
|
|
|
|
{ \
|
|
|
|
Optional<Color> result = get_color(#role); \
|
|
|
|
if (result.has_value()) \
|
|
|
|
data->color[(int)ColorRole::role] = result.value().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
|
|
|
|
2022-01-01 17:26:19 +00:00
|
|
|
#undef __ENUMERATE_ALIGNMENT_ROLE
|
|
|
|
#define __ENUMERATE_ALIGNMENT_ROLE(role) \
|
|
|
|
data->alignment[(int)AlignmentRole::role] = get_alignment(#role, (int)AlignmentRole::role);
|
|
|
|
ENUMERATE_ALIGNMENT_ROLES(__ENUMERATE_ALIGNMENT_ROLE)
|
|
|
|
#undef __ENUMERATE_ALIGNMENT_ROLE
|
|
|
|
|
2021-10-26 13:30:52 +00:00
|
|
|
#undef __ENUMERATE_FLAG_ROLE
|
2023-02-12 13:48:23 +00:00
|
|
|
#define __ENUMERATE_FLAG_ROLE(role) \
|
|
|
|
{ \
|
|
|
|
if (#role != "BoldTextAsBright"sv) \
|
|
|
|
data->flag[(int)FlagRole::role] = get_flag(#role); \
|
|
|
|
}
|
2021-10-26 13:30:52 +00:00
|
|
|
ENUMERATE_FLAG_ROLES(__ENUMERATE_FLAG_ROLE)
|
|
|
|
#undef __ENUMERATE_FLAG_ROLE
|
|
|
|
|
2021-09-15 19:05:33 +00:00
|
|
|
#undef __ENUMERATE_METRIC_ROLE
|
|
|
|
#define __ENUMERATE_METRIC_ROLE(role) \
|
|
|
|
data->metric[(int)MetricRole::role] = get_metric(#role, (int)MetricRole::role);
|
|
|
|
ENUMERATE_METRIC_ROLES(__ENUMERATE_METRIC_ROLE)
|
|
|
|
#undef __ENUMERATE_METRIC_ROLE
|
2020-07-17 00:27:55 +00:00
|
|
|
|
2023-02-12 13:48:23 +00:00
|
|
|
if (!color_scheme.has_value() || color_scheme.value() != "Custom"sv) {
|
|
|
|
auto maybe_color_config = Core::ConfigFile::open(data->path[(int)PathRole::ColorScheme]);
|
|
|
|
if (!maybe_color_config.is_error()) {
|
|
|
|
auto color_config = maybe_color_config.release_value();
|
|
|
|
data->flag[(int)FlagRole::BoldTextAsBright] = color_config->read_bool_entry("Options", "ShowBoldTextAsBright", true);
|
|
|
|
}
|
2022-12-02 16:15:28 +00:00
|
|
|
}
|
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
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ErrorOr<Core::AnonymousBuffer> load_system_theme(ByteString const& path, Optional<ByteString> const& color_scheme)
|
2021-08-25 20:22:07 +00:00
|
|
|
{
|
2022-12-06 16:26:13 +00:00
|
|
|
auto config_file = TRY(Core::ConfigFile::open(path));
|
2023-02-12 13:48:23 +00:00
|
|
|
return TRY(load_system_theme(config_file, color_scheme));
|
2021-08-25 20:22:07 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 16:26:13 +00:00
|
|
|
ErrorOr<Vector<SystemThemeMetaData>> list_installed_system_themes()
|
2022-04-02 17:27:22 +00:00
|
|
|
{
|
|
|
|
Vector<SystemThemeMetaData> system_themes;
|
|
|
|
Core::DirIterator dt("/res/themes", Core::DirIterator::SkipDots);
|
|
|
|
while (dt.has_next()) {
|
|
|
|
auto theme_name = dt.next_path();
|
2023-12-16 14:19:34 +00:00
|
|
|
auto theme_path = ByteString::formatted("/res/themes/{}", theme_name);
|
2024-01-03 04:20:42 +00:00
|
|
|
auto config_file = TRY(Core::ConfigFile::open(theme_path));
|
|
|
|
auto menu_name = config_file->read_entry("Menu", "Name", theme_name);
|
|
|
|
TRY(system_themes.try_append({ LexicalPath::title(theme_name), menu_name, theme_path }));
|
2022-04-02 17:27:22 +00:00
|
|
|
}
|
|
|
|
quick_sort(system_themes, [](auto& a, auto& b) { return a.name < b.name; });
|
|
|
|
return system_themes;
|
|
|
|
}
|
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
}
|