Step 183: tab reorder and rename
This commit is contained in:
@@ -122,6 +122,17 @@ public:
|
||||
// Get number of open buffers
|
||||
size_t bufferCount() const { return buffers_.size(); }
|
||||
|
||||
bool renameBuffer(const std::string& oldPath, const std::string& newPath) {
|
||||
if (oldPath == newPath) return false;
|
||||
if (!hasBuffer(oldPath) || hasBuffer(newPath)) return false;
|
||||
BufferInfo info = buffers_[oldPath];
|
||||
buffers_.erase(oldPath);
|
||||
info.path = newPath;
|
||||
buffers_[newPath] = info;
|
||||
if (activeBuffer_ == oldPath) activeBuffer_ = newPath;
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<std::string, BufferInfo> buffers_;
|
||||
std::string activeBuffer_;
|
||||
|
||||
@@ -596,6 +596,25 @@ struct EditorState {
|
||||
activeBuffer = nullptr;
|
||||
}
|
||||
|
||||
bool renameBufferPath(const std::string& oldPath, const std::string& newPath) {
|
||||
if (oldPath == newPath) return false;
|
||||
if (!buffers.hasBuffer(oldPath) || buffers.hasBuffer(newPath)) return false;
|
||||
auto it = bufferStates.find(oldPath);
|
||||
if (it == bufferStates.end()) return false;
|
||||
auto node = std::move(it->second);
|
||||
bufferStates.erase(it);
|
||||
node->path = newPath;
|
||||
bufferStates[newPath] = std::move(node);
|
||||
buffers.renameBuffer(oldPath, newPath);
|
||||
if (oldPath.rfind("(untitled", 0) != 0) watcher.unwatch(oldPath);
|
||||
if (newPath.rfind("(untitled", 0) != 0) watcher.watch(newPath);
|
||||
if (active() && active()->path == oldPath) {
|
||||
active()->path = newPath;
|
||||
}
|
||||
notify(NotificationLevel::Success, "Renamed buffer to " + newPath);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool saveProject(const std::string& path) {
|
||||
ProjectFile project;
|
||||
project.workspaceRoot = workspaceRoot;
|
||||
|
||||
@@ -54,7 +54,11 @@ static void renderEditorPanel(EditorState& state) {
|
||||
ImGui::PushFont(state.monoFont);
|
||||
|
||||
// Tab bar for the file
|
||||
if (ImGui::BeginTabBar("EditorTabs")) {
|
||||
static bool renameOpen = false;
|
||||
static char renameBuf[260] = {};
|
||||
static std::string renameTarget;
|
||||
if (ImGui::BeginTabBar("EditorTabs",
|
||||
ImGuiTabBarFlags_Reorderable | ImGuiTabBarFlags_AutoSelectNewTabs)) {
|
||||
if (state.buffers.bufferCount() == 0) {
|
||||
if (ImGui::BeginTabItem("Welcome")) {
|
||||
RenderWelcome(state.welcome, state, state.lastDialogPath);
|
||||
@@ -760,6 +764,14 @@ static void renderEditorPanel(EditorState& state) {
|
||||
ImGui::PopStyleVar();
|
||||
ImGui::EndTabItem();
|
||||
}
|
||||
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
|
||||
if (path.rfind("(untitled", 0) == 0) {
|
||||
renameOpen = true;
|
||||
renameTarget = path;
|
||||
std::snprintf(renameBuf, sizeof(renameBuf), "%s", path.c_str());
|
||||
ImGui::OpenPopup("Rename Buffer");
|
||||
}
|
||||
}
|
||||
if (!open) {
|
||||
state.buffers.closeBuffer(path);
|
||||
state.bufferStates.erase(path);
|
||||
@@ -775,6 +787,27 @@ static void renderEditorPanel(EditorState& state) {
|
||||
ImGui::EndTabBar();
|
||||
}
|
||||
|
||||
if (renameOpen && ImGui::BeginPopupModal("Rename Buffer", nullptr,
|
||||
ImGuiWindowFlags_AlwaysAutoResize)) {
|
||||
ImGui::TextUnformatted("Rename untitled buffer:");
|
||||
ImGui::SetNextItemWidth(320.0f);
|
||||
ImGui::InputText("##renameBuf", renameBuf, sizeof(renameBuf));
|
||||
if (ImGui::Button("Rename")) {
|
||||
std::string newName = renameBuf;
|
||||
if (!newName.empty() && renameTarget.rfind("(untitled", 0) == 0) {
|
||||
state.renameBufferPath(renameTarget, newName);
|
||||
}
|
||||
renameOpen = false;
|
||||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Cancel")) {
|
||||
renameOpen = false;
|
||||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
|
||||
ImGui::PopFont();
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user