SystemTheme.cpp 3.7 KB

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