Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
file_browser.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <pulp/view/view.hpp>
5#include <string>
6#include <functional>
7#include <vector>
8#include <filesystem>
9
10namespace pulp::view {
11
15class FileBrowser : public View {
16public:
18
20 void set_root(const std::filesystem::path& root) { root_ = root; current_ = root; refresh(); }
21 const std::filesystem::path& root() const { return root_; }
22
24 void navigate(const std::filesystem::path& dir) { current_ = dir; refresh(); }
25 const std::filesystem::path& current_directory() const { return current_; }
26
28 void set_filters(std::vector<std::string> patterns) { filters_ = std::move(patterns); refresh(); }
29 const std::vector<std::string>& filters() const { return filters_; }
30
32 void set_show_hidden(bool v) { show_hidden_ = v; refresh(); }
33 bool show_hidden() const { return show_hidden_; }
34
36 const std::filesystem::path& selected_file() const { return selected_; }
37
39 std::function<void(const std::filesystem::path&)> on_select;
40
42 std::function<void(const std::filesystem::path&)> on_activate;
43
44 void paint(canvas::Canvas& canvas) override;
45
46private:
47 void refresh();
48
49 std::filesystem::path root_;
50 std::filesystem::path current_;
51 std::filesystem::path selected_;
52 std::vector<std::string> filters_;
53 bool show_hidden_ = false;
54
55 struct Entry {
56 std::string name;
57 bool is_directory = false;
58 uintmax_t size = 0;
59 };
60 std::vector<Entry> entries_;
61};
62
65class FileTree : public View {
66public:
68
69 void set_root(const std::filesystem::path& root) { root_ = root; refresh(); }
70 const std::filesystem::path& root() const { return root_; }
71
73 std::function<void(const std::filesystem::path&)> on_select;
74
75 void paint(canvas::Canvas& canvas) override;
76
77 struct FileTreeNode {
78 std::string name;
79 std::filesystem::path path;
80 bool is_directory = false;
81 bool expanded = false;
82 int depth = 0;
83 };
84
85private:
86 void refresh();
87 std::filesystem::path root_;
88 std::vector<FileTreeNode> nodes_;
89};
90
98public:
100 static void share_file(const std::filesystem::path& file, void* parent_window = nullptr);
101
103 static void share_text(const std::string& text, void* parent_window = nullptr);
104};
105
108class MultiDocumentPanel : public View {
109public:
111
113 void add_document(std::string title, std::unique_ptr<View> content);
114
116 void remove_document(int index);
117
119 int active_index() const { return active_; }
120
122 void set_active(int index);
123
125 int document_count() const { return static_cast<int>(documents_.size()); }
126
128 std::function<void(int index)> on_close;
129
131 std::function<void(int index)> on_active_changed;
132
133 void paint(canvas::Canvas& canvas) override;
134
135private:
136 struct Document {
137 std::string title;
138 std::unique_ptr<View> content;
139 };
140 std::vector<Document> documents_;
141 int active_ = -1;
142};
143
144} // namespace pulp::view
Definition canvas.hpp:240
Content opener for files and text.
Definition file_browser.hpp:97
static void share_text(const std::string &text, void *parent_window=nullptr)
Open text through a temporary file on supported platforms.
static void share_file(const std::filesystem::path &file, void *parent_window=nullptr)
Open a file with the platform's default opener when supported.
std::function< void(const std::filesystem::path &)> on_select
Callback when a file is selected (single click).
Definition file_browser.hpp:39
bool show_hidden() const
Definition file_browser.hpp:33
void set_root(const std::filesystem::path &root)
Set the root directory to browse from.
Definition file_browser.hpp:20
const std::filesystem::path & selected_file() const
The selected file (empty if no selection).
Definition file_browser.hpp:36
void set_filters(std::vector< std::string > patterns)
File filter patterns (e.g., {"*.wav", "*.aiff"}).
Definition file_browser.hpp:28
const std::filesystem::path & root() const
Definition file_browser.hpp:21
std::function< void(const std::filesystem::path &)> on_activate
Callback when a file is activated (double click or enter).
Definition file_browser.hpp:42
void paint(canvas::Canvas &canvas) override
void set_show_hidden(bool v)
Whether to show hidden files.
Definition file_browser.hpp:32
const std::vector< std::string > & filters() const
Definition file_browser.hpp:29
const std::filesystem::path & current_directory() const
Definition file_browser.hpp:25
void navigate(const std::filesystem::path &dir)
Set the current directory.
Definition file_browser.hpp:24
std::function< void(const std::filesystem::path &)> on_select
Callback when a file node is selected.
Definition file_browser.hpp:73
void paint(canvas::Canvas &canvas) override
const std::filesystem::path & root() const
Definition file_browser.hpp:70
void set_root(const std::filesystem::path &root)
Definition file_browser.hpp:69
void add_document(std::string title, std::unique_ptr< View > content)
Add a document tab.
std::function< void(int index)> on_active_changed
Callback when the active document changes.
Definition file_browser.hpp:131
void remove_document(int index)
Remove a document tab by index.
void set_active(int index)
Set the active document.
std::function< void(int index)> on_close
Callback when a document is closed.
Definition file_browser.hpp:128
void paint(canvas::Canvas &canvas) override
int active_index() const
Get the active document index (-1 if none).
Definition file_browser.hpp:119
int document_count() const
Number of open documents.
Definition file_browser.hpp:125
Definition affine_transform.hpp:24
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
@ text
Plain text (default color).
Definition code_editor_tokenizer.hpp:37
@ v
Definition input_events.hpp:41
Definition file_browser.hpp:77
bool is_directory
Definition file_browser.hpp:80
bool expanded
Definition file_browser.hpp:81
std::string name
Definition file_browser.hpp:78
std::filesystem::path path
Definition file_browser.hpp:79
int depth
Definition file_browser.hpp:82
Hierarchical tree display widget for file browsers, plugin lists, etc.