1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:57:44 +00:00

LibGUI+About: Make AboutDialog creation fallible

This commit is contained in:
Sam Atkins 2023-02-02 17:07:49 +00:00 committed by Sam Atkins
parent 65c8dfe923
commit 89b8d346fe
4 changed files with 37 additions and 28 deletions

View file

@ -21,6 +21,6 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
TRY(Core::System::unveil(nullptr, nullptr));
auto app_icon = TRY(GUI::Icon::try_create_default_icon("ladyball"sv));
GUI::AboutDialog::show("SerenityOS"sv, Core::Version::read_long_version_string(), app_icon.bitmap_for_size(32), nullptr, app_icon.bitmap_for_size(16));
TRY(GUI::AboutDialog::show("SerenityOS"sv, Core::Version::read_long_version_string(), app_icon.bitmap_for_size(32), nullptr, app_icon.bitmap_for_size(16)));
return app->exec();
}

View file

@ -19,6 +19,36 @@
namespace GUI {
ErrorOr<NonnullRefPtr<AboutDialog>> AboutDialog::try_create(StringView name, StringView version, 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(StringView name, StringView version, Gfx::Bitmap const* icon, Window* parent_window)
: Dialog(parent_window)
, m_name(name)
@ -26,33 +56,10 @@ AboutDialog::AboutDialog(StringView name, StringView version, Gfx::Bitmap const*
, m_version_string(version)
{
resize(413, 204);
set_title(DeprecatedString::formatted("About {}", m_name));
set_resizable(false);
if (parent_window)
set_icon(parent_window->icon());
auto widget = set_main_widget<Widget>().release_value_but_fixme_should_propagate_errors();
widget->load_from_gml(about_dialog_gml).release_value_but_fixme_should_propagate_errors();
auto icon_wrapper = find_descendant_of_type_named<Widget>("icon_wrapper");
if (icon) {
icon_wrapper->set_visible(true);
auto icon_image = find_descendant_of_type_named<ImageWidget>("icon");
icon_image->set_bitmap(m_icon);
} else {
icon_wrapper->set_visible(false);
}
find_descendant_of_type_named<GUI::Label>("name")->set_text(m_name);
// If we are displaying a dialog for an application, insert 'SerenityOS' below the application name
find_descendant_of_type_named<GUI::Label>("serenity_os")->set_visible(m_name != "SerenityOS");
find_descendant_of_type_named<GUI::Label>("version")->set_text(m_version_string);
auto ok_button = find_descendant_of_type_named<DialogButton>("ok_button");
ok_button->on_click = [this](auto) {
done(ExecResult::OK);
};
}
}

View file

@ -13,16 +13,18 @@
namespace GUI {
class AboutDialog final : public Dialog {
C_OBJECT(AboutDialog)
C_OBJECT_ABSTRACT(AboutDialog)
public:
static ErrorOr<NonnullRefPtr<AboutDialog>> try_create(StringView name, StringView version, Gfx::Bitmap const* icon = nullptr, Window* parent_window = nullptr);
virtual ~AboutDialog() override = default;
static void show(StringView name, StringView version, Gfx::Bitmap const* icon = nullptr, Window* parent_window = nullptr, Gfx::Bitmap const* window_icon = nullptr)
static ErrorOr<void> show(StringView name, StringView version, Gfx::Bitmap const* icon = nullptr, Window* parent_window = nullptr, Gfx::Bitmap const* window_icon = nullptr)
{
auto dialog = AboutDialog::construct(name, version, icon, parent_window);
auto dialog = TRY(AboutDialog::try_create(name, version, icon, parent_window));
if (window_icon)
dialog->set_icon(window_icon);
dialog->exec();
return {};
}
private:

View file

@ -21,7 +21,7 @@ NonnullRefPtr<Action> make_about_action(DeprecatedString const& app_name, Icon c
{
auto weak_parent = AK::make_weak_ptr_if_nonnull<Window>(parent);
auto action = Action::create(DeprecatedString::formatted("&About {}", app_name), app_icon.bitmap_for_size(16), [=](auto&) {
AboutDialog::show(app_name, Core::Version::read_long_version_string(), app_icon.bitmap_for_size(32), weak_parent.ptr());
AboutDialog::show(app_name, Core::Version::read_long_version_string(), app_icon.bitmap_for_size(32), weak_parent.ptr()).release_value_but_fixme_should_propagate_errors();
});
action->set_status_tip("Show application about box");
return action;