SystemTheme.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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(const String& path)
  24. {
  25. auto file = Core::ConfigFile::open(path);
  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_metric = [&](auto& name, auto role) {
  36. int metric = file->read_num_entry("Metrics", name, -1);
  37. if (metric == -1) {
  38. switch (role) {
  39. case (int)MetricRole::TitleHeight:
  40. return 19;
  41. case (int)MetricRole::TitleButtonHeight:
  42. return 15;
  43. case (int)MetricRole::TitleButtonWidth:
  44. return 15;
  45. default:
  46. dbgln("Metric {} has no fallback value!", name);
  47. return 16;
  48. }
  49. }
  50. return metric;
  51. };
  52. auto get_path = [&](auto& name, auto role, bool allow_empty) {
  53. auto path = file->read_entry("Paths", name);
  54. if (path.is_empty()) {
  55. switch (role) {
  56. case (int)PathRole::TitleButtonIcons:
  57. return "/res/icons/16x16/";
  58. default:
  59. return allow_empty ? "" : "/res/";
  60. }
  61. }
  62. return &path[0];
  63. };
  64. #undef __ENUMERATE_COLOR_ROLE
  65. #define __ENUMERATE_COLOR_ROLE(role) \
  66. data->color[(int)ColorRole::role] = get_color(#role).value();
  67. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  68. #undef __ENUMERATE_COLOR_ROLE
  69. #define DO_METRIC(x) \
  70. data->metric[(int)MetricRole::x] = get_metric(#x, (int)MetricRole::x)
  71. DO_METRIC(TitleHeight);
  72. DO_METRIC(TitleButtonWidth);
  73. DO_METRIC(TitleButtonHeight);
  74. #define DO_PATH(x, allow_empty) \
  75. do { \
  76. auto path = get_path(#x, (int)PathRole::x, allow_empty); \
  77. memcpy(data->path[(int)PathRole::x], path, min(strlen(path) + 1, sizeof(data->path[(int)PathRole::x]))); \
  78. data->path[(int)PathRole::x][sizeof(data->path[(int)PathRole::x]) - 1] = '\0'; \
  79. } while (0)
  80. DO_PATH(TitleButtonIcons, false);
  81. DO_PATH(ActiveWindowShadow, true);
  82. DO_PATH(InactiveWindowShadow, true);
  83. DO_PATH(TaskbarShadow, true);
  84. DO_PATH(MenuShadow, true);
  85. DO_PATH(TooltipShadow, true);
  86. return buffer;
  87. }
  88. }