Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
code_editor.hpp
Go to the documentation of this file.
1#pragma once
2
3// CodeEditorComponent — native code editor view.
4// Provides text editing, line numbers, lightweight syntax coloring, and language modes.
5// Monaco-class editing can be built separately with the WebView bridge.
6
7#include <pulp/view/view.hpp>
8#include <string>
9#include <functional>
10#include <optional>
11
12namespace pulp::view {
13
27
31 bool line_numbers = true;
32 bool minimap = false;
33 bool word_wrap = true;
34 bool read_only = false;
35 int tab_size = 4;
36 std::string theme = "vs-dark"; // "vs-dark", "vs-light", "hc-black"
37 float font_size = 13.0f;
38 std::string font_family = "monospace";
39};
40
42class CodeEditor : public View {
43public:
44 CodeEditor() = default;
45
47 void configure(const CodeEditorConfig& config);
48
50 void set_text(std::string text);
51 std::string text() const { return text_; }
52
55
57 void set_read_only(bool ro);
58
60 std::function<void(const std::string& new_text)> on_change;
61
63 std::function<void(int line, int column)> on_cursor_change;
64
66 int cursor_line() const { return cursor_line_; }
67 int cursor_column() const { return cursor_col_; }
68
70 std::string selected_text() const { return selection_; }
71
73 void insert_text(std::string_view text);
74
76 void go_to_line(int line);
77
79 void set_markers(const std::vector<std::pair<int, std::string>>& markers);
80
81 void paint(canvas::Canvas& canvas) override;
82
83private:
84 CodeEditorConfig config_;
85 std::string text_;
86 std::string selection_;
87 int cursor_line_ = 1;
88 int cursor_col_ = 1;
89 bool initialized_ = false;
90};
91
94public:
95 SystemTrayIcon() = default;
97
99 void set_icon(std::string_view icon_path);
100
102 void set_tooltip(std::string_view text);
103
105 void show();
106
108 void hide();
109
111 std::function<void()> on_click;
112
114 std::function<void()> on_right_click;
115
116private:
117 void* native_handle_ = nullptr;
118 std::string icon_path_;
119 std::string tooltip_;
120 bool visible_ = false;
121};
122
125public:
126 FileBasedDocument() = default;
127 virtual ~FileBasedDocument() = default;
128
130 bool load(std::string_view path);
131
133 bool save();
134
136 bool save_as(std::string_view path);
137
139 bool is_dirty() const { return dirty_; }
140 void set_dirty(bool d = true) { dirty_ = d; }
141
143 const std::string& file_path() const { return path_; }
144
146 std::string title() const;
147
149 virtual bool load_from_file(std::string_view path) = 0;
150
152 virtual bool save_to_file(std::string_view path) = 0;
153
155 std::function<void(bool is_dirty)> on_dirty_changed;
156
157private:
158 std::string path_;
159 bool dirty_ = false;
160};
161
164public:
166
168 void add(std::string_view path);
169
171 void remove(std::string_view path);
172
174 const std::vector<std::string>& files() const { return files_; }
175
177 void set_max_entries(int max) { max_entries_ = max; trim(); }
178
180 void clear() { files_.clear(); }
181
183 bool load(std::string_view path);
184
186 bool save(std::string_view path) const;
187
188private:
189 std::vector<std::string> files_;
190 int max_entries_ = 10;
191
192 void trim();
193};
194
195} // namespace pulp::view
Definition canvas.hpp:240
int cursor_line() const
Get cursor position.
Definition code_editor.hpp:66
void insert_text(std::string_view text)
Insert text at cursor.
std::function< void(int line, int column)> on_cursor_change
Called when the cursor position changes.
Definition code_editor.hpp:63
std::string text() const
Definition code_editor.hpp:51
void set_read_only(bool ro)
Set read-only mode.
void set_language(CodeLanguage lang)
Set the language mode.
std::string selected_text() const
Get selected text.
Definition code_editor.hpp:70
std::function< void(const std::string &new_text)> on_change
Called when the text changes.
Definition code_editor.hpp:60
int cursor_column() const
Definition code_editor.hpp:67
void set_markers(const std::vector< std::pair< int, std::string > > &markers)
Accept error markers (line → message); native fallback currently treats them as a no-op.
void paint(canvas::Canvas &canvas) override
void set_text(std::string text)
Get/set the editor content.
void go_to_line(int line)
Go to a specific line.
void configure(const CodeEditorConfig &config)
Set the editor configuration.
bool save()
Save to the current path.
std::string title() const
Document title (filename without extension).
bool save_as(std::string_view path)
Save to a new path.
const std::string & file_path() const
Current file path.
Definition code_editor.hpp:143
virtual ~FileBasedDocument()=default
void set_dirty(bool d=true)
Definition code_editor.hpp:140
bool load(std::string_view path)
Load from a file path.
virtual bool save_to_file(std::string_view path)=0
Override to implement saving.
virtual bool load_from_file(std::string_view path)=0
Override to implement loading.
bool is_dirty() const
Whether the document has unsaved changes.
Definition code_editor.hpp:139
std::function< void(bool is_dirty)> on_dirty_changed
Called when dirty state changes.
Definition code_editor.hpp:155
bool load(std::string_view path)
Load from a JSON file.
void set_max_entries(int max)
Maximum number of entries (default: 10).
Definition code_editor.hpp:177
void add(std::string_view path)
Add a file to the recent list (moves to front if already present).
void clear()
Clear the list.
Definition code_editor.hpp:180
const std::vector< std::string > & files() const
Get the list (most recent first).
Definition code_editor.hpp:174
bool save(std::string_view path) const
Save to a JSON file.
void remove(std::string_view path)
Remove a file from the list.
void show()
Show the tray icon.
void hide()
Hide the tray icon.
void set_icon(std::string_view icon_path)
Set the tray icon image (path to PNG or system icon name).
std::function< void()> on_click
Called when the icon is clicked.
Definition code_editor.hpp:111
void set_tooltip(std::string_view text)
Set the tooltip text.
std::function< void()> on_right_click
Called when the icon is right-clicked (for context menu).
Definition code_editor.hpp:114
Definition affine_transform.hpp:24
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
@ path
Definition anchor_strategy.hpp:42
@ text
Plain text (default color).
Definition code_editor_tokenizer.hpp:37
@ d
Definition input_events.hpp:39
@ column
Definition design_ir.hpp:53
CodeLanguage
Language mode for syntax highlighting.
Definition code_editor.hpp:15
@ Lua
Definition code_editor.hpp:19
@ JSON
Definition code_editor.hpp:21
@ GLSL
Definition code_editor.hpp:23
@ XML
Definition code_editor.hpp:22
@ JSFX
Definition code_editor.hpp:25
@ JavaScript
Definition code_editor.hpp:18
@ Faust
Definition code_editor.hpp:24
@ Python
Definition code_editor.hpp:20
@ PlainText
Definition code_editor.hpp:16
@ Cpp
Definition code_editor.hpp:17
Configuration for the code editor.
Definition code_editor.hpp:29
bool word_wrap
Definition code_editor.hpp:33
CodeLanguage language
Definition code_editor.hpp:30
bool minimap
Definition code_editor.hpp:32
int tab_size
Definition code_editor.hpp:35
bool line_numbers
Definition code_editor.hpp:31
bool read_only
Definition code_editor.hpp:34
float font_size
Definition code_editor.hpp:37
std::string theme
Definition code_editor.hpp:36
std::string font_family
Definition code_editor.hpp:38