yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
file_browser.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_SYSTEM_FILE_BROWSER_H_
2#define YAZE_APP_EDITOR_SYSTEM_FILE_BROWSER_H_
3
4#include <filesystem>
5#include <functional>
6#include <set>
7#include <string>
8#include <vector>
9
10namespace yaze {
11namespace editor {
12
17struct FileEntry {
18 std::string name;
19 std::string full_path;
21 bool is_expanded = false; // For directories: whether to show children
22 std::vector<FileEntry> children;
23
24 // File type detection for icons
25 enum class FileType {
28 kSource,
29 kHeader,
30 kText,
31 kConfig,
32 kJson,
33 kImage,
34 kBinary,
36 };
38};
39
52 public:
53 void LoadFromFile(const std::string& gitignore_path);
54 void AddPattern(const std::string& pattern);
55 bool IsIgnored(const std::string& path, bool is_directory) const;
56 void Clear();
57
58 private:
59 struct Pattern {
60 std::string pattern;
61 bool is_negation = false;
62 bool directory_only = false;
63 };
64
65 std::vector<Pattern> patterns_;
66
67 bool MatchPattern(const std::string& path, const Pattern& pattern) const;
68 bool MatchGlob(const std::string& text, const std::string& pattern) const;
69};
70
93 public:
94 FileBrowser() = default;
95
100 void SetRootPath(const std::string& path);
101
105 const std::string& GetRootPath() const { return root_path_; }
106
110 bool HasRootPath() const { return !root_path_.empty(); }
111
115 void Refresh();
116
120 void Draw();
121
125 void DrawCompact();
126
127 // Configuration
128 void SetShowHiddenFiles(bool show) { show_hidden_files_ = show; }
129 bool GetShowHiddenFiles() const { return show_hidden_files_; }
130
131 void SetRespectGitignore(bool respect) { respect_gitignore_ = respect; }
133
137 void SetFileFilter(const std::vector<std::string>& extensions);
138 void ClearFileFilter();
139
140 // Callbacks
141 using FileClickedCallback = std::function<void(const std::string& path)>;
142 using DirectoryClickedCallback = std::function<void(const std::string& path)>;
143
145 on_file_clicked_ = std::move(callback);
146 }
147
149 on_directory_clicked_ = std::move(callback);
150 }
151
152 // Statistics
153 size_t GetFileCount() const { return file_count_; }
154 size_t GetDirectoryCount() const { return directory_count_; }
155
156 private:
157 void ScanDirectory(const std::filesystem::path& path, FileEntry& parent,
158 int depth = 0);
159 bool ShouldShow(const std::filesystem::path& path, bool is_directory) const;
160 bool MatchesFilter(const std::string& filename) const;
161 FileEntry::FileType DetectFileType(const std::string& filename) const;
162 const char* GetFileIcon(FileEntry::FileType type) const;
163 void DrawEntry(FileEntry& entry, int depth = 0);
164
165 // State
166 std::string root_path_;
168 bool needs_refresh_ = true;
169
170 // Configuration
171 bool show_hidden_files_ = false;
173 std::set<std::string> file_filter_; // Empty = show all
174
175 // Gitignore handling
177
178 // Statistics
179 size_t file_count_ = 0;
181
182 // Callbacks
185
186 // UI state
187 std::string selected_path_;
188
189 // Constants
190 static constexpr int kMaxDepth = 10;
191 static constexpr size_t kMaxEntries = 1000;
192};
193
194} // namespace editor
195} // namespace yaze
196
197#endif // YAZE_APP_EDITOR_SYSTEM_FILE_BROWSER_H_
File system browser for the sidebar.
void DrawCompact()
Draw a compact version for narrow sidebars.
std::set< std::string > file_filter_
bool GetShowHiddenFiles() const
std::function< void(const std::string &path)> FileClickedCallback
const char * GetFileIcon(FileEntry::FileType type) const
void SetShowHiddenFiles(bool show)
size_t GetDirectoryCount() const
void SetFileClickedCallback(FileClickedCallback callback)
std::function< void(const std::string &path)> DirectoryClickedCallback
void Refresh()
Refresh the file tree from disk.
bool GetRespectGitignore() const
bool ShouldShow(const std::filesystem::path &path, bool is_directory) const
bool HasRootPath() const
Check if a root path is set.
void SetFileFilter(const std::vector< std::string > &extensions)
Add file extensions to filter (empty = show all)
void SetRootPath(const std::string &path)
Set the root path for the file browser.
static constexpr size_t kMaxEntries
const std::string & GetRootPath() const
Get the current root path.
void Draw()
Draw the file tree in ImGui.
DirectoryClickedCallback on_directory_clicked_
FileEntry::FileType DetectFileType(const std::string &filename) const
void ScanDirectory(const std::filesystem::path &path, FileEntry &parent, int depth=0)
static constexpr int kMaxDepth
void SetRespectGitignore(bool respect)
FileClickedCallback on_file_clicked_
void DrawEntry(FileEntry &entry, int depth=0)
void SetDirectoryClickedCallback(DirectoryClickedCallback callback)
GitignoreParser gitignore_parser_
size_t GetFileCount() const
bool MatchesFilter(const std::string &filename) const
Simple .gitignore pattern matcher.
std::vector< Pattern > patterns_
bool MatchPattern(const std::string &path, const Pattern &pattern) const
void AddPattern(const std::string &pattern)
bool IsIgnored(const std::string &path, bool is_directory) const
bool MatchGlob(const std::string &text, const std::string &pattern) const
void LoadFromFile(const std::string &gitignore_path)
Represents a file or folder in the file browser.
std::vector< FileEntry > children