SystemTheme.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/LexicalPath.h>
  9. #include <AK/QuickSort.h>
  10. #include <LibCore/ConfigFile.h>
  11. #include <LibCore/DirIterator.h>
  12. #include <LibGfx/SystemTheme.h>
  13. #include <string.h>
  14. namespace Gfx {
  15. static SystemTheme dummy_theme;
  16. static SystemTheme const* theme_page = &dummy_theme;
  17. static Core::AnonymousBuffer theme_buffer;
  18. Core::AnonymousBuffer& current_system_theme_buffer()
  19. {
  20. VERIFY(theme_buffer.is_valid());
  21. return theme_buffer;
  22. }
  23. void set_system_theme(Core::AnonymousBuffer buffer)
  24. {
  25. theme_buffer = move(buffer);
  26. theme_page = theme_buffer.data<SystemTheme>();
  27. }
  28. ErrorOr<Core::AnonymousBuffer> load_system_theme(Core::ConfigFile const& file, Optional<ByteString> const& color_scheme)
  29. {
  30. auto buffer = TRY(Core::AnonymousBuffer::create_with_size(sizeof(SystemTheme)));
  31. auto* data = buffer.data<SystemTheme>();
  32. if (color_scheme.has_value()) {
  33. if (color_scheme.value().length() > 255)
  34. return Error::from_string_literal("Passed an excessively long color scheme pathname");
  35. if (color_scheme.value() != "Custom"sv)
  36. memcpy(data->path[(int)PathRole::ColorScheme], color_scheme.value().characters(), color_scheme.value().length());
  37. else
  38. memcpy(buffer.data<SystemTheme>(), theme_buffer.data<SystemTheme>(), sizeof(SystemTheme));
  39. }
  40. auto get_color = [&](auto& name) -> Optional<Color> {
  41. auto color_string = file.read_entry("Colors", name);
  42. auto color = Color::from_string(color_string);
  43. if (color_scheme.has_value() && color_scheme.value() == "Custom"sv)
  44. return color;
  45. if (!color.has_value()) {
  46. auto maybe_color_config = Core::ConfigFile::open(data->path[(int)PathRole::ColorScheme]);
  47. if (maybe_color_config.is_error())
  48. maybe_color_config = Core::ConfigFile::open("/res/color-schemes/Default.ini");
  49. auto color_config = maybe_color_config.release_value();
  50. if (name == "ColorSchemeBackground"sv)
  51. color = Gfx::Color::from_string(color_config->read_entry("Primary", "Background"));
  52. else if (name == "ColorSchemeForeground"sv)
  53. color = Gfx::Color::from_string(color_config->read_entry("Primary", "Foreground"));
  54. else if (strncmp(name, "Bright", 6) == 0)
  55. color = Gfx::Color::from_string(color_config->read_entry("Bright", name + 6));
  56. else
  57. color = Gfx::Color::from_string(color_config->read_entry("Normal", name));
  58. if (!color.has_value())
  59. return Color(Color::Black);
  60. }
  61. return color.value();
  62. };
  63. auto get_flag = [&](auto& name) {
  64. return file.read_bool_entry("Flags", name, false);
  65. };
  66. auto get_alignment = [&](auto& name, auto role) {
  67. auto alignment = file.read_entry("Alignments", name).to_lowercase();
  68. if (alignment.is_empty()) {
  69. switch (role) {
  70. case (int)AlignmentRole::TitleAlignment:
  71. return Gfx::TextAlignment::CenterLeft;
  72. default:
  73. dbgln("Alignment {} has no fallback value!", name);
  74. return Gfx::TextAlignment::CenterLeft;
  75. }
  76. }
  77. if (alignment == "left" || alignment == "centerleft")
  78. return Gfx::TextAlignment::CenterLeft;
  79. else if (alignment == "right" || alignment == "centerright")
  80. return Gfx::TextAlignment::CenterRight;
  81. else if (alignment == "center")
  82. return Gfx::TextAlignment::Center;
  83. dbgln("Alignment {} has an invalid value!", name);
  84. return Gfx::TextAlignment::CenterLeft;
  85. };
  86. auto get_metric = [&](auto& name, auto role) {
  87. int metric = file.read_num_entry("Metrics", name, -1);
  88. if (metric == -1) {
  89. switch (role) {
  90. case (int)MetricRole::BorderThickness:
  91. return 4;
  92. case (int)MetricRole::BorderRadius:
  93. return 0;
  94. case (int)MetricRole::TitleHeight:
  95. return 19;
  96. case (int)MetricRole::TitleButtonHeight:
  97. return 15;
  98. case (int)MetricRole::TitleButtonWidth:
  99. return 15;
  100. default:
  101. dbgln("Metric {} has no fallback value!", name);
  102. return 16;
  103. }
  104. }
  105. return metric;
  106. };
  107. auto get_path = [&](auto& name, auto role, bool allow_empty) {
  108. auto path = file.read_entry("Paths", name);
  109. if (path.is_empty()) {
  110. switch (role) {
  111. case (int)PathRole::TitleButtonIcons:
  112. return "/res/icons/16x16/";
  113. default:
  114. return allow_empty ? "" : "/res/";
  115. }
  116. }
  117. return &path[0];
  118. };
  119. #define ENCODE_PATH(x, allow_empty) \
  120. do { \
  121. auto path = get_path(#x, (int)PathRole::x, allow_empty); \
  122. memcpy(data->path[(int)PathRole::x], path, min(strlen(path) + 1, sizeof(data->path[(int)PathRole::x]))); \
  123. data->path[(int)PathRole::x][sizeof(data->path[(int)PathRole::x]) - 1] = '\0'; \
  124. } while (0)
  125. ENCODE_PATH(TitleButtonIcons, false);
  126. ENCODE_PATH(ActiveWindowShadow, true);
  127. ENCODE_PATH(InactiveWindowShadow, true);
  128. ENCODE_PATH(TaskbarShadow, true);
  129. ENCODE_PATH(MenuShadow, true);
  130. ENCODE_PATH(TooltipShadow, true);
  131. if (!color_scheme.has_value())
  132. ENCODE_PATH(ColorScheme, true);
  133. #undef __ENUMERATE_COLOR_ROLE
  134. #define __ENUMERATE_COLOR_ROLE(role) \
  135. { \
  136. Optional<Color> result = get_color(#role); \
  137. if (result.has_value()) \
  138. data->color[(int)ColorRole::role] = result.value().value(); \
  139. }
  140. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  141. #undef __ENUMERATE_COLOR_ROLE
  142. #undef __ENUMERATE_ALIGNMENT_ROLE
  143. #define __ENUMERATE_ALIGNMENT_ROLE(role) \
  144. data->alignment[(int)AlignmentRole::role] = get_alignment(#role, (int)AlignmentRole::role);
  145. ENUMERATE_ALIGNMENT_ROLES(__ENUMERATE_ALIGNMENT_ROLE)
  146. #undef __ENUMERATE_ALIGNMENT_ROLE
  147. #undef __ENUMERATE_FLAG_ROLE
  148. #define __ENUMERATE_FLAG_ROLE(role) \
  149. { \
  150. if (#role != "BoldTextAsBright"sv) \
  151. data->flag[(int)FlagRole::role] = get_flag(#role); \
  152. }
  153. ENUMERATE_FLAG_ROLES(__ENUMERATE_FLAG_ROLE)
  154. #undef __ENUMERATE_FLAG_ROLE
  155. #undef __ENUMERATE_METRIC_ROLE
  156. #define __ENUMERATE_METRIC_ROLE(role) \
  157. data->metric[(int)MetricRole::role] = get_metric(#role, (int)MetricRole::role);
  158. ENUMERATE_METRIC_ROLES(__ENUMERATE_METRIC_ROLE)
  159. #undef __ENUMERATE_METRIC_ROLE
  160. if (!color_scheme.has_value() || color_scheme.value() != "Custom"sv) {
  161. auto maybe_color_config = Core::ConfigFile::open(data->path[(int)PathRole::ColorScheme]);
  162. if (!maybe_color_config.is_error()) {
  163. auto color_config = maybe_color_config.release_value();
  164. data->flag[(int)FlagRole::BoldTextAsBright] = color_config->read_bool_entry("Options", "ShowBoldTextAsBright", true);
  165. }
  166. }
  167. return buffer;
  168. }
  169. ErrorOr<Core::AnonymousBuffer> load_system_theme(ByteString const& path, Optional<ByteString> const& color_scheme)
  170. {
  171. auto config_file = TRY(Core::ConfigFile::open(path));
  172. return TRY(load_system_theme(config_file, color_scheme));
  173. }
  174. ErrorOr<Vector<SystemThemeMetaData>> list_installed_system_themes()
  175. {
  176. Vector<SystemThemeMetaData> system_themes;
  177. Core::DirIterator dt("/res/themes", Core::DirIterator::SkipDots);
  178. while (dt.has_next()) {
  179. auto theme_name = dt.next_path();
  180. auto theme_path = ByteString::formatted("/res/themes/{}", theme_name);
  181. auto config_file = TRY(Core::ConfigFile::open(theme_path));
  182. auto menu_name = config_file->read_entry("Menu", "Name", theme_name);
  183. TRY(system_themes.try_append({ LexicalPath::title(theme_name), menu_name, theme_path }));
  184. }
  185. quick_sort(system_themes, [](auto& a, auto& b) { return a.name < b.name; });
  186. return system_themes;
  187. }
  188. }