main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Julius Heijmen <julius.heijmen@gmail.com>
  4. * Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
  5. * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/URL.h>
  10. #include <LibConfig/Client.h>
  11. #include <LibCore/ArgsParser.h>
  12. #include <LibCore/System.h>
  13. #include <LibDesktop/Launcher.h>
  14. #include <LibFileSystemAccessClient/Client.h>
  15. #include <LibGUI/ActionGroup.h>
  16. #include <LibGUI/Application.h>
  17. #include <LibGUI/GML/AutocompleteProvider.h>
  18. #include <LibGUI/GML/Formatter.h>
  19. #include <LibGUI/GML/SyntaxHighlighter.h>
  20. #include <LibGUI/Icon.h>
  21. #include <LibGUI/Menu.h>
  22. #include <LibGUI/Menubar.h>
  23. #include <LibGUI/MessageBox.h>
  24. #include <LibGUI/Painter.h>
  25. #include <LibGUI/RegularEditingEngine.h>
  26. #include <LibGUI/Splitter.h>
  27. #include <LibGUI/TextEditor.h>
  28. #include <LibGUI/Toolbar.h>
  29. #include <LibGUI/VimEditingEngine.h>
  30. #include <LibGUI/Window.h>
  31. #include <LibMain/Main.h>
  32. #include <Userland/DevTools/GMLPlayground/GMLPlaygroundWindowGML.h>
  33. namespace {
  34. class UnregisteredWidget final : public GUI::Widget {
  35. C_OBJECT(UnregisteredWidget);
  36. private:
  37. UnregisteredWidget(DeprecatedString const& class_name);
  38. virtual void paint_event(GUI::PaintEvent& event) override;
  39. DeprecatedString m_text;
  40. };
  41. UnregisteredWidget::UnregisteredWidget(DeprecatedString const& class_name)
  42. {
  43. StringBuilder builder;
  44. builder.append(class_name);
  45. builder.append("\nnot registered"sv);
  46. m_text = builder.to_deprecated_string();
  47. }
  48. void UnregisteredWidget::paint_event(GUI::PaintEvent& event)
  49. {
  50. GUI::Painter painter(*this);
  51. painter.add_clip_rect(event.rect());
  52. painter.fill_rect(event.rect(), Gfx::Color::DarkRed);
  53. painter.draw_text(rect(), m_text, Gfx::TextAlignment::Center, Color::White);
  54. }
  55. }
  56. ErrorOr<int> serenity_main(Main::Arguments arguments)
  57. {
  58. TRY(Core::System::pledge("stdio thread recvfd sendfd cpath rpath wpath unix"));
  59. auto app = TRY(GUI::Application::create(arguments));
  60. Config::pledge_domains({ "GMLPlayground", "Calendar" });
  61. app->set_config_domain(TRY("GMLPlayground"_string));
  62. TRY(Core::System::unveil("/res", "r"));
  63. TRY(Core::System::unveil("/tmp/session/%sid/portal/launch", "rw"));
  64. TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
  65. TRY(Core::System::unveil(nullptr, nullptr));
  66. TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_scheme("/usr/share/man/man1/Applications/GMLPlayground.md") }));
  67. TRY(Desktop::Launcher::seal_allowlist());
  68. StringView path;
  69. Core::ArgsParser args_parser;
  70. args_parser.add_positional_argument(path, "GML file to edit", "file", Core::ArgsParser::Required::No);
  71. args_parser.parse(arguments);
  72. auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-gml-playground"sv));
  73. auto window = TRY(GUI::Window::try_create());
  74. window->set_title("GML Playground");
  75. window->set_icon(app_icon.bitmap_for_size(16));
  76. window->resize(800, 600);
  77. auto main_widget = TRY(window->set_main_widget<GUI::Widget>());
  78. TRY(main_widget->load_from_gml(gml_playground_window_gml));
  79. auto toolbar = main_widget->find_descendant_of_type_named<GUI::Toolbar>("toolbar");
  80. auto splitter = main_widget->find_descendant_of_type_named<GUI::HorizontalSplitter>("splitter");
  81. auto editor = main_widget->find_descendant_of_type_named<GUI::TextEditor>("text_editor");
  82. auto preview_frame_widget = main_widget->find_descendant_of_type_named<GUI::Frame>("preview_frame");
  83. auto preview_window = TRY(GUI::Window::try_create());
  84. preview_window->set_title("Preview - GML Playground");
  85. preview_window->set_icon(app_icon.bitmap_for_size(16));
  86. auto preview_window_widget = TRY(preview_window->set_main_widget<GUI::Widget>());
  87. preview_window_widget->set_fill_with_background_color(true);
  88. GUI::Widget* preview = preview_frame_widget;
  89. editor->set_syntax_highlighter(make<GUI::GML::SyntaxHighlighter>());
  90. editor->set_autocomplete_provider(make<GUI::GML::AutocompleteProvider>());
  91. editor->set_should_autocomplete_automatically(true);
  92. editor->set_automatic_indentation_enabled(true);
  93. editor->set_ruler_visible(true);
  94. DeprecatedString file_path;
  95. auto update_title = [&] {
  96. window->set_title(DeprecatedString::formatted("{}[*] - GML Playground", file_path.is_empty() ? "Untitled"sv : file_path.view()));
  97. };
  98. editor->on_change = [&] {
  99. preview->remove_all_children();
  100. // FIXME: Parsing errors happen while the user is typing. What should we do about them?
  101. (void)preview->load_from_gml(editor->text(), [](DeprecatedString const& class_name) -> ErrorOr<NonnullRefPtr<Core::Object>> {
  102. return UnregisteredWidget::try_create(class_name);
  103. });
  104. };
  105. editor->on_modified_change = [&](bool modified) {
  106. window->set_modified(modified);
  107. };
  108. auto load_file = [&](auto file) {
  109. auto buffer_or_error = file.stream().read_until_eof();
  110. if (buffer_or_error.is_error())
  111. return;
  112. editor->set_text(buffer_or_error.release_value());
  113. editor->set_focus(true);
  114. file_path = file.filename().to_deprecated_string();
  115. update_title();
  116. GUI::Application::the()->set_most_recently_open_file(file.filename());
  117. };
  118. auto file_menu = TRY(window->try_add_menu("&File"_short_string));
  119. auto save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
  120. auto response = FileSystemAccessClient::Client::the().save_file(window, "Untitled", "gml");
  121. if (response.is_error())
  122. return;
  123. auto file = response.value().release_stream();
  124. if (auto result = editor->write_to_file(*file); result.is_error()) {
  125. GUI::MessageBox::show(window, DeprecatedString::formatted("Unable to save file: {}\n"sv, result.release_error()), "Error"sv, GUI::MessageBox::Type::Error);
  126. return;
  127. }
  128. file_path = response.value().filename().to_deprecated_string();
  129. update_title();
  130. GUI::Application::the()->set_most_recently_open_file(response.value().filename());
  131. });
  132. auto save_action = GUI::CommonActions::make_save_action([&](auto&) {
  133. if (file_path.is_empty()) {
  134. save_as_action->activate();
  135. return;
  136. }
  137. auto response = FileSystemAccessClient::Client::the().request_file(window, file_path, Core::File::OpenMode::Truncate | Core::File::OpenMode::Write);
  138. if (response.is_error())
  139. return;
  140. auto file = response.value().release_stream();
  141. if (auto result = editor->write_to_file(*file); result.is_error()) {
  142. GUI::MessageBox::show(window, DeprecatedString::formatted("Unable to save file: {}\n"sv, result.release_error()), "Error"sv, GUI::MessageBox::Type::Error);
  143. return;
  144. }
  145. update_title();
  146. });
  147. auto open_action = GUI::CommonActions::make_open_action([&](auto&) {
  148. if (window->is_modified()) {
  149. auto result = GUI::MessageBox::ask_about_unsaved_changes(window, file_path, editor->document().undo_stack().last_unmodified_timestamp());
  150. if (result == GUI::MessageBox::ExecResult::Yes)
  151. save_action->activate();
  152. if (result != GUI::MessageBox::ExecResult::No && window->is_modified())
  153. return;
  154. }
  155. auto response = FileSystemAccessClient::Client::the().open_file(window);
  156. if (response.is_error())
  157. return;
  158. load_file(response.release_value());
  159. });
  160. TRY(file_menu->try_add_action(open_action));
  161. TRY(file_menu->try_add_action(save_action));
  162. TRY(file_menu->try_add_action(save_as_action));
  163. TRY(file_menu->try_add_separator());
  164. TRY(file_menu->add_recent_files_list([&](auto& action) {
  165. if (window->is_modified()) {
  166. auto result = GUI::MessageBox::ask_about_unsaved_changes(window, file_path, editor->document().undo_stack().last_unmodified_timestamp());
  167. if (result == GUI::MessageBox::ExecResult::Yes)
  168. save_action->activate();
  169. if (result != GUI::MessageBox::ExecResult::No && window->is_modified())
  170. return;
  171. }
  172. auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, action.text());
  173. if (response.is_error())
  174. return;
  175. load_file(response.release_value());
  176. }));
  177. TRY(file_menu->try_add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  178. if (window->on_close_request() == GUI::Window::CloseRequestDecision::Close)
  179. app->quit();
  180. })));
  181. auto edit_menu = TRY(window->try_add_menu("&Edit"_short_string));
  182. TRY(edit_menu->try_add_action(editor->undo_action()));
  183. TRY(edit_menu->try_add_action(editor->redo_action()));
  184. TRY(edit_menu->try_add_separator());
  185. TRY(edit_menu->try_add_action(editor->cut_action()));
  186. TRY(edit_menu->try_add_action(editor->copy_action()));
  187. TRY(edit_menu->try_add_action(editor->paste_action()));
  188. TRY(edit_menu->try_add_separator());
  189. TRY(edit_menu->try_add_action(editor->select_all_action()));
  190. TRY(edit_menu->try_add_action(editor->go_to_line_action()));
  191. TRY(edit_menu->try_add_separator());
  192. auto format_gml_action = GUI::Action::create("&Format GML", { Mod_Ctrl | Mod_Shift, Key_I }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/reformat.png"sv)), [&](auto&) {
  193. auto formatted_gml_or_error = GUI::GML::format_gml(editor->text());
  194. if (!formatted_gml_or_error.is_error()) {
  195. editor->replace_all_text_without_resetting_undo_stack(formatted_gml_or_error.release_value());
  196. } else {
  197. GUI::MessageBox::show(
  198. window,
  199. DeprecatedString::formatted("GML could not be formatted: {}", formatted_gml_or_error.error()),
  200. "Error"sv,
  201. GUI::MessageBox::Type::Error);
  202. }
  203. });
  204. TRY(edit_menu->try_add_action(format_gml_action));
  205. auto vim_emulation_setting_action = GUI::Action::create_checkable("&Vim Emulation", { Mod_Ctrl | Mod_Shift | Mod_Alt, Key_V }, [&](auto& action) {
  206. if (action.is_checked())
  207. editor->set_editing_engine(make<GUI::VimEditingEngine>());
  208. else
  209. editor->set_editing_engine(make<GUI::RegularEditingEngine>());
  210. });
  211. vim_emulation_setting_action->set_checked(false);
  212. TRY(edit_menu->try_add_action(vim_emulation_setting_action));
  213. auto view_menu = TRY(window->try_add_menu("&View"_short_string));
  214. GUI::ActionGroup views_group;
  215. views_group.set_exclusive(true);
  216. views_group.set_unchecking_allowed(false);
  217. auto view_frame_action = GUI::Action::create_checkable("&Frame", [&](auto&) {
  218. dbgln("View switched to frame");
  219. preview = preview_frame_widget;
  220. editor->on_change();
  221. preview_window->hide();
  222. preview_frame_widget->set_preferred_width(splitter->width() / 2);
  223. preview_frame_widget->set_visible(true);
  224. });
  225. view_menu->add_action(view_frame_action);
  226. views_group.add_action(view_frame_action);
  227. view_frame_action->set_checked(true);
  228. auto view_window_action = GUI::Action::create_checkable("&Window", [&](auto&) {
  229. dbgln("View switched to window");
  230. preview = preview_window_widget;
  231. editor->on_change();
  232. preview_window->resize(400, 300);
  233. preview_window->show();
  234. preview_frame_widget->set_visible(false);
  235. });
  236. view_menu->add_action(view_window_action);
  237. views_group.add_action(view_window_action);
  238. preview_window->on_close = [&] {
  239. view_frame_action->activate();
  240. };
  241. auto help_menu = TRY(window->try_add_menu("&Help"_short_string));
  242. TRY(help_menu->try_add_action(GUI::CommonActions::make_command_palette_action(window)));
  243. TRY(help_menu->try_add_action(GUI::CommonActions::make_help_action([](auto&) {
  244. Desktop::Launcher::open(URL::create_with_file_scheme("/usr/share/man/man1/Applications/GMLPlayground.md"), "/bin/Help");
  245. })));
  246. TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("GML Playground", app_icon, window)));
  247. (void)TRY(toolbar->try_add_action(open_action));
  248. (void)TRY(toolbar->try_add_action(save_action));
  249. (void)TRY(toolbar->try_add_action(save_as_action));
  250. TRY(toolbar->try_add_separator());
  251. (void)TRY(toolbar->try_add_action(editor->cut_action()));
  252. (void)TRY(toolbar->try_add_action(editor->copy_action()));
  253. (void)TRY(toolbar->try_add_action(editor->paste_action()));
  254. TRY(toolbar->try_add_separator());
  255. (void)TRY(toolbar->try_add_action(editor->undo_action()));
  256. (void)TRY(toolbar->try_add_action(editor->redo_action()));
  257. TRY(toolbar->try_add_separator());
  258. (void)TRY(toolbar->try_add_action(format_gml_action));
  259. window->on_close_request = [&] {
  260. if (!window->is_modified())
  261. return GUI::Window::CloseRequestDecision::Close;
  262. auto result = GUI::MessageBox::ask_about_unsaved_changes(window, file_path, editor->document().undo_stack().last_unmodified_timestamp());
  263. if (result == GUI::MessageBox::ExecResult::Yes) {
  264. save_action->activate();
  265. if (window->is_modified())
  266. return GUI::Window::CloseRequestDecision::StayOpen;
  267. return GUI::Window::CloseRequestDecision::Close;
  268. }
  269. if (result == GUI::MessageBox::ExecResult::No)
  270. return GUI::Window::CloseRequestDecision::Close;
  271. return GUI::Window::CloseRequestDecision::StayOpen;
  272. };
  273. window->show();
  274. if (DeprecatedString(path).is_empty()) {
  275. editor->set_text(R"~~~(@GUI::Frame {
  276. layout: @GUI::VerticalBoxLayout {
  277. }
  278. // Now add some widgets!
  279. }
  280. )~~~"sv);
  281. editor->set_cursor(4, 28); // after "...widgets!"
  282. update_title();
  283. } else {
  284. auto file = TRY(FileSystemAccessClient::Client::the().request_file_read_only_approved(window, path));
  285. load_file(move(file));
  286. }
  287. return app->exec();
  288. }