HackStudioWidget.cpp 36 KB

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