SystemTheme.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibCore/ConfigFile.h>
  27. #include <LibGfx/SystemTheme.h>
  28. #include <string.h>
  29. namespace Gfx {
  30. static SystemTheme dummy_theme;
  31. static const SystemTheme* theme_page = &dummy_theme;
  32. static Core::AnonymousBuffer theme_buffer;
  33. const SystemTheme& current_system_theme()
  34. {
  35. ASSERT(theme_page);
  36. return *theme_page;
  37. }
  38. Core::AnonymousBuffer& current_system_theme_buffer()
  39. {
  40. ASSERT(theme_buffer.is_valid());
  41. return theme_buffer;
  42. }
  43. void set_system_theme(Core::AnonymousBuffer buffer)
  44. {
  45. theme_buffer = move(buffer);
  46. theme_page = theme_buffer.data<SystemTheme>();
  47. }
  48. Core::AnonymousBuffer load_system_theme(const String& path)
  49. {
  50. auto file = Core::ConfigFile::open(path);
  51. auto buffer = Core::AnonymousBuffer::create_with_size(sizeof(SystemTheme));
  52. auto* data = buffer.data<SystemTheme>();
  53. auto get_color = [&](auto& name) {
  54. auto color_string = file->read_entry("Colors", name);
  55. auto color = Color::from_string(color_string);
  56. if (!color.has_value())
  57. return Color(Color::Black);
  58. return color.value();
  59. };
  60. auto get_metric = [&](auto& name, auto role) {
  61. int metric = file->read_num_entry("Metrics", name, -1);
  62. if (metric == -1) {
  63. switch (role) {
  64. case (int)MetricRole::TitleHeight:
  65. return 19;
  66. case (int)MetricRole::TitleButtonHeight:
  67. return 15;
  68. case (int)MetricRole::TitleButtonWidth:
  69. return 15;
  70. default:
  71. dbgln("Metric {} has no fallback value!", name);
  72. return 16;
  73. }
  74. }
  75. return metric;
  76. };
  77. auto get_path = [&](auto& name, auto role) {
  78. auto path = file->read_entry("Paths", name);
  79. if (path.is_empty()) {
  80. switch (role) {
  81. case (int)PathRole::TitleButtonIcons:
  82. return "/res/icons/16x16/";
  83. default:
  84. return "/res/";
  85. }
  86. }
  87. return &path[0];
  88. };
  89. #undef __ENUMERATE_COLOR_ROLE
  90. #define __ENUMERATE_COLOR_ROLE(role) \
  91. data->color[(int)ColorRole::role] = get_color(#role).value();
  92. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  93. #undef __ENUMERATE_COLOR_ROLE
  94. #define DO_METRIC(x) \
  95. data->metric[(int)MetricRole::x] = get_metric(#x, (int)MetricRole::x)
  96. DO_METRIC(TitleHeight);
  97. DO_METRIC(TitleButtonWidth);
  98. DO_METRIC(TitleButtonHeight);
  99. #define DO_PATH(x) \
  100. do { \
  101. auto path = get_path(#x, (int)PathRole::x); \
  102. memcpy(data->path[(int)PathRole::x], path, min(strlen(path) + 1, sizeof(data->path[(int)PathRole::x]))); \
  103. data->path[(int)PathRole::x][sizeof(data->path[(int)PathRole::x]) - 1] = '\0'; \
  104. } while (0)
  105. DO_PATH(TitleButtonIcons);
  106. DO_PATH(WindowShadow);
  107. return buffer;
  108. }
  109. }