Skip to content

Commit e5ec0e5

Browse files
author
Santiago
committed
add "Show in folder view" option to file text edit panel
1 parent 331a4c3 commit e5ec0e5

File tree

7 files changed

+79
-12
lines changed

7 files changed

+79
-12
lines changed

src/ImGuiController.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace scex::ImGuiController
3131
void OnFindInFolder(const std::string& folderPath, int folderViewId);
3232
void OnShowInFileExplorer(const std::string& filePath, int folderViewId);
3333
void OnFileClickedInFolderView(const std::string& filePath, int folderViewId);
34+
void OnShowInFolderView(const std::string& filePath, int folderViewId);
3435
void OnFolderSearchResultClick(const std::string& filePath, const DirectoryFinderSearchResult& searchResult, int folderViewId);
3536
void OnFolderSearchResultFoundOrSearchFinished();
3637
void OnPanelFocused(int folderViewId);
@@ -57,7 +58,7 @@ namespace scex::ImGuiController
5758
FileTextEdit* CreateNewEditor(const char* filePath = nullptr, int fromFolderView = -1)
5859
{
5960
int fileTextEditId = fileTextEdits.size();
60-
fileTextEdits.push_back(new FileTextEdit(filePath, fileTextEditId, fromFolderView, OnPanelFocused));
61+
fileTextEdits.push_back(new FileTextEdit(filePath, fileTextEditId, fromFolderView, OnPanelFocused, OnShowInFolderView));
6162
fileTextEdits.back()->SetShowDebugPanel(textEditDebugInfo);
6263
return fileTextEdits.back();
6364
}
@@ -101,7 +102,15 @@ namespace scex::ImGuiController
101102
editorToFocus = fileToEditorMap[filePath];
102103
}
103104

104-
// ---- Callback from folder finder ---- //
105+
// ---- Callbacks from file text edit ---- //
106+
void OnShowInFolderView(const std::string& filePath, int folderViewId)
107+
{
108+
assert(folderViewId > -1);
109+
assert(folderViewers[folderViewId] != nullptr);
110+
folderViewers[folderViewId]->ShowFile(filePath);
111+
}
112+
113+
// ---- Callbacks from folder finder ---- //
105114
void OnFolderSearchResultClick(const std::string& filePath, const DirectoryFinderSearchResult& searchResult, int folderViewId)
106115
{
107116
FileTextEdit* targetEditor;
@@ -187,7 +196,7 @@ void scex::ImGuiController::OnPathsDropped(const char** paths, int pathCount)
187196
}
188197
}
189198

190-
void scex::ImGuiController::Tick()
199+
void scex::ImGuiController::Tick(double deltaTime)
191200
{
192201
ImGui_ImplOpenGL3_NewFrame();
193202
ImGui_ImplGlfw_NewFrame();
@@ -266,13 +275,21 @@ void scex::ImGuiController::Tick()
266275
if (folderView == nullptr)
267276
continue;
268277
ImGui::SetNextWindowDockID(leftDockID, ImGuiCond_FirstUseEver);
269-
if (!folderView->OnImGui())
278+
if (!folderView->OnImGui(deltaTime))
270279
folderViewToDelete = i;
271280
}
272281
if (folderViewToDelete > -1)
273282
{
274283
delete folderViewers[folderViewToDelete];
275284
folderViewers[folderViewToDelete] = nullptr;
285+
// notify text editors so they don't try to show file in folder view
286+
for (int i = 0; i < fileTextEdits.size(); i++)
287+
{
288+
FileTextEdit* fte = fileTextEdits[i];
289+
if (fte == nullptr)
290+
continue;
291+
fte->OnFolderViewDeleted(folderViewToDelete);
292+
}
276293
}
277294
}
278295
{

src/ImGuiController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ namespace scex::ImGuiController
99
void Setup(GLFWwindow* window, const std::string& fileToOpen);
1010
bool HasControl();
1111
void OnPathsDropped(const char** paths, int pathCount);
12-
void Tick();
12+
void Tick(double deltaTime);
1313
}

