diff options
author | Lucas CHOLLET <lucas.chollet@free.fr> | 2023-05-05 00:24:53 -0400 |
---|---|---|
committer | Sam Atkins <atkinssj@gmail.com> | 2023-05-05 16:41:21 +0100 |
commit | 1a97382305f517dcc4c4f71746b6ab5d96012b0d (patch) | |
tree | 71bc8573bea401d8a27847c1dadeeceb518fd1a0 /Userland/Libraries/LibGUI | |
parent | f132751faeaa486c3eebc1ed23117efe9701da22 (diff) | |
download | serenity-1a97382305f517dcc4c4f71746b6ab5d96012b0d.zip |
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.
Diffstat (limited to 'Userland/Libraries/LibGUI')
-rw-r--r-- | Userland/Libraries/LibGUI/Application.cpp | 44 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/Application.h | 10 |
2 files changed, 29 insertions, 25 deletions
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<NonnullRefPtr<Application>> Application::create(Main::Arguments const& arguments) { - VERIFY(!*s_the); - *s_the = *this; - m_event_loop = make<Core::EventLoop>(); + 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<Core::EventLoop>()); + 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<Application>()] { + weak_application->request_tooltip_show(); + })); + + application->m_tooltip_hide_timer = TRY(Core::Timer::create_single_shot(50, [weak_application = application->make_weak_ptr<Application>()] { + 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<NonnullRefPtr<Application>> create(Main::Arguments const& arguments); + ~Application(); static bool in_teardown(); @@ -97,11 +99,7 @@ public: void register_recent_file_actions(Badge<GUI::Menu>, Vector<NonnullRefPtr<GUI::Action>>); private: - Application(int argc, char** argv); - Application(Main::Arguments const& arguments) - : Application(arguments.argc, arguments.argv) - { - } + Application() = default; virtual void event(Core::Event&) override; |