Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
preferences_panel.hpp
Go to the documentation of this file.
1#pragma once
2
28
29#include <algorithm>
30#include <functional>
31#include <memory>
32#include <optional>
33#include <string>
34#include <vector>
35
37#include <pulp/view/view.hpp>
38
39namespace pulp::view {
40
41class PreferencesPanel : public View {
42public:
43 struct Page {
44 std::string title;
45 std::string icon;
46 View* content = nullptr;
49 };
50
51 PreferencesPanel() = default;
52
53 // ── Page registry ────────────────────────────────────────────────
54
59 void add_page(std::string title,
60 std::string icon,
61 std::unique_ptr<View> content) {
62 auto* content_raw = content.get();
63 // First page must establish active_=0; later pages stay hidden.
64 if (!pages_.empty()) {
65 content_raw->set_visible(false);
66 }
67 add_child(std::move(content));
68 pages_.push_back({std::move(title), std::move(icon), content_raw});
69 if (pages_.size() == 1) {
70 active_ = 0;
71 notify_change();
72 }
73 }
74
75 size_t page_count() const { return pages_.size(); }
76
79 View* page_content(size_t index) const {
80 return index < pages_.size() ? pages_[index].content : nullptr;
81 }
82 const std::string& page_title(size_t index) const {
83 return index < pages_.size() ? pages_[index].title : empty_string();
84 }
85 const std::string& page_icon(size_t index) const {
86 return index < pages_.size() ? pages_[index].icon : empty_string();
87 }
88
89 // ── Active page ──────────────────────────────────────────────────
90
91 int active_page() const { return active_; }
92 std::string active_page_title() const {
93 return (active_ >= 0 && static_cast<size_t>(active_) < pages_.size())
94 ? pages_[static_cast<size_t>(active_)].title
95 : std::string{};
96 }
97
99 void set_active_page(int index) {
100 if (index < 0 || static_cast<size_t>(index) >= pages_.size()) return;
101 if (index == active_) return;
102 // Hide previous, show new.
103 if (active_ >= 0 && static_cast<size_t>(active_) < pages_.size()) {
104 if (auto* v = pages_[static_cast<size_t>(active_)].content) v->set_visible(false);
105 }
106 active_ = index;
107 if (auto* v = pages_[static_cast<size_t>(active_)].content) v->set_visible(true);
108 persist_active();
109 notify_change();
110 }
111
113 bool set_active_page(std::string_view title) {
114 for (size_t i = 0; i < pages_.size(); ++i) {
115 if (pages_[i].title == title) {
116 set_active_page(static_cast<int>(i));
117 return true;
118 }
119 }
120 return false;
121 }
122
124 void simulate_select(int index) { set_active_page(index); }
125
126 // ── Persistence ──────────────────────────────────────────────────
127
133 props_ = props;
134 if (!props_) return;
135 if (auto stored = props_->get_string(persistence_key_)) {
136 set_active_page(*stored);
137 }
138 }
139
140 void clear_persistence() { props_ = nullptr; }
141 bool has_persistence() const { return props_ != nullptr; }
142
143 void set_persistence_key(std::string key) {
144 persistence_key_ = std::move(key);
145 }
146 const std::string& persistence_key() const { return persistence_key_; }
147
148 // ── Observers ────────────────────────────────────────────────────
149
150 std::function<void(int active_index)> on_page_change;
151
152private:
153 void persist_active() const {
154 if (!props_) return;
155 if (active_ < 0 || static_cast<size_t>(active_) >= pages_.size()) return;
156 props_->set_string(persistence_key_, pages_[static_cast<size_t>(active_)].title);
157 }
158 void notify_change() {
159 if (on_page_change) on_page_change(active_);
160 }
161 static const std::string& empty_string() {
162 static const std::string s;
163 return s;
164 }
165
166 std::vector<Page> pages_;
167 int active_ = -1;
168 pulp::state::PropertiesFile* props_ = nullptr;
169 std::string persistence_key_ = "preferences.active_page";
170};
171
172} // namespace pulp::view
JSON-backed persistent settings file.
Definition properties_file.hpp:16
void simulate_select(int index)
Test helper — mirrors what a sidebar click would do.
Definition preferences_panel.hpp:124
std::function< void(int active_index)> on_page_change
Definition preferences_panel.hpp:150
const std::string & page_icon(size_t index) const
Definition preferences_panel.hpp:85
std::string active_page_title() const
Definition preferences_panel.hpp:92
const std::string & page_title(size_t index) const
Definition preferences_panel.hpp:82
const std::string & persistence_key() const
Definition preferences_panel.hpp:146
int active_page() const
Definition preferences_panel.hpp:91
bool has_persistence() const
Definition preferences_panel.hpp:141
void set_persistence_key(std::string key)
Definition preferences_panel.hpp:143
void clear_persistence()
Detach from persistence without touching the store.
Definition preferences_panel.hpp:140
void set_active_page(int index)
Switch by index. No-op when out of range.
Definition preferences_panel.hpp:99
size_t page_count() const
Definition preferences_panel.hpp:75
bool set_active_page(std::string_view title)
Switch by title. Returns true if a matching page was found.
Definition preferences_panel.hpp:113
View * page_content(size_t index) const
Returns nullptr if index is out of range.
Definition preferences_panel.hpp:79
void add_page(std::string title, std::string icon, std::unique_ptr< View > content)
Register a new page.
Definition preferences_panel.hpp:59
void bind_persistence(pulp::state::PropertiesFile *props)
Bind to a JSON-backed PropertiesFile (typically ApplicationProperties::user_settings()).
Definition preferences_panel.hpp:132
std::unique_ptr< View > add_child(std::unique_ptr< View > child)
Attaches child and returns nullptr.
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
@ s
Definition input_events.hpp:41
@ i
Definition input_events.hpp:40
@ v
Definition input_events.hpp:41
Definition preferences_panel.hpp:43
View * content
Non-owning — actual ownership is in the View tree (children_), so the page entry just remembers the s...
Definition preferences_panel.hpp:46
std::string title
Definition preferences_panel.hpp:44
std::string icon
Icon glyph / asset id; opaque to the panel.
Definition preferences_panel.hpp:45