Skip to content

Commit

Permalink
Make some adjustments and add folder creation
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaMKW committed Oct 21, 2024
1 parent fa0b767 commit 9662506
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 8 deletions.
6 changes: 6 additions & 0 deletions include/model/fsmodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ namespace Toolbox {

[[nodiscard]] std::any getData(const ModelIndex &index, int role) const override;

[[nodiscard]] std::string findUniqueName(const ModelIndex &index,
const std::string &name) const;

ModelIndex mkdir(const ModelIndex &parent, const std::string &name);
ModelIndex touch(const ModelIndex &parent, const std::string &name);
ModelIndex rename(const ModelIndex &file, const std::string &new_name);
Expand Down Expand Up @@ -157,6 +160,9 @@ namespace Toolbox {
// Implementation of public API for mutex locking reasons
[[nodiscard]] std::any getData_(const ModelIndex &index, int role) const;

[[nodiscard]] std::string findUniqueName_(const ModelIndex &index,
const std::string &name) const;

ModelIndex mkdir_(const ModelIndex &parent, const std::string &name);
ModelIndex touch_(const ModelIndex &parent, const std::string &name);
ModelIndex rename_(const ModelIndex &file, const std::string &new_name);
Expand Down
4 changes: 3 additions & 1 deletion src/gui/dragdrop/dragdropmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ namespace Toolbox::UI {

void DragDropManager::shutdown() {
OleUninitialize();
m_drag_thread.join();
if (m_is_thread_running) {
m_drag_thread.join();
}
}

static std::vector<std::string_view> splitLines(std::string_view s) {
Expand Down
23 changes: 20 additions & 3 deletions src/gui/project/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,19 @@ namespace Toolbox::UI {
m_folder_view_context_menu.addDivider();

m_folder_view_context_menu.addOption(
"New...", KeyBind({KeyCode::KEY_LEFTCONTROL, KeyCode::KEY_N}),
"New Folder", KeyBind({KeyCode::KEY_LEFTCONTROL, KeyCode::KEY_LEFTSHIFT, KeyCode::KEY_N}),
[this]() { return m_selected_indices_ctx.size() == 0; },
[this](const ModelIndex &view_index) {
std::string folder_name =
m_file_system_model->findUniqueName(view_index, "New Folder");
ModelIndex new_index = m_file_system_model->mkdir(view_index, folder_name);
if (m_file_system_model->validateIndex(new_index)) {
actionRenameIndex(new_index);
}
});

m_folder_view_context_menu.addOption(
"New Item...", KeyBind({KeyCode::KEY_LEFTCONTROL, KeyCode::KEY_N}),
[this]() { return m_selected_indices_ctx.size() == 0; },
[this](const ModelIndex &view_index) {
RefPtr<NewItemWindow> window =
Expand Down Expand Up @@ -786,11 +798,16 @@ namespace Toolbox::UI {

void ProjectViewWindow::renderFolderTree(const ModelIndex &index) {
bool is_open = false;
if (m_tree_proxy.hasChildren(index)) {
if (m_tree_proxy.isDirectory(index)) {
if (m_tree_proxy.canFetchMore(index)) {
m_tree_proxy.fetchMore(index);
}
ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_OpenOnArrow;
ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_None;
if (m_tree_proxy.hasChildren(index)) {
flags |= ImGuiTreeNodeFlags_OpenOnArrow;
} else {
flags |= ImGuiTreeNodeFlags_Leaf;
}

if (m_view_index == m_tree_proxy.toSourceIndex(index)) {
flags |= ImGuiTreeNodeFlags_Selected;
Expand Down
59 changes: 55 additions & 4 deletions src/model/fsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ namespace Toolbox {
return getData_(index, role);
}

std::string FileSystemModel::findUniqueName(const ModelIndex &index,
const std::string &name) const {
std::scoped_lock lock(m_mutex);
return findUniqueName_(index, name);
}

ModelIndex FileSystemModel::mkdir(const ModelIndex &parent, const std::string &name) {
std::scoped_lock lock(m_mutex);
return mkdir_(parent, name);
Expand Down Expand Up @@ -484,6 +490,45 @@ namespace Toolbox {
}
}

std::string FileSystemModel::findUniqueName_(const ModelIndex &parent,
const std::string &name) const {
if (!validateIndex(parent)) {
return name;
}

if (isFile_(parent)) {
return name;
}

if (isArchive_(parent)) {
// TODO: Implement for virtual FS
TOOLBOX_ERROR("[FileSystemModel] Archive probing is unimplemented!");
return name;
}

std::string result_name = name;
size_t collisions = 0;
std::vector<std::string> child_paths;

size_t dir_size = getDirSize_(parent, false);
for (size_t i = 0; i < dir_size; ++i) {
ModelIndex child = getIndex_(i, 0, parent);
child_paths.emplace_back(std::move(getPath_(child).filename().string()));
}

for (size_t i = 0; i < child_paths.size();) {
if (child_paths[i] == result_name) {
collisions += 1;
result_name = std::format("{} ({})", name, collisions);
i = 0;
continue;
}
++i;
}

return result_name;
}

ModelIndex FileSystemModel::mkdir_(const ModelIndex &parent, const std::string &name) {
if (!validateIndex(parent)) {
return ModelIndex();
Expand Down Expand Up @@ -519,7 +564,7 @@ namespace Toolbox {
return ModelIndex();
}

return makeIndex(parent.data<_FileSystemIndexData>()->m_path / name, getRowCount(parent),
return makeIndex(parent.data<_FileSystemIndexData>()->m_path / name, getRowCount_(parent),
parent);
}

Expand Down Expand Up @@ -549,7 +594,7 @@ namespace Toolbox {
return ModelIndex();
}

return makeIndex(parent.data<_FileSystemIndexData>()->m_path / name, getRowCount(parent),
return makeIndex(parent.data<_FileSystemIndexData>()->m_path / name, getRowCount_(parent),
parent);
}

Expand Down Expand Up @@ -604,6 +649,7 @@ namespace Toolbox {
parent_data->m_children.end(),
index.getUUID()),
parent_data->m_children.end());
parent_data->m_size -= 1;
}
delete index.data<_FileSystemIndexData>();
m_index_map.erase(index.getUUID());
Expand Down Expand Up @@ -649,6 +695,7 @@ namespace Toolbox {
parent_data->m_children.end(),
index.getUUID()),
parent_data->m_children.end());
parent_data->m_size -= 1;
}
delete index.data<_FileSystemIndexData>();
m_index_map.erase(index.getUUID());
Expand Down Expand Up @@ -681,6 +728,7 @@ namespace Toolbox {
parent_data->m_children.erase(std::remove(parent_data->m_children.begin(),
parent_data->m_children.end(), file.getUUID()),
parent_data->m_children.end());
parent_data->m_size -= 1;
return makeIndex(to, dest_index, parent);
}
ModelIndex FileSystemModel::copy_(const fs_path &file, const ModelIndex &new_parent,
Expand All @@ -690,8 +738,7 @@ namespace Toolbox {
return ModelIndex();
}
if (!Filesystem::exists(file)) {
TOOLBOX_ERROR_V("[FileSystemModel] \"{}\" is not a directory or file!",
file.string());
TOOLBOX_ERROR_V("[FileSystemModel] \"{}\" is not a directory or file!", file.string());
return ModelIndex();
}
fs_path to = new_parent.data<_FileSystemIndexData>()->m_path / new_name;
Expand Down Expand Up @@ -941,6 +988,7 @@ namespace Toolbox {
if (parent_data) {
parent_data->m_children.insert(parent_data->m_children.begin() + row,
index.getUUID());
parent_data->m_size += 1;
}

m_index_map[index.getUUID()] = std::move(index);
Expand Down Expand Up @@ -1130,6 +1178,7 @@ namespace Toolbox {
old_parent_data->m_children.end(),
index.getUUID()),
old_parent_data->m_children.end());
old_parent_data->m_size -= 1;

ModelIndex new_parent = getIndex_(new_path.parent_path());
if (!validateIndex(new_parent)) {
Expand All @@ -1138,6 +1187,7 @@ namespace Toolbox {

_FileSystemIndexData *new_parent_data = new_parent.data<_FileSystemIndexData>();
new_parent_data->m_children.push_back(index.getUUID());
new_parent_data->m_size += 1;
}
}

Expand Down Expand Up @@ -1169,6 +1219,7 @@ namespace Toolbox {
parent_data->m_children.end(),
index.getUUID()),
parent_data->m_children.end());
parent_data->m_size -= 1;
delete index.data<_FileSystemIndexData>();
m_index_map.erase(index.getUUID());
}
Expand Down

0 comments on commit 9662506

Please sign in to comment.