HackStudioWidget.cpp 39 KB

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