SystemTheme.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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)
  29. {
  30. auto buffer = TRY(Core::AnonymousBuffer::create_with_size(sizeof(SystemTheme)));
  31. auto* data = buffer.data<SystemTheme>();
  32. auto get_color = [&](auto& name) {
  33. auto color_string = file.read_entry("Colors", name);
  34. auto color = Color::from_string(color_string);
  35. if (!color.has_value())
  36. return Color(Color::Black);
  37. return color.value();
  38. };
  39. auto get_flag = [&](auto& name) {
  40. return file.read_bool_entry("Flags", name, false);
  41. };
  42. auto get_alignment = [&](auto& name, auto role) {
  43. auto alignment = file.read_entry("Alignments", name).to_lowercase();
  44. if (alignment.is_empty()) {
  45. switch (role) {
  46. case (int)AlignmentRole::TitleAlignment:
  47. return Gfx::TextAlignment::CenterLeft;
  48. default:
  49. dbgln("Alignment {} has no fallback value!", name);
  50. return Gfx::TextAlignment::CenterLeft;
  51. }
  52. }
  53. if (alignment == "left" || alignment == "centerleft")
  54. return Gfx::TextAlignment::CenterLeft;
  55. else if (alignment == "right" || alignment == "centerright")
  56. return Gfx::TextAlignment::CenterRight;
  57. else if (alignment == "center")
  58. return Gfx::TextAlignment::Center;
  59. dbgln("Alignment {} has an invalid value!", name);
  60. return Gfx::TextAlignment::CenterLeft;
  61. };
  62. auto get_metric = [&](auto& name, auto role) {
  63. int metric = file.read_num_entry("Metrics", name, -1);
  64. if (metric == -1) {
  65. switch (role) {
  66. case (int)MetricRole::BorderThickness:
  67. return 4;
  68. case (int)MetricRole::BorderRadius:
  69. return 0;
  70. case (int)MetricRole::TitleHeight:
  71. return 19;
  72. case (int)MetricRole::TitleButtonHeight:
  73. return 15;
  74. case (int)MetricRole::TitleButtonWidth:
  75. return 15;
  76. default:
  77. dbgln("Metric {} has no fallback value!", name);
  78. return 16;
  79. }
  80. }
  81. return metric;
  82. };
  83. auto get_path = [&](auto& name, auto role, bool allow_empty) {
  84. auto path = file.read_entry("Paths", name);
  85. if (path.is_empty()) {
  86. switch (role) {
  87. case (int)PathRole::TitleButtonIcons:
  88. return "/res/icons/16x16/";
  89. default:
  90. return allow_empty ? "" : "/res/";
  91. }
  92. }
  93. return &path[0];
  94. };
  95. #undef __ENUMERATE_COLOR_ROLE
  96. #define __ENUMERATE_COLOR_ROLE(role) \
  97. data->color[(int)ColorRole::role] = get_color(#role).value();
  98. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  99. #undef __ENUMERATE_COLOR_ROLE
  100. #undef __ENUMERATE_ALIGNMENT_ROLE
  101. #define __ENUMERATE_ALIGNMENT_ROLE(role) \
  102. data->alignment[(int)AlignmentRole::role] = get_alignment(#role, (int)AlignmentRole::role);
  103. ENUMERATE_ALIGNMENT_ROLES(__ENUMERATE_ALIGNMENT_ROLE)
  104. #undef __ENUMERATE_ALIGNMENT_ROLE
  105. #undef __ENUMERATE_FLAG_ROLE
  106. #define __ENUMERATE_FLAG_ROLE(role) \
  107. data->flag[(int)FlagRole::role] = get_flag(#role);
  108. ENUMERATE_FLAG_ROLES(__ENUMERATE_FLAG_ROLE)
  109. #undef __ENUMERATE_FLAG_ROLE
  110. #undef __ENUMERATE_METRIC_ROLE
  111. #define __ENUMERATE_METRIC_ROLE(role) \
  112. data->metric[(int)MetricRole::role] = get_metric(#role, (int)MetricRole::role);
  113. ENUMERATE_METRIC_ROLES(__ENUMERATE_METRIC_ROLE)
  114. #undef __ENUMERATE_METRIC_ROLE
  115. #define ENCODE_PATH(x, allow_empty) \
  116. do { \
  117. auto path = get_path(#x, (int)PathRole::x, allow_empty); \
  118. memcpy(data->path[(int)PathRole::x], path, min(strlen(path) + 1, sizeof(data->path[(int)PathRole::x]))); \
  119. data->path[(int)PathRole::x][sizeof(data->path[(int)PathRole::x]) - 1] = '\0'; \
  120. } while (0)
  121. ENCODE_PATH(TitleButtonIcons, false);
  122. ENCODE_PATH(ActiveWindowShadow, true);
  123. ENCODE_PATH(InactiveWindowShadow, true);
  124. ENCODE_PATH(TaskbarShadow, true);
  125. ENCODE_PATH(MenuShadow, true);
  126. ENCODE_PATH(TooltipShadow, true);
  127. return buffer;
  128. }
  129. ErrorOr<Core::AnonymousBuffer> load_system_theme(DeprecatedString const& path)
  130. {
  131. auto config_file = TRY(Core::ConfigFile::open(path));
  132. return TRY(load_system_theme(config_file));
  133. }
  134. ErrorOr<Vector<SystemThemeMetaData>> list_installed_system_themes()
  135. {
  136. Vector<SystemThemeMetaData> system_themes;
  137. Core::DirIterator dt("/res/themes", Core::DirIterator::SkipDots);
  138. while (dt.has_next()) {
  139. auto theme_name = dt.next_path();
  140. auto theme_path = DeprecatedString::formatted("/res/themes/{}", theme_name);
  141. TRY(system_themes.try_append({ LexicalPath::title(theme_name), theme_path }));
  142. }
  143. quick_sort(system_themes, [](auto& a, auto& b) { return a.name < b.name; });
  144. return system_themes;
  145. }
  146. }