src/main.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,21 @@ int main(int argc, char** argv)
137137
glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, GL_TRUE);
138138
}
139139
#endif
140-
140+
float programTime = 0.0;
141+
double lastFrameTime = 0.0;
142+
double currentFrameTime = 0.0;
143+
double deltaTime = 0.0;
141144
/* Loop until the user closes the window */
142145
while (!glfwWindowShouldClose(window))
143146
{
147+
currentFrameTime = glfwGetTime();
148+
deltaTime = currentFrameTime - lastFrameTime;
149+
144150
/* Draw scene */
145151
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
146-
scex::ImGuiController::Tick();
152+
scex::ImGuiController::Tick(deltaTime);
153+
154+
programTime += deltaTime;
147155

148156
/* Swap front and back buffers */
149157
glfwSwapBuffers(window);
@@ -157,6 +165,7 @@ int main(int argc, char** argv)
157165
else
158166
redrawCounter--;
159167
glfwPollEvents();
168+
lastFrameTime = currentFrameTime;
160169
}
161170

162171
glfwTerminate();

src/panels/DirectoryTreeView.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include "DirectoryTreeView.h"
22

33
#include <iostream>
4+
#include <PathUtils.h>
45

56
#define MAX_SEARCH_RESULTS 50
7+
#define SECONDS_TO_SELECT_ON_SHOW_FILE 2.0f
68

79
DirectoryTreeView::DirectoryTreeView(
810
const std::string& folderPath,
@@ -29,7 +31,7 @@ DirectoryTreeView::~DirectoryTreeView()
2931
Trie::Free(item.second);
3032
}
3133

32-
bool DirectoryTreeView::OnImGui()
34+
bool DirectoryTreeView::OnImGui(double deltaTime)
3335
{
3436
bool windowIsOpen = true;
3537
if (requestingFocus)
@@ -88,6 +90,12 @@ bool DirectoryTreeView::OnImGui()
8890
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, { 0.0f, 2.0f });
8991
ImGui::SetNextItemOpen(true);
9092
RecursivelyDisplayDirectoryNode(directoryTreeRoot);
93+
if (selectionTimer > 0.0f)
94+
{
95+
selectionTimer -= deltaTime;
96+
if (selectionTimer <= 0.0f)
97+
fileToHighlight = "";
98+
}
9199
ImGui::PopStyleVar(2);
92100

93101
if (lastHoveredNode != nullptr)
@@ -128,6 +136,12 @@ void DirectoryTreeView::RunSearch()
128136
searchResults.clear();
129137
}
130138

