mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 08:14:58 +00:00

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.
86 lines
2.9 KiB
C++
86 lines
2.9 KiB
C++
/*
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
|
* Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include "MainWidget.h"
|
|
#include <LibConfig/Client.h>
|
|
#include <LibCore/ArgsParser.h>
|
|
#include <LibCore/System.h>
|
|
#include <LibFileSystemAccessClient/Client.h>
|
|
#include <LibGUI/Action.h>
|
|
#include <LibGUI/Application.h>
|
|
#include <LibGUI/Icon.h>
|
|
#include <LibGUI/MessageBox.h>
|
|
#include <LibGUI/Statusbar.h>
|
|
#include <LibGUI/Window.h>
|
|
#include <LibGfx/Painter.h>
|
|
#include <LibMain/Main.h>
|
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|
{
|
|
TRY(Core::System::pledge("stdio thread recvfd sendfd rpath unix wpath cpath"));
|
|
|
|
auto app = TRY(GUI::Application::create(arguments));
|
|
Config::pledge_domain("PixelPaint");
|
|
app->set_config_domain(TRY(String::from_utf8("PixelPaint"sv)));
|
|
|
|
StringView image_file;
|
|
Core::ArgsParser args_parser;
|
|
args_parser.add_positional_argument(image_file, "Image file to open", "path", Core::ArgsParser::Required::No);
|
|
args_parser.parse(arguments);
|
|
|
|
TRY(Core::System::unveil("/res", "r"));
|
|
TRY(Core::System::unveil("/tmp/session/%sid/portal/clipboard", "rw"));
|
|
TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
|
|
TRY(Core::System::unveil("/tmp/session/%sid/portal/image", "rw"));
|
|
TRY(Core::System::unveil("/etc/FileIconProvider.ini", "r"));
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
|
|
|
auto app_icon = GUI::Icon::default_icon("app-pixel-paint"sv);
|
|
|
|
PixelPaint::g_icon_bag = TRY(PixelPaint::IconBag::create());
|
|
|
|
auto window = GUI::Window::construct();
|
|
window->set_title("Pixel Paint");
|
|
window->resize(800, 520);
|
|
window->set_icon(app_icon.bitmap_for_size(16));
|
|
|
|
auto main_widget = TRY(window->set_main_widget<PixelPaint::MainWidget>());
|
|
|
|
TRY(main_widget->initialize_menubar(*window));
|
|
|
|
window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
|
|
if (main_widget->request_close())
|
|
return GUI::Window::CloseRequestDecision::Close;
|
|
return GUI::Window::CloseRequestDecision::StayOpen;
|
|
};
|
|
|
|
auto& statusbar = *main_widget->find_descendant_of_type_named<GUI::Statusbar>("statusbar");
|
|
|
|
app->on_action_enter = [&statusbar](GUI::Action& action) {
|
|
auto text = action.status_tip();
|
|
if (text.is_empty())
|
|
text = Gfx::parse_ampersand_string(action.text());
|
|
statusbar.set_override_text(move(text));
|
|
};
|
|
|
|
app->on_action_leave = [&statusbar](GUI::Action&) {
|
|
statusbar.set_override_text({});
|
|
};
|
|
|
|
window->show();
|
|
|
|
if (!image_file.is_empty()) {
|
|
auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, image_file);
|
|
if (response.is_error())
|
|
return 1;
|
|
main_widget->open_image(response.release_value());
|
|
} else {
|
|
TRY(main_widget->create_default_image());
|
|
}
|
|
|
|
return app->exec();
|
|
}
|