SystemTheme.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #define DO_COLOR(x) \
  61. data->color[(int)ColorRole::x] = get_color(#x)
  62. DO_COLOR(DesktopBackground);
  63. DO_COLOR(ThreedHighlight);
  64. DO_COLOR(ThreedShadow1);
  65. DO_COLOR(ThreedShadow2);
  66. DO_COLOR(HoverHighlight);
  67. DO_COLOR(Selection);
  68. DO_COLOR(SelectionText);
  69. DO_COLOR(InactiveSelection);
  70. DO_COLOR(InactiveSelectionText);
  71. DO_COLOR(Window);
  72. DO_COLOR(WindowText);
  73. DO_COLOR(Base);
  74. DO_COLOR(BaseText);
  75. DO_COLOR(Button);
  76. DO_COLOR(ButtonText);
  77. DO_COLOR(DesktopBackground);
  78. DO_COLOR(ActiveWindowBorder1);
  79. DO_COLOR(ActiveWindowBorder2);
  80. DO_COLOR(ActiveWindowTitle);
  81. DO_COLOR(InactiveWindowBorder1);
  82. DO_COLOR(InactiveWindowBorder2);
  83. DO_COLOR(InactiveWindowTitle);
  84. DO_COLOR(MovingWindowBorder1);
  85. DO_COLOR(MovingWindowBorder2);
  86. DO_COLOR(MovingWindowTitle);
  87. DO_COLOR(HighlightWindowBorder1);
  88. DO_COLOR(HighlightWindowBorder2);
  89. DO_COLOR(HighlightWindowTitle);
  90. DO_COLOR(MenuStripe);
  91. DO_COLOR(MenuBase);
  92. DO_COLOR(MenuBaseText);
  93. DO_COLOR(MenuSelection);
  94. DO_COLOR(MenuSelectionText);
  95. DO_COLOR(RubberBandFill);
  96. DO_COLOR(RubberBandBorder);
  97. DO_COLOR(Link);
  98. DO_COLOR(ActiveLink);
  99. DO_COLOR(VisitedLink);
  100. DO_COLOR(Ruler);
  101. DO_COLOR(RulerBorder);
  102. DO_COLOR(RulerActiveText);
  103. DO_COLOR(RulerInactiveText);
  104. DO_COLOR(TextCursor);
  105. DO_COLOR(FocusOutline);
  106. DO_COLOR(SyntaxComment);
  107. DO_COLOR(SyntaxNumber);
  108. DO_COLOR(SyntaxString);
  109. DO_COLOR(SyntaxType);
  110. DO_COLOR(SyntaxPunctuation);
  111. DO_COLOR(SyntaxOperator);
  112. DO_COLOR(SyntaxKeyword);
  113. DO_COLOR(SyntaxControlKeyword);
  114. DO_COLOR(SyntaxIdentifier);
  115. DO_COLOR(SyntaxPreprocessorStatement);
  116. DO_COLOR(SyntaxPreprocessorValue);
  117. buffer->seal();
  118. buffer->share_globally();
  119. return buffer;
  120. }
  121. }