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