| 1 | # Line Decorator |
| 2 | |
| 3 | The text editor provides an option to add a line decorator. This decorator gets called for each visible line and the integrator can render whatever they want in the requested space and can also handle user inputs the usual Dear ImGui way. |
| 4 | |
| 5 | Below is an example where a 41 pixel wide column is requested for custom decorations and the provided callback renders a regular ImGui Button and also draws a custom button with a red circle. The first button has a tooltip and the second button responds to a right-click with a popup to demonstrate some of the possibilities. |
| 6 | |
| 7 | When the line decorator is called, the ImGui position is already set (so you can render ImGui widgets immediately) and a new ID scope is established (to avoid ID collisions between lines). If you push style changes, make sure you also pop them before leaving the callback. |
| 8 | |
| 9 | If you don't want to express the decorator width in pixels, you can pass a negative number which is interpreted as the width of a specified number of glyphs. |
| 10 | |
| 11 | ```c++ |
| 12 | editor.SetLineDecorator(41.0f, [](TextEditor::Decorator& decorator) { |
| 13 | if (decorator.line == 10 || decorator.line == 15|| decorator.line == 16) { |
| 14 | auto size = decorator.height - 1.0f; |
| 15 | |
| 16 | if (decorator.line == 15) { |
| 17 | if (ImGui::Button("^", ImVec2(size, size))) { |
| 18 | // performing useless action |
| 19 | } |
| 20 | |
| 21 | if (ImGui::IsItemHovered()) { |
| 22 | ImGui::SetTooltip("Perform useless action"); |
| 23 | } |
| 24 | |
| 25 | } else { |
| 26 | ImGui::Dummy(ImVec2(size, size)); |
| 27 | } |
| 28 | |
| 29 | ImGui::SameLine(0.0f, 1.0f); |
| 30 | |
| 31 | auto pos = ImGui::GetCursorScreenPos(); |
| 32 | auto drawlist = ImGui::GetWindowDrawList(); |
| 33 | |
| 34 | drawlist->AddCircleFilled( |
| 35 | ImVec2(pos.x + size * 0.5f, pos.y + size * 0.5f), |
| 36 | (size - 6.0f) * 0.5f, |
| 37 | IM_COL32(128, 0, 0, 255)); |
| 38 | |
| 39 | ImGui::InvisibleButton("Invisible", ImVec2(size, size)); |
| 40 | |
| 41 | if (ImGui::BeginPopupContextItem()) { |
| 42 | if (ImGui::MenuItem("Call Something")) { /* handle click */ } |
| 43 | if (ImGui::MenuItem("Call Something Else")) { /* handle click */ } |
| 44 | ImGui::EndPopup(); |
| 45 | } |
| 46 | } |
| 47 | }); |
| 48 | ``` |
| 49 | |
| 50 |  |