MainWidget.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "MainWidget.h"
  8. #include <AK/Optional.h>
  9. #include <AK/StringBuilder.h>
  10. #include <AK/URL.h>
  11. #include <Applications/TextEditor/TextEditorWindowGML.h>
  12. #include <LibConfig/Client.h>
  13. #include <LibCore/File.h>
  14. #include <LibCpp/SyntaxHighlighter.h>
  15. #include <LibDesktop/Launcher.h>
  16. #include <LibGUI/Action.h>
  17. #include <LibGUI/BoxLayout.h>
  18. #include <LibGUI/Button.h>
  19. #include <LibGUI/CheckBox.h>
  20. #include <LibGUI/FilePicker.h>
  21. #include <LibGUI/FontPicker.h>
  22. #include <LibGUI/GML/SyntaxHighlighter.h>
  23. #include <LibGUI/GitCommitSyntaxHighlighter.h>
  24. #include <LibGUI/GroupBox.h>
  25. #include <LibGUI/INISyntaxHighlighter.h>
  26. #include <LibGUI/Menu.h>
  27. #include <LibGUI/Menubar.h>
  28. #include <LibGUI/MessageBox.h>
  29. #include <LibGUI/RegularEditingEngine.h>
  30. #include <LibGUI/Statusbar.h>
  31. #include <LibGUI/TextBox.h>
  32. #include <LibGUI/TextEditor.h>
  33. #include <LibGUI/Toolbar.h>
  34. #include <LibGUI/ToolbarContainer.h>
  35. #include <LibGUI/VimEditingEngine.h>
  36. #include <LibGfx/Font/Font.h>
  37. #include <LibGfx/Painter.h>
  38. #include <LibJS/SyntaxHighlighter.h>
  39. #include <LibMarkdown/Document.h>
  40. #include <LibSQL/AST/SyntaxHighlighter.h>
  41. #include <LibWeb/CSS/SyntaxHighlighter/SyntaxHighlighter.h>
  42. #include <LibWeb/HTML/SyntaxHighlighter/SyntaxHighlighter.h>
  43. #include <LibWeb/OutOfProcessWebView.h>
  44. #include <Shell/SyntaxHighlighter.h>
  45. namespace TextEditor {
  46. MainWidget::MainWidget()
  47. {
  48. load_from_gml(text_editor_window_gml);
  49. m_toolbar = *find_descendant_of_type_named<GUI::Toolbar>("toolbar");
  50. m_toolbar_container = *find_descendant_of_type_named<GUI::ToolbarContainer>("toolbar_container");
  51. m_editor = *find_descendant_of_type_named<GUI::TextEditor>("editor");
  52. m_editor->set_ruler_visible(true);
  53. m_editor->set_automatic_indentation_enabled(true);
  54. if (m_editor->editing_engine()->is_regular())
  55. m_editor->set_editing_engine(make<GUI::RegularEditingEngine>());
  56. else if (m_editor->editing_engine()->is_vim())
  57. m_editor->set_editing_engine(make<GUI::VimEditingEngine>());
  58. else
  59. VERIFY_NOT_REACHED();
  60. m_editor->on_change = [this] {
  61. update_preview();
  62. };
  63. m_editor->on_modified_change = [this](bool modified) {
  64. window()->set_modified(modified);
  65. };
  66. m_find_replace_widget = *find_descendant_of_type_named<GUI::GroupBox>("find_replace_widget");
  67. m_find_widget = *find_descendant_of_type_named<GUI::Widget>("find_widget");
  68. m_replace_widget = *find_descendant_of_type_named<GUI::Widget>("replace_widget");
  69. m_find_textbox = *find_descendant_of_type_named<GUI::TextBox>("find_textbox");
  70. m_find_textbox->set_placeholder("Find");
  71. m_replace_textbox = *find_descendant_of_type_named<GUI::TextBox>("replace_textbox");
  72. m_replace_textbox->set_placeholder("Replace");
  73. m_match_case_checkbox = *find_descendant_of_type_named<GUI::CheckBox>("match_case_checkbox");
  74. m_match_case_checkbox->on_checked = [this](auto is_checked) {
  75. m_match_case = is_checked;
  76. };
  77. m_match_case_checkbox->set_checked(true);
  78. m_regex_checkbox = *find_descendant_of_type_named<GUI::CheckBox>("regex_checkbox");
  79. m_regex_checkbox->on_checked = [this](auto is_checked) {
  80. m_use_regex = is_checked;
  81. };
  82. m_regex_checkbox->set_checked(false);
  83. m_wrap_around_checkbox = *find_descendant_of_type_named<GUI::CheckBox>("wrap_around_checkbox");
  84. m_wrap_around_checkbox->on_checked = [this](auto is_checked) {
  85. m_should_wrap = is_checked;
  86. };
  87. m_wrap_around_checkbox->set_checked(true);
  88. m_find_next_action = GUI::Action::create("Find &Next", { Mod_Ctrl, Key_G }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/find-next.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  89. find_text(GUI::TextEditor::SearchDirection::Forward, ShowMessageIfNoResutls::Yes);
  90. });
  91. m_find_previous_action = GUI::Action::create("Find Pr&evious", { Mod_Ctrl | Mod_Shift, Key_G }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/find-previous.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  92. find_text(GUI::TextEditor::SearchDirection::Backward, ShowMessageIfNoResutls::Yes);
  93. });
  94. m_replace_action = GUI::Action::create("Rep&lace", { Mod_Ctrl, Key_F1 }, [&](auto&) {
  95. auto needle = m_find_textbox->text();
  96. auto substitute = m_replace_textbox->text();
  97. if (needle.is_empty())
  98. return;
  99. if (m_use_regex)
  100. m_editor->document().update_regex_matches(needle);
  101. auto found_range = m_editor->document().find_next(needle, m_editor->normalized_selection().start(), m_should_wrap ? GUI::TextDocument::SearchShouldWrap::Yes : GUI::TextDocument::SearchShouldWrap::No, m_use_regex, m_match_case);
  102. if (found_range.is_valid()) {
  103. m_editor->set_selection(found_range);
  104. m_editor->insert_at_cursor_or_replace_selection(substitute);
  105. } else {
  106. GUI::MessageBox::show(window(),
  107. String::formatted("Not found: \"{}\"", needle),
  108. "Not found",
  109. GUI::MessageBox::Type::Information);
  110. }
  111. });
  112. m_replace_all_action = GUI::Action::create("Replace &All", { Mod_Ctrl, Key_F2 }, [&](auto&) {
  113. auto needle = m_find_textbox->text();
  114. auto substitute = m_replace_textbox->text();
  115. auto length_delta = substitute.length() - needle.length();
  116. if (needle.is_empty())
  117. return;
  118. if (m_use_regex)
  119. m_editor->document().update_regex_matches(needle);
  120. auto found_range = m_editor->document().find_next(needle, {}, GUI::TextDocument::SearchShouldWrap::No, m_use_regex, m_match_case);
  121. while (found_range.is_valid()) {
  122. m_editor->set_selection(found_range);
  123. m_editor->insert_at_cursor_or_replace_selection(substitute);
  124. auto next_start = GUI::TextPosition(found_range.end().line(), found_range.end().column() + length_delta);
  125. found_range = m_editor->document().find_next(needle, next_start, GUI::TextDocument::SearchShouldWrap::No, m_use_regex, m_match_case);
  126. }
  127. });
  128. m_find_previous_button = *find_descendant_of_type_named<GUI::Button>("find_previous_button");
  129. m_find_previous_button->set_action(*m_find_previous_action);
  130. m_find_previous_button->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/find-previous.png").release_value_but_fixme_should_propagate_errors());
  131. m_find_next_button = *find_descendant_of_type_named<GUI::Button>("find_next_button");
  132. m_find_next_button->set_action(*m_find_next_action);
  133. m_find_next_button->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/find-next.png").release_value_but_fixme_should_propagate_errors());
  134. m_find_textbox->on_return_pressed = [this] {
  135. m_find_next_button->click();
  136. };
  137. m_find_textbox->on_escape_pressed = [this] {
  138. m_find_replace_widget->set_visible(false);
  139. m_editor->set_focus(true);
  140. m_editor->reset_search_results();
  141. };
  142. m_find_textbox->on_change = [this] {
  143. m_editor->reset_search_results();
  144. find_text(GUI::TextEditor::SearchDirection::Forward, ShowMessageIfNoResutls::No);
  145. };
  146. m_replace_button = *find_descendant_of_type_named<GUI::Button>("replace_button");
  147. m_replace_button->set_action(*m_replace_action);
  148. m_replace_all_button = *find_descendant_of_type_named<GUI::Button>("replace_all_button");
  149. m_replace_all_button->set_action(*m_replace_all_action);
  150. m_replace_textbox->on_return_pressed = [this] {
  151. m_replace_button->click();
  152. };
  153. m_replace_textbox->on_escape_pressed = [this] {
  154. m_find_replace_widget->set_visible(false);
  155. m_editor->set_focus(true);
  156. };
  157. m_vim_emulation_setting_action = GUI::Action::create_checkable("&Vim Emulation", { Mod_Ctrl | Mod_Shift | Mod_Alt, Key_V }, [&](auto& action) {
  158. if (action.is_checked())
  159. m_editor->set_editing_engine(make<GUI::VimEditingEngine>());
  160. else
  161. m_editor->set_editing_engine(make<GUI::RegularEditingEngine>());
  162. });
  163. m_vim_emulation_setting_action->set_checked(false);
  164. m_find_replace_action = GUI::Action::create("&Find/Replace...", { Mod_Ctrl, Key_F }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/find.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  165. m_find_replace_widget->set_visible(true);
  166. m_find_widget->set_visible(true);
  167. m_replace_widget->set_visible(true);
  168. m_find_textbox->set_focus(true);
  169. if (m_editor->has_selection()) {
  170. auto selected_text = m_editor->document().text_in_range(m_editor->normalized_selection());
  171. m_find_textbox->set_text(selected_text);
  172. }
  173. m_find_textbox->select_all();
  174. });
  175. m_editor->add_custom_context_menu_action(*m_find_replace_action);
  176. m_editor->add_custom_context_menu_action(*m_find_next_action);
  177. m_editor->add_custom_context_menu_action(*m_find_previous_action);
  178. m_line_column_statusbar_menu = GUI::Menu::construct();
  179. m_syntax_statusbar_menu = GUI::Menu::construct();
  180. m_statusbar = *find_descendant_of_type_named<GUI::Statusbar>("statusbar");
  181. m_statusbar->segment(1).set_mode(GUI::Statusbar::Segment::Mode::Auto);
  182. m_statusbar->segment(1).set_clickable(true);
  183. m_statusbar->segment(1).set_menu(m_syntax_statusbar_menu);
  184. m_statusbar->segment(2).set_mode(GUI::Statusbar::Segment::Mode::Fixed);
  185. auto width = font().width("Ln 0000, Col 000") + font().max_glyph_width();
  186. m_statusbar->segment(2).set_fixed_width(width);
  187. m_statusbar->segment(2).set_clickable(true);
  188. m_statusbar->segment(2).set_menu(m_line_column_statusbar_menu);
  189. GUI::Application::the()->on_action_enter = [this](GUI::Action& action) {
  190. auto text = action.status_tip();
  191. if (text.is_empty())
  192. text = Gfx::parse_ampersand_string(action.text());
  193. m_statusbar->set_override_text(move(text));
  194. };
  195. GUI::Application::the()->on_action_leave = [this](GUI::Action&) {
  196. m_statusbar->set_override_text({});
  197. };
  198. m_editor->on_cursor_change = [this] { update_statusbar(); };
  199. m_editor->on_selection_change = [this] { update_statusbar(); };
  200. m_editor->on_highlighter_change = [this] { update_statusbar(); };
  201. m_new_action = GUI::Action::create("&New", { Mod_Ctrl, Key_N }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/new.png").release_value_but_fixme_should_propagate_errors(), [this](GUI::Action const&) {
  202. if (editor().document().is_modified()) {
  203. auto save_document_first_result = GUI::MessageBox::ask_about_unsaved_changes(window(), m_path, editor().document().undo_stack().last_unmodified_timestamp());
  204. if (save_document_first_result == GUI::Dialog::ExecResult::Yes)
  205. m_save_action->activate();
  206. if (save_document_first_result != GUI::Dialog::ExecResult::No && editor().document().is_modified())
  207. return;
  208. }
  209. m_editor->set_text(StringView());
  210. set_path({});
  211. update_title();
  212. });
  213. m_open_action = GUI::CommonActions::make_open_action([this](auto&) {
  214. if (editor().document().is_modified()) {
  215. auto save_document_first_result = GUI::MessageBox::ask_about_unsaved_changes(window(), m_path, editor().document().undo_stack().last_unmodified_timestamp());
  216. if (save_document_first_result == GUI::Dialog::ExecResult::Yes)
  217. m_save_action->activate();
  218. if (save_document_first_result != GUI::Dialog::ExecResult::No && editor().document().is_modified())
  219. return;
  220. }
  221. auto response = FileSystemAccessClient::Client::the().try_open_file(window());
  222. if (response.is_error())
  223. return;
  224. read_file(*response.value());
  225. });
  226. m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
  227. auto response = FileSystemAccessClient::Client::the().try_save_file(window(), m_name, m_extension);
  228. if (response.is_error())
  229. return;
  230. auto file = response.release_value();
  231. if (!m_editor->write_to_file(*file)) {
  232. GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
  233. return;
  234. }
  235. set_path(file->filename());
  236. dbgln("Wrote document to {}", file->filename());
  237. });
  238. m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
  239. if (m_path.is_empty()) {
  240. m_save_as_action->activate();
  241. return;
  242. }
  243. auto response = FileSystemAccessClient::Client::the().try_request_file(window(), m_path, Core::OpenMode::Truncate | Core::OpenMode::WriteOnly);
  244. if (response.is_error())
  245. return;
  246. if (!m_editor->write_to_file(*response.value())) {
  247. GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
  248. }
  249. });
  250. m_toolbar->add_action(*m_new_action);
  251. m_toolbar->add_action(*m_open_action);
  252. m_toolbar->add_action(*m_save_action);
  253. m_toolbar->add_separator();
  254. m_toolbar->add_action(m_editor->cut_action());
  255. m_toolbar->add_action(m_editor->copy_action());
  256. m_toolbar->add_action(m_editor->paste_action());
  257. m_toolbar->add_separator();
  258. m_toolbar->add_action(m_editor->undo_action());
  259. m_toolbar->add_action(m_editor->redo_action());
  260. }
  261. Web::OutOfProcessWebView& MainWidget::ensure_web_view()
  262. {
  263. if (!m_page_view) {
  264. auto& web_view_container = *find_descendant_of_type_named<GUI::Widget>("web_view_container");
  265. m_page_view = web_view_container.add<Web::OutOfProcessWebView>();
  266. m_page_view->on_link_hover = [this](auto& url) {
  267. if (url.is_valid())
  268. m_statusbar->set_text(url.to_string());
  269. else
  270. update_statusbar();
  271. };
  272. m_page_view->on_link_click = [&](auto& url, auto&, unsigned) {
  273. if (!Desktop::Launcher::open(url)) {
  274. GUI::MessageBox::show(
  275. window(),
  276. String::formatted("The link to '{}' could not be opened.", url),
  277. "Failed to open link",
  278. GUI::MessageBox::Type::Error);
  279. }
  280. };
  281. }
  282. return *m_page_view;
  283. }
  284. void MainWidget::initialize_menubar(GUI::Window& window)
  285. {
  286. auto& file_menu = window.add_menu("&File");
  287. file_menu.add_action(*m_new_action);
  288. file_menu.add_action(*m_open_action);
  289. file_menu.add_action(*m_save_action);
  290. file_menu.add_action(*m_save_as_action);
  291. file_menu.add_separator();
  292. file_menu.add_action(GUI::CommonActions::make_quit_action([this](auto&) {
  293. if (!request_close())
  294. return;
  295. GUI::Application::the()->quit();
  296. }));
  297. auto& edit_menu = window.add_menu("&Edit");
  298. edit_menu.add_action(m_editor->undo_action());
  299. edit_menu.add_action(m_editor->redo_action());
  300. edit_menu.add_separator();
  301. edit_menu.add_action(m_editor->cut_action());
  302. edit_menu.add_action(m_editor->copy_action());
  303. edit_menu.add_action(m_editor->paste_action());
  304. edit_menu.add_separator();
  305. edit_menu.add_action(*m_vim_emulation_setting_action);
  306. edit_menu.add_separator();
  307. edit_menu.add_action(*m_find_replace_action);
  308. edit_menu.add_action(*m_find_next_action);
  309. edit_menu.add_action(*m_find_previous_action);
  310. edit_menu.add_action(*m_replace_action);
  311. edit_menu.add_action(*m_replace_all_action);
  312. m_no_preview_action = GUI::Action::create_checkable(
  313. "&No Preview", [this](auto&) {
  314. set_preview_mode(PreviewMode::None);
  315. });
  316. m_markdown_preview_action = GUI::Action::create_checkable(
  317. "&Markdown Preview", [this](auto&) {
  318. set_preview_mode(PreviewMode::Markdown);
  319. },
  320. this);
  321. m_html_preview_action = GUI::Action::create_checkable(
  322. "&HTML Preview", [this](auto&) {
  323. set_preview_mode(PreviewMode::HTML);
  324. },
  325. this);
  326. m_preview_actions.add_action(*m_no_preview_action);
  327. m_preview_actions.add_action(*m_markdown_preview_action);
  328. m_preview_actions.add_action(*m_html_preview_action);
  329. m_preview_actions.set_exclusive(true);
  330. m_layout_toolbar_action = GUI::Action::create_checkable("&Toolbar", [&](auto& action) {
  331. action.is_checked() ? m_toolbar_container->set_visible(true) : m_toolbar_container->set_visible(false);
  332. Config::write_bool("TextEditor", "Layout", "ShowToolbar", action.is_checked());
  333. });
  334. auto show_toolbar = Config::read_bool("TextEditor", "Layout", "ShowToolbar", true);
  335. m_layout_toolbar_action->set_checked(show_toolbar);
  336. m_toolbar_container->set_visible(show_toolbar);
  337. m_layout_statusbar_action = GUI::Action::create_checkable("&Status Bar", [&](auto& action) {
  338. action.is_checked() ? m_statusbar->set_visible(true) : m_statusbar->set_visible(false);
  339. Config::write_bool("TextEditor", "Layout", "ShowStatusbar", action.is_checked());
  340. update_statusbar();
  341. });
  342. auto show_statusbar = Config::read_bool("TextEditor", "Layout", "ShowStatusbar", true);
  343. m_layout_statusbar_action->set_checked(show_statusbar);
  344. m_statusbar->set_visible(show_statusbar);
  345. m_layout_ruler_action = GUI::Action::create_checkable("&Ruler", [&](auto& action) {
  346. action.is_checked() ? m_editor->set_ruler_visible(true) : m_editor->set_ruler_visible(false);
  347. Config::write_bool("TextEditor", "Layout", "ShowRuler", action.is_checked());
  348. });
  349. auto show_ruler = Config::read_bool("TextEditor", "Layout", "ShowRuler", true);
  350. m_layout_ruler_action->set_checked(show_ruler);
  351. m_editor->set_ruler_visible(show_ruler);
  352. auto& view_menu = window.add_menu("&View");
  353. auto& layout_menu = view_menu.add_submenu("&Layout");
  354. layout_menu.add_action(*m_layout_toolbar_action);
  355. layout_menu.add_action(*m_layout_statusbar_action);
  356. layout_menu.add_action(*m_layout_ruler_action);
  357. view_menu.add_separator();
  358. view_menu.add_action(GUI::Action::create("Editor &Font...", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-font-editor.png").release_value_but_fixme_should_propagate_errors(),
  359. [&](auto&) {
  360. auto picker = GUI::FontPicker::construct(&window, &m_editor->font(), false);
  361. if (picker->exec() == GUI::Dialog::ExecResult::OK) {
  362. dbgln("setting font {}", picker->font()->qualified_name());
  363. m_editor->set_font(picker->font());
  364. }
  365. }));
  366. view_menu.add_separator();
  367. m_wrapping_mode_actions.set_exclusive(true);
  368. auto& wrapping_mode_menu = view_menu.add_submenu("&Wrapping Mode");
  369. m_no_wrapping_action = GUI::Action::create_checkable("&No Wrapping", [&](auto&) {
  370. m_editor->set_wrapping_mode(GUI::TextEditor::WrappingMode::NoWrap);
  371. Config::write_string("TextEditor", "View", "WrappingMode", "None");
  372. });
  373. m_wrap_anywhere_action = GUI::Action::create_checkable("Wrap &Anywhere", [&](auto&) {
  374. m_editor->set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAnywhere);
  375. Config::write_string("TextEditor", "View", "WrappingMode", "Anywhere");
  376. });
  377. m_wrap_at_words_action = GUI::Action::create_checkable("Wrap at &Words", [&](auto&) {
  378. m_editor->set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAtWords);
  379. Config::write_string("TextEditor", "View", "WrappingMode", "Words");
  380. });
  381. m_wrapping_mode_actions.add_action(*m_no_wrapping_action);
  382. m_wrapping_mode_actions.add_action(*m_wrap_anywhere_action);
  383. m_wrapping_mode_actions.add_action(*m_wrap_at_words_action);
  384. wrapping_mode_menu.add_action(*m_no_wrapping_action);
  385. wrapping_mode_menu.add_action(*m_wrap_anywhere_action);
  386. wrapping_mode_menu.add_action(*m_wrap_at_words_action);
  387. auto word_wrap = Config::read_string("TextEditor", "View", "WrappingMode", "Words");
  388. if (word_wrap == "None") {
  389. m_no_wrapping_action->set_checked(true);
  390. m_editor->set_wrapping_mode(GUI::TextEditor::WrappingMode::NoWrap);
  391. } else if (word_wrap == "Anywhere") {
  392. m_wrap_anywhere_action->set_checked(true);
  393. m_editor->set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAnywhere);
  394. } else {
  395. m_wrap_at_words_action->set_checked(true);
  396. m_editor->set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAtWords);
  397. }
  398. m_soft_tab_width_actions.set_exclusive(true);
  399. auto& soft_tab_width_menu = view_menu.add_submenu("&Tab Width");
  400. m_soft_tab_1_width_action = GUI::Action::create_checkable("1", [&](auto&) {
  401. m_editor->set_soft_tab_width(1);
  402. });
  403. m_soft_tab_2_width_action = GUI::Action::create_checkable("2", [&](auto&) {
  404. m_editor->set_soft_tab_width(2);
  405. });
  406. m_soft_tab_4_width_action = GUI::Action::create_checkable("4", [&](auto&) {
  407. m_editor->set_soft_tab_width(4);
  408. });
  409. m_soft_tab_8_width_action = GUI::Action::create_checkable("8", [&](auto&) {
  410. m_editor->set_soft_tab_width(8);
  411. });
  412. m_soft_tab_16_width_action = GUI::Action::create_checkable("16", [&](auto&) {
  413. m_editor->set_soft_tab_width(16);
  414. });
  415. m_soft_tab_width_actions.add_action(*m_soft_tab_1_width_action);
  416. m_soft_tab_width_actions.add_action(*m_soft_tab_2_width_action);
  417. m_soft_tab_width_actions.add_action(*m_soft_tab_4_width_action);
  418. m_soft_tab_width_actions.add_action(*m_soft_tab_8_width_action);
  419. m_soft_tab_width_actions.add_action(*m_soft_tab_16_width_action);
  420. soft_tab_width_menu.add_action(*m_soft_tab_1_width_action);
  421. soft_tab_width_menu.add_action(*m_soft_tab_2_width_action);
  422. soft_tab_width_menu.add_action(*m_soft_tab_4_width_action);
  423. soft_tab_width_menu.add_action(*m_soft_tab_8_width_action);
  424. soft_tab_width_menu.add_action(*m_soft_tab_16_width_action);
  425. m_soft_tab_4_width_action->set_checked(true);
  426. view_menu.add_separator();
  427. m_visualize_trailing_whitespace_action = GUI::Action::create_checkable("T&railing Whitespace", [&](auto&) {
  428. m_editor->set_visualize_trailing_whitespace(m_visualize_trailing_whitespace_action->is_checked());
  429. });
  430. m_visualize_leading_whitespace_action = GUI::Action::create_checkable("L&eading Whitespace", [&](auto&) {
  431. m_editor->set_visualize_leading_whitespace(m_visualize_leading_whitespace_action->is_checked());
  432. });
  433. m_visualize_trailing_whitespace_action->set_checked(true);
  434. m_visualize_trailing_whitespace_action->set_status_tip("Visualize trailing whitespace");
  435. m_visualize_leading_whitespace_action->set_status_tip("Visualize leading whitespace");
  436. view_menu.add_action(*m_visualize_trailing_whitespace_action);
  437. view_menu.add_action(*m_visualize_leading_whitespace_action);
  438. m_cursor_line_highlighting_action = GUI::Action::create_checkable("L&ine Highlighting", [&](auto&) {
  439. m_editor->set_cursor_line_highlighting(m_cursor_line_highlighting_action->is_checked());
  440. });
  441. m_cursor_line_highlighting_action->set_checked(true);
  442. m_cursor_line_highlighting_action->set_status_tip("Highlight the current line");
  443. view_menu.add_action(*m_cursor_line_highlighting_action);
  444. view_menu.add_separator();
  445. view_menu.add_action(*m_no_preview_action);
  446. view_menu.add_action(*m_markdown_preview_action);
  447. view_menu.add_action(*m_html_preview_action);
  448. m_no_preview_action->set_checked(true);
  449. view_menu.add_separator();
  450. syntax_actions.set_exclusive(true);
  451. auto& syntax_menu = view_menu.add_submenu("&Syntax");
  452. m_plain_text_highlight = GUI::Action::create_checkable("&Plain Text", [&](auto&) {
  453. m_statusbar->set_text(1, "Plain Text");
  454. m_editor->set_syntax_highlighter({});
  455. m_editor->update();
  456. });
  457. m_plain_text_highlight->set_checked(true);
  458. m_statusbar->set_text(1, "Plain Text");
  459. syntax_actions.add_action(*m_plain_text_highlight);
  460. syntax_menu.add_action(*m_plain_text_highlight);
  461. m_cpp_highlight = GUI::Action::create_checkable("&C++", [&](auto&) {
  462. m_editor->set_syntax_highlighter(make<Cpp::SyntaxHighlighter>());
  463. m_editor->update();
  464. });
  465. syntax_actions.add_action(*m_cpp_highlight);
  466. syntax_menu.add_action(*m_cpp_highlight);
  467. m_js_highlight = GUI::Action::create_checkable("&JavaScript", [&](auto&) {
  468. m_editor->set_syntax_highlighter(make<JS::SyntaxHighlighter>());
  469. m_editor->update();
  470. });
  471. syntax_actions.add_action(*m_js_highlight);
  472. syntax_menu.add_action(*m_js_highlight);
  473. m_css_highlight = GUI::Action::create_checkable("C&SS", [&](auto&) {
  474. m_editor->set_syntax_highlighter(make<Web::CSS::SyntaxHighlighter>());
  475. m_editor->update();
  476. });
  477. syntax_actions.add_action(*m_css_highlight);
  478. syntax_menu.add_action(*m_css_highlight);
  479. m_html_highlight = GUI::Action::create_checkable("&HTML File", [&](auto&) {
  480. m_editor->set_syntax_highlighter(make<Web::HTML::SyntaxHighlighter>());
  481. m_editor->update();
  482. });
  483. syntax_actions.add_action(*m_html_highlight);
  484. syntax_menu.add_action(*m_html_highlight);
  485. m_git_highlight = GUI::Action::create_checkable("Gi&t Commit", [&](auto&) {
  486. m_editor->set_syntax_highlighter(make<GUI::GitCommitSyntaxHighlighter>());
  487. m_editor->update();
  488. });
  489. syntax_actions.add_action(*m_git_highlight);
  490. syntax_menu.add_action(*m_git_highlight);
  491. m_gml_highlight = GUI::Action::create_checkable("&GML", [&](auto&) {
  492. m_editor->set_syntax_highlighter(make<GUI::GML::SyntaxHighlighter>());
  493. m_editor->update();
  494. });
  495. syntax_actions.add_action(*m_gml_highlight);
  496. syntax_menu.add_action(*m_gml_highlight);
  497. m_ini_highlight = GUI::Action::create_checkable("&INI File", [&](auto&) {
  498. m_editor->set_syntax_highlighter(make<GUI::IniSyntaxHighlighter>());
  499. m_editor->update();
  500. });
  501. syntax_actions.add_action(*m_ini_highlight);
  502. syntax_menu.add_action(*m_ini_highlight);
  503. m_shell_highlight = GUI::Action::create_checkable("Sh&ell File", [&](auto&) {
  504. m_editor->set_syntax_highlighter(make<Shell::SyntaxHighlighter>());
  505. m_editor->update();
  506. });
  507. syntax_actions.add_action(*m_shell_highlight);
  508. syntax_menu.add_action(*m_shell_highlight);
  509. m_sql_highlight = GUI::Action::create_checkable("S&QL File", [&](auto&) {
  510. m_editor->set_syntax_highlighter(make<SQL::AST::SyntaxHighlighter>());
  511. m_editor->update();
  512. });
  513. syntax_actions.add_action(*m_sql_highlight);
  514. syntax_menu.add_action(*m_sql_highlight);
  515. auto& help_menu = window.add_menu("&Help");
  516. help_menu.add_action(GUI::CommonActions::make_help_action([](auto&) {
  517. Desktop::Launcher::open(URL::create_with_file_protocol("/usr/share/man/man1/TextEditor.md"), "/bin/Help");
  518. }));
  519. help_menu.add_action(GUI::CommonActions::make_about_action("Text Editor", GUI::Icon::default_icon("app-text-editor"), &window));
  520. auto& wrapping_statusbar_menu = m_line_column_statusbar_menu->add_submenu("&Wrapping Mode");
  521. wrapping_statusbar_menu.add_action(*m_no_wrapping_action);
  522. wrapping_statusbar_menu.add_action(*m_wrap_anywhere_action);
  523. wrapping_statusbar_menu.add_action(*m_wrap_at_words_action);
  524. auto& tab_width_statusbar_menu = m_line_column_statusbar_menu->add_submenu("&Tab Width");
  525. tab_width_statusbar_menu.add_action(*m_soft_tab_1_width_action);
  526. tab_width_statusbar_menu.add_action(*m_soft_tab_2_width_action);
  527. tab_width_statusbar_menu.add_action(*m_soft_tab_4_width_action);
  528. tab_width_statusbar_menu.add_action(*m_soft_tab_8_width_action);
  529. tab_width_statusbar_menu.add_action(*m_soft_tab_16_width_action);
  530. m_line_column_statusbar_menu->add_separator();
  531. m_line_column_statusbar_menu->add_action(*m_cursor_line_highlighting_action);
  532. m_syntax_statusbar_menu->add_action(*m_plain_text_highlight);
  533. m_syntax_statusbar_menu->add_action(*m_cpp_highlight);
  534. m_syntax_statusbar_menu->add_action(*m_css_highlight);
  535. m_syntax_statusbar_menu->add_action(*m_git_highlight);
  536. m_syntax_statusbar_menu->add_action(*m_gml_highlight);
  537. m_syntax_statusbar_menu->add_action(*m_html_highlight);
  538. m_syntax_statusbar_menu->add_action(*m_ini_highlight);
  539. m_syntax_statusbar_menu->add_action(*m_js_highlight);
  540. m_syntax_statusbar_menu->add_action(*m_shell_highlight);
  541. m_syntax_statusbar_menu->add_action(*m_sql_highlight);
  542. }
  543. void MainWidget::set_path(StringView path)
  544. {
  545. if (path.is_empty()) {
  546. m_path = {};
  547. m_name = {};
  548. m_extension = {};
  549. } else {
  550. auto lexical_path = LexicalPath(path);
  551. m_path = lexical_path.string();
  552. m_name = lexical_path.title();
  553. m_extension = lexical_path.extension();
  554. }
  555. if (m_extension == "c" || m_extension == "cc" || m_extension == "cxx" || m_extension == "cpp" || m_extension == "c++"
  556. || m_extension == "h" || m_extension == "hh" || m_extension == "hxx" || m_extension == "hpp" || m_extension == "h++") {
  557. m_cpp_highlight->activate();
  558. } else if (m_extension == "js" || m_extension == "mjs" || m_extension == "json") {
  559. m_js_highlight->activate();
  560. } else if (m_name == "COMMIT_EDITMSG") {
  561. m_git_highlight->activate();
  562. } else if (m_extension == "gml") {
  563. m_gml_highlight->activate();
  564. } else if (m_extension == "ini" || m_extension == "af") {
  565. m_ini_highlight->activate();
  566. } else if (m_extension == "sh" || m_extension == "bash") {
  567. m_shell_highlight->activate();
  568. } else if (m_extension == "sql") {
  569. m_sql_highlight->activate();
  570. } else if (m_extension == "html" || m_extension == "htm") {
  571. m_html_highlight->activate();
  572. } else if (m_extension == "css") {
  573. m_css_highlight->activate();
  574. } else {
  575. m_plain_text_highlight->activate();
  576. }
  577. if (m_auto_detect_preview_mode) {
  578. if (m_extension == "md")
  579. set_preview_mode(PreviewMode::Markdown);
  580. else if (m_extension == "html" || m_extension == "htm")
  581. set_preview_mode(PreviewMode::HTML);
  582. else
  583. set_preview_mode(PreviewMode::None);
  584. }
  585. update_title();
  586. }
  587. void MainWidget::update_title()
  588. {
  589. StringBuilder builder;
  590. if (m_path.is_empty())
  591. builder.append("Untitled");
  592. else
  593. builder.append(m_path);
  594. builder.append("[*] - Text Editor");
  595. window()->set_title(builder.to_string());
  596. }
  597. bool MainWidget::read_file(Core::File& file)
  598. {
  599. m_editor->set_text(file.read_all());
  600. set_path(file.filename());
  601. m_editor->set_focus(true);
  602. return true;
  603. }
  604. void MainWidget::open_nonexistent_file(String const& path)
  605. {
  606. m_editor->set_text({});
  607. set_path(path);
  608. m_editor->set_focus(true);
  609. }
  610. bool MainWidget::request_close()
  611. {
  612. if (!editor().document().is_modified())
  613. return true;
  614. auto result = GUI::MessageBox::ask_about_unsaved_changes(window(), m_path, editor().document().undo_stack().last_unmodified_timestamp());
  615. if (result == GUI::MessageBox::ExecResult::Yes) {
  616. m_save_action->activate();
  617. if (editor().document().is_modified())
  618. return false;
  619. return true;
  620. }
  621. if (result == GUI::MessageBox::ExecResult::No)
  622. return true;
  623. return false;
  624. }
  625. void MainWidget::drop_event(GUI::DropEvent& event)
  626. {
  627. event.accept();
  628. window()->move_to_front();
  629. if (event.mime_data().has_urls()) {
  630. auto urls = event.mime_data().urls();
  631. if (urls.is_empty())
  632. return;
  633. if (urls.size() > 1) {
  634. GUI::MessageBox::show(window(), "TextEditor can only open one file at a time!", "One at a time please!", GUI::MessageBox::Type::Error);
  635. return;
  636. }
  637. // TODO: A drop event should be considered user consent for opening a file
  638. auto response = FileSystemAccessClient::Client::the().try_request_file(window(), urls.first().path(), Core::OpenMode::ReadOnly);
  639. if (response.is_error())
  640. return;
  641. read_file(*response.value());
  642. }
  643. }
  644. void MainWidget::set_web_view_visible(bool visible)
  645. {
  646. if (!visible && !m_page_view)
  647. return;
  648. ensure_web_view();
  649. auto& web_view_container = *find_descendant_of_type_named<GUI::Widget>("web_view_container");
  650. web_view_container.set_visible(visible);
  651. }
  652. void MainWidget::set_preview_mode(PreviewMode mode)
  653. {
  654. if (m_preview_mode == mode)
  655. return;
  656. m_preview_mode = mode;
  657. if (m_preview_mode == PreviewMode::HTML) {
  658. m_html_preview_action->set_checked(true);
  659. set_web_view_visible(true);
  660. update_html_preview();
  661. } else if (m_preview_mode == PreviewMode::Markdown) {
  662. m_markdown_preview_action->set_checked(true);
  663. set_web_view_visible(true);
  664. update_markdown_preview();
  665. } else {
  666. m_no_preview_action->set_checked(true);
  667. set_web_view_visible(false);
  668. }
  669. }
  670. void MainWidget::update_preview()
  671. {
  672. switch (m_preview_mode) {
  673. case PreviewMode::Markdown:
  674. update_markdown_preview();
  675. break;
  676. case PreviewMode::HTML:
  677. update_html_preview();
  678. break;
  679. default:
  680. break;
  681. }
  682. }
  683. void MainWidget::update_markdown_preview()
  684. {
  685. auto document = Markdown::Document::parse(m_editor->text());
  686. if (document) {
  687. auto html = document->render_to_html();
  688. auto current_scroll_pos = m_page_view->visible_content_rect();
  689. m_page_view->load_html(html, URL::create_with_file_protocol(m_path));
  690. m_page_view->scroll_into_view(current_scroll_pos, true, true);
  691. }
  692. }
  693. void MainWidget::update_html_preview()
  694. {
  695. auto current_scroll_pos = m_page_view->visible_content_rect();
  696. m_page_view->load_html(m_editor->text(), URL::create_with_file_protocol(m_path));
  697. m_page_view->scroll_into_view(current_scroll_pos, true, true);
  698. }
  699. void MainWidget::update_statusbar()
  700. {
  701. if (!m_statusbar->is_visible())
  702. return;
  703. StringBuilder builder;
  704. if (m_editor->has_selection()) {
  705. String selected_text = m_editor->selected_text();
  706. auto word_count = m_editor->number_of_selected_words();
  707. builder.appendff("{} {} ({} {}) selected", selected_text.length(), selected_text.length() == 1 ? "character" : "characters", word_count, word_count != 1 ? "words" : "word");
  708. } else {
  709. String text = m_editor->text();
  710. auto word_count = m_editor->number_of_words();
  711. builder.appendff("{} {} ({} {})", text.length(), text.length() == 1 ? "character" : "characters", word_count, word_count != 1 ? "words" : "word");
  712. }
  713. m_statusbar->set_text(0, builder.to_string());
  714. if (m_editor && m_editor->syntax_highlighter()) {
  715. auto language = m_editor->syntax_highlighter()->language();
  716. m_statusbar->set_text(1, m_editor->syntax_highlighter()->language_string(language));
  717. }
  718. m_statusbar->set_text(2, String::formatted("Ln {}, Col {}", m_editor->cursor().line() + 1, m_editor->cursor().column()));
  719. }
  720. void MainWidget::find_text(GUI::TextEditor::SearchDirection direction, ShowMessageIfNoResutls show_message)
  721. {
  722. auto needle = m_find_textbox->text();
  723. if (needle.is_empty())
  724. return;
  725. if (m_use_regex)
  726. m_editor->document().update_regex_matches(needle);
  727. auto result = m_editor->find_text(needle, direction,
  728. m_should_wrap ? GUI::TextDocument::SearchShouldWrap::Yes : GUI::TextDocument::SearchShouldWrap::No,
  729. m_use_regex, m_match_case);
  730. if (!result.is_valid() && show_message == ShowMessageIfNoResutls::Yes) {
  731. GUI::MessageBox::show(window(),
  732. String::formatted("Not found: \"{}\"", needle),
  733. "Not found",
  734. GUI::MessageBox::Type::Information);
  735. }
  736. }
  737. }