MainWidget.cpp 42 KB

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