HackStudioWidget.cpp 42 KB

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