SystemTheme.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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)).release_value();
  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. return file.read_bool_entry("Flags", name, false);
  37. };
  38. auto get_metric = [&](auto& name, auto role) {
  39. int metric = file.read_num_entry("Metrics", name, -1);
  40. if (metric == -1) {
  41. switch (role) {
  42. case (int)MetricRole::BorderThickness:
  43. return 4;
  44. case (int)MetricRole::BorderRadius:
  45. return 0;
  46. case (int)MetricRole::TitleHeight:
  47. return 19;
  48. case (int)MetricRole::TitleButtonHeight:
  49. return 15;
  50. case (int)MetricRole::TitleButtonWidth:
  51. return 15;
  52. default:
  53. dbgln("Metric {} has no fallback value!", name);
  54. return 16;
  55. }
  56. }
  57. return metric;
  58. };
  59. auto get_path = [&](auto& name, auto role, bool allow_empty) {
  60. auto path = file.read_entry("Paths", name);
  61. if (path.is_empty()) {
  62. switch (role) {
  63. case (int)PathRole::TitleButtonIcons:
  64. return "/res/icons/16x16/";
  65. default:
  66. return allow_empty ? "" : "/res/";
  67. }
  68. }
  69. return &path[0];
  70. };
  71. #undef __ENUMERATE_COLOR_ROLE
  72. #define __ENUMERATE_COLOR_ROLE(role) \
  73. data->color[(int)ColorRole::role] = get_color(#role).value();
  74. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  75. #undef __ENUMERATE_COLOR_ROLE
  76. #undef __ENUMERATE_FLAG_ROLE
  77. #define __ENUMERATE_FLAG_ROLE(role) \
  78. data->flag[(int)FlagRole::role] = get_flag(#role);
  79. ENUMERATE_FLAG_ROLES(__ENUMERATE_FLAG_ROLE)
  80. #undef __ENUMERATE_FLAG_ROLE
  81. #undef __ENUMERATE_METRIC_ROLE
  82. #define __ENUMERATE_METRIC_ROLE(role) \
  83. data->metric[(int)MetricRole::role] = get_metric(#role, (int)MetricRole::role);
  84. ENUMERATE_METRIC_ROLES(__ENUMERATE_METRIC_ROLE)
  85. #undef __ENUMERATE_METRIC_ROLE
  86. #define DO_PATH(x, allow_empty) \
  87. do { \
  88. auto path = get_path(#x, (int)PathRole::x, allow_empty); \
  89. memcpy(data->path[(int)PathRole::x], path, min(strlen(path) + 1, sizeof(data->path[(int)PathRole::x]))); \
  90. data->path[(int)PathRole::x][sizeof(data->path[(int)PathRole::x]) - 1] = '\0'; \
  91. } while (0)
  92. DO_PATH(TitleButtonIcons, false);
  93. DO_PATH(ActiveWindowShadow, true);
  94. DO_PATH(InactiveWindowShadow, true);
  95. DO_PATH(TaskbarShadow, true);
  96. DO_PATH(MenuShadow, true);
  97. DO_PATH(TooltipShadow, true);
  98. return buffer;
  99. }
  100. Core::AnonymousBuffer load_system_theme(String const& path)
  101. {
  102. return load_system_theme(Core::ConfigFile::open(path));
  103. }
  104. }