139+
void DirectoryTreeView::ShowFile(const std::string& filePath)
140+
{
141+
fileToHighlight = filePath;
142+
selectionTimer = SECONDS_TO_SELECT_ON_SHOW_FILE;
143+
}
144+
131145
void DirectoryTreeView::Refresh()
132146
{
133147
for (auto& item : searchTrie.children)
@@ -178,6 +192,12 @@ void DirectoryTreeView::SetLastHoveredNode(const DirectoryTreeViewNode* node)
178192

179193
void DirectoryTreeView::RecursivelyDisplayDirectoryNode(const DirectoryTreeViewNode& parentNode)
180194
{
195+
bool needToSelectNode = false;
196+
if (fileToHighlight.length() > 0 && PathUtils::GetFolderPath(fileToHighlight).find(parentNode.fullPath) != std::string::npos) // show file in folder functionality
197+
ImGui::SetNextItemOpen(true);
198+
else if (fileToHighlight.compare(parentNode.fullPath) == 0)
199+
needToSelectNode = true;
200+
181201
ImGui::PushID(&parentNode);
182202
if (parentNode.isDirectory)
183203
{
@@ -197,7 +217,9 @@ void DirectoryTreeView::RecursivelyDisplayDirectoryNode(const DirectoryTreeViewN
197217
}
198218
else
199219
{
200-
if (ImGui::TreeNodeEx(parentNode.fileName.c_str(), ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_FramePadding))
220+
if (ImGui::TreeNodeEx(parentNode.fileName.c_str(),
221+
ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_SpanFullWidth |
222+
ImGuiTreeNodeFlags_FramePadding | (needToSelectNode ? ImGuiTreeNodeFlags_Selected : 0x0)))
201223
{
202224
if (ImGui::IsItemClicked(0))
203225
{

src/panels/DirectoryTreeView.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,16 @@ struct DirectoryTreeView
3232
std::vector<std::pair<std::string, OnContextMenuCallback>>* folderContextMenuOptions = nullptr,
3333
int id = -1);
3434
~DirectoryTreeView();
35-
bool OnImGui();
35+
bool OnImGui(double deltaTime);
3636
void RunSearch();
37+
void ShowFile(const std::string& filePath);
3738

3839
private:
3940
int id = -1;
4041

42+
std::string fileToHighlight = "";
43+
float selectionTimer = -1.0f;
44+
4145
bool requestingFocus = false;
4246
bool searching = false;
4347
char findFilesBuffer[FIND_FILES_BUFFER_SIZE];

src/panels/FileTextEdit.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ std::unordered_map<std::string, const TextEditor::LanguageDefinition*> FileTextE
2424
{".json",&TextEditor::LanguageDefinition::Json()}
2525
};
2626

27-
FileTextEdit::FileTextEdit(const char* filePath, int id, int createdFromFolderView, OnFocusedCallback onFocusedCallback)
27+
FileTextEdit::FileTextEdit(const char* filePath, int id, int createdFromFolderView, OnFocusedCallback onFocusedCallback, OnShowInFolderViewCallback onShowInFolderViewCallback)
2828
{
2929
this->id = id;
3030
this->createdFromFolderView = createdFromFolderView;
3131
this->onFocusedCallback = onFocusedCallback;
32+
this->onShowInFolderViewCallback = onShowInFolderViewCallback;
3233
editor = new TextEditor();
3334
if (filePath == nullptr)
3435
panelName = "untitled##" + std::to_string((int)this);
@@ -84,6 +85,10 @@ bool FileTextEdit::OnImGui()
8485
OnSaveCommand();
8586
if (this->hasAssociatedFile && ImGui::MenuItem("Show in file explorer"))
8687
Utils::ShowInFileExplorer(this->associatedFile);
88+
if (this->hasAssociatedFile &&
89+
this->onShowInFolderViewCallback != nullptr &&
90+
this->createdFromFolderView > -1 && ImGui::MenuItem("Show in folder view"))
91+
this->onShowInFolderViewCallback(this->associatedFile, this->createdFromFolderView);;
8792
ImGui::EndMenu();
8893
}
8994
if (ImGui::BeginMenu("Edit"))
@@ -254,6 +259,12 @@ const char* FileTextEdit::GetAssociatedFile()
254259
return associatedFile.c_str();
255260
}
256261

262+
void FileTextEdit::OnFolderViewDeleted(int folderViewId)
263+
{
264+
if (createdFromFolderView == folderViewId)
265+
createdFromFolderView = -1;
266+
}
267+
257268
void FileTextEdit::SetShowDebugPanel(bool value)
258269
{
259270
showDebugPanel = value;

src/panels/FileTextEdit.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
struct FileTextEdit
66
{
77
typedef void (*OnFocusedCallback)(int folderViewId);
8+
typedef void (*OnShowInFolderViewCallback)(const std::string& filePath, int folderViewId);
89

910
FileTextEdit(const char* filePath = nullptr,
1011
int id = -1,
1112
int createdFromFolderView = -1,
12-
OnFocusedCallback onFocusedCallback = nullptr);
13+
OnFocusedCallback onFocusedCallback = nullptr,
14+
OnShowInFolderViewCallback onShowInFolderViewCallback = nullptr);
1315
~FileTextEdit();
1416
bool OnImGui();
1517
void SetSelection(int startLine, int startChar, int endLine, int endChar);
1618
const char* GetAssociatedFile();
19+
void OnFolderViewDeleted(int folderViewId);
1720
void SetShowDebugPanel(bool value);
1821

1922
private:
@@ -27,6 +30,7 @@ struct FileTextEdit
2730
int createdFromFolderView = -1;
2831

2932
OnFocusedCallback onFocusedCallback = nullptr;
33+
OnShowInFolderViewCallback onShowInFolderViewCallback = nullptr;
3034

3135
TextEditor* editor = nullptr;
3236
bool showDebugPanel = false;

0 commit comments

Comments
 (0)