main.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, networkException <networkexception@serenityos.org>
  4. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2021, Antonio Di Stefano <tonio9681@gmail.com>
  6. * Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
  7. *
  8. * SPDX-License-Identifier: BSD-2-Clause
  9. */
  10. #include "MainWidget.h"
  11. #include "PreviewWidget.h"
  12. #include <LibCore/ArgsParser.h>
  13. #include <LibCore/System.h>
  14. #include <LibFileSystemAccessClient/Client.h>
  15. #include <LibGUI/Application.h>
  16. #include <LibGUI/Icon.h>
  17. #include <LibGUI/Menu.h>
  18. #include <LibGUI/Menubar.h>
  19. #include <LibGUI/MessageBox.h>
  20. #include <LibGUI/Window.h>
  21. #include <LibMain/Main.h>
  22. ErrorOr<int> serenity_main(Main::Arguments arguments)
  23. {
  24. TRY(Core::System::pledge("stdio recvfd sendfd thread rpath cpath wpath unix"));
  25. auto app = TRY(GUI::Application::try_create(arguments));
  26. StringView file_to_edit;
  27. Core::ArgsParser parser;
  28. parser.add_positional_argument(file_to_edit, "Theme file to edit", "file", Core::ArgsParser::Required::No);
  29. parser.parse(arguments);
  30. Optional<DeprecatedString> path = {};
  31. if (!file_to_edit.is_empty())
  32. path = Core::File::absolute_path(file_to_edit);
  33. TRY(Core::System::pledge("stdio recvfd sendfd thread rpath unix"));
  34. TRY(Core::System::unveil("/sys/kernel/processes", "r"));
  35. TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
  36. TRY(Core::System::unveil("/res", "r"));
  37. TRY(Core::System::unveil(nullptr, nullptr));
  38. auto app_icon = GUI::Icon::default_icon("app-theme-editor"sv);
  39. auto window = GUI::Window::construct();
  40. auto main_widget = TRY(window->try_set_main_widget<ThemeEditor::MainWidget>());
  41. if (path.has_value()) {
  42. // Note: This is deferred to ensure that the window has already popped and thus proper window stealing can be performed.
  43. app->event_loop().deferred_invoke(
  44. [&window, &path, &main_widget]() {
  45. auto response = FileSystemAccessClient::Client::the().try_request_file_read_only_approved(window, path.value());
  46. if (response.is_error())
  47. GUI::MessageBox::show_error(window, DeprecatedString::formatted("Opening \"{}\" failed: {}", path.value(), response.error()));
  48. else {
  49. auto load_from_file_result = main_widget->load_from_file(response.release_value());
  50. if (load_from_file_result.is_error())
  51. GUI::MessageBox::show_error(window, DeprecatedString::formatted("Loading theme from file has failed: {}", load_from_file_result.error()));
  52. }
  53. });
  54. }
  55. TRY(main_widget->initialize_menubar(window));
  56. main_widget->update_title();
  57. window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  58. return main_widget->request_close();
  59. };
  60. window->resize(820, 520);
  61. window->set_resizable(false);
  62. window->show();
  63. window->set_icon(app_icon.bitmap_for_size(16));
  64. return app->exec();
  65. }