SystemTheme.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <LibCore/CConfigFile.h>
  2. #include <LibDraw/SystemTheme.h>
  3. static SystemTheme dummy_theme;
  4. static const SystemTheme* theme_page = &dummy_theme;
  5. static RefPtr<SharedBuffer> theme_buffer;
  6. const SystemTheme& current_system_theme()
  7. {
  8. ASSERT(theme_page);
  9. return *theme_page;
  10. }
  11. int current_system_theme_buffer_id()
  12. {
  13. ASSERT(theme_buffer);
  14. return theme_buffer->shared_buffer_id();
  15. }
  16. void set_system_theme(SharedBuffer& buffer)
  17. {
  18. theme_buffer = buffer;
  19. theme_page = (SystemTheme*)theme_buffer->data();
  20. }
  21. RefPtr<SharedBuffer> load_system_theme(const String& path)
  22. {
  23. auto file = CConfigFile::open(path);
  24. auto buffer = SharedBuffer::create_with_size(sizeof(SystemTheme));
  25. auto* data = (SystemTheme*)buffer->data();
  26. auto get_color = [&](auto& name) {
  27. auto color_string = file->read_entry("Colors", name);
  28. auto color = Color::from_string(color_string);
  29. if (!color.has_value())
  30. return Color(Color::Black);
  31. return color.value();
  32. };
  33. #define DO_COLOR(x) \
  34. data->color[(int)ColorRole::x] = get_color(#x)
  35. DO_COLOR(DesktopBackground);
  36. DO_COLOR(ThreedHighlight);
  37. DO_COLOR(ThreedShadow1);
  38. DO_COLOR(ThreedShadow2);
  39. DO_COLOR(HoverHighlight);
  40. DO_COLOR(Selection);
  41. DO_COLOR(SelectionText);
  42. DO_COLOR(Window);
  43. DO_COLOR(WindowText);
  44. DO_COLOR(Base);
  45. DO_COLOR(BaseText);
  46. DO_COLOR(Button);
  47. DO_COLOR(ButtonText);
  48. DO_COLOR(DesktopBackground);
  49. DO_COLOR(ActiveWindowBorder1);
  50. DO_COLOR(ActiveWindowBorder2);
  51. DO_COLOR(ActiveWindowTitle);
  52. DO_COLOR(InactiveWindowBorder1);
  53. DO_COLOR(InactiveWindowBorder2);
  54. DO_COLOR(InactiveWindowTitle);
  55. DO_COLOR(MovingWindowBorder1);
  56. DO_COLOR(MovingWindowBorder2);
  57. DO_COLOR(MovingWindowTitle);
  58. DO_COLOR(HighlightWindowBorder1);
  59. DO_COLOR(HighlightWindowBorder2);
  60. DO_COLOR(HighlightWindowTitle);
  61. DO_COLOR(MenuStripe);
  62. DO_COLOR(MenuBase);
  63. DO_COLOR(MenuBaseText);
  64. DO_COLOR(MenuSelection);
  65. DO_COLOR(MenuSelectionText);
  66. DO_COLOR(RubberBandFill);
  67. DO_COLOR(RubberBandBorder);
  68. DO_COLOR(Link);
  69. DO_COLOR(ActiveLink);
  70. DO_COLOR(VisitedLink);
  71. buffer->seal();
  72. buffer->share_globally();
  73. return buffer;
  74. }