From 89b8d346fe4e339ac80882d43062d03a71a0d5df Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 2 Feb 2023 17:07:49 +0000 Subject: LibGUI+About: Make AboutDialog creation fallible --- Userland/Libraries/LibGUI/AboutDialog.cpp | 51 ++++++++++++++++------------- Userland/Libraries/LibGUI/AboutDialog.h | 8 +++-- Userland/Libraries/LibGUI/CommonActions.cpp | 2 +- 3 files changed, 35 insertions(+), 26 deletions(-) (limited to 'Userland/Libraries') diff --git a/Userland/Libraries/LibGUI/AboutDialog.cpp b/Userland/Libraries/LibGUI/AboutDialog.cpp index 27a6e56f63..5d57775221 100644 --- a/Userland/Libraries/LibGUI/AboutDialog.cpp +++ b/Userland/Libraries/LibGUI/AboutDialog.cpp @@ -19,40 +19,47 @@ namespace GUI { -AboutDialog::AboutDialog(StringView name, StringView version, Gfx::Bitmap const* icon, Window* parent_window) - : Dialog(parent_window) - , m_name(name) - , m_icon(icon) - , m_version_string(version) +ErrorOr> AboutDialog::try_create(StringView name, StringView version, Gfx::Bitmap const* icon, Window* parent_window) { - resize(413, 204); - set_title(DeprecatedString::formatted("About {}", m_name)); - set_resizable(false); - - if (parent_window) - set_icon(parent_window->icon()); + 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 = set_main_widget().release_value_but_fixme_should_propagate_errors(); - widget->load_from_gml(about_dialog_gml).release_value_but_fixme_should_propagate_errors(); + auto widget = TRY(dialog->set_main_widget()); + TRY(widget->load_from_gml(about_dialog_gml)); - auto icon_wrapper = find_descendant_of_type_named("icon_wrapper"); + auto icon_wrapper = widget->find_descendant_of_type_named("icon_wrapper"); if (icon) { icon_wrapper->set_visible(true); - auto icon_image = find_descendant_of_type_named("icon"); - icon_image->set_bitmap(m_icon); + auto icon_image = widget->find_descendant_of_type_named("icon"); + icon_image->set_bitmap(icon); } else { icon_wrapper->set_visible(false); } - find_descendant_of_type_named("name")->set_text(m_name); + widget->find_descendant_of_type_named("name")->set_text(name); // If we are displaying a dialog for an application, insert 'SerenityOS' below the application name - find_descendant_of_type_named("serenity_os")->set_visible(m_name != "SerenityOS"); - find_descendant_of_type_named("version")->set_text(m_version_string); + widget->find_descendant_of_type_named("serenity_os")->set_visible(name != "SerenityOS"); + widget->find_descendant_of_type_named("version")->set_text(version); - auto ok_button = find_descendant_of_type_named("ok_button"); - ok_button->on_click = [this](auto) { - done(ExecResult::OK); + auto ok_button = widget->find_descendant_of_type_named("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) + , m_icon(icon) + , m_version_string(version) +{ + resize(413, 204); + set_resizable(false); + + if (parent_window) + set_icon(parent_window->icon()); } } diff --git a/Userland/Libraries/LibGUI/AboutDialog.h b/Userland/Libraries/LibGUI/AboutDialog.h index 528563225a..92c2f4b233 100644 --- a/Userland/Libraries/LibGUI/AboutDialog.h +++ b/Userland/Libraries/LibGUI/AboutDialog.h @@ -13,16 +13,18 @@ namespace GUI { class AboutDialog final : public Dialog { - C_OBJECT(AboutDialog) + C_OBJECT_ABSTRACT(AboutDialog) public: + static ErrorOr> 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 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: diff --git a/Userland/Libraries/LibGUI/CommonActions.cpp b/Userland/Libraries/LibGUI/CommonActions.cpp index e58fe7dba1..437de549ce 100644 --- a/Userland/Libraries/LibGUI/CommonActions.cpp +++ b/Userland/Libraries/LibGUI/CommonActions.cpp @@ -21,7 +21,7 @@ NonnullRefPtr make_about_action(DeprecatedString const& app_name, Icon c { auto weak_parent = AK::make_weak_ptr_if_nonnull(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; -- cgit v1.2.3