From 012ba7f62e936d12046b24d66ea08818ff5b641c Mon Sep 17 00:00:00 2001 From: Pascal Thomet Date: Tue, 9 Jan 2024 17:52:44 +0100 Subject: [PATCH] Add RunnerCallbacks::AddEdgeToolbar --- src/hello_imgui/doc_params.md | 18 +++++++++---- src/hello_imgui/internal/docking_details.cpp | 24 +++++++++--------- src/hello_imgui/runner_callbacks.cpp | 11 ++++++++ src/hello_imgui/runner_callbacks.h | 18 +++++++++---- .../hello_imgui_demodocking.main.cpp | 25 +++++++++++-------- 5 files changed, 64 insertions(+), 32 deletions(-) diff --git a/src/hello_imgui/doc_params.md b/src/hello_imgui/doc_params.md index b4966d20..cbfc1b29 100644 --- a/src/hello_imgui/doc_params.md +++ b/src/hello_imgui/doc_params.md @@ -268,6 +268,9 @@ struct RunnerCallbacks // EdgesToolbars: A map that contains the definition of toolbars // that can be placed on the edges of the App window std::map edgesToolbars; + void AddEdgeToolbar(EdgeToolbarType edgeToolbarType, + VoidFunction callback, + const EdgeToolbarOptions& options = EdgeToolbarOptions()); // --------------- Startup sequence callbacks ------------------- @@ -387,12 +390,8 @@ enum class EdgeToolbarType Right }; -// EdgeToolbar :a toolbar that can be placed on the edges of the App window -// It will be placed in a non-dockable window -struct EdgeToolbar +struct EdgeToolbarOptions { - VoidFunction ShowToolbar = EmptyVoidFunction(); - // height or width the top toolbar, in em units // (i.e. multiples of the default font size, to be Dpi aware) float sizeEm = 2.5f; @@ -404,6 +403,15 @@ struct EdgeToolbar ImVec4 WindowBg = ImVec4(0.f, 0.f, 0.f, 0.f); }; + +// EdgeToolbar :a toolbar that can be placed on the edges of the App window +// It will be placed in a non-dockable window +struct EdgeToolbar +{ + VoidFunction ShowToolbar = EmptyVoidFunction(); + EdgeToolbarOptions options; +}; + std::vector AllEdgeToolbarTypes(); std::string EdgeToolbarTypeName(EdgeToolbarType e); ``` diff --git a/src/hello_imgui/internal/docking_details.cpp b/src/hello_imgui/internal/docking_details.cpp index 80446d86..557f393b 100644 --- a/src/hello_imgui/internal/docking_details.cpp +++ b/src/hello_imgui/internal/docking_details.cpp @@ -306,17 +306,17 @@ ImRect FixedWindowRect( float menuHeight = ImGui::GetFrameHeight() * 1.f; fullScreenPos.y += menuHeight; } - fullScreenSize.y = HelloImGui::EmSize(edgeToolbar.sizeEm); + fullScreenSize.y = HelloImGui::EmSize(edgeToolbar.options.sizeEm); } if ( edgeToolbarType == EdgeToolbarType::Bottom) { - float height = HelloImGui::EmSize(edgeToolbar.sizeEm); + float height = HelloImGui::EmSize(edgeToolbar.options.sizeEm); fullScreenPos.y = fullScreenPos.y + fullScreenSize.y - height - 1.f; // -1 to avoid a thin line fullScreenSize.y = height; } if ( (edgeToolbarType == EdgeToolbarType::Left) || (edgeToolbarType == EdgeToolbarType::Right)) { - float width = HelloImGui::EmSize(edgeToolbar.sizeEm); + float width = HelloImGui::EmSize(edgeToolbar.options.sizeEm); if (imGuiWindowParams.showMenuBar) { float menuHeight = ImGui::GetFrameHeight() * 1.f; @@ -326,27 +326,27 @@ ImRect FixedWindowRect( if (runnerParams.callbacks.edgesToolbars.find(EdgeToolbarType::Top) != runnerParams.callbacks.edgesToolbars.end()) { auto height = HelloImGui::EmSize( - runnerParams.callbacks.edgesToolbars.at(EdgeToolbarType::Top).sizeEm); + runnerParams.callbacks.edgesToolbars.at(EdgeToolbarType::Top).options.sizeEm); fullScreenPos.y += height; fullScreenSize.y -= height - 1.f; // -1 to avoid a thin line between the left and bottom toolbar } if (runnerParams.callbacks.edgesToolbars.find(EdgeToolbarType::Bottom) != runnerParams.callbacks.edgesToolbars.end()) { auto height = HelloImGui::EmSize( - runnerParams.callbacks.edgesToolbars.at(EdgeToolbarType::Bottom).sizeEm); + runnerParams.callbacks.edgesToolbars.at(EdgeToolbarType::Bottom).options.sizeEm); fullScreenSize.y -= height - 1.f; // -1 to avoid a thin line between the left and bottom toolbar } } if ( edgeToolbarType == EdgeToolbarType::Left) { auto width = HelloImGui::EmSize( - runnerParams.callbacks.edgesToolbars.at(EdgeToolbarType::Right).sizeEm); + runnerParams.callbacks.edgesToolbars.at(EdgeToolbarType::Right).options.sizeEm); fullScreenSize.x = width; } if ( edgeToolbarType == EdgeToolbarType::Right) { auto width = HelloImGui::EmSize( - runnerParams.callbacks.edgesToolbars.at(EdgeToolbarType::Right).sizeEm); + runnerParams.callbacks.edgesToolbars.at(EdgeToolbarType::Right).options.sizeEm); fullScreenPos.x = fullScreenPos.x + fullScreenSize.x - width; fullScreenSize.x = width + 1.f; // + 1 to avoid a thin line } @@ -375,24 +375,24 @@ ImRect FixedWindowRect( { if (edgeToolbarType == EdgeToolbarType::Top) { - float height = HelloImGui::EmSize(edgeToolbar.sizeEm); + float height = HelloImGui::EmSize(edgeToolbar.options.sizeEm); fullScreenPos.y += height; fullScreenSize.y -= height; } if (edgeToolbarType == EdgeToolbarType::Bottom) { - float height = HelloImGui::EmSize(edgeToolbar.sizeEm); + float height = HelloImGui::EmSize(edgeToolbar.options.sizeEm); fullScreenSize.y -= height; } if (edgeToolbarType == EdgeToolbarType::Left) { - float width = HelloImGui::EmSize(edgeToolbar.sizeEm); + float width = HelloImGui::EmSize(edgeToolbar.options.sizeEm); fullScreenPos.x += width; fullScreenSize.x -= width; } if (edgeToolbarType == EdgeToolbarType::Right) { - float width = HelloImGui::EmSize(edgeToolbar.sizeEm); + float width = HelloImGui::EmSize(edgeToolbar.options.sizeEm); fullScreenSize.x -= width; } } @@ -452,7 +452,7 @@ void ShowToolbars(const RunnerParams& runnerParams) auto& edgeToolbar = runnerParams.callbacks.edgesToolbars.at(edgeToolbarType); auto fullScreenRect = FixedWindowRect(runnerParams, edgeToolbarType); std::string windowName = std::string("##") + HelloImGui::EdgeToolbarTypeName(edgeToolbarType) + "_2123243"; - DoShowToolbar(fullScreenRect, edgeToolbar.ShowToolbar, windowName, edgeToolbar.WindowPaddingEm, edgeToolbar.WindowBg); + DoShowToolbar(fullScreenRect, edgeToolbar.ShowToolbar, windowName, edgeToolbar.options.WindowPaddingEm, edgeToolbar.options.WindowBg); } } } diff --git a/src/hello_imgui/runner_callbacks.cpp b/src/hello_imgui/runner_callbacks.cpp index 6610ccf4..da876480 100644 --- a/src/hello_imgui/runner_callbacks.cpp +++ b/src/hello_imgui/runner_callbacks.cpp @@ -32,4 +32,15 @@ namespace HelloImGui return ""; } + void RunnerCallbacks::AddEdgeToolbar(EdgeToolbarType edgeToolbarType, + VoidFunction callback, + const EdgeToolbarOptions& options) + { + EdgeToolbar edgeToolbar; + edgeToolbar.ShowToolbar = callback; + edgeToolbar.options = options; + edgesToolbars[edgeToolbarType] = edgeToolbar; + } + + } // namespace HelloImGui diff --git a/src/hello_imgui/runner_callbacks.h b/src/hello_imgui/runner_callbacks.h index 490dcefe..742ad542 100644 --- a/src/hello_imgui/runner_callbacks.h +++ b/src/hello_imgui/runner_callbacks.h @@ -78,12 +78,8 @@ enum class EdgeToolbarType Right }; -// EdgeToolbar :a toolbar that can be placed on the edges of the App window -// It will be placed in a non-dockable window -struct EdgeToolbar +struct EdgeToolbarOptions { - VoidFunction ShowToolbar = EmptyVoidFunction(); - // height or width the top toolbar, in em units // (i.e. multiples of the default font size, to be Dpi aware) float sizeEm = 2.5f; @@ -95,6 +91,15 @@ struct EdgeToolbar ImVec4 WindowBg = ImVec4(0.f, 0.f, 0.f, 0.f); }; + +// EdgeToolbar :a toolbar that can be placed on the edges of the App window +// It will be placed in a non-dockable window +struct EdgeToolbar +{ + VoidFunction ShowToolbar = EmptyVoidFunction(); + EdgeToolbarOptions options; +}; + std::vector AllEdgeToolbarTypes(); std::string EdgeToolbarTypeName(EdgeToolbarType e); // @@md @@ -135,6 +140,9 @@ struct RunnerCallbacks // EdgesToolbars: A map that contains the definition of toolbars // that can be placed on the edges of the App window std::map edgesToolbars; + void AddEdgeToolbar(EdgeToolbarType edgeToolbarType, + VoidFunction callback, + const EdgeToolbarOptions& options = EdgeToolbarOptions()); // --------------- Startup sequence callbacks ------------------- diff --git a/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp b/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp index 320328cf..06189dbf 100644 --- a/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp +++ b/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp @@ -694,18 +694,23 @@ int main(int, char**) // // Top and bottom toolbars // + // toolbar options + HelloImGui::EdgeToolbarOptions edgeToolbarOptions; + edgeToolbarOptions.sizeEm = 2.5f; + edgeToolbarOptions.WindowBg = ImVec4(0.8, 0.8, 0.8, 0.35f); // top toolbar - HelloImGui::EdgeToolbar topToolbar; - topToolbar.ShowToolbar = [&appState]() { ShowTopToolbar(appState); }; - topToolbar.sizeEm = 2.5f; - topToolbar.WindowBg = ImVec4(0.8, 0.8, 0.8, 0.35f); - runnerParams.callbacks.edgesToolbars[HelloImGui::EdgeToolbarType::Top] = topToolbar; + runnerParams.callbacks.AddEdgeToolbar( + HelloImGui::EdgeToolbarType::Top, + [&appState]() { ShowTopToolbar(appState); }, + edgeToolbarOptions + ); // right toolbar - HelloImGui::EdgeToolbar rightToolbar; - rightToolbar.sizeEm = 2.5f; - rightToolbar.WindowBg = ImVec4(0.8, 0.8, 0.8, 0.3f); - rightToolbar.ShowToolbar = [&appState]() { ShowRightToolbar(appState); }; - runnerParams.callbacks.edgesToolbars[HelloImGui::EdgeToolbarType::Right] = rightToolbar; + edgeToolbarOptions.WindowBg.w = 0.4f; + runnerParams.callbacks.AddEdgeToolbar( + HelloImGui::EdgeToolbarType::Right, + [&appState]() { ShowRightToolbar(appState); }, + edgeToolbarOptions + ); // // Load user settings at `PostInit` and save them at `BeforeExit`