-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
is there a way to know when a table cell is hovered? #6588
Comments
Dear ImGui really needs to add support for whole row hovers in tables, there are no official ways to do it. I had to read through a bunch of internal code to find a way to make it work. You'll need to ImGuiContext& g = *ImGui::GetCurrentContext();
if (ImGui::BeginTable("test", 3, flags))
{
ImGuiTable* table = g.CurrentTable;
ImGui::TableSetupColumn("a");
ImGui::TableSetupColumn("b");
ImGui::TableSetupColumn("c");
ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableHeadersRow();
for (size_t i = 0; i < 10; i++)
{
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::Text("title");
ImGui::TableNextColumn();
ImGui::Text("text");
ImGui::TableNextColumn();
ImGui::Text("properties");
// if we don't jump to the last column
// it will fail to calculate the rect size
// just in case we didn't render enough columns
ImGui::TableSetColumnIndex(table->Columns.size() - 1);
// get the row rect
ImRect row_rect(
table->WorkRect.Min.x,
table->RowPosY1,
table->WorkRect.Max.x,
table->RowPosY2
);
row_rect.ClipWith(table->BgClipRect);
bool bHover =
ImGui::IsMouseHoveringRect(row_rect.Min, row_rect.Max, false) &&
ImGui::IsWindowHovered(ImGuiHoveredFlags_None) &&
!ImGui::IsAnyItemHovered(); // optional
if (bHover)
{
// override row bg color
// see https://github.com/ocornut/imgui/blob/77eba4d0d1682917fee5638e746d5f599c47dc6e/imgui_tables.cpp#L1808
table->RowBgColor[1] = ImGui::GetColorU32(ImGuiCol_Border); // set to any color of your choice
}
}
ImGui::EndTable();
} This will work for any row height Recording.2023-07-13.221555.mp4 |
We have limited resources and I don't have a satisfying solution for this problem yet. See #6250 and #6347 for a fuller explanation. As mentioned in #6250 (comment) I have an internal patch for a |
Since this is likely going to be commonly requested, I have pushed my code for Vs a solution like the one you highlighted above can work without latency but with the constraint it needs to be called at end of row. Maybe we can come with an hybrid design that provides the best of both worlds, but that design should not lead to shearing inconsistency such as two rows reporting hovered different when the calls are made). |
Please note that the "usual" answer to OP problem is to submit a row height manually using |
Thanks. I will follow up when I try the explanations here. |
This issue can be marked as resolved based on the feedback provided in the comments. |
For posterity's sake, this is how I currently achieve this: {
ImGuiTable* table = g.CurrentTable;
u64 i = entry - visibleEntries[0]; // Some array that corresponds to table rows. I'm just calculating row index. entry is a ptr.
ImRect rowRect(table->WorkRect.Min.x, table->RowPosY1, table->WorkRect.Max.x, table->RowPosY2);
static constexpr auto MY_COL32_TABLE_HIGHLIGHT = IM_COL32(255,255,255,64); // 64 is lowered alpha
if (ImGui::TableGetHoveredRow() == i + 1)
{
ImGui::GetForegroundDrawList()->AddRectFilled(
rowRect.Min,
rowRect.Max,
MY_COL32_TABLE_HIGHLIGHT
);
}
} |
Version/Branch of Dear ImGui:
Version: 1.89.7
Branch: Docking
Back-end/Renderer/Compiler/OS
Back-ends: imgui_impl_glfw.cpp + imgui_impl.opengl3.cpp
Compiler: clang++
Operating System: Archlinux
My Issue/Question:
My objective is to have an entire row of a table highlighted with a different color, when a cell belonging to the same row is hovered. Is there a way to achieve this? I know how to do this for columns. I can get if a column is hovered using column flags, but not rows.
Screenshots/Video
None
Standalone, minimal, complete and verifiable example: (see #2261)
The text was updated successfully, but these errors were encountered: