SystemTheme.cpp 3.4 KB

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