HackStudioWidget.cpp 40 KB

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