HackStudioWidget.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  4. * Copyright (c) 2020, the SerenityOS developers
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice, this
  11. * list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "HackStudioWidget.h"
  29. #include "CursorTool.h"
  30. #include "Debugger/DebugInfoWidget.h"
  31. #include "Debugger/Debugger.h"
  32. #include "Debugger/DisassemblyWidget.h"
  33. #include "Dialogs/NewProjectDialog.h"
  34. #include "Editor.h"
  35. #include "EditorWrapper.h"
  36. #include "FindInFilesWidget.h"
  37. #include "FormEditorWidget.h"
  38. #include "FormWidget.h"
  39. #include "Git/DiffViewer.h"
  40. #include "Git/GitWidget.h"
  41. #include "HackStudio.h"
  42. #include "HackStudioWidget.h"
  43. #include "Locator.h"
  44. #include "Project.h"
  45. #include "TerminalWrapper.h"
  46. #include "WidgetTool.h"
  47. #include "WidgetTreeModel.h"
  48. #include <AK/LexicalPath.h>
  49. #include <AK/StringBuilder.h>
  50. #include <LibCore/ArgsParser.h>
  51. #include <LibCore/Event.h>
  52. #include <LibCore/EventLoop.h>
  53. #include <LibCore/File.h>
  54. #include <LibDebug/DebugSession.h>
  55. #include <LibGUI/Action.h>
  56. #include <LibGUI/ActionGroup.h>
  57. #include <LibGUI/Application.h>
  58. #include <LibGUI/BoxLayout.h>
  59. #include <LibGUI/Button.h>
  60. #include <LibGUI/Dialog.h>
  61. #include <LibGUI/EditingEngine.h>
  62. #include <LibGUI/FilePicker.h>
  63. #include <LibGUI/InputBox.h>
  64. #include <LibGUI/ItemListModel.h>
  65. #include <LibGUI/Label.h>
  66. #include <LibGUI/Menu.h>
  67. #include <LibGUI/MenuBar.h>
  68. #include <LibGUI/MessageBox.h>
  69. #include <LibGUI/RegularEditingEngine.h>
  70. #include <LibGUI/Splitter.h>
  71. #include <LibGUI/StackWidget.h>
  72. #include <LibGUI/TabWidget.h>
  73. #include <LibGUI/TableView.h>
  74. #include <LibGUI/TextBox.h>
  75. #include <LibGUI/TextEditor.h>
  76. #include <LibGUI/ToolBar.h>
  77. #include <LibGUI/ToolBarContainer.h>
  78. #include <LibGUI/TreeView.h>
  79. #include <LibGUI/VimEditingEngine.h>
  80. #include <LibGUI/Widget.h>
  81. #include <LibGUI/Window.h>
  82. #include <LibGfx/FontDatabase.h>
  83. #include <LibGfx/Palette.h>
  84. #include <LibThread/Lock.h>
  85. #include <LibThread/Thread.h>
  86. #include <LibVT/TerminalWidget.h>
  87. #include <fcntl.h>
  88. #include <spawn.h>
  89. #include <stdio.h>
  90. #include <sys/types.h>
  91. #include <sys/wait.h>
  92. #include <unistd.h>
  93. namespace HackStudio {
  94. HackStudioWidget::HackStudioWidget(const String& path_to_project)
  95. {
  96. set_fill_with_background_color(true);
  97. set_layout<GUI::VerticalBoxLayout>();
  98. layout()->set_spacing(2);
  99. open_project(path_to_project);
  100. auto& toolbar_container = add<GUI::ToolBarContainer>();
  101. auto& outer_splitter = add<GUI::HorizontalSplitter>();
  102. auto& left_hand_splitter = outer_splitter.add<GUI::VerticalSplitter>();
  103. left_hand_splitter.set_fixed_width(150);
  104. create_project_tree_view(left_hand_splitter);
  105. m_project_tree_view_context_menu = create_project_tree_view_context_menu();
  106. create_open_files_view(left_hand_splitter);
  107. m_right_hand_splitter = outer_splitter.add<GUI::VerticalSplitter>();
  108. m_right_hand_stack = m_right_hand_splitter->add<GUI::StackWidget>();
  109. // Put a placeholder widget front & center since we don't have a file open yet.
  110. m_right_hand_stack->add<GUI::Widget>();
  111. create_form_editor(*m_right_hand_stack);
  112. m_diff_viewer = m_right_hand_stack->add<DiffViewer>();
  113. m_editors_splitter = m_right_hand_stack->add<GUI::VerticalSplitter>();
  114. m_editors_splitter->layout()->set_margins({ 0, 3, 0, 0 });
  115. add_new_editor(*m_editors_splitter);
  116. m_switch_to_next_editor = create_switch_to_next_editor_action();
  117. m_switch_to_previous_editor = create_switch_to_previous_editor_action();
  118. m_remove_current_editor_action = create_remove_current_editor_action();
  119. m_open_action = create_open_action();
  120. m_save_action = create_save_action();
  121. m_new_project_action = create_new_project_action();
  122. create_action_tab(*m_right_hand_splitter);
  123. m_add_editor_action = create_add_editor_action();
  124. m_add_terminal_action = create_add_terminal_action();
  125. m_remove_current_terminal_action = create_remove_current_terminal_action();
  126. m_locator = add<Locator>();
  127. m_terminal_wrapper->on_command_exit = [this] {
  128. m_stop_action->set_enabled(false);
  129. };
  130. m_build_action = create_build_action();
  131. m_run_action = create_run_action();
  132. m_stop_action = create_stop_action();
  133. m_debug_action = create_debug_action();
  134. initialize_debugger();
  135. create_toolbar(toolbar_container);
  136. }
  137. void HackStudioWidget::update_actions()
  138. {
  139. auto is_remove_terminal_enabled = [this]() {
  140. auto widget = m_action_tab_widget->active_widget();
  141. if (!widget)
  142. return false;
  143. if (StringView { "TerminalWrapper" } != widget->class_name())
  144. return false;
  145. if (!reinterpret_cast<TerminalWrapper*>(widget)->user_spawned())
  146. return false;
  147. return true;
  148. };
  149. m_remove_current_editor_action->set_enabled(m_all_editor_wrappers.size() > 1);
  150. m_remove_current_terminal_action->set_enabled(is_remove_terminal_enabled());
  151. }
  152. void HackStudioWidget::on_action_tab_change()
  153. {
  154. update_actions();
  155. auto git_widget = m_action_tab_widget->active_widget();
  156. if (!git_widget)
  157. return;
  158. if (StringView { "GitWidget" } != git_widget->class_name())
  159. return;
  160. reinterpret_cast<GitWidget*>(git_widget)->refresh();
  161. }
  162. void HackStudioWidget::open_project(const String& root_path)
  163. {
  164. if (chdir(root_path.characters()) < 0) {
  165. perror("chdir");
  166. exit(1);
  167. }
  168. m_project = Project::open_with_root_path(root_path);
  169. VERIFY(m_project);
  170. if (m_project_tree_view) {
  171. m_project_tree_view->set_model(m_project->model());
  172. m_project_tree_view->update();
  173. }
  174. if (Debugger::is_initialized()) {
  175. Debugger::the().reset_breakpoints();
  176. }
  177. }
  178. Vector<String> HackStudioWidget::selected_file_paths() const
  179. {
  180. Vector<String> files;
  181. m_project_tree_view->selection().for_each_index([&](const GUI::ModelIndex& index) {
  182. String sub_path = index.data().as_string();
  183. GUI::ModelIndex parent_or_invalid = index.parent();
  184. while (parent_or_invalid.is_valid()) {
  185. sub_path = String::formatted("{}/{}", parent_or_invalid.data().as_string(), sub_path);
  186. parent_or_invalid = parent_or_invalid.parent();
  187. }
  188. files.append(sub_path);
  189. });
  190. return files;
  191. }
  192. void HackStudioWidget::open_file(const String& full_filename)
  193. {
  194. String filename = full_filename;
  195. if (full_filename.starts_with(project().root_path())) {
  196. filename = LexicalPath::relative_path(full_filename, project().root_path());
  197. }
  198. dbgln("HackStudio is opening {}", filename);
  199. if (Core::File::is_directory(filename))
  200. return;
  201. if (!currently_open_file().is_empty()) {
  202. // Since the file is previously open, it should always be in m_open_files.
  203. VERIFY(m_open_files.find(currently_open_file()) != m_open_files.end());
  204. auto previous_open_project_file = m_open_files.get(currently_open_file()).value();
  205. // Update the scrollbar values of the previous_open_project_file and save them to m_open_files.
  206. previous_open_project_file->vertical_scroll_value(current_editor().vertical_scrollbar().value());
  207. previous_open_project_file->horizontal_scroll_value(current_editor().horizontal_scrollbar().value());
  208. m_open_files.set(currently_open_file(), previous_open_project_file);
  209. }
  210. RefPtr<ProjectFile> new_project_file = nullptr;
  211. if (auto it = m_open_files.find(filename); it != m_open_files.end()) {
  212. new_project_file = it->value;
  213. } else {
  214. new_project_file = m_project->get_file(filename);
  215. m_open_files.set(filename, *new_project_file);
  216. m_open_files_vector.append(filename);
  217. m_open_files_view->model()->update();
  218. }
  219. current_editor().set_document(const_cast<GUI::TextDocument&>(new_project_file->document()));
  220. if (new_project_file->could_render_text()) {
  221. current_editor_wrapper().set_mode_displayable();
  222. } else {
  223. current_editor_wrapper().set_mode_non_displayable();
  224. }
  225. current_editor().horizontal_scrollbar().set_value(new_project_file->horizontal_scroll_value());
  226. current_editor().vertical_scrollbar().set_value(new_project_file->vertical_scroll_value());
  227. current_editor().set_editing_engine(make<GUI::RegularEditingEngine>());
  228. if (filename.ends_with(".frm")) {
  229. set_edit_mode(EditMode::Form);
  230. } else {
  231. set_edit_mode(EditMode::Text);
  232. }
  233. m_currently_open_file = filename;
  234. String relative_file_path = m_currently_open_file;
  235. if (m_currently_open_file.starts_with(m_project->root_path()))
  236. relative_file_path = m_currently_open_file.substring(m_project->root_path().length() + 1);
  237. window()->set_title(String::formatted("{} - {} - Hack Studio", relative_file_path, m_project->name()));
  238. m_project_tree_view->update();
  239. current_editor_wrapper().filename_label().set_text(filename);
  240. current_editor().set_focus(true);
  241. }
  242. EditorWrapper& HackStudioWidget::current_editor_wrapper()
  243. {
  244. VERIFY(m_current_editor_wrapper);
  245. return *m_current_editor_wrapper;
  246. }
  247. GUI::TextEditor& HackStudioWidget::current_editor()
  248. {
  249. return current_editor_wrapper().editor();
  250. }
  251. void HackStudioWidget::set_edit_mode(EditMode mode)
  252. {
  253. if (mode == EditMode::Text) {
  254. m_right_hand_stack->set_active_widget(m_editors_splitter);
  255. } else if (mode == EditMode::Form) {
  256. m_right_hand_stack->set_active_widget(m_form_inner_container);
  257. } else if (mode == EditMode::Diff) {
  258. m_right_hand_stack->set_active_widget(m_diff_viewer);
  259. } else {
  260. VERIFY_NOT_REACHED();
  261. }
  262. m_right_hand_stack->active_widget()->update();
  263. }
  264. NonnullRefPtr<GUI::Menu> HackStudioWidget::create_project_tree_view_context_menu()
  265. {
  266. m_open_selected_action = create_open_selected_action();
  267. m_new_file_action = create_new_file_action();
  268. m_new_directory_action = create_new_directory_action();
  269. m_delete_action = create_delete_action();
  270. auto project_tree_view_context_menu = GUI::Menu::construct("Project Files");
  271. project_tree_view_context_menu->add_action(*m_open_selected_action);
  272. // TODO: Rename, cut, copy, duplicate with new name, show containing folder ...
  273. project_tree_view_context_menu->add_separator();
  274. project_tree_view_context_menu->add_action(*m_new_file_action);
  275. project_tree_view_context_menu->add_action(*m_new_directory_action);
  276. project_tree_view_context_menu->add_action(*m_delete_action);
  277. return project_tree_view_context_menu;
  278. }
  279. NonnullRefPtr<GUI::Action> HackStudioWidget::create_new_file_action()
  280. {
  281. return GUI::Action::create("New file...", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [this](const GUI::Action&) {
  282. String filename;
  283. if (GUI::InputBox::show(window(), filename, "Enter name of new file:", "Add new file to project") != GUI::InputBox::ExecOK)
  284. return;
  285. auto path_to_selected = selected_file_paths();
  286. String filepath;
  287. if (!path_to_selected.is_empty()) {
  288. VERIFY(Core::File::exists(path_to_selected.first()));
  289. LexicalPath selected(path_to_selected.first());
  290. String dir_path;
  291. if (Core::File::is_directory(selected.string()))
  292. dir_path = selected.string();
  293. else
  294. dir_path = selected.dirname();
  295. filepath = String::formatted("{}/", dir_path);
  296. }
  297. filepath = String::formatted("{}{}", filepath, filename);
  298. auto file = Core::File::construct(filepath);
  299. if (!file->open((Core::IODevice::OpenMode)(Core::IODevice::WriteOnly | Core::IODevice::MustBeNew))) {
  300. GUI::MessageBox::show(window(), String::formatted("Failed to create '{}'", filepath), "Error", GUI::MessageBox::Type::Error);
  301. return;
  302. }
  303. open_file(filepath);
  304. });
  305. }
  306. NonnullRefPtr<GUI::Action> HackStudioWidget::create_new_directory_action()
  307. {
  308. return GUI::Action::create("New directory...", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/mkdir.png"), [this](const GUI::Action&) {
  309. String directory_name;
  310. if (GUI::InputBox::show(window(), directory_name, "Enter name of new directory:", "Add new folder to project") != GUI::InputBox::ExecOK)
  311. return;
  312. auto path_to_selected = selected_file_paths();
  313. if (!path_to_selected.is_empty()) {
  314. LexicalPath selected(path_to_selected.first());
  315. String dir_path;
  316. if (Core::File::is_directory(selected.string()))
  317. dir_path = selected.string();
  318. else
  319. dir_path = selected.dirname();
  320. directory_name = String::formatted("{}/{}", dir_path, directory_name);
  321. }
  322. auto formatted_dir_name = LexicalPath::canonicalized_path(String::formatted("{}/{}", m_project->model().root_path(), directory_name));
  323. int rc = mkdir(formatted_dir_name.characters(), 0755);
  324. if (rc < 0) {
  325. GUI::MessageBox::show(window(), "Failed to create new directory", "Error", GUI::MessageBox::Type::Error);
  326. return;
  327. }
  328. });
  329. }
  330. NonnullRefPtr<GUI::Action> HackStudioWidget::create_open_selected_action()
  331. {
  332. auto open_selected_action = GUI::Action::create("Open", [this](const GUI::Action&) {
  333. auto files = selected_file_paths();
  334. for (auto& file : files)
  335. open_file(file);
  336. });
  337. open_selected_action->set_enabled(true);
  338. return open_selected_action;
  339. }
  340. NonnullRefPtr<GUI::Action> HackStudioWidget::create_delete_action()
  341. {
  342. auto delete_action = GUI::CommonActions::make_delete_action([this](const GUI::Action&) {
  343. auto files = selected_file_paths();
  344. if (files.is_empty())
  345. return;
  346. String message;
  347. if (files.size() == 1) {
  348. LexicalPath file(files[0]);
  349. message = String::formatted("Really remove {} from disk?", file.basename());
  350. } else {
  351. message = String::formatted("Really remove {} files from disk?", files.size());
  352. }
  353. auto result = GUI::MessageBox::show(window(),
  354. message,
  355. "Confirm deletion",
  356. GUI::MessageBox::Type::Warning,
  357. GUI::MessageBox::InputType::OKCancel);
  358. if (result == GUI::MessageBox::ExecCancel)
  359. return;
  360. for (auto& file : files) {
  361. struct stat st;
  362. if (lstat(file.characters(), &st) < 0) {
  363. GUI::MessageBox::show(window(),
  364. String::formatted("lstat ({}) failed: {}", file, strerror(errno)),
  365. "Removal failed",
  366. GUI::MessageBox::Type::Error);
  367. break;
  368. }
  369. bool is_directory = S_ISDIR(st.st_mode);
  370. auto result = Core::File::remove(file, Core::File::RecursionMode::Allowed, false);
  371. if (result.is_error()) {
  372. auto& error = result.error();
  373. if (is_directory) {
  374. GUI::MessageBox::show(window(),
  375. String::formatted("Removing directory {} from the project failed: {}", error.file, error.error_code),
  376. "Removal failed",
  377. GUI::MessageBox::Type::Error);
  378. } else {
  379. GUI::MessageBox::show(window(),
  380. String::formatted("Removing file {} from the project failed: {}", error.file, error.error_code),
  381. "Removal failed",
  382. GUI::MessageBox::Type::Error);
  383. }
  384. break;
  385. }
  386. }
  387. });
  388. delete_action->set_enabled(false);
  389. return delete_action;
  390. }
  391. NonnullRefPtr<GUI::Action> HackStudioWidget::create_new_project_action()
  392. {
  393. return GUI::Action::create("New project...", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/hackstudio-project.png"), [this](const GUI::Action&) {
  394. auto dialog = NewProjectDialog::construct(window());
  395. dialog->set_icon(window()->icon());
  396. auto result = dialog->exec();
  397. if (result == GUI::Dialog::ExecResult::ExecOK && dialog->created_project_path().has_value())
  398. open_project(dialog->created_project_path().value());
  399. });
  400. }
  401. void HackStudioWidget::add_new_editor(GUI::Widget& parent)
  402. {
  403. auto wrapper = EditorWrapper::construct();
  404. if (m_action_tab_widget) {
  405. parent.insert_child_before(wrapper, *m_action_tab_widget);
  406. } else {
  407. parent.add_child(wrapper);
  408. }
  409. m_current_editor_wrapper = wrapper;
  410. m_all_editor_wrappers.append(wrapper);
  411. wrapper->editor().set_focus(true);
  412. }
  413. NonnullRefPtr<GUI::Action> HackStudioWidget::create_switch_to_next_editor_action()
  414. {
  415. return GUI::Action::create("Switch to next editor", { Mod_Ctrl, Key_E }, [this](auto&) {
  416. if (m_all_editor_wrappers.size() <= 1)
  417. return;
  418. Vector<EditorWrapper*> wrappers;
  419. m_editors_splitter->for_each_child_of_type<EditorWrapper>([this, &wrappers](auto& child) {
  420. wrappers.append(&child);
  421. return IterationDecision::Continue;
  422. });
  423. for (size_t i = 0; i < wrappers.size(); ++i) {
  424. if (m_current_editor_wrapper.ptr() == wrappers[i]) {
  425. if (i == wrappers.size() - 1)
  426. wrappers[0]->editor().set_focus(true);
  427. else
  428. wrappers[i + 1]->editor().set_focus(true);
  429. }
  430. }
  431. });
  432. }
  433. NonnullRefPtr<GUI::Action> HackStudioWidget::create_switch_to_previous_editor_action()
  434. {
  435. return GUI::Action::create("Switch to previous editor", { Mod_Ctrl | Mod_Shift, Key_E }, [this](auto&) {
  436. if (m_all_editor_wrappers.size() <= 1)
  437. return;
  438. Vector<EditorWrapper*> wrappers;
  439. m_editors_splitter->for_each_child_of_type<EditorWrapper>([this, &wrappers](auto& child) {
  440. wrappers.append(&child);
  441. return IterationDecision::Continue;
  442. });
  443. for (int i = wrappers.size() - 1; i >= 0; --i) {
  444. if (m_current_editor_wrapper.ptr() == wrappers[i]) {
  445. if (i == 0)
  446. wrappers.last()->editor().set_focus(true);
  447. else
  448. wrappers[i - 1]->editor().set_focus(true);
  449. }
  450. }
  451. });
  452. }
  453. NonnullRefPtr<GUI::Action> HackStudioWidget::create_remove_current_editor_action()
  454. {
  455. return GUI::Action::create("Remove current editor", { Mod_Alt | Mod_Shift, Key_E }, [this](auto&) {
  456. if (m_all_editor_wrappers.size() <= 1)
  457. return;
  458. auto wrapper = m_current_editor_wrapper;
  459. m_switch_to_next_editor->activate();
  460. m_editors_splitter->remove_child(*wrapper);
  461. m_all_editor_wrappers.remove_first_matching([&wrapper](auto& entry) { return entry == wrapper.ptr(); });
  462. update_actions();
  463. });
  464. }
  465. NonnullRefPtr<GUI::Action> HackStudioWidget::create_open_action()
  466. {
  467. return GUI::Action::create("Open project...", { Mod_Ctrl | Mod_Shift, Key_O }, Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), [this](auto&) {
  468. auto open_path = GUI::FilePicker::get_open_filepath(window(), "Open project");
  469. if (!open_path.has_value())
  470. return;
  471. open_project(open_path.value());
  472. update_actions();
  473. });
  474. }
  475. NonnullRefPtr<GUI::Action> HackStudioWidget::create_save_action()
  476. {
  477. return GUI::Action::create("Save", { Mod_Ctrl, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), [this](auto&) {
  478. if (m_currently_open_file.is_empty())
  479. return;
  480. current_editor().write_to_file(m_currently_open_file);
  481. if (m_git_widget->initialized())
  482. m_git_widget->refresh();
  483. });
  484. }
  485. NonnullRefPtr<GUI::Action> HackStudioWidget::create_remove_current_terminal_action()
  486. {
  487. return GUI::Action::create("Remove current Terminal", { Mod_Alt | Mod_Shift, Key_T }, [this](auto&) {
  488. auto widget = m_action_tab_widget->active_widget();
  489. if (!widget)
  490. return;
  491. if (!is<TerminalWrapper>(widget))
  492. return;
  493. auto& terminal = *static_cast<TerminalWrapper*>(widget);
  494. if (!terminal.user_spawned())
  495. return;
  496. m_action_tab_widget->remove_tab(terminal);
  497. update_actions();
  498. });
  499. }
  500. NonnullRefPtr<GUI::Action> HackStudioWidget::create_add_editor_action()
  501. {
  502. return GUI::Action::create("Add new editor", { Mod_Ctrl | Mod_Alt, Key_E },
  503. Gfx::Bitmap::load_from_file("/res/icons/16x16/app-text-editor.png"),
  504. [this](auto&) {
  505. add_new_editor(*m_editors_splitter);
  506. update_actions();
  507. });
  508. }
  509. NonnullRefPtr<GUI::Action> HackStudioWidget::create_add_terminal_action()
  510. {
  511. return GUI::Action::create("Add new Terminal", { Mod_Ctrl | Mod_Alt, Key_T },
  512. Gfx::Bitmap::load_from_file("/res/icons/16x16/app-terminal.png"),
  513. [this](auto&) {
  514. auto& terminal_wrapper = m_action_tab_widget->add_tab<TerminalWrapper>("Terminal");
  515. reveal_action_tab(terminal_wrapper);
  516. update_actions();
  517. terminal_wrapper.terminal().set_focus(true);
  518. });
  519. }
  520. void HackStudioWidget::reveal_action_tab(GUI::Widget& widget)
  521. {
  522. if (m_action_tab_widget->min_height() < 200)
  523. m_action_tab_widget->set_fixed_height(200);
  524. m_action_tab_widget->set_active_widget(&widget);
  525. }
  526. NonnullRefPtr<GUI::Action> HackStudioWidget::create_debug_action()
  527. {
  528. return GUI::Action::create("Debug", Gfx::Bitmap::load_from_file("/res/icons/16x16/debug-run.png"), [this](auto&) {
  529. if (!Core::File::exists(get_project_executable_path())) {
  530. GUI::MessageBox::show(window(), String::formatted("Could not find file: {}. (did you build the project?)", get_project_executable_path()), "Error", GUI::MessageBox::Type::Error);
  531. return;
  532. }
  533. if (Debugger::the().session()) {
  534. GUI::MessageBox::show(window(), "Debugger is already running", "Error", GUI::MessageBox::Type::Error);
  535. return;
  536. }
  537. Debugger::the().set_executable_path(get_project_executable_path());
  538. m_debugger_thread = LibThread::Thread::construct(Debugger::start_static);
  539. m_debugger_thread->start();
  540. });
  541. }
  542. void HackStudioWidget::initialize_debugger()
  543. {
  544. Debugger::initialize(
  545. m_project->root_path(),
  546. [this](const PtraceRegisters& regs) {
  547. VERIFY(Debugger::the().session());
  548. const auto& debug_session = *Debugger::the().session();
  549. auto source_position = debug_session.get_source_position(regs.eip);
  550. if (!source_position.has_value()) {
  551. dbgln("Could not find source position for address: {:p}", regs.eip);
  552. return Debugger::HasControlPassedToUser::No;
  553. }
  554. dbgln("Debugger stopped at source position: {}:{}", source_position.value().file_path, source_position.value().line_number);
  555. Core::EventLoop::main().post_event(
  556. *window(),
  557. make<Core::DeferredInvocationEvent>(
  558. [this, source_position, &regs](auto&) {
  559. m_current_editor_in_execution = get_editor_of_file(source_position.value().file_path);
  560. m_current_editor_in_execution->editor().set_execution_position(source_position.value().line_number - 1);
  561. m_debug_info_widget->update_state(*Debugger::the().session(), regs);
  562. m_debug_info_widget->set_debug_actions_enabled(true);
  563. m_disassembly_widget->update_state(*Debugger::the().session(), regs);
  564. HackStudioWidget::reveal_action_tab(*m_debug_info_widget);
  565. }));
  566. Core::EventLoop::wake();
  567. return Debugger::HasControlPassedToUser::Yes;
  568. },
  569. [this]() {
  570. Core::EventLoop::main().post_event(*window(), make<Core::DeferredInvocationEvent>([this](auto&) {
  571. m_debug_info_widget->set_debug_actions_enabled(false);
  572. if (m_current_editor_in_execution) {
  573. m_current_editor_in_execution->editor().clear_execution_position();
  574. }
  575. }));
  576. Core::EventLoop::wake();
  577. },
  578. [this]() {
  579. Core::EventLoop::main().post_event(*window(), make<Core::DeferredInvocationEvent>([this](auto&) {
  580. m_debug_info_widget->program_stopped();
  581. m_disassembly_widget->program_stopped();
  582. HackStudioWidget::hide_action_tabs();
  583. GUI::MessageBox::show(window(), "Program Exited", "Debugger", GUI::MessageBox::Type::Information);
  584. }));
  585. Core::EventLoop::wake();
  586. });
  587. }
  588. String HackStudioWidget::get_full_path_of_serenity_source(const String& file)
  589. {
  590. auto path_parts = LexicalPath(file).parts();
  591. VERIFY(path_parts[0] == "..");
  592. path_parts.remove(0);
  593. StringBuilder relative_path_builder;
  594. relative_path_builder.join("/", path_parts);
  595. constexpr char SERENITY_LIBS_PREFIX[] = "/usr/src/serenity";
  596. LexicalPath serenity_sources_base(SERENITY_LIBS_PREFIX);
  597. return String::formatted("{}/{}", serenity_sources_base, relative_path_builder.to_string());
  598. }
  599. NonnullRefPtr<EditorWrapper> HackStudioWidget::get_editor_of_file(const String& file_name)
  600. {
  601. String file_path = file_name;
  602. // TODO: We can probably do a more specific condition here, something like
  603. // "if (file.starts_with("../Libraries/") || file.starts_with("../AK/"))"
  604. if (file_name.starts_with("../")) {
  605. file_path = get_full_path_of_serenity_source(file_name);
  606. }
  607. open_file(file_path);
  608. return current_editor_wrapper();
  609. }
  610. String HackStudioWidget::get_project_executable_path() const
  611. {
  612. // FIXME: Dumb heuristic ahead!
  613. // e.g /my/project => /my/project/project
  614. // TODO: Perhaps a Makefile rule for getting the value of $(PROGRAM) would be better?
  615. return String::formatted("{}/{}", m_project->root_path(), LexicalPath(m_project->root_path()).basename());
  616. }
  617. void HackStudioWidget::build(TerminalWrapper& wrapper)
  618. {
  619. if (m_currently_open_file.ends_with(".js"))
  620. wrapper.run_command(String::formatted("js -A {}", m_currently_open_file));
  621. else
  622. wrapper.run_command("make");
  623. }
  624. void HackStudioWidget::run(TerminalWrapper& wrapper)
  625. {
  626. if (m_currently_open_file.ends_with(".js"))
  627. wrapper.run_command(String::formatted("js {}", m_currently_open_file));
  628. else
  629. wrapper.run_command("make run");
  630. }
  631. void HackStudioWidget::hide_action_tabs()
  632. {
  633. m_action_tab_widget->set_fixed_height(24);
  634. };
  635. Project& HackStudioWidget::project()
  636. {
  637. return *m_project;
  638. }
  639. void HackStudioWidget::set_current_editor_wrapper(RefPtr<EditorWrapper> editor_wrapper)
  640. {
  641. m_current_editor_wrapper = editor_wrapper;
  642. }
  643. void HackStudioWidget::create_project_tree_view(GUI::Widget& parent)
  644. {
  645. m_project_tree_view = parent.add<GUI::TreeView>();
  646. m_project_tree_view->set_model(m_project->model());
  647. m_project_tree_view->set_selection_mode(GUI::AbstractView::SelectionMode::MultiSelection);
  648. for (int column_index = 0; column_index < m_project->model().column_count(); ++column_index)
  649. m_project_tree_view->set_column_visible(column_index, false);
  650. m_project_tree_view->set_column_visible(GUI::FileSystemModel::Column::Name, true);
  651. m_project_tree_view->on_context_menu_request = [this](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  652. if (index.is_valid()) {
  653. m_project_tree_view_context_menu->popup(event.screen_position(), m_open_selected_action);
  654. }
  655. };
  656. m_project_tree_view->on_selection_change = [this] {
  657. m_open_selected_action->set_enabled(!m_project_tree_view->selection().is_empty());
  658. m_delete_action->set_enabled(!m_project_tree_view->selection().is_empty());
  659. };
  660. m_project_tree_view->on_activation = [this](auto& index) {
  661. auto full_path_to_file = m_project->model().full_path(index);
  662. open_file(full_path_to_file);
  663. };
  664. }
  665. void HackStudioWidget::create_open_files_view(GUI::Widget& parent)
  666. {
  667. m_open_files_view = parent.add<GUI::ListView>();
  668. auto open_files_model = GUI::ItemListModel<String>::create(m_open_files_vector);
  669. m_open_files_view->set_model(open_files_model);
  670. m_open_files_view->on_activation = [this](auto& index) {
  671. open_file(index.data().to_string());
  672. };
  673. }
  674. void HackStudioWidget::create_form_editor(GUI::Widget& parent)
  675. {
  676. m_form_inner_container = parent.add<GUI::Widget>();
  677. m_form_inner_container->set_layout<GUI::HorizontalBoxLayout>();
  678. auto& form_widgets_toolbar = m_form_inner_container->add<GUI::ToolBar>(Orientation::Vertical, 26);
  679. form_widgets_toolbar.set_fixed_width(38);
  680. GUI::ActionGroup tool_actions;
  681. tool_actions.set_exclusive(true);
  682. auto cursor_tool_action = GUI::Action::create_checkable("Cursor", Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Cursor.png"), [this](auto&) {
  683. m_form_editor_widget->set_tool(make<CursorTool>(*m_form_editor_widget));
  684. });
  685. cursor_tool_action->set_checked(true);
  686. tool_actions.add_action(cursor_tool_action);
  687. form_widgets_toolbar.add_action(cursor_tool_action);
  688. GUI::WidgetClassRegistration::for_each([&, this](const GUI::WidgetClassRegistration& reg) {
  689. constexpr size_t gui_namespace_prefix_length = sizeof("GUI::") - 1;
  690. auto icon_path = String::formatted("/res/icons/hackstudio/G{}.png",
  691. reg.class_name().substring(gui_namespace_prefix_length, reg.class_name().length() - gui_namespace_prefix_length));
  692. if (!Core::File::exists(icon_path))
  693. return;
  694. auto action = GUI::Action::create_checkable(reg.class_name(), Gfx::Bitmap::load_from_file(icon_path), [&reg, this](auto&) {
  695. m_form_editor_widget->set_tool(make<WidgetTool>(*m_form_editor_widget, reg));
  696. auto widget = reg.construct();
  697. m_form_editor_widget->form_widget().add_child(widget);
  698. widget->set_relative_rect(30, 30, 30, 30);
  699. m_form_editor_widget->model().update();
  700. });
  701. action->set_checked(false);
  702. tool_actions.add_action(action);
  703. form_widgets_toolbar.add_action(move(action));
  704. });
  705. auto& form_editor_inner_splitter = m_form_inner_container->add<GUI::HorizontalSplitter>();
  706. m_form_editor_widget = form_editor_inner_splitter.add<FormEditorWidget>();
  707. auto& form_editing_pane_container = form_editor_inner_splitter.add<GUI::VerticalSplitter>();
  708. form_editing_pane_container.set_fixed_width(190);
  709. form_editing_pane_container.set_layout<GUI::VerticalBoxLayout>();
  710. auto add_properties_pane = [&](auto& text, auto& pane_widget) {
  711. auto& wrapper = form_editing_pane_container.add<GUI::Widget>();
  712. wrapper.set_layout<GUI::VerticalBoxLayout>();
  713. auto& label = wrapper.add<GUI::Label>(text);
  714. label.set_fill_with_background_color(true);
  715. label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  716. label.set_font(Gfx::FontDatabase::default_bold_font());
  717. label.set_fixed_height(16);
  718. wrapper.add_child(pane_widget);
  719. };
  720. m_form_widget_tree_view = GUI::TreeView::construct();
  721. m_form_widget_tree_view->set_model(m_form_editor_widget->model());
  722. m_form_widget_tree_view->on_selection_change = [this] {
  723. m_form_editor_widget->selection().disable_hooks();
  724. m_form_editor_widget->selection().clear();
  725. m_form_widget_tree_view->selection().for_each_index([this](auto& index) {
  726. // NOTE: Make sure we don't add the FormWidget itself to the selection,
  727. // since that would allow you to drag-move the FormWidget.
  728. if (index.internal_data() != &m_form_editor_widget->form_widget())
  729. m_form_editor_widget->selection().add(*(GUI::Widget*)index.internal_data());
  730. });
  731. m_form_editor_widget->update();
  732. m_form_editor_widget->selection().enable_hooks();
  733. };
  734. m_form_editor_widget->selection().on_add = [this](auto& widget) {
  735. m_form_widget_tree_view->selection().add(m_form_editor_widget->model().index_for_widget(widget));
  736. };
  737. m_form_editor_widget->selection().on_remove = [this](auto& widget) {
  738. m_form_widget_tree_view->selection().remove(m_form_editor_widget->model().index_for_widget(widget));
  739. };
  740. m_form_editor_widget->selection().on_clear = [this] {
  741. m_form_widget_tree_view->selection().clear();
  742. };
  743. add_properties_pane("Form widget tree:", *m_form_widget_tree_view);
  744. add_properties_pane("Widget properties:", *GUI::TableView::construct());
  745. }
  746. void HackStudioWidget::create_toolbar(GUI::Widget& parent)
  747. {
  748. auto& toolbar = parent.add<GUI::ToolBar>();
  749. toolbar.add_action(*m_new_file_action);
  750. toolbar.add_action(*m_new_directory_action);
  751. toolbar.add_action(*m_save_action);
  752. toolbar.add_action(*m_delete_action);
  753. toolbar.add_separator();
  754. toolbar.add_action(GUI::CommonActions::make_cut_action([this](auto&) { current_editor().cut_action().activate(); }));
  755. toolbar.add_action(GUI::CommonActions::make_copy_action([this](auto&) { current_editor().copy_action().activate(); }));
  756. toolbar.add_action(GUI::CommonActions::make_paste_action([this](auto&) { current_editor().paste_action().activate(); }));
  757. toolbar.add_separator();
  758. toolbar.add_action(GUI::CommonActions::make_undo_action([this](auto&) { current_editor().undo_action().activate(); }));
  759. toolbar.add_action(GUI::CommonActions::make_redo_action([this](auto&) { current_editor().redo_action().activate(); }));
  760. toolbar.add_separator();
  761. toolbar.add_action(*m_build_action);
  762. toolbar.add_separator();
  763. toolbar.add_action(*m_run_action);
  764. toolbar.add_action(*m_stop_action);
  765. toolbar.add_separator();
  766. toolbar.add_action(*m_debug_action);
  767. }
  768. NonnullRefPtr<GUI::Action> HackStudioWidget::create_build_action()
  769. {
  770. return GUI::Action::create("Build", { Mod_Ctrl, Key_B }, Gfx::Bitmap::load_from_file("/res/icons/16x16/build.png"), [this](auto&) {
  771. reveal_action_tab(*m_terminal_wrapper);
  772. build(*m_terminal_wrapper);
  773. m_stop_action->set_enabled(true);
  774. });
  775. }
  776. NonnullRefPtr<GUI::Action> HackStudioWidget::create_run_action()
  777. {
  778. return GUI::Action::create("Run", { Mod_Ctrl, Key_R }, Gfx::Bitmap::load_from_file("/res/icons/16x16/program-run.png"), [this](auto&) {
  779. reveal_action_tab(*m_terminal_wrapper);
  780. run(*m_terminal_wrapper);
  781. m_stop_action->set_enabled(true);
  782. });
  783. }
  784. void HackStudioWidget::create_action_tab(GUI::Widget& parent)
  785. {
  786. m_action_tab_widget = parent.add<GUI::TabWidget>();
  787. m_action_tab_widget->set_fixed_height(24);
  788. m_action_tab_widget->on_change = [this](auto&) {
  789. on_action_tab_change();
  790. static bool first_time = true;
  791. if (!first_time)
  792. m_action_tab_widget->set_fixed_height(200);
  793. first_time = false;
  794. };
  795. m_find_in_files_widget = m_action_tab_widget->add_tab<FindInFilesWidget>("Find in files");
  796. m_terminal_wrapper = m_action_tab_widget->add_tab<TerminalWrapper>("Build", false);
  797. m_debug_info_widget = m_action_tab_widget->add_tab<DebugInfoWidget>("Debug");
  798. m_disassembly_widget = m_action_tab_widget->add_tab<DisassemblyWidget>("Disassembly");
  799. m_git_widget = m_action_tab_widget->add_tab<GitWidget>("Git", LexicalPath(m_project->root_path()));
  800. m_git_widget->set_view_diff_callback([this](const auto& original_content, const auto& diff) {
  801. m_diff_viewer->set_content(original_content, diff);
  802. set_edit_mode(EditMode::Diff);
  803. });
  804. }
  805. void HackStudioWidget::create_app_menubar(GUI::MenuBar& menubar)
  806. {
  807. auto& app_menu = menubar.add_menu("File");
  808. app_menu.add_action(*m_new_project_action);
  809. app_menu.add_action(*m_open_action);
  810. app_menu.add_action(*m_save_action);
  811. app_menu.add_separator();
  812. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  813. GUI::Application::the()->quit();
  814. }));
  815. }
  816. void HackStudioWidget::create_project_menubar(GUI::MenuBar& menubar)
  817. {
  818. auto& project_menu = menubar.add_menu("Project");
  819. project_menu.add_action(*m_new_file_action);
  820. project_menu.add_action(*m_new_directory_action);
  821. project_menu.add_separator();
  822. project_menu.add_action(*create_set_autocomplete_mode_action());
  823. }
  824. void HackStudioWidget::create_edit_menubar(GUI::MenuBar& menubar)
  825. {
  826. auto& edit_menu = menubar.add_menu("Edit");
  827. edit_menu.add_action(GUI::Action::create("Find in files...", { Mod_Ctrl | Mod_Shift, Key_F }, Gfx::Bitmap::load_from_file("/res/icons/16x16/find.png"), [this](auto&) {
  828. reveal_action_tab(*m_find_in_files_widget);
  829. m_find_in_files_widget->focus_textbox_and_select_all();
  830. }));
  831. edit_menu.add_separator();
  832. auto vim_emulation_setting_action = GUI::Action::create_checkable("Vim emulation", { Mod_Ctrl | Mod_Shift | Mod_Alt, Key_V }, [this](auto& action) {
  833. if (action.is_checked())
  834. current_editor().set_editing_engine(make<GUI::VimEditingEngine>());
  835. else
  836. current_editor().set_editing_engine(make<GUI::RegularEditingEngine>());
  837. });
  838. vim_emulation_setting_action->set_checked(false);
  839. edit_menu.add_action(vim_emulation_setting_action);
  840. }
  841. void HackStudioWidget::create_build_menubar(GUI::MenuBar& menubar)
  842. {
  843. auto& build_menu = menubar.add_menu("Build");
  844. build_menu.add_action(*m_build_action);
  845. build_menu.add_separator();
  846. build_menu.add_action(*m_run_action);
  847. build_menu.add_action(*m_stop_action);
  848. build_menu.add_separator();
  849. build_menu.add_action(*m_debug_action);
  850. }
  851. void HackStudioWidget::create_view_menubar(GUI::MenuBar& menubar)
  852. {
  853. auto hide_action_tabs_action = GUI::Action::create("Hide action tabs", { Mod_Ctrl | Mod_Shift, Key_X }, [this](auto&) {
  854. hide_action_tabs();
  855. });
  856. auto open_locator_action = GUI::Action::create("Open locator", { Mod_Ctrl, Key_K }, [this](auto&) {
  857. m_locator->open();
  858. });
  859. auto& view_menu = menubar.add_menu("View");
  860. view_menu.add_action(hide_action_tabs_action);
  861. view_menu.add_action(open_locator_action);
  862. view_menu.add_separator();
  863. m_wrapping_mode_actions.set_exclusive(true);
  864. auto& wrapping_mode_menu = view_menu.add_submenu("Wrapping mode");
  865. m_no_wrapping_action = GUI::Action::create_checkable("No wrapping", [&](auto&) {
  866. for (auto& wrapper : m_all_editor_wrappers)
  867. wrapper.editor().set_wrapping_mode(GUI::TextEditor::WrappingMode::NoWrap);
  868. });
  869. m_wrap_anywhere_action = GUI::Action::create_checkable("Wrap anywhere", [&](auto&) {
  870. for (auto& wrapper : m_all_editor_wrappers)
  871. wrapper.editor().set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAnywhere);
  872. });
  873. m_wrap_at_words_action = GUI::Action::create_checkable("Wrap at words", [&](auto&) {
  874. for (auto& wrapper : m_all_editor_wrappers)
  875. wrapper.editor().set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAtWords);
  876. });
  877. m_wrapping_mode_actions.add_action(*m_no_wrapping_action);
  878. m_wrapping_mode_actions.add_action(*m_wrap_anywhere_action);
  879. m_wrapping_mode_actions.add_action(*m_wrap_at_words_action);
  880. wrapping_mode_menu.add_action(*m_no_wrapping_action);
  881. wrapping_mode_menu.add_action(*m_wrap_anywhere_action);
  882. wrapping_mode_menu.add_action(*m_wrap_at_words_action);
  883. m_no_wrapping_action->set_checked(true);
  884. view_menu.add_separator();
  885. view_menu.add_action(*m_add_editor_action);
  886. view_menu.add_action(*m_remove_current_editor_action);
  887. view_menu.add_action(*m_add_terminal_action);
  888. view_menu.add_action(*m_remove_current_terminal_action);
  889. }
  890. void HackStudioWidget::create_help_menubar(GUI::MenuBar& menubar)
  891. {
  892. auto& help_menu = menubar.add_menu("Help");
  893. help_menu.add_action(GUI::CommonActions::make_about_action("Hack Studio", GUI::Icon::default_icon("app-hack-studio"), window()));
  894. }
  895. NonnullRefPtr<GUI::Action> HackStudioWidget::create_stop_action()
  896. {
  897. auto action = GUI::Action::create("Stop", Gfx::Bitmap::load_from_file("/res/icons/16x16/program-stop.png"), [this](auto&) {
  898. m_terminal_wrapper->kill_running_command();
  899. });
  900. action->set_enabled(false);
  901. return action;
  902. }
  903. NonnullRefPtr<GUI::Action> HackStudioWidget::create_set_autocomplete_mode_action()
  904. {
  905. auto action = GUI::Action::create_checkable("AutoComplete C++ with Parser", [this](auto& action) {
  906. get_language_client<LanguageClients::Cpp::ServerConnection>(project().root_path())->set_autocomplete_mode(action.is_checked() ? "Parser" : "Lexer");
  907. });
  908. action->set_checked(true);
  909. return action;
  910. }
  911. void HackStudioWidget::initialize_menubar(GUI::MenuBar& menubar)
  912. {
  913. create_app_menubar(menubar);
  914. create_project_menubar(menubar);
  915. create_edit_menubar(menubar);
  916. create_build_menubar(menubar);
  917. create_view_menubar(menubar);
  918. create_help_menubar(menubar);
  919. }
  920. HackStudioWidget::~HackStudioWidget()
  921. {
  922. if (!m_debugger_thread.is_null()) {
  923. Debugger::the().set_requested_debugger_action(Debugger::DebuggerAction::Exit);
  924. dbgln("Waiting for debugger thread to terminate");
  925. auto rc = m_debugger_thread->join();
  926. if (rc.is_error()) {
  927. warnln("pthread_join: {}", strerror(rc.error().value()));
  928. dbgln("error joining debugger thread");
  929. }
  930. }
  931. }
  932. }