mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 02:48:11 +00:00
HackStudio: Implement adding a new file to the project
You can now press Ctrl+N to create and add a new file to the project!
This commit is contained in:
parent
b513a787fb
commit
9129dbe0b9
3 changed files with 49 additions and 6 deletions
|
@ -24,13 +24,17 @@ public:
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
virtual void update() override {}
|
virtual void update() override
|
||||||
|
{
|
||||||
|
did_update();
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Project& m_project;
|
Project& m_project;
|
||||||
};
|
};
|
||||||
|
|
||||||
Project::Project(Vector<String>&& filenames)
|
Project::Project(const String& path, Vector<String>&& filenames)
|
||||||
|
: m_path(path)
|
||||||
{
|
{
|
||||||
for (auto& filename : filenames) {
|
for (auto& filename : filenames) {
|
||||||
m_files.append(TextDocument::construct_with_name(filename));
|
m_files.append(TextDocument::construct_with_name(filename));
|
||||||
|
@ -52,5 +56,26 @@ OwnPtr<Project> Project::load_from_file(const String& path)
|
||||||
files.append(String::copy(line, Chomp));
|
files.append(String::copy(line, Chomp));
|
||||||
}
|
}
|
||||||
|
|
||||||
return OwnPtr(new Project(move(files)));
|
return OwnPtr(new Project(path, move(files)));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Project::add_file(const String& filename)
|
||||||
|
{
|
||||||
|
auto project_file = CFile::construct(m_path);
|
||||||
|
if (!project_file->open(CFile::WriteOnly))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for (auto& file : m_files) {
|
||||||
|
// FIXME: Check for error here. CIODevice::printf() needs some work on error reporting.
|
||||||
|
project_file->printf("%s\n", file.name().characters());
|
||||||
|
}
|
||||||
|
|
||||||
|
project_file->printf("%s\n", filename.characters());
|
||||||
|
|
||||||
|
if (!project_file->close())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
m_files.append(TextDocument::construct_with_name(filename));
|
||||||
|
m_model->update();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,8 @@ class Project {
|
||||||
public:
|
public:
|
||||||
static OwnPtr<Project> load_from_file(const String& path);
|
static OwnPtr<Project> load_from_file(const String& path);
|
||||||
|
|
||||||
|
[[nodiscard]] bool add_file(const String& filename);
|
||||||
|
|
||||||
GModel& model() { return *m_model; }
|
GModel& model() { return *m_model; }
|
||||||
|
|
||||||
template<typename Callback>
|
template<typename Callback>
|
||||||
|
@ -22,10 +24,12 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class ProjectModel;
|
friend class ProjectModel;
|
||||||
explicit Project(Vector<String>&& files);
|
explicit Project(const String& path, Vector<String>&& files);
|
||||||
|
|
||||||
|
String m_path;
|
||||||
RefPtr<GModel> m_model;
|
RefPtr<GModel> m_model;
|
||||||
NonnullRefPtrVector<TextDocument> m_files;
|
NonnullRefPtrVector<TextDocument> m_files;
|
||||||
};
|
};
|
||||||
|
|
|
@ -76,8 +76,22 @@ int main(int argc, char** argv)
|
||||||
g_text_editor->set_line_wrapping_enabled(true);
|
g_text_editor->set_line_wrapping_enabled(true);
|
||||||
g_text_editor->set_automatic_indentation_enabled(true);
|
g_text_editor->set_automatic_indentation_enabled(true);
|
||||||
|
|
||||||
auto new_action = GAction::create("New", { Mod_Ctrl, Key_N }, GraphicsBitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GAction&) {
|
auto new_action = GAction::create("Add new file to project", { Mod_Ctrl, Key_N }, GraphicsBitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GAction&) {
|
||||||
// FIXME: Implement.
|
auto input_box = GInputBox::construct("Enter name of new file:", "Add new file to project", g_window);
|
||||||
|
if (input_box->exec() == GInputBox::ExecCancel)
|
||||||
|
return;
|
||||||
|
auto filename = input_box->text_value();
|
||||||
|
auto file = CFile::construct(filename);
|
||||||
|
if (!file->open((CIODevice::OpenMode)(CIODevice::WriteOnly | CIODevice::MustBeNew))) {
|
||||||
|
GMessageBox::show(String::format("Failed to create '%s'", filename.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, g_window);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!g_project->add_file(filename)) {
|
||||||
|
GMessageBox::show(String::format("Failed to add '%s' to project", filename.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, g_window);
|
||||||
|
// FIXME: Should we unlink the file here maybe?
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
open_file(filename);
|
||||||
});
|
});
|
||||||
|
|
||||||
auto open_action = GCommonActions::make_open_action([&](auto&) {
|
auto open_action = GCommonActions::make_open_action([&](auto&) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue