main.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "MainWidget.h"
  8. #include <LibConfig/Client.h>
  9. #include <LibCore/ArgsParser.h>
  10. #include <LibCore/System.h>
  11. #include <LibFileSystemAccessClient/Client.h>
  12. #include <LibGUI/Action.h>
  13. #include <LibGUI/Application.h>
  14. #include <LibGUI/Icon.h>
  15. #include <LibGUI/MessageBox.h>
  16. #include <LibGUI/Statusbar.h>
  17. #include <LibGUI/Window.h>
  18. #include <LibGfx/Painter.h>
  19. #include <LibMain/Main.h>
  20. ErrorOr<int> serenity_main(Main::Arguments arguments)
  21. {
  22. TRY(Core::System::pledge("stdio thread recvfd sendfd rpath unix wpath cpath", nullptr));
  23. auto app = GUI::Application::construct(arguments);
  24. Config::pledge_domains("PixelPaint");
  25. const char* image_file = nullptr;
  26. Core::ArgsParser args_parser;
  27. args_parser.add_positional_argument(image_file, "Image file to open", "path", Core::ArgsParser::Required::No);
  28. args_parser.parse(arguments);
  29. TRY(Core::System::unveil("/res", "r"));
  30. TRY(Core::System::unveil("/tmp/portal/clipboard", "rw"));
  31. TRY(Core::System::unveil("/tmp/portal/filesystemaccess", "rw"));
  32. TRY(Core::System::unveil("/tmp/portal/image", "rw"));
  33. TRY(Core::System::unveil(nullptr, nullptr));
  34. auto app_icon = GUI::Icon::default_icon("app-pixel-paint");
  35. auto window = GUI::Window::construct();
  36. window->set_title("Pixel Paint");
  37. window->resize(800, 510);
  38. window->set_icon(app_icon.bitmap_for_size(16));
  39. auto& main_widget = window->set_main_widget<PixelPaint::MainWidget>();
  40. main_widget.initialize_menubar(*window);
  41. window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  42. if (main_widget.request_close())
  43. return GUI::Window::CloseRequestDecision::Close;
  44. return GUI::Window::CloseRequestDecision::StayOpen;
  45. };
  46. auto& statusbar = *main_widget.find_descendant_of_type_named<GUI::Statusbar>("statusbar");
  47. app->on_action_enter = [&statusbar](GUI::Action& action) {
  48. auto text = action.status_tip();
  49. if (text.is_empty())
  50. text = Gfx::parse_ampersand_string(action.text());
  51. statusbar.set_override_text(move(text));
  52. };
  53. app->on_action_leave = [&statusbar](GUI::Action&) {
  54. statusbar.set_override_text({});
  55. };
  56. window->show();
  57. if (image_file) {
  58. auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window->window_id(), image_file);
  59. if (response.error != 0) {
  60. if (response.error != -1)
  61. GUI::MessageBox::show_error(window, String::formatted("Opening \"{}\" failed: {}", *response.chosen_file, strerror(response.error)));
  62. return 1;
  63. }
  64. main_widget.open_image_fd(*response.fd, *response.chosen_file);
  65. } else {
  66. main_widget.create_default_image();
  67. }
  68. return app->exec();
  69. }