HackStudioWidget.cpp 37 KB

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