SystemTheme.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. namespace Gfx {
  30. static SystemTheme dummy_theme;
  31. static const SystemTheme* theme_page = &dummy_theme;
  32. static RefPtr<SharedBuffer> theme_buffer;
  33. const SystemTheme& current_system_theme()
  34. {
  35. ASSERT(theme_page);
  36. return *theme_page;
  37. }
  38. int current_system_theme_buffer_id()
  39. {
  40. ASSERT(theme_buffer);
  41. return theme_buffer->shbuf_id();
  42. }
  43. void set_system_theme(SharedBuffer& buffer)
  44. {
  45. theme_buffer = buffer;
  46. theme_page = (SystemTheme*)theme_buffer->data();
  47. }
  48. RefPtr<SharedBuffer> load_system_theme(const String& path)
  49. {
  50. auto file = Core::ConfigFile::open(path);
  51. auto buffer = SharedBuffer::create_with_size(sizeof(SystemTheme));
  52. auto* data = (SystemTheme*)buffer->data();
  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. dbg() << "Metric " << name << " has no fallback value!";
  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. #define DO_COLOR(x) \
  90. data->color[(int)ColorRole::x] = get_color(#x)
  91. DO_COLOR(DesktopBackground);
  92. DO_COLOR(ThreedHighlight);
  93. DO_COLOR(ThreedShadow1);
  94. DO_COLOR(ThreedShadow2);
  95. DO_COLOR(HoverHighlight);
  96. DO_COLOR(Selection);
  97. DO_COLOR(SelectionText);
  98. DO_COLOR(InactiveSelection);
  99. DO_COLOR(InactiveSelectionText);
  100. DO_COLOR(Window);
  101. DO_COLOR(WindowText);
  102. DO_COLOR(Base);
  103. DO_COLOR(BaseText);
  104. DO_COLOR(Button);
  105. DO_COLOR(ButtonText);
  106. DO_COLOR(DesktopBackground);
  107. DO_COLOR(ActiveWindowBorder1);
  108. DO_COLOR(ActiveWindowBorder2);
  109. DO_COLOR(ActiveWindowTitle);
  110. DO_COLOR(ActiveWindowTitleShadow);
  111. DO_COLOR(ActiveWindowTitleStripes);
  112. DO_COLOR(InactiveWindowBorder1);
  113. DO_COLOR(InactiveWindowBorder2);
  114. DO_COLOR(InactiveWindowTitle);
  115. DO_COLOR(InactiveWindowTitleShadow);
  116. DO_COLOR(InactiveWindowTitleStripes);
  117. DO_COLOR(MovingWindowBorder1);
  118. DO_COLOR(MovingWindowBorder2);
  119. DO_COLOR(MovingWindowTitle);
  120. DO_COLOR(MovingWindowTitleShadow);
  121. DO_COLOR(MovingWindowTitleStripes);
  122. DO_COLOR(HighlightWindowBorder1);
  123. DO_COLOR(HighlightWindowBorder2);
  124. DO_COLOR(HighlightWindowTitle);
  125. DO_COLOR(HighlightWindowTitleShadow);
  126. DO_COLOR(HighlightWindowTitleStripes);
  127. DO_COLOR(MenuStripe);
  128. DO_COLOR(MenuBase);
  129. DO_COLOR(MenuBaseText);
  130. DO_COLOR(MenuSelection);
  131. DO_COLOR(MenuSelectionText);
  132. DO_COLOR(RubberBandFill);
  133. DO_COLOR(RubberBandBorder);
  134. DO_COLOR(Link);
  135. DO_COLOR(ActiveLink);
  136. DO_COLOR(VisitedLink);
  137. DO_COLOR(Ruler);
  138. DO_COLOR(RulerBorder);
  139. DO_COLOR(RulerActiveText);
  140. DO_COLOR(RulerInactiveText);
  141. DO_COLOR(TextCursor);
  142. DO_COLOR(FocusOutline);
  143. DO_COLOR(SyntaxComment);
  144. DO_COLOR(SyntaxNumber);
  145. DO_COLOR(SyntaxString);
  146. DO_COLOR(SyntaxType);
  147. DO_COLOR(SyntaxPunctuation);
  148. DO_COLOR(SyntaxOperator);
  149. DO_COLOR(SyntaxKeyword);
  150. DO_COLOR(SyntaxControlKeyword);
  151. DO_COLOR(SyntaxIdentifier);
  152. DO_COLOR(SyntaxPreprocessorStatement);
  153. DO_COLOR(SyntaxPreprocessorValue);
  154. #define DO_METRIC(x) \
  155. data->metric[(int)MetricRole::x] = get_metric(#x, (int)MetricRole::x)
  156. DO_METRIC(TitleHeight);
  157. DO_METRIC(TitleButtonWidth);
  158. DO_METRIC(TitleButtonHeight);
  159. #define DO_PATH(x) \
  160. data->path[(int)PathRole::x] = get_path(#x, (int)PathRole::x)
  161. DO_PATH(TitleButtonIcons);
  162. buffer->seal();
  163. buffer->share_globally();
  164. return buffer;
  165. }
  166. }