Skip to content

Commit

Permalink
Add RunnerCallbacks::AddEdgeToolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Jan 9, 2024
1 parent 70368ba commit 012ba7f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 32 deletions.
18 changes: 13 additions & 5 deletions src/hello_imgui/doc_params.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<EdgeToolbarType, EdgeToolbar> edgesToolbars;
void AddEdgeToolbar(EdgeToolbarType edgeToolbarType,
VoidFunction callback,
const EdgeToolbarOptions& options = EdgeToolbarOptions());


// --------------- Startup sequence callbacks -------------------
Expand Down Expand Up @@ -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;
Expand All @@ -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<EdgeToolbarType> AllEdgeToolbarTypes();
std::string EdgeToolbarTypeName(EdgeToolbarType e);
```
Expand Down
24 changes: 12 additions & 12 deletions src/hello_imgui/internal/docking_details.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/hello_imgui/runner_callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 13 additions & 5 deletions src/hello_imgui/runner_callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<EdgeToolbarType> AllEdgeToolbarTypes();
std::string EdgeToolbarTypeName(EdgeToolbarType e);
// @@md
Expand Down Expand Up @@ -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<EdgeToolbarType, EdgeToolbar> edgesToolbars;
void AddEdgeToolbar(EdgeToolbarType edgeToolbarType,
VoidFunction callback,
const EdgeToolbarOptions& options = EdgeToolbarOptions());


// --------------- Startup sequence callbacks -------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down

0 comments on commit 012ba7f

Please sign in to comment.