HackStudioWidget.cpp 44 KB

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