AbstractThemePreview.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2021, Antonio Di Stefano <tonio9681@gmail.com>
  5. * Copyright (c) 2022, the SerenityOS developers.
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/Array.h>
  10. #include <AK/LexicalPath.h>
  11. #include <AK/StringView.h>
  12. #include <LibCore/ConfigFile.h>
  13. #include <LibCore/File.h>
  14. #include <LibGUI/AbstractThemePreview.h>
  15. #include <LibGUI/Painter.h>
  16. #include <LibGfx/Bitmap.h>
  17. namespace GUI {
  18. AbstractThemePreview::AbstractThemePreview(Gfx::Palette const& preview_palette)
  19. : m_preview_palette(preview_palette)
  20. {
  21. m_active_window_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/window.png").release_value_but_fixme_should_propagate_errors();
  22. m_inactive_window_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/window.png").release_value_but_fixme_should_propagate_errors();
  23. m_default_close_bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/window-close.png").release_value_but_fixme_should_propagate_errors();
  24. m_default_maximize_bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/upward-triangle.png").release_value_but_fixme_should_propagate_errors();
  25. m_default_minimize_bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/downward-triangle.png").release_value_but_fixme_should_propagate_errors();
  26. VERIFY(m_active_window_icon);
  27. VERIFY(m_inactive_window_icon);
  28. VERIFY(m_default_close_bitmap);
  29. VERIFY(m_default_maximize_bitmap);
  30. VERIFY(m_default_minimize_bitmap);
  31. load_theme_bitmaps();
  32. }
  33. void AbstractThemePreview::load_theme_bitmaps()
  34. {
  35. auto load_bitmap = [](String const& path, String& last_path, RefPtr<Gfx::Bitmap>& bitmap) {
  36. if (path.is_empty()) {
  37. last_path = String::empty();
  38. bitmap = nullptr;
  39. } else if (last_path != path) {
  40. auto bitmap_or_error = Gfx::Bitmap::try_load_from_file(path);
  41. if (bitmap_or_error.is_error()) {
  42. last_path = String::empty();
  43. bitmap = nullptr;
  44. } else {
  45. last_path = path;
  46. bitmap = bitmap_or_error.release_value();
  47. }
  48. }
  49. };
  50. auto buttons_path = m_preview_palette.title_button_icons_path();
  51. load_bitmap(LexicalPath::absolute_path(buttons_path, "window-close.png"), m_last_close_path, m_close_bitmap);
  52. load_bitmap(LexicalPath::absolute_path(buttons_path, "window-maximize.png"), m_last_maximize_path, m_maximize_bitmap);
  53. load_bitmap(LexicalPath::absolute_path(buttons_path, "window-minimize.png"), m_last_minimize_path, m_minimize_bitmap);
  54. load_bitmap(m_preview_palette.active_window_shadow_path(), m_last_active_window_shadow_path, m_active_window_shadow);
  55. load_bitmap(m_preview_palette.inactive_window_shadow_path(), m_last_inactive_window_shadow_path, m_inactive_window_shadow);
  56. load_bitmap(m_preview_palette.menu_shadow_path(), m_last_menu_shadow_path, m_menu_shadow);
  57. load_bitmap(m_preview_palette.taskbar_shadow_path(), m_last_taskbar_shadow_path, m_taskbar_shadow);
  58. load_bitmap(m_preview_palette.tooltip_shadow_path(), m_last_tooltip_shadow_path, m_tooltip_shadow);
  59. }
  60. void AbstractThemePreview::set_preview_palette(Gfx::Palette const& palette)
  61. {
  62. m_preview_palette = palette;
  63. if (on_palette_change) {
  64. on_palette_change();
  65. }
  66. load_theme_bitmaps();
  67. update();
  68. }
  69. void AbstractThemePreview::set_theme_from_file(Core::File& file)
  70. {
  71. auto config_file = Core::ConfigFile::open(file.filename(), file.leak_fd()).release_value_but_fixme_should_propagate_errors();
  72. auto theme = Gfx::load_system_theme(config_file);
  73. VERIFY(theme.is_valid());
  74. m_preview_palette = Gfx::Palette(Gfx::PaletteImpl::create_with_anonymous_buffer(theme));
  75. set_preview_palette(m_preview_palette);
  76. if (on_theme_load_from_file)
  77. on_theme_load_from_file(file.filename());
  78. }
  79. void AbstractThemePreview::paint_window(StringView title, Gfx::IntRect const& rect, Gfx::WindowTheme::WindowState state, Gfx::Bitmap const& icon, int button_count)
  80. {
  81. GUI::Painter painter(*this);
  82. struct Button {
  83. Gfx::IntRect rect;
  84. RefPtr<Gfx::Bitmap> bitmap;
  85. };
  86. int window_button_width = m_preview_palette.window_title_button_width();
  87. int window_button_height = m_preview_palette.window_title_button_height();
  88. auto titlebar_text_rect = Gfx::WindowTheme::current().titlebar_text_rect(Gfx::WindowTheme::WindowType::Normal, rect, m_preview_palette);
  89. int pos = titlebar_text_rect.right() + 1;
  90. Array possible_buttons {
  91. Button { {}, m_close_bitmap.is_null() ? m_default_close_bitmap : m_close_bitmap },
  92. Button { {}, m_maximize_bitmap.is_null() ? m_default_maximize_bitmap : m_maximize_bitmap },
  93. Button { {}, m_minimize_bitmap.is_null() ? m_default_minimize_bitmap : m_minimize_bitmap }
  94. };
  95. auto buttons = possible_buttons.span().trim(button_count);
  96. for (auto& button : buttons) {
  97. pos -= window_button_width;
  98. Gfx::IntRect button_rect { pos, 0, window_button_width, window_button_height };
  99. button_rect.center_vertically_within(titlebar_text_rect);
  100. button.rect = button_rect;
  101. }
  102. auto frame_rect = Gfx::WindowTheme::current().frame_rect_for_window(Gfx::WindowTheme::WindowType::Normal, rect, m_preview_palette, 0);
  103. auto paint_shadow = [](Gfx::Painter& painter, Gfx::IntRect& frame_rect, Gfx::Bitmap const& shadow_bitmap) {
  104. auto total_shadow_size = shadow_bitmap.height();
  105. auto shadow_rect = frame_rect.inflated(total_shadow_size, total_shadow_size);
  106. Gfx::StylePainter::paint_simple_rect_shadow(painter, shadow_rect, shadow_bitmap);
  107. };
  108. if ((state == Gfx::WindowTheme::WindowState::Active || state == Gfx::WindowTheme::WindowState::Highlighted) && m_active_window_shadow) {
  109. paint_shadow(painter, frame_rect, *m_active_window_shadow);
  110. } else if (state == Gfx::WindowTheme::WindowState::Inactive && m_inactive_window_shadow) {
  111. paint_shadow(painter, frame_rect, *m_inactive_window_shadow);
  112. }
  113. Gfx::PainterStateSaver saver(painter);
  114. painter.translate(frame_rect.location());
  115. Gfx::WindowTheme::current().paint_normal_frame(painter, state, rect, title, icon, m_preview_palette, buttons.last().rect, 0, false);
  116. painter.fill_rect(rect.translated(-frame_rect.location()), m_preview_palette.color(Gfx::ColorRole::Background));
  117. for (auto& button : buttons) {
  118. Gfx::StylePainter::paint_button(painter, button.rect, m_preview_palette, Gfx::ButtonStyle::Normal, false);
  119. auto bitmap_rect = button.bitmap->rect().centered_within(button.rect);
  120. painter.blit(bitmap_rect.location(), *button.bitmap, button.bitmap->rect());
  121. }
  122. }
  123. void AbstractThemePreview::paint_event(GUI::PaintEvent& event)
  124. {
  125. GUI::Frame::paint_event(event);
  126. GUI::Painter painter(*this);
  127. painter.add_clip_rect(event.rect());
  128. painter.add_clip_rect(frame_inner_rect());
  129. painter.fill_rect(frame_inner_rect(), m_preview_palette.desktop_background());
  130. paint_preview(event);
  131. }
  132. }