Skip to main content

MainMenuWidget.ixx File

ImGui main menu bar widget with application settings. More...

Included Headers

#include <functional> #include <string> #include <cstring> #include <cstdio> #include "imgui.h" #include "imgui_internal.h" #include <helios.ext.imgui.ImGuiWidget>

Namespaces Index

namespacehelios
namespaceext

Platform-specific extensions and backend implementations. More...

namespaceimgui
namespacewidgets

Debug and developer widgets for ImGui overlays. More...

Classes Index

classMainMenuWidget

Main menu bar providing access to application settings. More...

Description

ImGui main menu bar widget with application settings.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file MainMenuWidget.ixx
3 * @brief ImGui main menu bar widget with application settings.
4 */
5module;
6
7#include <functional>
8#include <string>
9#include <cstring>
10#include <cstdio>
11#include "imgui.h"
12#include "imgui_internal.h"
13
14export module helios.ext.imgui.widgets.MainMenuWidget;
15
16import helios.ext.imgui.ImGuiWidget;
17
18export namespace helios::ext::imgui::widgets {
19
20 /**
21 * @class MainMenuWidget
22 * @brief Main menu bar providing access to application settings.
23 *
24 * This widget renders a menu bar at the top of the viewport with options for:
25 * - View settings (window transparency, docking)
26 * - Debug tools (show/hide log console, demo window)
27 * - Style presets
28 *
29 * Settings are automatically saved to and loaded from ImGui's imgui.ini file.
30 */
31 class MainMenuWidget : public ImGuiWidget {
32
33 private:
34 /**
35 * @brief Window background transparency (0.0 - 1.0).
36 */
37 float windowAlpha_ = 0.85f;
38
39 /**
40 * @brief Child window background transparency.
41 */
42 float childAlpha_ = 0.0f;
43
44 /**
45 * @brief Whether to show the ImGui demo window.
46 */
47 bool showDemoWindow_ = false;
48
49 /**
50 * @brief Whether to show the style editor.
51 */
52 bool showStyleEditor_ = false;
53
54 /**
55 * @brief Whether docking is enabled.
56 */
57 bool dockingEnabled_ = true;
58
59 /**
60 * @brief Whether this is the first draw call (for initial setup).
61 */
62 bool firstDraw_ = true;
63
64 /**
65 * @brief Whether the settings handler has been registered.
66 */
67 bool handlerRegistered_ = false;
68
69 /**
70 * @brief Callback when docking setting changes.
71 */
72 std::function<void(bool)> onDockingChanged_;
73
74 /**
75 * @brief Static pointer to the current instance (for ImGui callbacks).
76 */
77 static inline MainMenuWidget* instance_ = nullptr;
78
79 /**
80 * @brief Applies the current transparency settings to ImGui style.
81 */
82 void applyTransparency() {
83 ImGuiStyle& style = ImGui::GetStyle();
84 style.Colors[ImGuiCol_WindowBg].w = windowAlpha_;
85 style.Colors[ImGuiCol_ChildBg].w = childAlpha_;
86 style.Colors[ImGuiCol_PopupBg].w = std::min(windowAlpha_ + 0.05f, 1.0f);
87 style.Colors[ImGuiCol_TitleBg].w = std::min(windowAlpha_ + 0.05f, 1.0f);
88 style.Colors[ImGuiCol_TitleBgActive].w = std::min(windowAlpha_ + 0.1f, 1.0f);
89 }
90
91 /**
92 * @brief Registers the ImGui settings handler for persistence.
93 */
94 void registerSettingsHandler() {
95 if (handlerRegistered_) return;
96
97 instance_ = this;
98
99 ImGuiSettingsHandler handler;
100 handler.TypeName = "HeliosUserSettings";
101 handler.TypeHash = ImHashStr("HeliosUserSettings");
102 handler.ClearAllFn = nullptr;
103 handler.ReadOpenFn = [](ImGuiContext*, ImGuiSettingsHandler*, const char* name) -> void* {
104 return (std::strcmp(name, "Main") == 0) ? instance_ : nullptr;
105 };
106 handler.ReadLineFn = [](ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line) {
107 auto* widget = static_cast<MainMenuWidget*>(entry);
108 float f;
109 int i;
110 if (std::sscanf(line, "WindowAlpha=%f", &f) == 1) { widget->windowAlpha_ = f; }
111 else if (std::sscanf(line, "ChildAlpha=%f", &f) == 1) { widget->childAlpha_ = f; }
112 else if (std::sscanf(line, "DockingEnabled=%d", &i) == 1) { widget->dockingEnabled_ = (i != 0); }
113 };
114 handler.ApplyAllFn = [](ImGuiContext*, ImGuiSettingsHandler*) {
115 if (instance_) {
116 instance_->applyTransparency();
117 }
118 };
119 handler.WriteAllFn = [](ImGuiContext*, ImGuiSettingsHandler* h, ImGuiTextBuffer* buf) {
120 if (!instance_) return;
121 buf->appendf("[%s][Main]\n", h->TypeName);
122 buf->appendf("WindowAlpha=%.3f\n", instance_->windowAlpha_);
123 buf->appendf("ChildAlpha=%.3f\n", instance_->childAlpha_);
124 buf->appendf("DockingEnabled=%d\n", instance_->dockingEnabled_ ? 1 : 0);
125 buf->append("\n");
126 };
127
128 ImGui::GetCurrentContext()->SettingsHandlers.push_back(handler);
129 handlerRegistered_ = true;
130 }
131
132 public:
133 /**
134 * @brief Default constructor, registers settings handler with ImGui.
135 */
137 registerSettingsHandler();
138 }
139
140 ~MainMenuWidget() override {
141 if (instance_ == this) {
142 instance_ = nullptr;
143 }
144 }
145
146 /**
147 * @brief Sets a callback for when docking is enabled/disabled.
148 *
149 * @param callback Function called with the new docking state.
150 */
151 void setDockingCallback(std::function<void(bool)> callback) {
152 onDockingChanged_ = std::move(callback);
153 }
154
155 /**
156 * @brief Sets the window transparency level.
157 *
158 * @param alpha Transparency value (0.0 = transparent, 1 = opaque).
159 */
160 void setWindowAlpha(float alpha) {
161 windowAlpha_ = alpha;
162 applyTransparency();
163 ImGui::MarkIniSettingsDirty();
164 }
165
166 /**
167 * @brief Returns the current window transparency level.
168 */
169 [[nodiscard]] float windowAlpha() const noexcept {
170 return windowAlpha_;
171 }
172
173 /**
174 * @brief Returns whether docking is enabled.
175 */
176 [[nodiscard]] bool isDockingEnabled() const noexcept {
177 return dockingEnabled_;
178 }
179
180 /**
181 * @brief Renders the main menu bar.
182 */
183 void draw() override {
184 // Apply settings on first draw
185 if (firstDraw_) {
186 applyTransparency();
187 if (onDockingChanged_) {
188 onDockingChanged_(dockingEnabled_);
189 }
190 firstDraw_ = false;
191 }
192
193 if (ImGui::BeginMainMenuBar()) {
194
195 // === View Menu ===
196 if (ImGui::BeginMenu("View")) {
197
198 // Transparency settings
199 if (ImGui::BeginMenu("Transparency")) {
200 if (ImGui::SliderFloat("Window", &windowAlpha_, 0.3f, 1.0f, "%.2f")) {
201 applyTransparency();
202 ImGui::MarkIniSettingsDirty();
203 }
204 if (ImGui::SliderFloat("Child Areas", &childAlpha_, 0.0f, 1.0f, "%.2f")) {
205 applyTransparency();
206 ImGui::MarkIniSettingsDirty();
207 }
208
209 ImGui::Separator();
210
211 // Presets
212 if (ImGui::MenuItem("Solid")) {
213 windowAlpha_ = 1.0f;
214 childAlpha_ = 0.0f;
215 applyTransparency();
216 ImGui::MarkIniSettingsDirty();
217 }
218 if (ImGui::MenuItem("Semi-Transparent")) {
219 windowAlpha_ = 0.85f;
220 childAlpha_ = 0.0f;
221 applyTransparency();
222 ImGui::MarkIniSettingsDirty();
223 }
224 if (ImGui::MenuItem("Transparent")) {
225 windowAlpha_ = 0.6f;
226 childAlpha_ = 0.0f;
227 applyTransparency();
228 ImGui::MarkIniSettingsDirty();
229 }
230 if (ImGui::MenuItem("Glass")) {
231 windowAlpha_ = 0.4f;
232 childAlpha_ = 0.2f;
233 applyTransparency();
234 ImGui::MarkIniSettingsDirty();
235 }
236
237 ImGui::EndMenu();
238 }
239
240 // Docking toggle
241 if (ImGui::MenuItem("Docking", nullptr, &dockingEnabled_)) {
242 if (onDockingChanged_) {
243 onDockingChanged_(dockingEnabled_);
244 }
245 ImGui::MarkIniSettingsDirty();
246 }
247
248 ImGui::EndMenu();
249 }
250
251 // === Style Menu ===
252 if (ImGui::BeginMenu("Style")) {
253 if (ImGui::MenuItem("Dark")) {
254 ImGui::StyleColorsDark();
255 applyTransparency();
256 }
257 if (ImGui::MenuItem("Light")) {
258 ImGui::StyleColorsLight();
259 applyTransparency();
260 }
261 if (ImGui::MenuItem("Classic")) {
262 ImGui::StyleColorsClassic();
263 applyTransparency();
264 }
265
266 ImGui::Separator();
267
268 ImGui::MenuItem("Style Editor...", nullptr, &showStyleEditor_);
269
270 ImGui::EndMenu();
271 }
272
273 // === Debug Menu ===
274 if (ImGui::BeginMenu("Debug")) {
275 ImGui::MenuItem("ImGui Demo Window", nullptr, &showDemoWindow_);
276 ImGui::EndMenu();
277 }
278
279 ImGui::EndMainMenuBar();
280 }
281
282 // Render optional windows
283 if (showDemoWindow_) {
284 ImGui::ShowDemoWindow(&showDemoWindow_);
285 }
286
287 if (showStyleEditor_) {
288 if (ImGui::Begin("Style Editor", &showStyleEditor_)) {
289 ImGui::ShowStyleEditor();
290 }
291 ImGui::End();
292 }
293 }
294 };
295
296}
297

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.