2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2021-06-11 12:49:04 -03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2021-09-13 17:48:22 -03:00
|
|
|
* Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
PixelPaint: Refactor `main.cpp` into `MainWidget`
Previously, all the UI setup was done in `main.cpp`, with a whole
bunch of lambdas,, and actions etc just being stored in the main
function. This is probably an artifact from back when it was first
created.
Most other applications now have a "MainWidget" class of some sort
which handles setting up all the UI/menubars, etc. More importantly,,
it also lets us handle application-wide events which we were
previously not able to do directly, since the main widget was just
a default GUI::Widget.
This patch moves all the core functionality of the PixelPaint
application into PixelPaint::MainWidget, which is then instantiated
by the main function. There is likely some more refactoring that
would help, but this commit is big enough as it is doing mostly
a direct port.
2021-09-06 01:11:46 -03:00
|
|
|
#include "MainWidget.h"
|
2021-09-11 14:40:55 -03:00
|
|
|
#include <LibConfig/Client.h>
|
2021-02-17 15:15:28 -03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2021-11-23 06:59:50 -03:00
|
|
|
#include <LibCore/System.h>
|
2021-08-06 01:37:53 -03:00
|
|
|
#include <LibFileSystemAccessClient/Client.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
|
#include <LibGUI/Application.h>
|
2020-06-04 07:30:05 -03:00
|
|
|
#include <LibGUI/Icon.h>
|
2021-09-11 14:55:02 -03:00
|
|
|
#include <LibGUI/MessageBox.h>
|
2021-05-15 19:37:11 -03:00
|
|
|
#include <LibGUI/Statusbar.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Window.h>
|
PixelPaint: Refactor `main.cpp` into `MainWidget`
Previously, all the UI setup was done in `main.cpp`, with a whole
bunch of lambdas,, and actions etc just being stored in the main
function. This is probably an artifact from back when it was first
created.
Most other applications now have a "MainWidget" class of some sort
which handles setting up all the UI/menubars, etc. More importantly,,
it also lets us handle application-wide events which we were
previously not able to do directly, since the main widget was just
a default GUI::Widget.
This patch moves all the core functionality of the PixelPaint
application into PixelPaint::MainWidget, which is then instantiated
by the main function. There is likely some more refactoring that
would help, but this commit is big enough as it is doing mostly
a direct port.
2021-09-06 01:11:46 -03:00
|
|
|
#include <LibGfx/Painter.h>
|
2021-11-22 19:23:27 -03:00
|
|
|
#include <LibMain/Main.h>
|
2019-06-10 14:29:33 -03:00
|
|
|
|
2021-11-22 19:23:27 -03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-06-10 14:29:33 -03:00
|
|
|
{
|
2021-11-27 19:26:34 -03:00
|
|
|
TRY(Core::System::pledge("stdio thread recvfd sendfd rpath unix wpath cpath"));
|
2020-01-13 08:22:49 -03:00
|
|
|
|
2022-04-16 13:17:39 -03:00
|
|
|
auto app = TRY(GUI::Application::try_create(arguments));
|
2022-02-11 13:33:09 -03:00
|
|
|
Config::pledge_domain("PixelPaint");
|
2019-06-10 14:29:33 -03:00
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
char const* image_file = nullptr;
|
2021-02-17 15:15:28 -03:00
|
|
|
Core::ArgsParser args_parser;
|
2021-06-01 03:34:40 -03:00
|
|
|
args_parser.add_positional_argument(image_file, "Image file to open", "path", Core::ArgsParser::Required::No);
|
2021-11-22 21:03:05 -03:00
|
|
|
args_parser.parse(arguments);
|
2021-02-17 15:15:28 -03:00
|
|
|
|
2021-11-23 06:59:50 -03:00
|
|
|
TRY(Core::System::unveil("/res", "r"));
|
|
|
|
|
TRY(Core::System::unveil("/tmp/portal/clipboard", "rw"));
|
2022-07-24 10:28:42 -03:00
|
|
|
TRY(Core::System::unveil("/tmp/user/%uid/portal/filesystemaccess", "rw"));
|
2022-07-24 10:29:26 -03:00
|
|
|
TRY(Core::System::unveil("/tmp/user/%uid/portal/image", "rw"));
|
2022-01-06 08:38:54 -03:00
|
|
|
TRY(Core::System::unveil("/etc/FileIconProvider.ini", "r"));
|
2021-11-23 06:59:50 -03:00
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2021-08-06 01:37:53 -03:00
|
|
|
|
2022-07-11 14:32:29 -03:00
|
|
|
auto app_icon = GUI::Icon::default_icon("app-pixel-paint"sv);
|
2020-06-04 07:30:05 -03:00
|
|
|
|
2022-02-09 15:48:56 -03:00
|
|
|
PixelPaint::g_icon_bag = TRY(PixelPaint::IconBag::try_create());
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
auto window = GUI::Window::construct();
|
2021-05-15 19:55:23 -03:00
|
|
|
window->set_title("Pixel Paint");
|
2021-06-15 10:29:04 -03:00
|
|
|
window->resize(800, 510);
|
2020-06-04 07:30:05 -03:00
|
|
|
window->set_icon(app_icon.bitmap_for_size(16));
|
2019-06-10 14:29:33 -03:00
|
|
|
|
2022-01-07 11:14:07 -03:00
|
|
|
auto main_widget = TRY(window->try_set_main_widget<PixelPaint::MainWidget>());
|
2021-09-17 14:28:22 -03:00
|
|
|
|
2022-01-07 11:14:07 -03:00
|
|
|
main_widget->initialize_menubar(*window);
|
2020-05-13 16:46:19 -03:00
|
|
|
|
2021-09-17 14:28:22 -03:00
|
|
|
window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
|
2022-01-07 11:14:07 -03:00
|
|
|
if (main_widget->request_close())
|
2021-09-17 14:28:22 -03:00
|
|
|
return GUI::Window::CloseRequestDecision::Close;
|
|
|
|
|
return GUI::Window::CloseRequestDecision::StayOpen;
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-07 11:14:07 -03:00
|
|
|
auto& statusbar = *main_widget->find_descendant_of_type_named<GUI::Statusbar>("statusbar");
|
2021-05-15 19:37:11 -03:00
|
|
|
|
|
|
|
|
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({});
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-15 19:26:27 -03:00
|
|
|
window->show();
|
2021-09-11 14:55:02 -03:00
|
|
|
|
|
|
|
|
if (image_file) {
|
2022-01-16 06:30:06 -03:00
|
|
|
auto response = FileSystemAccessClient::Client::the().try_request_file_read_only_approved(window, image_file);
|
|
|
|
|
if (response.is_error())
|
2021-09-11 14:55:02 -03:00
|
|
|
return 1;
|
2022-01-16 06:30:06 -03:00
|
|
|
main_widget->open_image(response.value());
|
2021-09-11 14:55:02 -03:00
|
|
|
} else {
|
2022-01-07 11:14:07 -03:00
|
|
|
main_widget->create_default_image();
|
2021-09-11 14:55:02 -03:00
|
|
|
}
|
|
|
|
|
|
2020-07-04 09:05:19 -03:00
|
|
|
return app->exec();
|
2019-06-10 14:29:33 -03:00
|
|
|
}
|