8#include "../settings/settings.hpp"
9#include "../macros.hpp"
15hi_export_module(hikogui.GUI : theme_book);
17hi_export
namespace hi::inline
v1 {
26 observer<std::string> selected_theme =
"default";
37 void register_directory(
std::filesystem::path path) noexcept
39 _theme_directories.push_back(path);
42 void reload() noexcept
46 for (
auto const& theme_directory : _theme_directories) {
47 auto const theme_directory_glob = theme_directory /
"**" /
"*.theme.json";
48 for (
auto const& theme_path : glob(theme_directory_glob)) {
49 auto t =
trace<
"theme_scan">{};
52 themes.push_back(std::make_unique<theme>(theme_path));
54 hi_log_error(
"Failed parsing theme at {}. \"{}\"", theme_path.string(), e.
what());
59 if (ssize(themes) == 0) {
60 hi_log_fatal(
"Did not load any themes.");
68 for (
auto const& t : themes) {
84 [[nodiscard]]
theme const&
find(std::string_view name, theme_mode mode)
const noexcept
86 theme *default_theme =
nullptr;
87 theme *default_theme_and_mode =
nullptr;
88 theme *matching_theme =
nullptr;
89 theme *matching_theme_and_mode =
nullptr;
91 for (
auto const& t : themes) {
92 if (t->name == name and t->mode == mode) {
93 matching_theme_and_mode = t.get();
94 }
else if (t->name == name) {
95 matching_theme = t.get();
96 }
else if (t->name ==
"default" and t->mode == mode) {
97 default_theme_and_mode = t.get();
98 }
else if (t->name ==
"default") {
99 default_theme = t.get();
103 if (matching_theme_and_mode) {
104 return *matching_theme_and_mode;
105 }
else if (matching_theme) {
106 return *matching_theme;
107 }
else if (default_theme_and_mode) {
108 return *default_theme_and_mode;
109 }
else if (default_theme) {
110 return *default_theme;
111 }
else if (ssize(themes) > 0) {
112 return *themes[0].get();
127[[nodiscard]]
inline theme_book& theme_book::global() noexcept
129 if (not detail::theme_book_global) {
130 detail::theme_book_global = std::make_unique<theme_book>();
132 return *detail::theme_book_global;
135inline void register_theme_directory(std::filesystem::path
const &path)
noexcept
137 return theme_book::global().register_directory(path);
140inline void reload_themes() noexcept
142 return theme_book::global().reload();
145template<std::ranges::range R>
146inline void register_theme_directories(R &&r)
noexcept
148 for (
auto &path: r) {
149 register_theme_directory(path);
154[[nodiscard]]
inline theme
const& find_theme(std::string_view name, theme_mode mode)
noexcept
156 return theme_book::global().find(name, mode);
161 return theme_book::global().names();
164[[nodiscard]]
inline theme
const &get_selected_theme() noexcept
166 return find_theme(*theme_book::global().selected_theme, os_settings::theme_mode());
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
theme_book keeps track of multiple themes.
Definition theme_book.hpp:22
theme const & find(std::string_view name, theme_mode mode) const noexcept
Find a theme matching the name and mode.
Definition theme_book.hpp:84