HackStudioWidget.cpp 37 KB

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