HackStudioWidget.cpp 45 KB

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