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.imgui.ImGuiWidget>

Namespaces Index

namespacehelios
namespaceimgui
namespacewidgets

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
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.imgui.widgets.MainMenuWidget;
15
16import helios.imgui.ImGuiWidget;
17
18export namespace helios::imgui::widgets {
19
31 class MainMenuWidget : public ImGuiWidget {
32
33 private:
37 float windowAlpha_ = 0.85f;
38
42 float childAlpha_ = 0.0f;
43
47 bool showDemoWindow_ = false;
48
52 bool showStyleEditor_ = false;
53
57 bool dockingEnabled_ = true;
58
62 bool firstDraw_ = true;
63
67 bool handlerRegistered_ = false;
68
72 std::function<void(bool)> onDockingChanged_;
73
77 static inline MainMenuWidget* instance_ = nullptr;
78
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
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:
137 registerSettingsHandler();
138 }
139
140 ~MainMenuWidget() override {
141 if (instance_ == this) {
142 instance_ = nullptr;
143 }
144 }
145
151 void setDockingCallback(std::function<void(bool)> callback) {
152 onDockingChanged_ = std::move(callback);
153 }
154
160 void setWindowAlpha(float alpha) {
161 windowAlpha_ = alpha;
162 applyTransparency();
163 ImGui::MarkIniSettingsDirty();
164 }
165
169 [[nodiscard]] float windowAlpha() const noexcept {
170 return windowAlpha_;
171 }
172
176 [[nodiscard]] bool isDockingEnabled() const noexcept {
177 return dockingEnabled_;
178 }
179
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.9.8.