HackStudioWidget.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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. }
  398. }
  399. });
  400. delete_action->set_enabled(false);
  401. return delete_action;
  402. }
  403. NonnullRefPtr<GUI::Action> HackStudioWidget::create_new_project_action()
  404. {
  405. 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&) {
  406. auto dialog = NewProjectDialog::construct(window());
  407. dialog->set_icon(window()->icon());
  408. auto result = dialog->exec();
  409. if (result == GUI::Dialog::ExecResult::ExecOK && dialog->created_project_path().has_value())
  410. open_project(dialog->created_project_path().value());
  411. });
  412. }
  413. void HackStudioWidget::add_new_editor(GUI::Widget& parent)
  414. {
  415. auto wrapper = EditorWrapper::construct();
  416. if (m_action_tab_widget) {
  417. parent.insert_child_before(wrapper, *m_action_tab_widget);
  418. } else {
  419. parent.add_child(wrapper);
  420. }
  421. m_current_editor_wrapper = wrapper;
  422. m_all_editor_wrappers.append(wrapper);
  423. wrapper->editor().set_focus(true);
  424. wrapper->set_project_root(LexicalPath(m_project->root_path()));
  425. wrapper->editor().on_cursor_change = [this] { update_statusbar(); };
  426. }
  427. NonnullRefPtr<GUI::Action> HackStudioWidget::create_switch_to_next_editor_action()
  428. {
  429. return GUI::Action::create("Switch to &Next Editor", { Mod_Ctrl, Key_E }, [this](auto&) {
  430. if (m_all_editor_wrappers.size() <= 1)
  431. return;
  432. Vector<EditorWrapper&> wrappers;
  433. m_editors_splitter->for_each_child_of_type<EditorWrapper>([&wrappers](auto& child) {
  434. wrappers.append(child);
  435. return IterationDecision::Continue;
  436. });
  437. for (size_t i = 0; i < wrappers.size(); ++i) {
  438. if (m_current_editor_wrapper.ptr() == &wrappers[i]) {
  439. if (i == wrappers.size() - 1)
  440. wrappers[0].editor().set_focus(true);
  441. else
  442. wrappers[i + 1].editor().set_focus(true);
  443. }
  444. }
  445. });
  446. }
  447. NonnullRefPtr<GUI::Action> HackStudioWidget::create_switch_to_previous_editor_action()
  448. {
  449. return GUI::Action::create("Switch to &Previous Editor", { Mod_Ctrl | Mod_Shift, Key_E }, [this](auto&) {
  450. if (m_all_editor_wrappers.size() <= 1)
  451. return;
  452. Vector<EditorWrapper&> wrappers;
  453. m_editors_splitter->for_each_child_of_type<EditorWrapper>([&wrappers](auto& child) {
  454. wrappers.append(child);
  455. return IterationDecision::Continue;
  456. });
  457. for (int i = wrappers.size() - 1; i >= 0; --i) {
  458. if (m_current_editor_wrapper.ptr() == &wrappers[i]) {
  459. if (i == 0)
  460. wrappers.last().editor().set_focus(true);
  461. else
  462. wrappers[i - 1].editor().set_focus(true);
  463. }
  464. }
  465. });
  466. }
  467. NonnullRefPtr<GUI::Action> HackStudioWidget::create_remove_current_editor_action()
  468. {
  469. return GUI::Action::create("&Remove Current Editor", { Mod_Alt | Mod_Shift, Key_E }, [this](auto&) {
  470. if (m_all_editor_wrappers.size() <= 1)
  471. return;
  472. auto wrapper = m_current_editor_wrapper;
  473. m_switch_to_next_editor->activate();
  474. m_editors_splitter->remove_child(*wrapper);
  475. m_all_editor_wrappers.remove_first_matching([&wrapper](auto& entry) { return entry == wrapper.ptr(); });
  476. update_actions();
  477. });
  478. }
  479. NonnullRefPtr<GUI::Action> HackStudioWidget::create_open_action()
  480. {
  481. return GUI::Action::create("&Open Project...", { Mod_Ctrl | Mod_Shift, Key_O }, Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), [this](auto&) {
  482. auto open_path = GUI::FilePicker::get_open_filepath(window(), "Open project", Core::StandardPaths::home_directory(), true);
  483. if (!open_path.has_value())
  484. return;
  485. open_project(open_path.value());
  486. update_actions();
  487. });
  488. }
  489. NonnullRefPtr<GUI::Action> HackStudioWidget::create_save_action()
  490. {
  491. return GUI::CommonActions::make_save_action([&](auto&) {
  492. if (active_file().is_empty())
  493. return;
  494. current_editor_wrapper().save();
  495. if (m_git_widget->initialized())
  496. m_git_widget->refresh();
  497. });
  498. }
  499. NonnullRefPtr<GUI::Action> HackStudioWidget::create_remove_current_terminal_action()
  500. {
  501. return GUI::Action::create("Remove &Current Terminal", { Mod_Alt | Mod_Shift, Key_T }, [this](auto&) {
  502. auto widget = m_action_tab_widget->active_widget();
  503. if (!widget)
  504. return;
  505. if (!is<TerminalWrapper>(widget))
  506. return;
  507. auto& terminal = *static_cast<TerminalWrapper*>(widget);
  508. if (!terminal.user_spawned())
  509. return;
  510. m_action_tab_widget->remove_tab(terminal);
  511. update_actions();
  512. });
  513. }
  514. NonnullRefPtr<GUI::Action> HackStudioWidget::create_add_editor_action()
  515. {
  516. return GUI::Action::create("Add New &Editor", { Mod_Ctrl | Mod_Alt, Key_E },
  517. Gfx::Bitmap::load_from_file("/res/icons/16x16/app-text-editor.png"),
  518. [this](auto&) {
  519. add_new_editor(*m_editors_splitter);
  520. update_actions();
  521. });
  522. }
  523. NonnullRefPtr<GUI::Action> HackStudioWidget::create_add_terminal_action()
  524. {
  525. return GUI::Action::create("Add New &Terminal", { Mod_Ctrl | Mod_Alt, Key_T },
  526. Gfx::Bitmap::load_from_file("/res/icons/16x16/app-terminal.png"),
  527. [this](auto&) {
  528. auto& terminal_wrapper = m_action_tab_widget->add_tab<TerminalWrapper>("Terminal");
  529. reveal_action_tab(terminal_wrapper);
  530. update_actions();
  531. terminal_wrapper.terminal().set_focus(true);
  532. });
  533. }
  534. void HackStudioWidget::reveal_action_tab(GUI::Widget& widget)
  535. {
  536. if (m_action_tab_widget->min_height() < 200)
  537. m_action_tab_widget->set_fixed_height(200);
  538. m_action_tab_widget->set_active_widget(&widget);
  539. }
  540. NonnullRefPtr<GUI::Action> HackStudioWidget::create_debug_action()
  541. {
  542. return GUI::Action::create("&Debug", Gfx::Bitmap::load_from_file("/res/icons/16x16/debug-run.png"), [this](auto&) {
  543. if (!Core::File::exists(get_project_executable_path())) {
  544. GUI::MessageBox::show(window(), String::formatted("Could not find file: {}. (did you build the project?)", get_project_executable_path()), "Error", GUI::MessageBox::Type::Error);
  545. return;
  546. }
  547. if (Debugger::the().session()) {
  548. GUI::MessageBox::show(window(), "Debugger is already running", "Error", GUI::MessageBox::Type::Error);
  549. return;
  550. }
  551. Debugger::the().set_executable_path(get_project_executable_path());
  552. m_debugger_thread = Threading::Thread::construct(Debugger::start_static);
  553. m_debugger_thread->start();
  554. m_stop_action->set_enabled(true);
  555. m_run_action->set_enabled(false);
  556. for (auto& editor_wrapper : m_all_editor_wrappers) {
  557. editor_wrapper.set_debug_mode(true);
  558. }
  559. });
  560. }
  561. void HackStudioWidget::initialize_debugger()
  562. {
  563. Debugger::initialize(
  564. m_project->root_path(),
  565. [this](const PtraceRegisters& regs) {
  566. VERIFY(Debugger::the().session());
  567. const auto& debug_session = *Debugger::the().session();
  568. auto source_position = debug_session.get_source_position(regs.eip);
  569. if (!source_position.has_value()) {
  570. dbgln("Could not find source position for address: {:p}", regs.eip);
  571. return Debugger::HasControlPassedToUser::No;
  572. }
  573. dbgln("Debugger stopped at source position: {}:{}", source_position.value().file_path, source_position.value().line_number);
  574. Core::EventLoop::main().post_event(
  575. *window(),
  576. make<Core::DeferredInvocationEvent>(
  577. [this, source_position, &regs](auto&) {
  578. m_current_editor_in_execution = get_editor_of_file(source_position.value().file_path);
  579. if (m_current_editor_in_execution)
  580. m_current_editor_in_execution->editor().set_execution_position(source_position.value().line_number - 1);
  581. m_debug_info_widget->update_state(*Debugger::the().session(), regs);
  582. m_debug_info_widget->set_debug_actions_enabled(true);
  583. m_disassembly_widget->update_state(*Debugger::the().session(), regs);
  584. HackStudioWidget::reveal_action_tab(*m_debug_info_widget);
  585. }));
  586. Core::EventLoop::wake();
  587. return Debugger::HasControlPassedToUser::Yes;
  588. },
  589. [this]() {
  590. Core::EventLoop::main().post_event(*window(), make<Core::DeferredInvocationEvent>([this](auto&) {
  591. m_debug_info_widget->set_debug_actions_enabled(false);
  592. if (m_current_editor_in_execution)
  593. m_current_editor_in_execution->editor().clear_execution_position();
  594. }));
  595. Core::EventLoop::wake();
  596. },
  597. [this]() {
  598. Core::EventLoop::main().post_event(*window(), make<Core::DeferredInvocationEvent>([this](auto&) {
  599. m_debug_info_widget->set_debug_actions_enabled(false);
  600. if (m_current_editor_in_execution)
  601. m_current_editor_in_execution->editor().clear_execution_position();
  602. m_debug_info_widget->program_stopped();
  603. m_disassembly_widget->program_stopped();
  604. m_stop_action->set_enabled(false);
  605. m_run_action->set_enabled(true);
  606. m_debugger_thread.clear();
  607. for (auto& editor_wrapper : m_all_editor_wrappers) {
  608. editor_wrapper.set_debug_mode(false);
  609. }
  610. HackStudioWidget::hide_action_tabs();
  611. GUI::MessageBox::show(window(), "Program Exited", "Debugger", GUI::MessageBox::Type::Information);
  612. }));
  613. Core::EventLoop::wake();
  614. });
  615. }
  616. String HackStudioWidget::get_full_path_of_serenity_source(const String& file)
  617. {
  618. auto path_parts = LexicalPath(file).parts();
  619. VERIFY(path_parts[0] == "..");
  620. path_parts.remove(0);
  621. StringBuilder relative_path_builder;
  622. relative_path_builder.join("/", path_parts);
  623. constexpr char SERENITY_LIBS_PREFIX[] = "/usr/src/serenity";
  624. LexicalPath serenity_sources_base(SERENITY_LIBS_PREFIX);
  625. return String::formatted("{}/{}", serenity_sources_base, relative_path_builder.to_string());
  626. }
  627. RefPtr<EditorWrapper> HackStudioWidget::get_editor_of_file(const String& filename)
  628. {
  629. String file_path = filename;
  630. // TODO: We can probably do a more specific condition here, something like
  631. // "if (file.starts_with("../Libraries/") || file.starts_with("../AK/"))"
  632. if (filename.starts_with("../")) {
  633. file_path = get_full_path_of_serenity_source(filename);
  634. }
  635. if (!open_file(file_path))
  636. return nullptr;
  637. return current_editor_wrapper();
  638. }
  639. String HackStudioWidget::get_project_executable_path() const
  640. {
  641. // FIXME: Dumb heuristic ahead!
  642. // e.g /my/project => /my/project/project
  643. // TODO: Perhaps a Makefile rule for getting the value of $(PROGRAM) would be better?
  644. return String::formatted("{}/{}", m_project->root_path(), LexicalPath::basename(m_project->root_path()));
  645. }
  646. void HackStudioWidget::build(TerminalWrapper& wrapper)
  647. {
  648. if (active_file().ends_with(".js"))
  649. wrapper.run_command(String::formatted("js -A {}", active_file()));
  650. else
  651. wrapper.run_command("make");
  652. }
  653. void HackStudioWidget::run(TerminalWrapper& wrapper)
  654. {
  655. if (active_file().ends_with(".js"))
  656. wrapper.run_command(String::formatted("js {}", active_file()));
  657. else
  658. wrapper.run_command("make run");
  659. }
  660. void HackStudioWidget::hide_action_tabs()
  661. {
  662. m_action_tab_widget->set_fixed_height(24);
  663. };
  664. Project& HackStudioWidget::project()
  665. {
  666. return *m_project;
  667. }
  668. void HackStudioWidget::set_current_editor_wrapper(RefPtr<EditorWrapper> editor_wrapper)
  669. {
  670. m_current_editor_wrapper = editor_wrapper;
  671. }
  672. void HackStudioWidget::configure_project_tree_view()
  673. {
  674. m_project_tree_view->set_model(m_project->model());
  675. m_project_tree_view->set_selection_mode(GUI::AbstractView::SelectionMode::MultiSelection);
  676. for (int column_index = 0; column_index < m_project->model().column_count(); ++column_index)
  677. m_project_tree_view->set_column_visible(column_index, false);
  678. m_project_tree_view->set_column_visible(GUI::FileSystemModel::Column::Name, true);
  679. m_project_tree_view->on_context_menu_request = [this](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  680. if (index.is_valid()) {
  681. m_project_tree_view_context_menu->popup(event.screen_position(), m_open_selected_action);
  682. }
  683. };
  684. m_project_tree_view->on_selection_change = [this] {
  685. m_open_selected_action->set_enabled(!m_project_tree_view->selection().is_empty());
  686. auto selections = m_project_tree_view->selection().indices();
  687. auto it = selections.find_if([&](auto selected_file) {
  688. return access(m_project->model().full_path(selected_file.parent()).characters(), W_OK) == 0;
  689. });
  690. bool has_permissions = it != selections.end();
  691. m_delete_action->set_enabled(!m_project_tree_view->selection().is_empty() && has_permissions);
  692. };
  693. m_project_tree_view->on_activation = [this](auto& index) {
  694. auto full_path_to_file = m_project->model().full_path(index);
  695. open_file(full_path_to_file);
  696. };
  697. }
  698. void HackStudioWidget::create_open_files_view(GUI::Widget& parent)
  699. {
  700. m_open_files_view = parent.add<GUI::ListView>();
  701. auto open_files_model = GUI::ItemListModel<String>::create(m_open_files_vector);
  702. m_open_files_view->set_model(open_files_model);
  703. m_open_files_view->on_activation = [this](auto& index) {
  704. open_file(index.data().to_string());
  705. };
  706. }
  707. void HackStudioWidget::create_toolbar(GUI::Widget& parent)
  708. {
  709. auto& toolbar = parent.add<GUI::Toolbar>();
  710. toolbar.add_action(*m_new_file_action);
  711. toolbar.add_action(*m_new_directory_action);
  712. toolbar.add_action(*m_save_action);
  713. toolbar.add_action(*m_delete_action);
  714. toolbar.add_separator();
  715. toolbar.add_action(GUI::CommonActions::make_cut_action([this](auto&) { current_editor().cut_action().activate(); }));
  716. toolbar.add_action(GUI::CommonActions::make_copy_action([this](auto&) { current_editor().copy_action().activate(); }));
  717. toolbar.add_action(GUI::CommonActions::make_paste_action([this](auto&) { current_editor().paste_action().activate(); }));
  718. toolbar.add_separator();
  719. toolbar.add_action(GUI::CommonActions::make_undo_action([this](auto&) { current_editor().undo_action().activate(); }));
  720. toolbar.add_action(GUI::CommonActions::make_redo_action([this](auto&) { current_editor().redo_action().activate(); }));
  721. toolbar.add_separator();
  722. toolbar.add_action(*m_build_action);
  723. toolbar.add_separator();
  724. toolbar.add_action(*m_run_action);
  725. toolbar.add_action(*m_stop_action);
  726. toolbar.add_separator();
  727. toolbar.add_action(*m_debug_action);
  728. }
  729. NonnullRefPtr<GUI::Action> HackStudioWidget::create_build_action()
  730. {
  731. return GUI::Action::create("&Build", { Mod_Ctrl, Key_B }, Gfx::Bitmap::load_from_file("/res/icons/16x16/build.png"), [this](auto&) {
  732. if (warn_unsaved_changes("There are unsaved changes, do you want to save before building?") == ContinueDecision::No)
  733. return;
  734. reveal_action_tab(*m_terminal_wrapper);
  735. build(*m_terminal_wrapper);
  736. m_stop_action->set_enabled(true);
  737. });
  738. }
  739. NonnullRefPtr<GUI::Action> HackStudioWidget::create_run_action()
  740. {
  741. return GUI::Action::create("&Run", { Mod_Ctrl, Key_R }, Gfx::Bitmap::load_from_file("/res/icons/16x16/program-run.png"), [this](auto&) {
  742. reveal_action_tab(*m_terminal_wrapper);
  743. run(*m_terminal_wrapper);
  744. m_stop_action->set_enabled(true);
  745. });
  746. }
  747. void HackStudioWidget::create_action_tab(GUI::Widget& parent)
  748. {
  749. m_action_tab_widget = parent.add<GUI::TabWidget>();
  750. m_action_tab_widget->set_fixed_height(24);
  751. m_action_tab_widget->on_change = [this](auto&) {
  752. on_action_tab_change();
  753. static bool first_time = true;
  754. if (!first_time)
  755. m_action_tab_widget->set_fixed_height(200);
  756. first_time = false;
  757. };
  758. m_find_in_files_widget = m_action_tab_widget->add_tab<FindInFilesWidget>("Find in files");
  759. m_todo_entries_widget = m_action_tab_widget->add_tab<ToDoEntriesWidget>("TODO");
  760. m_terminal_wrapper = m_action_tab_widget->add_tab<TerminalWrapper>("Build", false);
  761. m_debug_info_widget = m_action_tab_widget->add_tab<DebugInfoWidget>("Debug");
  762. m_disassembly_widget = m_action_tab_widget->add_tab<DisassemblyWidget>("Disassembly");
  763. m_git_widget = m_action_tab_widget->add_tab<GitWidget>("Git", LexicalPath(m_project->root_path()));
  764. m_git_widget->set_view_diff_callback([this](const auto& original_content, const auto& diff) {
  765. m_diff_viewer->set_content(original_content, diff);
  766. set_edit_mode(EditMode::Diff);
  767. });
  768. ToDoEntries::the().on_update = [this]() {
  769. m_todo_entries_widget->refresh();
  770. };
  771. }
  772. void HackStudioWidget::create_project_tab(GUI::Widget& parent)
  773. {
  774. m_project_tab = parent.add<GUI::TabWidget>();
  775. m_project_tab->set_tab_position(GUI::TabWidget::TabPosition::Bottom);
  776. auto& tree_view_container = m_project_tab->add_tab<GUI::Widget>("Files");
  777. tree_view_container.set_layout<GUI::VerticalBoxLayout>();
  778. tree_view_container.layout()->set_margins({ 2, 2, 2, 2 });
  779. m_project_tree_view = tree_view_container.add<GUI::TreeView>();
  780. configure_project_tree_view();
  781. auto& class_view_container = m_project_tab->add_tab<GUI::Widget>("Classes");
  782. class_view_container.set_layout<GUI::VerticalBoxLayout>();
  783. class_view_container.layout()->set_margins({ 2, 2, 2, 2 });
  784. m_class_view = class_view_container.add<ClassViewWidget>();
  785. ProjectDeclarations::the().on_update = [this]() {
  786. m_class_view->refresh();
  787. };
  788. }
  789. void HackStudioWidget::create_file_menubar(GUI::Menubar& menubar)
  790. {
  791. auto& file_menu = menubar.add_menu("&File");
  792. file_menu.add_action(*m_new_project_action);
  793. file_menu.add_action(*m_open_action);
  794. file_menu.add_action(*m_save_action);
  795. file_menu.add_separator();
  796. file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  797. GUI::Application::the()->quit();
  798. }));
  799. }
  800. void HackStudioWidget::create_project_menubar(GUI::Menubar& menubar)
  801. {
  802. auto& project_menu = menubar.add_menu("&Project");
  803. project_menu.add_action(*m_new_file_action);
  804. project_menu.add_action(*m_new_directory_action);
  805. }
  806. void HackStudioWidget::create_edit_menubar(GUI::Menubar& menubar)
  807. {
  808. auto& edit_menu = menubar.add_menu("&Edit");
  809. 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&) {
  810. reveal_action_tab(*m_find_in_files_widget);
  811. m_find_in_files_widget->focus_textbox_and_select_all();
  812. }));
  813. edit_menu.add_separator();
  814. auto vim_emulation_setting_action = GUI::Action::create_checkable("&Vim Emulation", { Mod_Ctrl | Mod_Shift | Mod_Alt, Key_V }, [this](auto& action) {
  815. if (action.is_checked())
  816. current_editor().set_editing_engine(make<GUI::VimEditingEngine>());
  817. else
  818. current_editor().set_editing_engine(make<GUI::RegularEditingEngine>());
  819. });
  820. vim_emulation_setting_action->set_checked(false);
  821. edit_menu.add_action(vim_emulation_setting_action);
  822. }
  823. void HackStudioWidget::create_build_menubar(GUI::Menubar& menubar)
  824. {
  825. auto& build_menu = menubar.add_menu("&Build");
  826. build_menu.add_action(*m_build_action);
  827. build_menu.add_separator();
  828. build_menu.add_action(*m_run_action);
  829. build_menu.add_action(*m_stop_action);
  830. build_menu.add_separator();
  831. build_menu.add_action(*m_debug_action);
  832. }
  833. void HackStudioWidget::create_view_menubar(GUI::Menubar& menubar)
  834. {
  835. auto hide_action_tabs_action = GUI::Action::create("&Hide Action Tabs", { Mod_Ctrl | Mod_Shift, Key_X }, [this](auto&) {
  836. hide_action_tabs();
  837. });
  838. auto open_locator_action = GUI::Action::create("Open &Locator", { Mod_Ctrl, Key_K }, [this](auto&) {
  839. m_locator->open();
  840. });
  841. auto& view_menu = menubar.add_menu("&View");
  842. view_menu.add_action(hide_action_tabs_action);
  843. view_menu.add_action(open_locator_action);
  844. view_menu.add_separator();
  845. m_wrapping_mode_actions.set_exclusive(true);
  846. auto& wrapping_mode_menu = view_menu.add_submenu("&Wrapping Mode");
  847. m_no_wrapping_action = GUI::Action::create_checkable("&No Wrapping", [&](auto&) {
  848. for (auto& wrapper : m_all_editor_wrappers)
  849. wrapper.editor().set_wrapping_mode(GUI::TextEditor::WrappingMode::NoWrap);
  850. });
  851. m_wrap_anywhere_action = GUI::Action::create_checkable("Wrap &Anywhere", [&](auto&) {
  852. for (auto& wrapper : m_all_editor_wrappers)
  853. wrapper.editor().set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAnywhere);
  854. });
  855. m_wrap_at_words_action = GUI::Action::create_checkable("Wrap at &Words", [&](auto&) {
  856. for (auto& wrapper : m_all_editor_wrappers)
  857. wrapper.editor().set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAtWords);
  858. });
  859. m_wrapping_mode_actions.add_action(*m_no_wrapping_action);
  860. m_wrapping_mode_actions.add_action(*m_wrap_anywhere_action);
  861. m_wrapping_mode_actions.add_action(*m_wrap_at_words_action);
  862. wrapping_mode_menu.add_action(*m_no_wrapping_action);
  863. wrapping_mode_menu.add_action(*m_wrap_anywhere_action);
  864. wrapping_mode_menu.add_action(*m_wrap_at_words_action);
  865. m_no_wrapping_action->set_checked(true);
  866. view_menu.add_separator();
  867. view_menu.add_action(*m_add_editor_action);
  868. view_menu.add_action(*m_remove_current_editor_action);
  869. view_menu.add_action(*m_add_terminal_action);
  870. view_menu.add_action(*m_remove_current_terminal_action);
  871. }
  872. void HackStudioWidget::create_help_menubar(GUI::Menubar& menubar)
  873. {
  874. auto& help_menu = menubar.add_menu("&Help");
  875. help_menu.add_action(GUI::CommonActions::make_about_action("Hack Studio", GUI::Icon::default_icon("app-hack-studio"), window()));
  876. }
  877. NonnullRefPtr<GUI::Action> HackStudioWidget::create_stop_action()
  878. {
  879. auto action = GUI::Action::create("&Stop", Gfx::Bitmap::load_from_file("/res/icons/16x16/program-stop.png"), [this](auto&) {
  880. if (!Debugger::the().session()) {
  881. m_terminal_wrapper->kill_running_command();
  882. return;
  883. }
  884. Debugger::the().stop();
  885. });
  886. action->set_enabled(false);
  887. return action;
  888. }
  889. void HackStudioWidget::initialize_menubar(GUI::Menubar& menubar)
  890. {
  891. create_file_menubar(menubar);
  892. create_project_menubar(menubar);
  893. create_edit_menubar(menubar);
  894. create_build_menubar(menubar);
  895. create_view_menubar(menubar);
  896. create_help_menubar(menubar);
  897. }
  898. void HackStudioWidget::update_statusbar()
  899. {
  900. m_statusbar->set_text(0, String::formatted("Ln {}, Col {}", current_editor().cursor().line() + 1, current_editor().cursor().column()));
  901. StringBuilder builder;
  902. if (current_editor().has_selection()) {
  903. String selected_text = current_editor().selected_text();
  904. auto word_count = current_editor().number_of_selected_words();
  905. builder.appendff("Selected: {} {} ({} {})", selected_text.length(), selected_text.length() == 1 ? "character" : "characters", word_count, word_count != 1 ? "words" : "word");
  906. }
  907. m_statusbar->set_text(1, builder.to_string());
  908. m_statusbar->set_text(2, current_editor_wrapper().editor().code_document().language_name());
  909. }
  910. void HackStudioWidget::handle_external_file_deletion(const String& filepath)
  911. {
  912. m_open_files.remove(filepath);
  913. m_open_files_vector.remove_all_matching(
  914. [&filepath](const String& element) { return element == filepath; });
  915. for (auto& editor_wrapper : m_all_editor_wrappers) {
  916. Editor& editor = editor_wrapper.editor();
  917. String editor_file_path = editor.code_document().file_path();
  918. String relative_editor_file_path = LexicalPath::relative_path(editor_file_path, project().root_path());
  919. if (relative_editor_file_path == filepath) {
  920. if (m_open_files_vector.is_empty()) {
  921. editor.set_document(CodeDocument::create());
  922. editor_wrapper.set_filename("");
  923. } else {
  924. auto& first_path = m_open_files_vector[0];
  925. auto& document = m_open_files.get(first_path).value()->code_document();
  926. editor.set_document(document);
  927. editor_wrapper.set_filename(first_path);
  928. }
  929. }
  930. }
  931. m_open_files_view->model()->update();
  932. }
  933. HackStudioWidget::~HackStudioWidget()
  934. {
  935. if (!m_debugger_thread.is_null()) {
  936. Debugger::the().stop();
  937. dbgln("Waiting for debugger thread to terminate");
  938. auto rc = m_debugger_thread->join();
  939. if (rc.is_error()) {
  940. warnln("pthread_join: {}", strerror(rc.error().value()));
  941. dbgln("error joining debugger thread");
  942. }
  943. }
  944. }
  945. HackStudioWidget::ContinueDecision HackStudioWidget::warn_unsaved_changes(const String& prompt)
  946. {
  947. if (!any_document_is_dirty())
  948. return ContinueDecision::Yes;
  949. auto result = GUI::MessageBox::show(window(), prompt, "Unsaved changes", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel);
  950. if (result == GUI::MessageBox::ExecCancel)
  951. return ContinueDecision::No;
  952. if (result == GUI::MessageBox::ExecYes) {
  953. for (auto& editor_wrapper : m_all_editor_wrappers) {
  954. if (editor_wrapper.document_dirty()) {
  955. editor_wrapper.save();
  956. }
  957. }
  958. }
  959. return ContinueDecision::Yes;
  960. }
  961. bool HackStudioWidget::any_document_is_dirty() const
  962. {
  963. for (auto& editor_wrapper : m_all_editor_wrappers) {
  964. if (editor_wrapper.document_dirty()) {
  965. return true;
  966. }
  967. }
  968. return false;
  969. }
  970. }