main.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "GMLAutocompleteProvider.h"
  7. #include <AK/URL.h>
  8. #include <LibCore/ArgsParser.h>
  9. #include <LibCore/File.h>
  10. #include <LibDesktop/Launcher.h>
  11. #include <LibGUI/Application.h>
  12. #include <LibGUI/FilePicker.h>
  13. #include <LibGUI/GMLFormatter.h>
  14. #include <LibGUI/GMLLexer.h>
  15. #include <LibGUI/GMLSyntaxHighlighter.h>
  16. #include <LibGUI/Icon.h>
  17. #include <LibGUI/Menu.h>
  18. #include <LibGUI/Menubar.h>
  19. #include <LibGUI/MessageBox.h>
  20. #include <LibGUI/Painter.h>
  21. #include <LibGUI/Splitter.h>
  22. #include <LibGUI/TextEditor.h>
  23. #include <LibGUI/Window.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. namespace {
  27. class UnregisteredWidget final : public GUI::Widget {
  28. C_OBJECT(UnregisteredWidget);
  29. private:
  30. UnregisteredWidget(const String& class_name);
  31. virtual void paint_event(GUI::PaintEvent& event) override;
  32. String m_text;
  33. };
  34. UnregisteredWidget::UnregisteredWidget(const String& class_name)
  35. {
  36. StringBuilder builder;
  37. builder.append(class_name);
  38. builder.append("\nnot registered");
  39. m_text = builder.to_string();
  40. }
  41. void UnregisteredWidget::paint_event(GUI::PaintEvent& event)
  42. {
  43. GUI::Painter painter(*this);
  44. painter.add_clip_rect(event.rect());
  45. painter.fill_rect(event.rect(), Gfx::Color::DarkRed);
  46. painter.draw_text(rect(), m_text, Gfx::TextAlignment::Center, Color::White);
  47. }
  48. }
  49. int main(int argc, char** argv)
  50. {
  51. if (pledge("stdio thread recvfd sendfd cpath rpath wpath unix", nullptr) < 0) {
  52. perror("pledge");
  53. return 1;
  54. }
  55. auto app = GUI::Application::construct(argc, argv);
  56. if (pledge("stdio thread recvfd sendfd rpath cpath wpath unix", nullptr) < 0) {
  57. perror("pledge");
  58. return 1;
  59. }
  60. if (!Desktop::Launcher::add_allowed_handler_with_only_specific_urls(
  61. "/bin/Help",
  62. { URL::create_with_file_protocol("/usr/share/man/man1/Playground.md") })
  63. || !Desktop::Launcher::seal_allowlist()) {
  64. warnln("Failed to set up allowed launch URLs");
  65. return 1;
  66. }
  67. if (pledge("stdio thread recvfd sendfd rpath cpath wpath", nullptr) < 0) {
  68. perror("pledge");
  69. return 1;
  70. }
  71. const char* path = nullptr;
  72. Core::ArgsParser args_parser;
  73. args_parser.add_positional_argument(path, "GML file to edit", "file", Core::ArgsParser::Required::No);
  74. args_parser.parse(argc, argv);
  75. auto app_icon = GUI::Icon::default_icon("app-playground");
  76. auto window = GUI::Window::construct();
  77. window->set_title("GML Playground");
  78. window->set_icon(app_icon.bitmap_for_size(16));
  79. window->resize(800, 600);
  80. auto& splitter = window->set_main_widget<GUI::HorizontalSplitter>();
  81. auto& editor = splitter.add<GUI::TextEditor>();
  82. auto& preview = splitter.add<GUI::Widget>();
  83. editor.set_syntax_highlighter(make<GUI::GMLSyntaxHighlighter>());
  84. editor.set_autocomplete_provider(make<GMLAutocompleteProvider>());
  85. editor.set_should_autocomplete_automatically(true);
  86. editor.set_automatic_indentation_enabled(true);
  87. if (String(path).is_empty()) {
  88. editor.set_text(R"~~~(@GUI::Widget {
  89. layout: @GUI::VerticalBoxLayout {
  90. }
  91. // Now add some widgets!
  92. }
  93. )~~~");
  94. editor.set_cursor(4, 28); // after "...widgets!"
  95. } else {
  96. auto file = Core::File::construct(path);
  97. if (!file->open(Core::OpenMode::ReadOnly)) {
  98. GUI::MessageBox::show(window, String::formatted("Opening \"{}\" failed: {}", path, strerror(errno)), "Error", GUI::MessageBox::Type::Error);
  99. return 1;
  100. }
  101. if (file->is_device()) {
  102. GUI::MessageBox::show(window, String::formatted("Opening \"{}\" failed: Can't open device files", path), "Error", GUI::MessageBox::Type::Error);
  103. return 1;
  104. }
  105. editor.set_text(file->read_all());
  106. }
  107. editor.on_change = [&] {
  108. preview.remove_all_children();
  109. preview.load_from_gml(editor.text(), [](const String& class_name) -> RefPtr<Core::Object> {
  110. return UnregisteredWidget::construct(class_name);
  111. });
  112. };
  113. auto menubar = GUI::Menubar::construct();
  114. auto& file_menu = menubar->add_menu("&File");
  115. file_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
  116. Optional<String> open_path = GUI::FilePicker::get_open_filepath(window);
  117. if (!open_path.has_value())
  118. return;
  119. auto file = Core::File::construct(open_path.value());
  120. if (!file->open(Core::OpenMode::ReadOnly) && file->error() != ENOENT) {
  121. GUI::MessageBox::show(window, String::formatted("Opening \"{}\" failed: {}", open_path.value(), strerror(errno)), "Error", GUI::MessageBox::Type::Error);
  122. return;
  123. }
  124. if (file->is_device()) {
  125. GUI::MessageBox::show(window, String::formatted("Opening \"{}\" failed: Can't open device files", open_path.value()), "Error", GUI::MessageBox::Type::Error);
  126. return;
  127. }
  128. editor.set_text(file->read_all());
  129. editor.set_focus(true);
  130. }));
  131. file_menu.add_action(GUI::CommonActions::make_save_as_action([&](auto&) {
  132. Optional<String> save_path = GUI::FilePicker::get_save_filepath(window, "Untitled", "gml");
  133. if (!save_path.has_value())
  134. return;
  135. if (!editor.write_to_file(save_path.value())) {
  136. GUI::MessageBox::show(window, "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
  137. return;
  138. }
  139. }));
  140. file_menu.add_separator();
  141. file_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  142. app->quit();
  143. }));
  144. auto& edit_menu = menubar->add_menu("&Edit");
  145. edit_menu.add_action(GUI::Action::create("&Format GML", { Mod_Ctrl | Mod_Shift, Key_I }, [&](auto&) {
  146. auto source = editor.text();
  147. GUI::GMLLexer lexer(source);
  148. for (auto& token : lexer.lex()) {
  149. if (token.m_type == GUI::GMLToken::Type::Comment) {
  150. auto result = GUI::MessageBox::show(
  151. window,
  152. "Your GML contains comments, which currently are not supported by the formatter and will be removed. Proceed?",
  153. "Warning",
  154. GUI::MessageBox::Type::Warning,
  155. GUI::MessageBox::InputType::OKCancel);
  156. if (result == GUI::MessageBox::ExecCancel)
  157. return;
  158. break;
  159. }
  160. }
  161. auto formatted_gml = GUI::format_gml(source);
  162. if (!formatted_gml.is_null()) {
  163. editor.set_text(formatted_gml);
  164. } else {
  165. GUI::MessageBox::show(
  166. window,
  167. "GML could not be formatted, please check the debug console for parsing errors.",
  168. "Error",
  169. GUI::MessageBox::Type::Error);
  170. }
  171. }));
  172. auto& help_menu = menubar->add_menu("&Help");
  173. help_menu.add_action(GUI::CommonActions::make_help_action([](auto&) {
  174. Desktop::Launcher::open(URL::create_with_file_protocol("/usr/share/man/man1/Playground.md"), "/bin/Help");
  175. }));
  176. help_menu.add_action(GUI::CommonActions::make_about_action("GML Playground", app_icon, window));
  177. window->set_menubar(move(menubar));
  178. window->show();
  179. return app->exec();
  180. }