From 40da077f6c702fe39d4494cf421856635c023980 Mon Sep 17 00:00:00 2001 From: Matteo Sozzi Date: Sun, 31 Jan 2021 19:06:41 +0100 Subject: [PATCH] HackStudio: added new directory action Added the possibility to create a new directory in the current project. --- .../DevTools/HackStudio/HackStudioWidget.cpp | 30 +++++++++++++++---- .../DevTools/HackStudio/HackStudioWidget.h | 6 ++-- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/Userland/DevTools/HackStudio/HackStudioWidget.cpp b/Userland/DevTools/HackStudio/HackStudioWidget.cpp index 227e5feef50..2bcbdfe0d08 100644 --- a/Userland/DevTools/HackStudio/HackStudioWidget.cpp +++ b/Userland/DevTools/HackStudio/HackStudioWidget.cpp @@ -45,6 +45,7 @@ #include "TerminalWrapper.h" #include "WidgetTool.h" #include "WidgetTreeModel.h" +#include #include #include #include @@ -286,18 +287,20 @@ void HackStudioWidget::set_edit_mode(EditMode mode) NonnullRefPtr HackStudioWidget::create_project_tree_view_context_menu() { m_open_selected_action = create_open_selected_action(); - m_new_action = create_new_action(); + m_new_file_action = create_new_file_action(); + m_new_directory_action = create_new_directory_action(); m_delete_action = create_delete_action(); auto project_tree_view_context_menu = GUI::Menu::construct("Project Files"); project_tree_view_context_menu->add_action(*m_open_selected_action); // TODO: Rename, cut, copy, duplicate with new name, show containing folder ... project_tree_view_context_menu->add_separator(); - project_tree_view_context_menu->add_action(*m_new_action); + project_tree_view_context_menu->add_action(*m_new_file_action); + project_tree_view_context_menu->add_action(*m_new_directory_action); project_tree_view_context_menu->add_action(*m_delete_action); return project_tree_view_context_menu; } -NonnullRefPtr HackStudioWidget::create_new_action() +NonnullRefPtr HackStudioWidget::create_new_file_action() { 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&) { String filename; @@ -312,6 +315,21 @@ NonnullRefPtr HackStudioWidget::create_new_action() }); } +NonnullRefPtr HackStudioWidget::create_new_directory_action() +{ + 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&) { + String directory_name; + if (GUI::InputBox::show(directory_name, window(), "Enter name of new directory:", "Add new folder to project") != GUI::InputBox::ExecOK) + return; + auto formatted_dir_name = LexicalPath::canonicalized_path(String::formatted("{}/{}", m_project->model().root_path(), directory_name)); + int rc = mkdir(formatted_dir_name.characters(), 0755); + if (rc < 0) { + GUI::MessageBox::show(window(), "Failed to create new directory", "Error", GUI::MessageBox::Type::Error); + return; + } + }); +} + NonnullRefPtr HackStudioWidget::create_open_selected_action() { @@ -758,7 +776,8 @@ void HackStudioWidget::create_form_editor(GUI::Widget& parent) void HackStudioWidget::create_toolbar(GUI::Widget& parent) { auto& toolbar = parent.add(); - toolbar.add_action(*m_new_action); + toolbar.add_action(*m_new_file_action); + toolbar.add_action(*m_new_directory_action); toolbar.add_action(*m_save_action); toolbar.add_action(*m_delete_action); toolbar.add_separator(); @@ -838,7 +857,8 @@ void HackStudioWidget::create_app_menubar(GUI::MenuBar& menubar) void HackStudioWidget::create_project_menubar(GUI::MenuBar& menubar) { auto& project_menu = menubar.add_menu("Project"); - project_menu.add_action(*m_new_action); + project_menu.add_action(*m_new_file_action); + project_menu.add_action(*m_new_directory_action); project_menu.add_action(*create_set_autocomplete_mode_action()); } diff --git a/Userland/DevTools/HackStudio/HackStudioWidget.h b/Userland/DevTools/HackStudio/HackStudioWidget.h index d35ec852840..3723ba4874d 100644 --- a/Userland/DevTools/HackStudio/HackStudioWidget.h +++ b/Userland/DevTools/HackStudio/HackStudioWidget.h @@ -79,7 +79,8 @@ private: void set_edit_mode(EditMode); NonnullRefPtr create_project_tree_view_context_menu(); - NonnullRefPtr create_new_action(); + NonnullRefPtr create_new_file_action(); + NonnullRefPtr create_new_directory_action(); NonnullRefPtr create_open_selected_action(); NonnullRefPtr create_delete_action(); NonnullRefPtr create_switch_to_next_editor_action(); @@ -152,7 +153,8 @@ private: RefPtr m_debugger_thread; RefPtr m_current_editor_in_execution; - RefPtr m_new_action; + RefPtr m_new_file_action; + RefPtr m_new_directory_action; RefPtr m_open_selected_action; RefPtr m_delete_action; RefPtr m_switch_to_next_editor;