SystemTheme.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 <AK/SharedBuffer.h>
  27. #include <LibCore/ConfigFile.h>
  28. #include <LibGfx/SystemTheme.h>
  29. #include <string.h>
  30. namespace Gfx {
  31. static SystemTheme dummy_theme;
  32. static const SystemTheme* theme_page = &dummy_theme;
  33. static RefPtr<SharedBuffer> theme_buffer;
  34. const SystemTheme& current_system_theme()
  35. {
  36. ASSERT(theme_page);
  37. return *theme_page;
  38. }
  39. int current_system_theme_buffer_id()
  40. {
  41. ASSERT(theme_buffer);
  42. return theme_buffer->shbuf_id();
  43. }
  44. void set_system_theme(SharedBuffer& buffer)
  45. {
  46. theme_buffer = buffer;
  47. theme_page = theme_buffer->data<SystemTheme>();
  48. }
  49. RefPtr<SharedBuffer> load_system_theme(const String& path)
  50. {
  51. auto file = Core::ConfigFile::open(path);
  52. auto buffer = SharedBuffer::create_with_size(sizeof(SystemTheme));
  53. auto* data = buffer->data<SystemTheme>();
  54. auto get_color = [&](auto& name) {
  55. auto color_string = file->read_entry("Colors", name);
  56. auto color = Color::from_string(color_string);
  57. if (!color.has_value())
  58. return Color(Color::Black);
  59. return color.value();
  60. };
  61. auto get_metric = [&](auto& name, auto role) {
  62. int metric = file->read_num_entry("Metrics", name, -1);
  63. if (metric == -1) {
  64. switch (role) {
  65. case (int)MetricRole::TitleHeight:
  66. return 19;
  67. case (int)MetricRole::TitleButtonHeight:
  68. return 15;
  69. case (int)MetricRole::TitleButtonWidth:
  70. return 15;
  71. default:
  72. dbgln("Metric {} has no fallback value!", name);
  73. return 16;
  74. }
  75. }
  76. return metric;
  77. };
  78. auto get_path = [&](auto& name, auto role) {
  79. auto path = file->read_entry("Paths", name);
  80. if (path.is_empty()) {
  81. switch (role) {
  82. case (int)PathRole::TitleButtonIcons:
  83. return "/res/icons/16x16/";
  84. default:
  85. return "/res/";
  86. }
  87. }
  88. return &path[0];
  89. };
  90. #undef __ENUMERATE_COLOR_ROLE
  91. #define __ENUMERATE_COLOR_ROLE(role) \
  92. data->color[(int)ColorRole::role] = get_color(#role).value();
  93. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  94. #undef __ENUMERATE_COLOR_ROLE
  95. #define DO_METRIC(x) \
  96. data->metric[(int)MetricRole::x] = get_metric(#x, (int)MetricRole::x)
  97. DO_METRIC(TitleHeight);
  98. DO_METRIC(TitleButtonWidth);
  99. DO_METRIC(TitleButtonHeight);
  100. #define DO_PATH(x) \
  101. do { \
  102. auto path = get_path(#x, (int)PathRole::x); \
  103. memcpy(data->path[(int)PathRole::x], path, min(strlen(path) + 1, sizeof(data->path[(int)PathRole::x]))); \
  104. data->path[(int)PathRole::x][sizeof(data->path[(int)PathRole::x]) - 1] = '\0'; \
  105. } while (0)
  106. DO_PATH(TitleButtonIcons);
  107. buffer->seal();
  108. buffer->share_globally();
  109. return buffer;
  110. }
  111. }