MetaImGUI 1.0.0
ImGui Application Template for C++20
Loading...
Searching...
No Matches
DialogManager.h
Go to the documentation of this file.
1/*
2 MetaImGUI
3 Copyright (C) 2026 A P Nicholson
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
17*/
18
19#pragma once
20
21#include <coroutine>
22#include <functional>
23#include <memory>
24#include <string>
25#include <utility>
26#include <vector>
27
28namespace MetaImGUI {
29
34 OK,
35 OKCancel,
36 YesNo,
39};
40
44enum class MessageBoxIcon {
45 Info,
46 Warning,
47 Error,
49};
50
55
63public:
66
67 // Prevent copying and moving
68 DialogManager(const DialogManager&) = delete;
72
77 void Render();
78
79 // Message Box
88 void ShowMessageBox(const std::string& title, const std::string& message,
90 std::function<void(MessageBoxResult)> callback = nullptr);
91
92 // Input Dialog
100 void ShowInputDialog(const std::string& title, const std::string& prompt, const std::string& defaultValue = "",
101 std::function<void(const std::string&)> callback = nullptr);
102
103 // Progress Dialog
110 int ShowProgressDialog(const std::string& title, const std::string& message = "");
111
118 void UpdateProgress(int dialogId, float progress, const std::string& message = "");
119
124 void CloseProgress(int dialogId);
125
126 // List Selection Dialog
133 void ShowListDialog(const std::string& title, const std::vector<std::string>& items,
134 std::function<void(int)> callback = nullptr);
135
136 // Confirmation Dialog (convenience wrapper)
143 void ShowConfirmation(const std::string& title, const std::string& message,
144 std::function<void(bool)> callback = nullptr);
145
159 [[nodiscard]] auto AwaitConfirmation(std::string title, std::string message) {
160 struct Awaiter {
161 private:
162 DialogManager* manager;
163 std::string title;
164 std::string message;
165 bool result = false;
166
167 public:
168 Awaiter(DialogManager* mgr, std::string dialogTitle, std::string dialogMessage)
169 : manager(mgr), title(std::move(dialogTitle)), message(std::move(dialogMessage)) {}
170
171 [[nodiscard]] bool await_ready() const noexcept {
172 return false;
173 }
174 void await_suspend(std::coroutine_handle<> handle) {
175 manager->ShowConfirmation(title, message, [this, handle](bool confirmed) mutable {
176 result = confirmed;
177 handle.resume();
178 });
179 }
180 [[nodiscard]] bool await_resume() const noexcept {
181 return result;
182 }
183 };
184 return Awaiter{this, std::move(title), std::move(message)};
185 }
186
192 [[nodiscard]] auto AwaitInput(std::string title, std::string prompt, std::string defaultValue = "") {
193 struct Awaiter {
194 private:
195 DialogManager* manager;
196 std::string title;
197 std::string prompt;
198 std::string defaultValue;
199 std::string result;
200
201 public:
202 Awaiter(DialogManager* mgr, std::string dialogTitle, std::string dialogPrompt, std::string dialogDefault)
203 : manager(mgr), title(std::move(dialogTitle)), prompt(std::move(dialogPrompt)),
204 defaultValue(std::move(dialogDefault)) {}
205
206 [[nodiscard]] bool await_ready() const noexcept {
207 return false;
208 }
209 void await_suspend(std::coroutine_handle<> handle) {
210 manager->ShowInputDialog(title, prompt, defaultValue, [this, handle](const std::string& value) mutable {
211 result = value;
212 handle.resume();
213 });
214 }
215 [[nodiscard]] std::string await_resume() {
216 return std::move(result);
217 }
218 };
219 return Awaiter{this, std::move(title), std::move(prompt), std::move(defaultValue)};
220 }
221
225 [[nodiscard]] bool HasOpenDialog() const;
226
230 void CloseAll();
231
232private:
233 struct Impl;
234 std::unique_ptr<Impl> m_impl;
235
236 void RenderMessageBox();
237 void RenderInputDialog();
238 void RenderProgressDialogs();
239 void RenderListDialog();
240};
241
242} // namespace MetaImGUI
Manager for common UI dialogs.
void CloseProgress(int dialogId)
Close progress dialog.
auto AwaitConfirmation(std::string title, std::string message)
Awaitable variant of ShowConfirmation.
bool HasOpenDialog() const
Check if any dialog is currently open.
DialogManager & operator=(const DialogManager &)=delete
auto AwaitInput(std::string title, std::string prompt, std::string defaultValue="")
Awaitable variant of ShowInputDialog.
void ShowInputDialog(const std::string &title, const std::string &prompt, const std::string &defaultValue="", std::function< void(const std::string &)> callback=nullptr)
Show an input dialog.
void CloseAll()
Close all dialogs.
void Render()
Render all active dialogs Call this each frame in your main render loop.
DialogManager(DialogManager &&)=delete
void ShowMessageBox(const std::string &title, const std::string &message, MessageBoxButtons buttons=MessageBoxButtons::OK, MessageBoxIcon icon=MessageBoxIcon::Info, std::function< void(MessageBoxResult)> callback=nullptr)
Show a message box.
DialogManager & operator=(DialogManager &&)=delete
DialogManager(const DialogManager &)=delete
void ShowListDialog(const std::string &title, const std::vector< std::string > &items, std::function< void(int)> callback=nullptr)
Show a list selection dialog.
int ShowProgressDialog(const std::string &title, const std::string &message="")
Show a progress dialog.
void UpdateProgress(int dialogId, float progress, const std::string &message="")
Update progress dialog.
void ShowConfirmation(const std::string &title, const std::string &message, std::function< void(bool)> callback=nullptr)
Show a confirmation dialog (Yes/No)
MessageBoxButtons
Types of message box buttons.
@ OKCancel
OK and Cancel buttons.
@ RetryCancel
Retry and Cancel buttons.
@ YesNoCancel
Yes, No, and Cancel buttons.
@ YesNo
Yes and No buttons.
MessageBoxResult
Result from message box.
MessageBoxIcon
Message box icons/types.
@ Info
Information icon.
@ Question
Question icon.