SystemTheme.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibCore/ConfigFile.h>
  8. #include <LibGfx/SystemTheme.h>
  9. #include <string.h>
  10. namespace Gfx {
  11. static SystemTheme dummy_theme;
  12. static const SystemTheme* theme_page = &dummy_theme;
  13. static Core::AnonymousBuffer theme_buffer;
  14. Core::AnonymousBuffer& current_system_theme_buffer()
  15. {
  16. VERIFY(theme_buffer.is_valid());
  17. return theme_buffer;
  18. }
  19. void set_system_theme(Core::AnonymousBuffer buffer)
  20. {
  21. theme_buffer = move(buffer);
  22. theme_page = theme_buffer.data<SystemTheme>();
  23. }
  24. Core::AnonymousBuffer load_system_theme(Core::ConfigFile const& file)
  25. {
  26. auto buffer = Core::AnonymousBuffer::create_with_size(sizeof(SystemTheme));
  27. auto* data = buffer.data<SystemTheme>();
  28. auto get_color = [&](auto& name) {
  29. auto color_string = file.read_entry("Colors", name);
  30. auto color = Color::from_string(color_string);
  31. if (!color.has_value())
  32. return Color(Color::Black);
  33. return color.value();
  34. };
  35. auto get_flag = [&](auto& name) {
  36. auto flag_string = file.read_entry("Flags", name);
  37. return flag_string.equals_ignoring_case("true");
  38. };
  39. auto get_metric = [&](auto& name, auto role) {
  40. int metric = file.read_num_entry("Metrics", name, -1);
  41. if (metric == -1) {
  42. switch (role) {
  43. case (int)MetricRole::TitleHeight:
  44. return 19;
  45. case (int)MetricRole::TitleButtonHeight:
  46. return 15;
  47. case (int)MetricRole::TitleButtonWidth:
  48. return 15;
  49. default:
  50. dbgln("Metric {} has no fallback value!", name);
  51. return 16;
  52. }
  53. }
  54. return metric;
  55. };
  56. auto get_path = [&](auto& name, auto role, bool allow_empty) {
  57. auto path = file.read_entry("Paths", name);
  58. if (path.is_empty()) {
  59. switch (role) {
  60. case (int)PathRole::TitleButtonIcons:
  61. return "/res/icons/16x16/";
  62. default:
  63. return allow_empty ? "" : "/res/";
  64. }
  65. }
  66. return &path[0];
  67. };
  68. #undef __ENUMERATE_COLOR_ROLE
  69. #define __ENUMERATE_COLOR_ROLE(role) \
  70. data->color[(int)ColorRole::role] = get_color(#role).value();
  71. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  72. #undef __ENUMERATE_COLOR_ROLE
  73. #undef __ENUMERATE_FLAG_ROLE
  74. #define __ENUMERATE_FLAG_ROLE(role) \
  75. data->flag[(int)FlagRole::role] = get_flag(#role);
  76. ENUMERATE_FLAG_ROLES(__ENUMERATE_FLAG_ROLE)
  77. #undef __ENUMERATE_FLAG_ROLE
  78. #undef __ENUMERATE_METRIC_ROLE
  79. #define __ENUMERATE_METRIC_ROLE(role) \
  80. data->metric[(int)MetricRole::role] = get_metric(#role, (int)MetricRole::role);
  81. ENUMERATE_METRIC_ROLES(__ENUMERATE_METRIC_ROLE)
  82. #undef __ENUMERATE_METRIC_ROLE
  83. #define DO_PATH(x, allow_empty) \
  84. do { \
  85. auto path = get_path(#x, (int)PathRole::x, allow_empty); \
  86. memcpy(data->path[(int)PathRole::x], path, min(strlen(path) + 1, sizeof(data->path[(int)PathRole::x]))); \
  87. data->path[(int)PathRole::x][sizeof(data->path[(int)PathRole::x]) - 1] = '\0'; \
  88. } while (0)
  89. DO_PATH(TitleButtonIcons, false);
  90. DO_PATH(ActiveWindowShadow, true);
  91. DO_PATH(InactiveWindowShadow, true);
  92. DO_PATH(TaskbarShadow, true);
  93. DO_PATH(MenuShadow, true);
  94. DO_PATH(TooltipShadow, true);
  95. return buffer;
  96. }
  97. Core::AnonymousBuffer load_system_theme(String const& path)
  98. {
  99. return load_system_theme(Core::ConfigFile::open(path));
  100. }
  101. }