summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/AboutDialog.cpp
blob: b1e3d31fee5be63ce747341c40f7f78b06cc1286 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/StringBuilder.h>
#include <LibGUI/AboutDialog.h>
#include <LibGUI/AboutDialogGML.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
#include <LibGUI/ImageWidget.h>
#include <LibGUI/Label.h>
#include <LibGUI/Widget.h>
#include <LibGfx/Font/Font.h>
#include <LibGfx/Font/FontDatabase.h>

namespace GUI {

ErrorOr<NonnullRefPtr<AboutDialog>> AboutDialog::try_create(String name, String version, RefPtr<Gfx::Bitmap const> icon, Window* parent_window)
{
    auto dialog = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) AboutDialog(name, version, icon, parent_window)));
    dialog->set_title(DeprecatedString::formatted("About {}", name));

    auto widget = TRY(dialog->set_main_widget<Widget>());
    TRY(widget->load_from_gml(about_dialog_gml));

    auto icon_wrapper = widget->find_descendant_of_type_named<Widget>("icon_wrapper");
    if (icon) {
        icon_wrapper->set_visible(true);
        auto icon_image = widget->find_descendant_of_type_named<ImageWidget>("icon");
        icon_image->set_bitmap(icon);
    } else {
        icon_wrapper->set_visible(false);
    }

    widget->find_descendant_of_type_named<GUI::Label>("name")->set_text(name);
    // If we are displaying a dialog for an application, insert 'SerenityOS' below the application name
    widget->find_descendant_of_type_named<GUI::Label>("serenity_os")->set_visible(name != "SerenityOS");
    widget->find_descendant_of_type_named<GUI::Label>("version")->set_text(version);

    auto ok_button = widget->find_descendant_of_type_named<DialogButton>("ok_button");
    ok_button->on_click = [dialog](auto) {
        dialog->done(ExecResult::OK);
    };

    return dialog;
}

AboutDialog::AboutDialog(String name, String version, RefPtr<Gfx::Bitmap const> icon, Window* parent_window)
    : Dialog(parent_window)
    , m_name(move(name))
    , m_version_string(move(version))
    , m_icon(move(icon))
{
    resize(413, 204);
    set_resizable(false);

    if (parent_window)
        set_icon(parent_window->icon());
}

ErrorOr<void> AboutDialog::show(String name, String version, RefPtr<Gfx::Bitmap const> icon, Window* parent_window, RefPtr<Gfx::Bitmap const> window_icon)
{
    auto dialog = TRY(AboutDialog::try_create(move(name), move(version), move(icon), parent_window));
    if (window_icon)
        dialog->set_icon(window_icon);
    dialog->exec();
    return {};
}

}