main.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 <LibConfig/Client.h>
  12. #include <LibCore/ArgsParser.h>
  13. #include <LibCore/System.h>
  14. #include <LibFileSystem/FileSystem.h>
  15. #include <LibFileSystemAccessClient/Client.h>
  16. #include <LibGUI/Application.h>
  17. #include <LibGUI/Icon.h>
  18. #include <LibGUI/Menu.h>
  19. #include <LibGUI/Menubar.h>
  20. #include <LibGUI/MessageBox.h>
  21. #include <LibGUI/Window.h>
  22. #include <LibMain/Main.h>
  23. ErrorOr<int> serenity_main(Main::Arguments arguments)
  24. {
  25. TRY(Core::System::pledge("stdio recvfd sendfd thread rpath cpath wpath unix"));
  26. auto app = TRY(GUI::Application::create(arguments));
  27. Config::pledge_domain("ThemeEditor");
  28. app->set_config_domain("ThemeEditor"_string);
  29. StringView file_to_edit;
  30. Core::ArgsParser parser;
  31. parser.add_positional_argument(file_to_edit, "Theme file to edit", "file", Core::ArgsParser::Required::No);
  32. parser.parse(arguments);
  33. Optional<ByteString> path = {};
  34. if (auto error_or_path = FileSystem::absolute_path(file_to_edit); !file_to_edit.is_empty() && !error_or_path.is_error())
  35. path = error_or_path.release_value();
  36. TRY(Core::System::pledge("stdio recvfd sendfd thread rpath unix"));
  37. TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
  38. TRY(Core::System::unveil("/res", "r"));
  39. TRY(Core::System::unveil(nullptr, nullptr));
  40. auto app_icon = GUI::Icon::default_icon("app-theme-editor"sv);
  41. auto window = GUI::Window::construct();
  42. auto main_widget = TRY(ThemeEditor::MainWidget::try_create());
  43. window->set_main_widget(main_widget);
  44. if (path.has_value()) {
  45. // Note: This is deferred to ensure that the window has already popped and any error dialog boxes would show up correctly.
  46. app->event_loop().deferred_invoke(
  47. [&window, &path, &main_widget]() {
  48. auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, path.value());
  49. if (!response.is_error()) {
  50. auto load_from_file_result = main_widget->load_from_file(response.value().filename(), response.value().release_stream());
  51. if (load_from_file_result.is_error())
  52. GUI::MessageBox::show_error(window, ByteString::formatted("Loading theme from file has failed: {}", load_from_file_result.error()));
  53. }
  54. });
  55. }
  56. TRY(main_widget->initialize_menubar(window));
  57. main_widget->update_title();
  58. window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  59. return main_widget->request_close();
  60. };
  61. window->restore_size_and_position("ThemeEditor"sv, "Window"sv, { { 820, 520 } });
  62. window->save_size_and_position_on_close("ThemeEditor"sv, "Window"sv);
  63. window->set_resizable(false);
  64. window->show();
  65. window->set_icon(app_icon.bitmap_for_size(16));
  66. return app->exec();
  67. }