AbstractThemePreview.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, 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 <LibGUI/AbstractThemePreview.h>
  14. #include <LibGUI/Painter.h>
  15. #include <LibGfx/Bitmap.h>
  16. namespace GUI {
  17. AbstractThemePreview::AbstractThemePreview(Gfx::Palette const& preview_palette)
  18. : m_preview_palette(preview_palette)
  19. {
  20. m_active_window_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window.png"sv).release_value_but_fixme_should_propagate_errors();
  21. m_inactive_window_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window.png"sv).release_value_but_fixme_should_propagate_errors();
  22. m_default_close_bitmap = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-close.png"sv).release_value_but_fixme_should_propagate_errors();
  23. m_default_maximize_bitmap = Gfx::Bitmap::load_from_file("/res/icons/16x16/upward-triangle.png"sv).release_value_but_fixme_should_propagate_errors();
  24. m_default_minimize_bitmap = Gfx::Bitmap::load_from_file("/res/icons/16x16/downward-triangle.png"sv).release_value_but_fixme_should_propagate_errors();
  25. VERIFY(m_active_window_icon);
  26. VERIFY(m_inactive_window_icon);
  27. VERIFY(m_default_close_bitmap);
  28. VERIFY(m_default_maximize_bitmap);
  29. VERIFY(m_default_minimize_bitmap);
  30. load_theme_bitmaps();
  31. }
  32. void AbstractThemePreview::load_theme_bitmaps()
  33. {
  34. auto load_bitmap = [](DeprecatedString const& path, DeprecatedString& last_path, RefPtr<Gfx::Bitmap>& bitmap) {
  35. if (path.is_empty()) {
  36. last_path = DeprecatedString::empty();
  37. bitmap = nullptr;
  38. } else if (last_path != path) {
  39. auto bitmap_or_error = Gfx::Bitmap::load_from_file(path);
  40. if (bitmap_or_error.is_error()) {
  41. last_path = DeprecatedString::empty();
  42. bitmap = nullptr;
  43. } else {
  44. last_path = path;
  45. bitmap = bitmap_or_error.release_value();
  46. }
  47. }
  48. };
  49. auto buttons_path = m_preview_palette.title_button_icons_path();
  50. load_bitmap(LexicalPath::absolute_path(buttons_path, "window-close.png"), m_last_close_path, m_close_bitmap);
  51. load_bitmap(LexicalPath::absolute_path(buttons_path, "window-maximize.png"), m_last_maximize_path, m_maximize_bitmap);
  52. load_bitmap(LexicalPath::absolute_path(buttons_path, "window-minimize.png"), m_last_minimize_path, m_minimize_bitmap);
  53. load_bitmap(m_preview_palette.active_window_shadow_path(), m_last_active_window_shadow_path, m_active_window_shadow);
  54. load_bitmap(m_preview_palette.inactive_window_shadow_path(), m_last_inactive_window_shadow_path, m_inactive_window_shadow);
  55. load_bitmap(m_preview_palette.menu_shadow_path(), m_last_menu_shadow_path, m_menu_shadow);
  56. load_bitmap(m_preview_palette.taskbar_shadow_path(), m_last_taskbar_shadow_path, m_taskbar_shadow);
  57. load_bitmap(m_preview_palette.tooltip_shadow_path(), m_last_tooltip_shadow_path, m_tooltip_shadow);
  58. }
  59. void AbstractThemePreview::set_preview_palette(Gfx::Palette& palette)
  60. {
  61. m_preview_palette = palette;
  62. palette_changed();
  63. if (on_palette_change)
  64. on_palette_change();
  65. load_theme_bitmaps();
  66. update();
  67. }
  68. void AbstractThemePreview::set_theme(Core::AnonymousBuffer const& theme_buffer)
  69. {
  70. VERIFY(theme_buffer.is_valid());
  71. m_preview_palette = Gfx::Palette(Gfx::PaletteImpl::create_with_anonymous_buffer(theme_buffer));
  72. set_preview_palette(m_preview_palette);
  73. }
  74. ErrorOr<void> AbstractThemePreview::set_theme_from_file(StringView path, NonnullOwnPtr<Core::File> file)
  75. {
  76. auto config_file = TRY(Core::ConfigFile::open(path, move(file)));
  77. auto theme = TRY(Gfx::load_system_theme(config_file));
  78. m_preview_palette = Gfx::Palette(Gfx::PaletteImpl::create_with_anonymous_buffer(theme));
  79. set_preview_palette(m_preview_palette);
  80. return {};
  81. }
  82. void AbstractThemePreview::paint_window(StringView title, Gfx::IntRect const& rect, Gfx::WindowTheme::WindowState state, Gfx::Bitmap const& icon, int button_count)
  83. {
  84. GUI::Painter painter(*this);
  85. struct Button {
  86. Gfx::IntRect rect;
  87. RefPtr<Gfx::Bitmap> bitmap;
  88. };
  89. int window_button_width = m_preview_palette.window_title_button_width();
  90. int window_button_height = m_preview_palette.window_title_button_height();
  91. auto titlebar_text_rect = Gfx::WindowTheme::current().titlebar_text_rect(Gfx::WindowTheme::WindowType::Normal, Gfx::WindowTheme::WindowMode::Other, rect, m_preview_palette);
  92. int pos = titlebar_text_rect.right();
  93. Array possible_buttons {
  94. Button { {}, m_close_bitmap.is_null() ? m_default_close_bitmap : m_close_bitmap },
  95. Button { {}, m_maximize_bitmap.is_null() ? m_default_maximize_bitmap : m_maximize_bitmap },
  96. Button { {}, m_minimize_bitmap.is_null() ? m_default_minimize_bitmap : m_minimize_bitmap }
  97. };
  98. auto buttons = possible_buttons.span().trim(button_count);
  99. for (auto& button : buttons) {
  100. pos -= window_button_width;
  101. Gfx::IntRect button_rect { pos, 0, window_button_width, window_button_height };
  102. button_rect.center_vertically_within(titlebar_text_rect);
  103. button.rect = button_rect;
  104. }
  105. auto frame_rect = Gfx::WindowTheme::current().frame_rect_for_window(Gfx::WindowTheme::WindowType::Normal, Gfx::WindowTheme::WindowMode::Other, rect, m_preview_palette, 0);
  106. auto paint_shadow = [](Gfx::Painter& painter, Gfx::IntRect& frame_rect, Gfx::Bitmap const& shadow_bitmap) {
  107. auto total_shadow_size = shadow_bitmap.height();
  108. auto shadow_rect = frame_rect.inflated(total_shadow_size, total_shadow_size);
  109. Gfx::StylePainter::paint_simple_rect_shadow(painter, shadow_rect, shadow_bitmap);
  110. };
  111. if ((state == Gfx::WindowTheme::WindowState::Active || state == Gfx::WindowTheme::WindowState::Highlighted) && m_active_window_shadow) {
  112. paint_shadow(painter, frame_rect, *m_active_window_shadow);
  113. } else if (state == Gfx::WindowTheme::WindowState::Inactive && m_inactive_window_shadow) {
  114. paint_shadow(painter, frame_rect, *m_inactive_window_shadow);
  115. }
  116. Gfx::PainterStateSaver saver(painter);
  117. painter.translate(frame_rect.location());
  118. Gfx::WindowTheme::current().paint_normal_frame(painter, state, Gfx::WindowTheme::WindowMode::Other, rect, title, icon, m_preview_palette, buttons.last().rect, 0, false);
  119. painter.fill_rect(rect.translated(-frame_rect.location()), m_preview_palette.color(Gfx::ColorRole::Background));
  120. for (auto& button : buttons) {
  121. if (!m_preview_palette.title_buttons_icon_only())
  122. Gfx::StylePainter::paint_button(painter, button.rect, m_preview_palette, Gfx::ButtonStyle::Normal, false);
  123. auto bitmap_rect = button.bitmap->rect().centered_within(button.rect);
  124. painter.blit(bitmap_rect.location(), *button.bitmap, button.bitmap->rect());
  125. }
  126. }
  127. void AbstractThemePreview::paint_event(GUI::PaintEvent& event)
  128. {
  129. GUI::Frame::paint_event(event);
  130. GUI::Painter painter(*this);
  131. painter.add_clip_rect(event.rect());
  132. painter.add_clip_rect(frame_inner_rect());
  133. painter.fill_rect(frame_inner_rect(), m_preview_palette.desktop_background());
  134. paint_preview(event);
  135. }
  136. void AbstractThemePreview::center_window_group_within(Span<Window> windows, Gfx::IntRect const& bounds)
  137. {
  138. VERIFY(windows.size() >= 1);
  139. auto to_frame_rect = [&](Gfx::IntRect const& rect) {
  140. return Gfx::WindowTheme::current().frame_rect_for_window(
  141. Gfx::WindowTheme::WindowType::Normal, Gfx::WindowTheme::WindowMode::Other, rect, m_preview_palette, 0);
  142. };
  143. auto leftmost_x_value = windows[0].rect.x();
  144. auto topmost_y_value = windows[0].rect.y();
  145. auto combind_frame_rect = to_frame_rect(windows[0].rect);
  146. for (auto& window : windows.slice(1)) {
  147. if (window.rect.x() < leftmost_x_value)
  148. leftmost_x_value = window.rect.x();
  149. if (window.rect.y() < topmost_y_value)
  150. topmost_y_value = window.rect.y();
  151. combind_frame_rect = combind_frame_rect.united(to_frame_rect(window.rect));
  152. }
  153. combind_frame_rect.center_within(bounds);
  154. auto frame_offset = to_frame_rect({}).location();
  155. for (auto& window : windows) {
  156. window.rect.set_left(combind_frame_rect.left() + (window.rect.x() - leftmost_x_value) - frame_offset.x());
  157. window.rect.set_top(combind_frame_rect.top() + (window.rect.y() - topmost_y_value) - frame_offset.y());
  158. }
  159. }
  160. }