summaryrefslogtreecommitdiff
path: root/Userland/Applications/DisplaySettings/ThemePreviewWidget.cpp
blob: 35abe94a0354328962890a209e0d7a04667b8cf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 * Copyright (c) 2022, MacDue <macdue@dueutil.tech>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "ThemePreviewWidget.h"
#include <AK/Array.h>
#include <LibGUI/Painter.h>
#include <LibGfx/Painter.h>
#include <LibGfx/StylePainter.h>

namespace DisplaySettings {

ThemePreviewWidget::ThemePreviewWidget(Gfx::Palette const& palette)
    : GUI::AbstractThemePreview(palette)
{
    set_fixed_size(304, 201);
}

ErrorOr<void> ThemePreviewWidget::set_theme(DeprecatedString path)
{
    auto config_file = TRY(Core::Stream::File::open(path, Core::Stream::OpenMode::Read));
    TRY(set_theme_from_file(path, move(config_file)));
    return {};
}

void ThemePreviewWidget::paint_preview(GUI::PaintEvent&)
{
    GUI::Painter painter(*this);

    auto active_window_rect = frame_inner_rect().shrunken(48, 100);
    auto inactive_window_rect = active_window_rect.translated(-8, -32);
    auto message_box = active_window_rect.shrunken(100, 60);

    Array window_group {
        Window { active_window_rect },
        Window { inactive_window_rect },
        Window { message_box }
    };
    center_window_group_within(window_group, frame_inner_rect());

    paint_window("Inactive Window"sv, inactive_window_rect, Gfx::WindowTheme::WindowState::Inactive, inactive_window_icon());
    paint_window("Active Window"sv, active_window_rect, Gfx::WindowTheme::WindowState::Active, active_window_icon());
    paint_window("Alert"sv, message_box, Gfx::WindowTheme::WindowState::Highlighted, active_window_icon(), 1);

    auto draw_centered_button = [&](auto window_rect, auto text, int button_width, int button_height) {
        Gfx::IntRect button_rect { 0, 0, button_width, button_height };
        button_rect.center_within(window_rect);
        Gfx::StylePainter::paint_button(painter, button_rect, preview_palette(), Gfx::ButtonStyle::Normal, false, false, false, true, false, false);
        painter.draw_text(button_rect, text, Gfx::TextAlignment::Center, preview_palette().color(foreground_role()), Gfx::TextElision::Right, Gfx::TextWrapping::DontWrap);
    };

    draw_centered_button(message_box, "Ok"sv, 32, 16);
}

}