HackStudioWidget.cpp 42 KB

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