HackStudioWidget.cpp 39 KB

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