From 1a97382305f517dcc4c4f71746b6ab5d96012b0d Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Fri, 5 May 2023 00:24:53 -0400 Subject: LibGUI: Make `Application`'s construction fallible The pattern to construct `Application` was to use the `try_create` method from the `C_OBJECT` macro. While being safe from an OOM perspective, this method doesn't propagate errors from the constructor. This patch make `Application` use the `C_OBJECT_ABSTRACT` and manually define a `create` method that can bubble up errors from the construction stage. This commit also removes the ability to use `argc` and `argv` to create an `Application`, only `Main`'s `Arguments` can be used. From a user point of view, the patch renames `try_create` => `create`, hence the huge number of modified files. --- Userland/Libraries/LibGUI/Application.cpp | 44 ++++++++++++++++++------------- Userland/Libraries/LibGUI/Application.h | 10 +++---- 2 files changed, 29 insertions(+), 25 deletions(-) (limited to 'Userland/Libraries/LibGUI') diff --git a/Userland/Libraries/LibGUI/Application.cpp b/Userland/Libraries/LibGUI/Application.cpp index 1fc3c107a4..cbb4403493 100644 --- a/Userland/Libraries/LibGUI/Application.cpp +++ b/Userland/Libraries/LibGUI/Application.cpp @@ -68,37 +68,43 @@ Application* Application::the() return *s_the; } -Application::Application(int argc, char** argv) +ErrorOr> Application::create(Main::Arguments const& arguments) { - VERIFY(!*s_the); - *s_the = *this; - m_event_loop = make(); + if (*s_the) + return Error::from_string_literal("An Application has already been created for this process!"); + + auto application = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Application {})); + *s_the = *application; + + application->m_event_loop = TRY(try_make()); + ConnectionToWindowServer::the(); Clipboard::initialize({}); - if (argc > 0) - m_invoked_as = argv[0]; + + if (arguments.argc > 0) + application->m_invoked_as = arguments.argv[0]; if (getenv("GUI_FOCUS_DEBUG")) - m_focus_debugging_enabled = true; + application->m_focus_debugging_enabled = true; if (getenv("GUI_HOVER_DEBUG")) - m_hover_debugging_enabled = true; + application->m_hover_debugging_enabled = true; if (getenv("GUI_DND_DEBUG")) - m_dnd_debugging_enabled = true; + application->m_dnd_debugging_enabled = true; - for (int i = 1; i < argc; i++) { - DeprecatedString arg(argv[i]); - m_args.append(move(arg)); - } + for (auto arg : arguments.strings.slice(1)) + TRY(application->m_args.try_append(arg)); - m_tooltip_show_timer = Core::Timer::create_single_shot(700, [this] { - request_tooltip_show(); - }).release_value_but_fixme_should_propagate_errors(); + application->m_tooltip_show_timer = TRY(Core::Timer::create_single_shot(700, [weak_application = application->make_weak_ptr()] { + weak_application->request_tooltip_show(); + })); + + application->m_tooltip_hide_timer = TRY(Core::Timer::create_single_shot(50, [weak_application = application->make_weak_ptr()] { + weak_application->tooltip_hide_timer_did_fire(); + })); - m_tooltip_hide_timer = Core::Timer::create_single_shot(50, [this] { - tooltip_hide_timer_did_fire(); - }).release_value_but_fixme_should_propagate_errors(); + return application; } static bool s_in_teardown; diff --git a/Userland/Libraries/LibGUI/Application.h b/Userland/Libraries/LibGUI/Application.h index 387f53bebb..b25674bfd6 100644 --- a/Userland/Libraries/LibGUI/Application.h +++ b/Userland/Libraries/LibGUI/Application.h @@ -22,11 +22,13 @@ namespace GUI { class Application : public Core::Object { - C_OBJECT(Application); + C_OBJECT_ABSTRACT(Application); public: static Application* the(); + static ErrorOr> create(Main::Arguments const& arguments); + ~Application(); static bool in_teardown(); @@ -97,11 +99,7 @@ public: void register_recent_file_actions(Badge, Vector>); private: - Application(int argc, char** argv); - Application(Main::Arguments const& arguments) - : Application(arguments.argc, arguments.argv) - { - } + Application() = default; virtual void event(Core::Event&) override; -- cgit v1.2.3