MainWidget.cpp 41 KB

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