[studio] Add support for adding and deleting directories

This commit is contained in:
2025-01-19 09:06:16 -06:00
parent 9d1155843e
commit cc466a9f1d
12 changed files with 184 additions and 20 deletions

View File

@ -5,6 +5,7 @@ add_library(
deleteconfirmation.cpp
filedialogmanager.cpp
main.cpp
newdir.cpp
newmenu.cpp
newproject.cpp
projectexplorer.cpp

View File

@ -43,7 +43,7 @@ void DeleteConfirmation::draw(StudioContext &ctx) noexcept {
case Stage::Open:
drawWindow(ctx.tctx, m_open, [this] {
ImGui::Text("Are you sure you want to delete %s?", m_path.c_str());
if (ig::PopupControlsOkCancel(m_open) != ig::PopupResponse::None) {
if (ig::PopupControlsOkCancel(m_open, "Yes", "No") != ig::PopupResponse::None) {
deleteFile.emit(m_path);
close();
}

View File

@ -0,0 +1,59 @@
/*
* Copyright 2016 - 2025 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include <studio/imguiutil.hpp>
#include "newdir.hpp"
namespace studio {
NewDir::NewDir() noexcept {
setTitle("New Directory");
setSize({280, 0});
}
void NewDir::openPath(ox::StringViewCR path) noexcept {
open();
m_path = path;
}
void NewDir::open() noexcept {
m_path = "";
m_stage = Stage::Opening;
}
void NewDir::close() noexcept {
m_stage = Stage::Closed;
m_open = false;
}
bool NewDir::isOpen() const noexcept {
return m_open;
}
void NewDir::draw(StudioContext &ctx) noexcept {
switch (m_stage) {
case Stage::Closed:
break;
case Stage::Opening:
ImGui::OpenPopup(title().c_str());
m_open = true;
[[fallthrough]];
case Stage::Open:
drawWindow(ctx.tctx, m_open, [this] {
if (m_stage == Stage::Opening) {
ImGui::SetKeyboardFocusHere();
}
ig::InputText("Name", m_str);
if (ig::PopupControlsOkCancel(m_open) == ig::PopupResponse::OK) {
newDir.emit(m_path + "/" + m_str);
close();
}
});
m_stage = Stage::Open;
break;
}
}
}

View File

@ -0,0 +1,49 @@
/*
* Copyright 2016 - 2025 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#include <ox/std/string.hpp>
#include <studio/context.hpp>
#include <studio/popup.hpp>
namespace studio {
class NewDir final: public Popup {
private:
enum class Stage {
Closed,
Opening,
Open,
};
Stage m_stage = Stage::Closed;
bool m_open{};
ox::String m_path;
ox::IString<255> m_str;
public:
ox::Signal<ox::Error(ox::StringViewCR path)> newDir;
NewDir() noexcept;
void openPath(ox::StringViewCR path) noexcept;
void open() noexcept override;
void close() noexcept override;
[[nodiscard]]
bool isOpen() const noexcept override;
void draw(StudioContext &ctx) noexcept override;
[[nodiscard]]
constexpr ox::CStringView value() const noexcept {
return m_str;
}
};
}

View File

@ -27,13 +27,13 @@ ProjectTreeModel::ProjectTreeModel(ProjectTreeModel &&other) noexcept:
m_children(std::move(other.m_children)) {
}
void ProjectTreeModel::draw(turbine::Context &ctx) const noexcept {
void ProjectTreeModel::draw(turbine::Context &tctx) const noexcept {
constexpr auto dirFlags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick;
if (!m_children.empty()) {
if (ImGui::TreeNodeEx(m_name.c_str(), dirFlags)) {
drawDirContextMenu();
for (auto const&child : m_children) {
child->draw(ctx);
child->draw(tctx);
}
ImGui::TreePop();
} else {
@ -54,6 +54,9 @@ void ProjectTreeModel::draw(turbine::Context &ctx) const noexcept {
ImGui::EndPopup();
}
ImGui::TreePop();
ig::dragDropSource([this] {
ImGui::Text("%s", m_name.c_str());
});
}
}
}
@ -70,13 +73,16 @@ void ProjectTreeModel::drawDirContextMenu() const noexcept {
if (ImGui::MenuItem("Add Directory")) {
m_explorer.addDir.emit(fullPath());
}
if (ImGui::MenuItem("Delete")) {
m_explorer.deleteItem.emit(fullPath());
}
ImGui::EndPopup();
}
}
ox::BasicString<255> ProjectTreeModel::fullPath() const noexcept {
if (m_parent) {
return m_parent->fullPath() + "/" + ox::StringView(m_name);
return m_parent->fullPath() + "/" + m_name;
}
return {};
}

View File

@ -25,7 +25,7 @@ class ProjectTreeModel {
ProjectTreeModel(ProjectTreeModel &&other) noexcept;
void draw(turbine::Context &ctx) const noexcept;
void draw(turbine::Context &tctx) const noexcept;
void setChildren(ox::Vector<ox::UPtr<ProjectTreeModel>> children) noexcept;

View File

@ -52,6 +52,7 @@ StudioUI::StudioUI(turbine::Context &ctx, ox::StringParam projectDataDir) noexce
m_aboutPopup(m_tctx) {
turbine::setApplicationData(m_tctx, &m_sctx);
m_projectExplorer.fileChosen.connect(this, &StudioUI::openFile);
m_projectExplorer.addDir.connect(this, &StudioUI::addDir);
m_projectExplorer.addItem.connect(this, &StudioUI::addFile);
m_projectExplorer.deleteItem.connect(this, &StudioUI::deleteFile);
m_newProject.finished.connect(this, &StudioUI::createOpenProject);
@ -344,6 +345,11 @@ void StudioUI::handleKeyInput() noexcept {
}
}
ox::Error StudioUI::addDir(ox::StringViewCR path) noexcept {
m_newDirDialog.openPath(path);
return {};
}
ox::Error StudioUI::addFile(ox::StringViewCR path) noexcept {
m_newMenu.openPath(path);
return {};
@ -369,8 +375,10 @@ ox::Error StudioUI::openProjectPath(ox::StringParam path) noexcept {
ox::make_unique_catch<Project>(keelCtx(m_tctx), std::move(path), m_projectDataDir)
.moveTo(m_project));
m_sctx.project = m_project.get();
m_deleteConfirmation.deleteFile.connect(m_sctx.project, &Project::deleteItem);
turbine::setWindowTitle(m_tctx, ox::sfmt("{} - {}", keelCtx(m_tctx).appName, m_project->projectPath()));
m_deleteConfirmation.deleteFile.connect(m_sctx.project, &Project::deleteItem);
m_newDirDialog.newDir.connect(m_sctx.project, &Project::mkdir);
m_project->dirAdded.connect(&m_projectExplorer, &ProjectExplorer::refreshProjectTreeModel);
m_project->fileAdded.connect(&m_projectExplorer, &ProjectExplorer::refreshProjectTreeModel);
m_project->fileDeleted.connect(&m_projectExplorer, &ProjectExplorer::refreshProjectTreeModel);
m_openFiles.clear();

View File

@ -15,6 +15,7 @@
#include "aboutpopup.hpp"
#include "deleteconfirmation.hpp"
#include "newdir.hpp"
#include "newmenu.hpp"
#include "newproject.hpp"
#include "projectexplorer.hpp"
@ -41,13 +42,15 @@ class StudioUI: public ox::SignalHandler {
BaseEditor *m_activeEditorUpdatePending = nullptr;
NewMenu m_newMenu;
DeleteConfirmation m_deleteConfirmation;
NewDir m_newDirDialog;
NewProject m_newProject;
AboutPopup m_aboutPopup;
ox::Array<Popup*, 4> const m_popups = {
ox::Array<Popup*, 5> const m_popups = {
&m_newMenu,
&m_newProject,
&m_aboutPopup,
&m_deleteConfirmation,
&m_newDirDialog,
};
bool m_showProjectExplorer = true;
@ -87,6 +90,8 @@ class StudioUI: public ox::SignalHandler {
void handleKeyInput() noexcept;
ox::Error addDir(ox::StringViewCR path) noexcept;
ox::Error addFile(ox::StringViewCR path) noexcept;
ox::Error deleteFile(ox::StringViewCR path) noexcept;