Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
window_classes.hpp
Go to the documentation of this file.
1#pragma once
2
3// High-level window classes.
4//
5// The low-level `WindowHost` contract owns native chrome,
6// `attach_native_child_view`, and multi-window plumbing via WindowManager.
7// These Pulp-native wrappers cover the three window patterns a standalone app
8// — or a plugin's secondary window — needs in practice:
9//
10// - `DocumentWindow` — a top-level document/editor window with a stored menu
11// descriptor, a content area that hosts a View tree, and a close-confirmation hook
12// ("Save changes?") that fires before the close actually happens.
13//
14// - `DialogWindow` — a modal/quasi-modal dialog with explicit OK / Cancel
15// buttons. Dispatches a `DialogResult` (`ok` / `cancel` / `closed`) to
16// a user-supplied completion handler.
17//
18// - `AlertWindow` — a simple titled message + N labeled buttons + an
19// icon hint (info / warning / error / question). Convenience overloads
20// for the three most common shapes: `info()`, `confirm()`, `error()`.
21//
22// All three are composition wrappers around `pulp::view::WindowHost`. They do
23// NOT subclass it — that keeps the test surface small and avoids leaking
24// platform plumbing into the high-level API. Headless tests construct them
25// without a window factory and drive the lifecycle hooks directly.
26
27#include <pulp/view/view.hpp>
29
30#include <functional>
31#include <memory>
32#include <string>
33#include <vector>
34
35namespace pulp::view {
36
37// ── DocumentWindow ─────────────────────────────────────────────────────────
38
40public:
46 using CloseConfirmationHandler = std::function<bool()>;
47
52 struct MenuItem {
53 std::string title;
54 std::function<void()> action;
55 // Future: shortcut, enabled flag, submenu pointer. Kept minimal so
56 // the API can grow additively without breaking callers.
57 };
58
60 struct Menu {
61 std::string title;
62 std::vector<MenuItem> items;
63 };
64
65 DocumentWindow(const std::string& title, View& content_root);
67
72
77 void set_menus(std::vector<Menu> menus);
78 const std::vector<Menu>& menus() const { return menus_; }
79
84
89
91 View& content() { return *content_root_; }
92 const View& content() const { return *content_root_; }
93
95 const std::string& title() const { return title_; }
96
100 WindowHost* host() { return host_.get(); }
101
106 bool show();
107
109 void hide();
110
112 bool has_native_window() const { return host_ != nullptr; }
113
114private:
115 std::string title_;
116 View* content_root_ = nullptr;
117 std::vector<Menu> menus_;
118 CloseConfirmationHandler close_handler_;
119 std::unique_ptr<WindowHost> host_;
120};
121
122// ── DialogWindow ───────────────────────────────────────────────────────────
123
129
131public:
132 using CompletionHandler = std::function<void(DialogResult)>;
133
134 DialogWindow(const std::string& title, View& content_root);
136
137 DialogWindow(const DialogWindow&) = delete;
139
142 void set_ok_label(std::string label) { ok_label_ = std::move(label); }
143 const std::string& ok_label() const { return ok_label_; }
144
148 void set_cancel_label(std::string label) { cancel_label_ = std::move(label); }
149 const std::string& cancel_label() const { return cancel_label_; }
150
154 completion_ = std::move(handler);
155 }
156
161 bool show(bool modal = true);
162
166 void dismiss(DialogResult result);
167
168 bool is_dismissed() const { return dismissed_; }
169 DialogResult last_result() const { return last_result_; }
170 bool is_modal() const { return modal_; }
171 const std::string& title() const { return title_; }
172
173 View& content() { return *content_root_; }
174 WindowHost* host() { return host_.get(); }
175
176private:
177 std::string title_;
178 View* content_root_ = nullptr;
179 std::string ok_label_ = "OK";
180 std::string cancel_label_ = "Cancel";
181 CompletionHandler completion_;
182 std::unique_ptr<WindowHost> host_;
183 bool dismissed_ = false;
184 bool modal_ = false;
185 DialogResult last_result_ = DialogResult::closed;
186};
187
188// ── AlertWindow ────────────────────────────────────────────────────────────
189
197
199public:
200 using ButtonHandler = std::function<void(std::size_t /*index*/)>;
201
202 AlertWindow(const std::string& title, const std::string& message,
205
206 AlertWindow(const AlertWindow&) = delete;
208
210 std::size_t add_button(std::string label);
211
215 button_handler_ = std::move(handler);
216 }
217
219 bool show();
220
223 void click_button(std::size_t index);
224
225 bool is_dismissed() const { return dismissed_; }
226 std::size_t button_count() const { return buttons_.size(); }
227 const std::string& button_label(std::size_t i) const { return buttons_.at(i); }
228 AlertIcon icon() const { return icon_; }
229 const std::string& title() const { return title_; }
230 const std::string& message() const { return message_; }
231 WindowHost* host() { return host_.get(); }
232
233 // ── Convenience factories ────────────────────────────────────────────
234
237 static std::unique_ptr<AlertWindow> info(const std::string& title,
238 const std::string& message,
239 ButtonHandler handler = {});
240
242 static std::unique_ptr<AlertWindow> confirm(const std::string& title,
243 const std::string& message,
244 ButtonHandler handler = {});
245
247 static std::unique_ptr<AlertWindow> error(const std::string& title,
248 const std::string& message,
249 ButtonHandler handler = {});
250
253 static std::unique_ptr<AlertWindow> warning(const std::string& title,
254 const std::string& message,
255 ButtonHandler handler = {});
256
257 // ── Styled-variant theme tokens ───────────────────────────────────
258
264 struct StyleTokens {
265 const char* icon_color = "";
266 const char* accent_color = "";
267 const char* glyph = "";
268 };
269
280
282 StyleTokens style_tokens() const { return style_tokens_for(icon_); }
283
284private:
285 std::string title_;
286 std::string message_;
287 AlertIcon icon_;
288 std::vector<std::string> buttons_;
289 ButtonHandler button_handler_;
290 std::unique_ptr<View> root_view_;
291 std::unique_ptr<WindowHost> host_;
292 bool dismissed_ = false;
293};
294
295} // namespace pulp::view
const std::string & button_label(std::size_t i) const
Definition window_classes.hpp:227
AlertWindow(const AlertWindow &)=delete
WindowHost * host()
Definition window_classes.hpp:231
static std::unique_ptr< AlertWindow > warning(const std::string &title, const std::string &message, ButtonHandler handler={})
"Warning" alert with a single OK button — sibling of info / error.
void set_button_handler(ButtonHandler handler)
Install a handler invoked with the index of the chosen button.
Definition window_classes.hpp:214
AlertWindow(const std::string &title, const std::string &message, AlertIcon icon=AlertIcon::info)
static std::unique_ptr< AlertWindow > info(const std::string &title, const std::string &message, ButtonHandler handler={})
"Info" alert with a single OK button.
void click_button(std::size_t index)
Programmatic button click — fires the handler with index and tears the alert down.
std::size_t button_count() const
Definition window_classes.hpp:226
std::function< void(std::size_t)> ButtonHandler
Definition window_classes.hpp:200
StyleTokens style_tokens() const
Convenience that returns this alert's styled tokens.
Definition window_classes.hpp:282
AlertIcon icon() const
Definition window_classes.hpp:228
bool is_dismissed() const
Definition window_classes.hpp:225
static std::unique_ptr< AlertWindow > error(const std::string &title, const std::string &message, ButtonHandler handler={})
"Error" alert with a single OK button.
const std::string & message() const
Definition window_classes.hpp:230
static std::unique_ptr< AlertWindow > confirm(const std::string &title, const std::string &message, ButtonHandler handler={})
"Question" alert with Cancel (0) and OK (1) buttons.
std::size_t add_button(std::string label)
Append a button. Returns the index assigned to this button (0-based).
const std::string & title() const
Definition window_classes.hpp:229
static StyleTokens style_tokens_for(AlertIcon icon)
Returns the style tokens for an icon.
bool show()
Show the alert. Returns false if no WindowHost::Factory exists.
AlertWindow & operator=(const AlertWindow &)=delete
DialogWindow(const std::string &title, View &content_root)
const std::string & title() const
Definition window_classes.hpp:171
const std::string & cancel_label() const
Definition window_classes.hpp:149
DialogWindow & operator=(const DialogWindow &)=delete
bool is_dismissed() const
Definition window_classes.hpp:168
const std::string & ok_label() const
Definition window_classes.hpp:143
DialogWindow(const DialogWindow &)=delete
std::function< void(DialogResult)> CompletionHandler
Definition window_classes.hpp:132
WindowHost * host()
Definition window_classes.hpp:174
DialogResult last_result() const
Definition window_classes.hpp:169
bool is_modal() const
Definition window_classes.hpp:170
void set_ok_label(std::string label)
Customize the OK button label (default "OK").
Definition window_classes.hpp:142
View & content()
Definition window_classes.hpp:173
bool show(bool modal=true)
Show the dialog.
void dismiss(DialogResult result)
Programmatic dismissal — call from a button handler.
void set_cancel_label(std::string label)
Customize the Cancel button label (default "Cancel").
Definition window_classes.hpp:148
void set_completion_handler(CompletionHandler handler)
Set the completion handler.
Definition window_classes.hpp:153
void hide()
Hide the window without closing it (close-confirm is NOT consulted).
const View & content() const
Definition window_classes.hpp:92
void set_menus(std::vector< Menu > menus)
Set or replace the logical menu model.
const std::vector< Menu > & menus() const
Definition window_classes.hpp:78
DocumentWindow & operator=(DocumentWindow &&)=delete
View & content()
The content View attached to this window's content area.
Definition window_classes.hpp:91
const std::string & title() const
The window's title.
Definition window_classes.hpp:95
std::function< bool()> CloseConfirmationHandler
Hook called when the user requests the window to close (clicked the close button, hit ⌘W,...
Definition window_classes.hpp:46
bool has_native_window() const
True if show() produced a live native window.
Definition window_classes.hpp:112
bool show()
Create the native window and show it.
DocumentWindow(const std::string &title, View &content_root)
CloseConfirmationHandler set_close_confirmation_handler(CloseConfirmationHandler handler)
Install a close-confirmation hook.
bool request_close()
Programmatic close request — runs the close-confirmation handler and (if it returns true) tears down ...
DocumentWindow(const DocumentWindow &)=delete
DocumentWindow(DocumentWindow &&)=delete
WindowHost * host()
Underlying WindowHost (nullptr until show() is called or until a factory becomes available).
Definition window_classes.hpp:100
DocumentWindow & operator=(const DocumentWindow &)=delete
Definition view.hpp:73
Definition window_host.hpp:113
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
@ none
Definition appearance_defects.hpp:53
@ modal
A ModalOverlay in the tree consumed Escape through its own handler.
Definition overlay_dismissal.hpp:259
@ ok
bound; the widget exists and accepts this target
Definition binding_diagnostics.hpp:27
@ i
Definition input_events.hpp:40
AlertIcon
Definition window_classes.hpp:190
@ question
Definition window_classes.hpp:195
@ info
Definition window_classes.hpp:192
DialogResult
Definition window_classes.hpp:124
@ cancel
Definition window_classes.hpp:126
@ closed
User dismissed the dialog without explicitly choosing.
Definition window_classes.hpp:127
@ warning
Definition design_ir.hpp:696
@ info
Definition design_ir.hpp:695
@ error
Definition design_ir.hpp:697
Theme color-token names the host should look up when painting each AlertIcon.
Definition window_classes.hpp:264
const char * icon_color
Token for the icon glyph color.
Definition window_classes.hpp:265
const char * accent_color
Token for the chrome accent (border / title).
Definition window_classes.hpp:266
const char * glyph
Glyph hint (e.g. "i", "!", "x", "?").
Definition window_classes.hpp:267
A menu item in this window's menu bar.
Definition window_classes.hpp:52
std::string title
Definition window_classes.hpp:53
std::function< void()> action
Definition window_classes.hpp:54
A top-level menu (e.g. "File", "Edit", "View") with its items.
Definition window_classes.hpp:60
std::string title
Definition window_classes.hpp:61
std::vector< MenuItem > items
Definition window_classes.hpp:62