MainWidget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. * Copyright (c) 2023, Karol Kosek <krkk@serenityos.org>
  7. *
  8. * SPDX-License-Identifier: BSD-2-Clause
  9. */
  10. #include "MainWidget.h"
  11. #include <LibDesktop/Launcher.h>
  12. #include <LibFileSystemAccessClient/Client.h>
  13. #include <LibGUI/Application.h>
  14. #include <LibGUI/GML/AutocompleteProvider.h>
  15. #include <LibGUI/GML/Formatter.h>
  16. #include <LibGUI/GML/SyntaxHighlighter.h>
  17. #include <LibGUI/Icon.h>
  18. #include <LibGUI/Menu.h>
  19. #include <LibGUI/Menubar.h>
  20. #include <LibGUI/MessageBox.h>
  21. #include <LibGUI/Painter.h>
  22. #include <LibGUI/RegularEditingEngine.h>
  23. #include <LibGUI/Splitter.h>
  24. #include <LibGUI/TextEditor.h>
  25. #include <LibGUI/Toolbar.h>
  26. #include <LibGUI/VimEditingEngine.h>
  27. #include <Userland/DevTools/GMLPlayground/GMLPlaygroundWindowGML.h>
  28. namespace {
  29. class UnregisteredWidget final : public GUI::Widget {
  30. C_OBJECT(UnregisteredWidget);
  31. private:
  32. UnregisteredWidget(DeprecatedString const& class_name);
  33. virtual void paint_event(GUI::PaintEvent& event) override;
  34. DeprecatedString m_text;
  35. };
  36. UnregisteredWidget::UnregisteredWidget(DeprecatedString const& class_name)
  37. {
  38. StringBuilder builder;
  39. builder.append(class_name);
  40. builder.append("\nnot registered"sv);
  41. m_text = builder.to_deprecated_string();
  42. }
  43. void UnregisteredWidget::paint_event(GUI::PaintEvent& event)
  44. {
  45. GUI::Painter painter(*this);
  46. painter.add_clip_rect(event.rect());
  47. painter.fill_rect(event.rect(), Gfx::Color::DarkRed);
  48. painter.draw_text(rect(), m_text, Gfx::TextAlignment::Center, Color::White);
  49. }
  50. }
  51. ErrorOr<NonnullRefPtr<MainWidget>> MainWidget::try_create(GUI::Icon const& icon)
  52. {
  53. auto main_widget = TRY(try_make_ref_counted<MainWidget>());
  54. TRY(main_widget->load_from_gml(gml_playground_window_gml));
  55. main_widget->m_icon = icon;
  56. main_widget->m_toolbar = main_widget->find_descendant_of_type_named<GUI::Toolbar>("toolbar");
  57. main_widget->m_splitter = main_widget->find_descendant_of_type_named<GUI::HorizontalSplitter>("splitter");
  58. main_widget->m_editor = main_widget->find_descendant_of_type_named<GUI::TextEditor>("text_editor");
  59. main_widget->m_preview_frame_widget = main_widget->find_descendant_of_type_named<GUI::Frame>("preview_frame");
  60. main_widget->m_preview_window = TRY(GUI::Window::try_create());
  61. main_widget->m_preview_window->set_title("Preview - GML Playground");
  62. main_widget->m_preview_window->set_icon(icon.bitmap_for_size(16));
  63. main_widget->m_preview_window_widget = TRY(main_widget->m_preview_window->set_main_widget<GUI::Widget>());
  64. main_widget->m_preview_window_widget->set_fill_with_background_color(true);
  65. main_widget->m_preview = main_widget->m_preview_frame_widget;
  66. main_widget->m_editor->set_syntax_highlighter(make<GUI::GML::SyntaxHighlighter>());
  67. main_widget->m_editor->set_autocomplete_provider(make<GUI::GML::AutocompleteProvider>());
  68. main_widget->m_editor->set_should_autocomplete_automatically(true);
  69. main_widget->m_editor->set_automatic_indentation_enabled(true);
  70. main_widget->m_editor->set_ruler_visible(true);
  71. main_widget->m_editor->on_change = [main_widget = main_widget.ptr()] {
  72. main_widget->m_preview->remove_all_children();
  73. // FIXME: Parsing errors happen while the user is typing. What should we do about them?
  74. (void)main_widget->m_preview->load_from_gml(main_widget->m_editor->text(), [](DeprecatedString const& class_name) -> ErrorOr<NonnullRefPtr<Core::Object>> {
  75. return UnregisteredWidget::try_create(class_name);
  76. });
  77. };
  78. main_widget->m_editor->on_modified_change = [main_widget = main_widget.ptr()](bool modified) {
  79. main_widget->window()->set_modified(modified);
  80. };
  81. return main_widget;
  82. }
  83. void MainWidget::update_title()
  84. {
  85. window()->set_title(DeprecatedString::formatted("{}[*] - GML Playground", m_file_path.is_empty() ? "Untitled"sv : m_file_path.view()));
  86. };
  87. void MainWidget::load_file(FileSystemAccessClient::File file)
  88. {
  89. auto buffer_or_error = file.stream().read_until_eof();
  90. if (buffer_or_error.is_error())
  91. return;
  92. m_editor->set_text(buffer_or_error.release_value());
  93. m_editor->set_focus(true);
  94. m_file_path = file.filename().to_deprecated_string();
  95. update_title();
  96. GUI::Application::the()->set_most_recently_open_file(file.filename());
  97. };
  98. ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
  99. {
  100. auto file_menu = TRY(window.try_add_menu("&File"_short_string));
  101. auto save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
  102. auto response = FileSystemAccessClient::Client::the().save_file(&window, "Untitled", "gml");
  103. if (response.is_error())
  104. return;
  105. auto file = response.value().release_stream();
  106. if (auto result = m_editor->write_to_file(*file); result.is_error()) {
  107. GUI::MessageBox::show(&window, DeprecatedString::formatted("Unable to save file: {}\n"sv, result.release_error()), "Error"sv, GUI::MessageBox::Type::Error);
  108. return;
  109. }
  110. m_file_path = response.value().filename().to_deprecated_string();
  111. update_title();
  112. GUI::Application::the()->set_most_recently_open_file(response.value().filename());
  113. });
  114. m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
  115. if (m_file_path.is_empty()) {
  116. save_as_action->activate();
  117. return;
  118. }
  119. auto response = FileSystemAccessClient::Client::the().request_file(&window, m_file_path, Core::File::OpenMode::Truncate | Core::File::OpenMode::Write);
  120. if (response.is_error())
  121. return;
  122. auto file = response.value().release_stream();
  123. if (auto result = m_editor->write_to_file(*file); result.is_error()) {
  124. GUI::MessageBox::show(&window, DeprecatedString::formatted("Unable to save file: {}\n"sv, result.release_error()), "Error"sv, GUI::MessageBox::Type::Error);
  125. return;
  126. }
  127. update_title();
  128. });
  129. auto open_action = GUI::CommonActions::make_open_action([&](auto&) {
  130. if (window.is_modified()) {
  131. auto result = GUI::MessageBox::ask_about_unsaved_changes(&window, m_file_path, m_editor->document().undo_stack().last_unmodified_timestamp());
  132. if (result == GUI::MessageBox::ExecResult::Yes)
  133. m_save_action->activate();
  134. if (result != GUI::MessageBox::ExecResult::No && window.is_modified())
  135. return;
  136. }
  137. auto response = FileSystemAccessClient::Client::the().open_file(&window);
  138. if (response.is_error())
  139. return;
  140. load_file(response.release_value());
  141. });
  142. TRY(file_menu->try_add_action(open_action));
  143. TRY(file_menu->try_add_action(*m_save_action));
  144. TRY(file_menu->try_add_action(save_as_action));
  145. TRY(file_menu->try_add_separator());
  146. TRY(file_menu->add_recent_files_list([&](auto& action) {
  147. if (window.is_modified()) {
  148. auto result = GUI::MessageBox::ask_about_unsaved_changes(&window, m_file_path, m_editor->document().undo_stack().last_unmodified_timestamp());
  149. if (result == GUI::MessageBox::ExecResult::Yes)
  150. m_save_action->activate();
  151. if (result != GUI::MessageBox::ExecResult::No && window.is_modified())
  152. return;
  153. }
  154. auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(&window, action.text());
  155. if (response.is_error())
  156. return;
  157. load_file(response.release_value());
  158. }));
  159. TRY(file_menu->try_add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  160. if (window.on_close_request() == GUI::Window::CloseRequestDecision::Close)
  161. GUI::Application::the()->quit();
  162. })));
  163. auto edit_menu = TRY(window.try_add_menu("&Edit"_short_string));
  164. TRY(edit_menu->try_add_action(m_editor->undo_action()));
  165. TRY(edit_menu->try_add_action(m_editor->redo_action()));
  166. TRY(edit_menu->try_add_separator());
  167. TRY(edit_menu->try_add_action(m_editor->cut_action()));
  168. TRY(edit_menu->try_add_action(m_editor->copy_action()));
  169. TRY(edit_menu->try_add_action(m_editor->paste_action()));
  170. TRY(edit_menu->try_add_separator());
  171. TRY(edit_menu->try_add_action(m_editor->select_all_action()));
  172. TRY(edit_menu->try_add_action(m_editor->go_to_line_action()));
  173. TRY(edit_menu->try_add_separator());
  174. 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&) {
  175. auto formatted_gml_or_error = GUI::GML::format_gml(m_editor->text());
  176. if (!formatted_gml_or_error.is_error()) {
  177. m_editor->replace_all_text_without_resetting_undo_stack(formatted_gml_or_error.release_value());
  178. } else {
  179. GUI::MessageBox::show(
  180. &window,
  181. DeprecatedString::formatted("GML could not be formatted: {}", formatted_gml_or_error.error()),
  182. "Error"sv,
  183. GUI::MessageBox::Type::Error);
  184. }
  185. });
  186. TRY(edit_menu->try_add_action(format_gml_action));
  187. auto vim_emulation_setting_action = GUI::Action::create_checkable("&Vim Emulation", { Mod_Ctrl | Mod_Shift | Mod_Alt, Key_V }, [&](auto& action) {
  188. if (action.is_checked())
  189. m_editor->set_editing_engine(make<GUI::VimEditingEngine>());
  190. else
  191. m_editor->set_editing_engine(make<GUI::RegularEditingEngine>());
  192. });
  193. vim_emulation_setting_action->set_checked(false);
  194. TRY(edit_menu->try_add_action(vim_emulation_setting_action));
  195. auto view_menu = TRY(window.try_add_menu("&View"_short_string));
  196. m_views_group.set_exclusive(true);
  197. m_views_group.set_unchecking_allowed(false);
  198. auto view_frame_action = GUI::Action::create_checkable("&Frame", [&](auto&) {
  199. dbgln("View switched to frame");
  200. m_preview = m_preview_frame_widget;
  201. m_editor->on_change();
  202. m_preview_window->hide();
  203. m_preview_frame_widget->set_preferred_width(m_splitter->width() / 2);
  204. m_preview_frame_widget->set_visible(true);
  205. });
  206. view_menu->add_action(view_frame_action);
  207. m_views_group.add_action(view_frame_action);
  208. view_frame_action->set_checked(true);
  209. auto view_window_action = GUI::Action::create_checkable("&Window", [&](auto&) {
  210. dbgln("View switched to window");
  211. m_preview = m_preview_window_widget;
  212. m_editor->on_change();
  213. m_preview_window->resize(400, 300);
  214. m_preview_window->show();
  215. m_preview_frame_widget->set_visible(false);
  216. });
  217. view_menu->add_action(view_window_action);
  218. m_views_group.add_action(view_window_action);
  219. m_preview_window->on_close = [&] {
  220. view_frame_action->activate();
  221. };
  222. auto help_menu = TRY(window.try_add_menu("&Help"_short_string));
  223. TRY(help_menu->try_add_action(GUI::CommonActions::make_command_palette_action(&window)));
  224. TRY(help_menu->try_add_action(GUI::CommonActions::make_help_action([](auto&) {
  225. Desktop::Launcher::open(URL::create_with_file_scheme("/usr/share/man/man1/Applications/GMLPlayground.md"), "/bin/Help");
  226. })));
  227. TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("GML Playground", m_icon, &window)));
  228. (void)TRY(m_toolbar->try_add_action(open_action));
  229. (void)TRY(m_toolbar->try_add_action(*m_save_action));
  230. (void)TRY(m_toolbar->try_add_action(save_as_action));
  231. TRY(m_toolbar->try_add_separator());
  232. (void)TRY(m_toolbar->try_add_action(m_editor->cut_action()));
  233. (void)TRY(m_toolbar->try_add_action(m_editor->copy_action()));
  234. (void)TRY(m_toolbar->try_add_action(m_editor->paste_action()));
  235. TRY(m_toolbar->try_add_separator());
  236. (void)TRY(m_toolbar->try_add_action(m_editor->undo_action()));
  237. (void)TRY(m_toolbar->try_add_action(m_editor->redo_action()));
  238. TRY(m_toolbar->try_add_separator());
  239. (void)TRY(m_toolbar->try_add_action(format_gml_action));
  240. return {};
  241. }
  242. GUI::Window::CloseRequestDecision MainWidget::request_close()
  243. {
  244. if (!window()->is_modified())
  245. return GUI::Window::CloseRequestDecision::Close;
  246. auto result = GUI::MessageBox::ask_about_unsaved_changes(window(), m_file_path, m_editor->document().undo_stack().last_unmodified_timestamp());
  247. if (result == GUI::MessageBox::ExecResult::Yes) {
  248. m_save_action->activate();
  249. if (window()->is_modified())
  250. return GUI::Window::CloseRequestDecision::StayOpen;
  251. return GUI::Window::CloseRequestDecision::Close;
  252. }
  253. if (result == GUI::MessageBox::ExecResult::No)
  254. return GUI::Window::CloseRequestDecision::Close;
  255. return GUI::Window::CloseRequestDecision::StayOpen;
  256. }