Serenity Operating System
at master 152 lines 4.9 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 "PreviewWidget.h" 11#include <AK/LexicalPath.h> 12#include <AK/StringView.h> 13#include <Applications/ThemeEditor/WindowPreviewGML.h> 14#include <LibCore/MimeData.h> 15#include <LibFileSystemAccessClient/Client.h> 16#include <LibGUI/MessageBox.h> 17#include <LibGUI/Painter.h> 18#include <LibGfx/Bitmap.h> 19#include <LibGfx/WindowTheme.h> 20 21REGISTER_WIDGET(ThemeEditor, PreviewWidget); 22 23namespace ThemeEditor { 24 25class MiniWidgetGallery final : public GUI::Widget { 26 C_OBJECT_ABSTRACT(MiniWidgetGallery); 27 28public: 29 static ErrorOr<NonnullRefPtr<MiniWidgetGallery>> try_create() 30 { 31 auto gallery = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) MiniWidgetGallery())); 32 TRY(gallery->load_from_gml(window_preview_gml)); 33 34 gallery->for_each_child_widget([](auto& child) { 35 child.set_focus_policy(GUI::FocusPolicy::NoFocus); 36 return IterationDecision::Continue; 37 }); 38 39 return gallery; 40 } 41 42 void set_preview_palette(Gfx::Palette& palette) 43 { 44 set_palette(palette); 45 Function<void(GUI::Widget&)> recurse = [&](GUI::Widget& parent_widget) { 46 parent_widget.for_each_child_widget([&](auto& widget) { 47 widget.set_palette(palette); 48 recurse(widget); 49 return IterationDecision::Continue; 50 }); 51 }; 52 recurse(*this); 53 } 54 55private: 56 MiniWidgetGallery() 57 { 58 } 59}; 60 61ErrorOr<NonnullRefPtr<PreviewWidget>> PreviewWidget::try_create() 62{ 63 auto preview_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) PreviewWidget())); 64 preview_widget->m_gallery = TRY(preview_widget->try_add<MiniWidgetGallery>()); 65 return preview_widget; 66} 67 68PreviewWidget::PreviewWidget() 69 : GUI::AbstractThemePreview(GUI::Application::the()->palette()) 70{ 71 set_greedy_for_hits(true); 72} 73 74void PreviewWidget::palette_changed() 75{ 76 m_gallery->set_preview_palette(preview_palette()); 77 update_preview_window_locations(); 78} 79 80void PreviewWidget::set_color_filter(OwnPtr<Gfx::ColorBlindnessFilter> color_filter) 81{ 82 m_color_filter = move(color_filter); 83 repaint(); 84} 85 86void PreviewWidget::update_preview_window_locations() 87{ 88 auto& palette = preview_palette(); 89 int window_title_height = palette.metric(Gfx::MetricRole::TitleHeight) 90 + palette.metric(Gfx::MetricRole::BorderThickness); 91 92 constexpr int inactive_offset_x = -20; 93 int inactive_offset_y = -(window_title_height + 4); 94 constexpr int hightlight_offset_x = 140; 95 int hightlight_offset_y = window_title_height + 40; 96 97 m_active_window_rect = Gfx::IntRect(0, 0, 320, 220); 98 m_inactive_window_rect = m_active_window_rect.translated(inactive_offset_x, inactive_offset_y); 99 m_highlight_window_rect = Gfx::IntRect(m_active_window_rect.location(), { 160, 70 }).translated(hightlight_offset_x, hightlight_offset_y); 100 101 auto window_group = Array { 102 Window { m_active_window_rect }, 103 Window { m_inactive_window_rect }, 104 Window { m_highlight_window_rect }, 105 }; 106 107 center_window_group_within(window_group, frame_inner_rect()); 108 109 m_gallery->set_relative_rect(m_active_window_rect); 110} 111 112void PreviewWidget::paint_preview(GUI::PaintEvent&) 113{ 114 paint_window("Inactive window"sv, m_inactive_window_rect, Gfx::WindowTheme::WindowState::Inactive, active_window_icon()); 115 paint_window("Active window"sv, m_active_window_rect, Gfx::WindowTheme::WindowState::Active, inactive_window_icon()); 116} 117 118void PreviewWidget::paint_hightlight_window() 119{ 120 GUI::Painter painter(*this); 121 paint_window("Highlight window"sv, m_highlight_window_rect, Gfx::WindowTheme::WindowState::Highlighted, inactive_window_icon(), 1); 122 auto button_rect = Gfx::IntRect(0, 0, 80, 22).centered_within(m_highlight_window_rect); 123 Gfx::StylePainter::paint_button(painter, button_rect, preview_palette(), Gfx::ButtonStyle::Normal, false, false, false, true, false, false); 124 painter.draw_text(button_rect, ":^)"sv, Gfx::TextAlignment::Center, preview_palette().color(foreground_role()), Gfx::TextElision::Right, Gfx::TextWrapping::DontWrap); 125} 126 127void PreviewWidget::second_paint_event(GUI::PaintEvent&) 128{ 129 GUI::Painter painter(*this); 130 131 paint_hightlight_window(); 132 133 if (!m_color_filter) 134 return; 135 136 auto target = painter.target(); 137 auto bitmap_clone_or_error = target->clone(); 138 if (bitmap_clone_or_error.is_error()) 139 return; 140 141 auto clone = bitmap_clone_or_error.release_value(); 142 auto rect = target->rect(); 143 144 m_color_filter->apply(*target, rect, *clone, rect); 145} 146 147void PreviewWidget::resize_event(GUI::ResizeEvent&) 148{ 149 update_preview_window_locations(); 150} 151 152}